I run Home Assistant operating system on a Raspberry Pi for my home automation. The built-in backup integration creates backup files in the backups folder in the root of the default folder. This is fine, but what happens if you experience an hardware failure and need to restore your Home Assistant configuration from a backup file? There are integrations available to save backups to the cloud (e.g., Google drive), which is fine, if you have the storage capacity available with your cloud provider. However, I wanted to copy my backups to my Ubuntu server based storage server. In concept, this is easy to achieve with rsync.
SSH key based authentication
The first step to configuring ssh key based authentication is to generate a key-pair on the host from which you will be initiating the connection. We use the ‘ssh-keygen’ utility included with the OpenSSH suit included with most flavours of Linux. By default, ‘ssh-keygen’ generates RSA keys, however, I chose to go with “ed25519” keys for the following reasons:
- Faster to generate and validate keys.
- More secure than RSA keys.
- More resilient against hash-function collision attacks.
- The keys are smaller.
Generate a new key-pair with:
ssh-keygen -t ed25519
Assuming you accepts the defaults, the private and public key pair, id_ed25519 and id_ed25519.pub respectively, will be saved to the .ssh folder of your home directory.
Next, copy the public key to the target host. You can manually copy the public key (id_ed25519.pub) to the target host manually, however, it’s much easier to use the “ssh-copy-id” utility included with the OpenSSH suit:
ssh-copy-id user@host
where “user” is the username and “host” is the IP or hostname for your target host. You will be required to enter the password for the target host. Once completed, your public key will be added to the “authorized_keys” file in the .ssh folder on the target host. Confirm that key based authentication works by entering:
ssh user@host
I use the “Advanced SSH & web Terminal” add-on available in the Home Assistant Community available here. This add-on facilitate ssh and rsync access to Home Assistant. If all was configured correctly, you should be logged in to the remote host without a password.
Automating the backup
Create a new file named “ha-backup.sh” in your home folder with the following code:
#!/bin/bash
# Copy files that do not already exist in the backup folder
rsync -rtvu user@homeassistant:/backup/ /mnt/md0/ha-backup/
# Delete backups older than 14 days
ssh user@homeassistant find /backup/ -type f -name '*.tar' -mtime +14 -exec rm {} \;
# Rename tar files based on mtime
cd /mnt/md0/ha-backup/
for f in *.tar
do
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").tar"
done
Replace “user@homeassistant” with the username and hostname for your Home Assistant installation. Likewise, replace “/mnt/md0/ha-backup/” with the absolute path to your backup folder on the target host. Make the file executable with:
chmod 755 ha-backup.sh
Next, open your crontab file for editing with:
crontab -e
Cron will run the following script at 1am daily:
0 1 * * * /bin/bash /home/user/ha-backup.sh 1> /home/user/ha-backup.log 2> /home/user/ha-error.log
Hopefully, you will never need to recover Home Assistant from a backup, but having copies of your backups stored at locations outside of the Home Assistant server is good insurance. This solution is a quick and simple solution if you have a NAS or local server on your network.