Skip to content

Hass.io backup

Oncleben31 edited this page Oct 9, 2021 · 3 revisions

Introduction

You know backup is key to ensure the sustainability of your Home automation system. If something happen you should be able to repair and restore a functional state of your systems.

I used to rely on an Hass.io add-on synchronizing the Home Assistant snapshots on my Google Drive. I wasn't satisfied by this solution. One major problem is my HA instance crashed sometimes during the upload of new snapshot to Google Drive. On top of that I prefere avoiding add-on when possible.

Design of my current backup solution

I'm relying on the SSH & Web Terminal Hass.io add-on. With it, I can call from an Home Assistant automation a shell script in the add-on environement where hassio-cli is already installed.

The script create a new snapshot, start the synchronisation with my NAS using rsync, and clean old snapshots.

On top of that I have asked my NAS to synchronise regularly in the cloud. I'm using Google Drive but it could be any other cloud services supported by your NAS.

Implementation of the backup solution

The shell script (hassio_backup.sh) with explanation in comments is:

#!/bin/bash

# Inspired by https://blog.ktz.me/backing-up-home-assistant/
# And key based authentification activated on my Synology NAS by following instructions from
# https://blog.aaronlenoir.com/2018/05/06/ssh-into-synology-nas-with-ssh-key/

# create new snapshot
hassio snapshots new --name $(date +"%Y_%m_%d__%H_%M_%S")

# use rsync to copy only archives that dont already exist on NAS
rsync -rtvu --rsync-path=/usr/bin/rsync /backup/ nas:~/hassio-backup/snapshots/

# delete snapshots older than 14 days to save disk space
find /backup/ -type f -name '*.tar' -mtime +14 -exec rm {} \;

echo Script competed

To make it work, you need:

  • To activate key based SSH authentification on your NAS. I've used the instructions from Aaron Lenoir's blog for my Synology NAS.
  • To configure the ssh client (in the add-on) with private/public keys and by configuring the host corresponding to you NAS.

Example for ssh host configuration file /root/.ssh/config:

Host nas
  User YOUR_NAS_USER
  Port SSH_PORT_NUMBER
  Hostname NAS_IP

The shell script is launched every monday at 3:00AM by the following Home Assistant automation (backup_config.yaml):

---
# Automation for HA weekly backup.
#
# Use bash script to create the snapshot, synchronize with NAS by rsync
# and clean local old backup.
#

# Generate the HA config archive weekly
- alias: Weekly Backup Monday at 3 AM
  initial_state: 'on'
  trigger:
    platform: time
    at: '3:00:00'
  condition:
    - condition: time
      weekday:
        - mon
  action:
    - service: hassio.addon_stdin
      data:
        addon: a0d7b954_ssh
        input: "/config/scripts/shell/hassio_backup.sh"
    - service: notify.telegram
      data_template:
        message: Creation du backup hebomadaire."

I have added a notification to inform me when the backup is done.