Easily outsource your Home Assistant backup

Nico Writing by Nico
  7 min of reading

This page has been visited ... times

Easily outsource your Home Assistant backup

Even back then, outsourcing Home Assistant backups was a concern for me. Given my Raspberry Pi setup, everything was stored on the SD card, I wrote an article in 2023: How ​​to outsource your backup with Syncthing(/fr/blog/simple-sauvegarde-externe-homeassistant-syncthing-plus-gestion-backup). This method still works, but Syncthing is still quite complex.

Let me explain: Haade offers a module that allows you to connect via a web address for free, and I also offer my entire network the option to back up their latest backups to an external server. I needed to install something simpler.

Between 2023 and today, a lot has changed, and Home Assistant has evolved considerably. Since backup management is entirely handled by the application. It is possible, among other things, to add network storage supporting SMB/NFS protocols, which is for use on an internal network, but with WireGuard management, it would be possible to export backups to an external server.

Home Assistant external backup settings

Rsync, an open-source utility, is used to copy or move directories, similar to mv or cp, but also provides comprehensive synchronization and remote transfer features (based on SSH). Therefore, it can also be used in more advanced ways to maintain a duplicate of a storage space, or to set up rudimentary remote backup systems.

All this with a single command line, making it easy to use.

To follow this tutorial, you’ll need some basic knowledge, but don’t worry, it’s nothing insurmountable!

Prerequisites

  • Home Assistant OS
  • A NAS with OpenMediaVault (preferably)
  • File Editor add-on for Home Assistant
  • Rsync add-on for Home Assistant
  • Open port 22 on your router
  • Have a static external IP address (preferably)

Installing Rsync in HAOS

Let’s not waste any time and start by adding the Poeschl repository to the module manager. Go to: Settings > Add-ons > Add-on Stores > Menu > Repositories

Added the Poeschl module directory

To add repositories, click directly on the link below or add them manually.

Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.

Direct link to the repository: https://github.com/Poeschl-HomeAssistant-Addons/repository and click Add.

Refresh the add-ons page, go to the Poeschl tab, and install Rsync.

Poeschl add-on module page and rsync search

Using and Configuring the Rsync Module

Once Rsync is installed, start it without changing the configuration. This will initially generate the SSH keys in the /ssh/rsync/id_rsa directory. We will retrieve these keys to connect to our NAS or remote server.

Rsync logs, SSH key generation

Now let’s move on to configuring the rsync module.

Configuring the Rsync module on the Home Assistant side
  1. Directory where SSH keys are stored
  2. user is the rsync username must match the user in omv. I used Nicotest.
  3. Local and remote folder configuration + options if necessary
    1. local: (This is the backup folder in HA by default: /backup)
    2. remote: This is the remote folder. For me, backups will be saved in /export/data/Backup/Nicotest
    3. Additional options during rsync transfers
    4. Direction: pull or push by default (push pushes data from HA to external sources)
  4. remote_host: IP/HTTP address of the external server.

In Conclusion: On the OMV side, I’m going to create a Nicotest user with their own home directory. Therefore, in rsync for Home Assistant, the user should be Nicotest, and the remote directory will ultimately be Nicotest**.

Retrieving the SSH Key

Now, we need to retrieve the SSH key, which is essential for the remote connection between the two systems. It’s quite simple; we’ll use the popular file editor module, available by default in the Home Assistant add-ons section.

Using a file editor for Home Assistant

If you haven’t installed it, click on the link below.

Open your Home Assistant instance and show the dashboard of an add-on.

Next, to access the root directory in the file editor module, and yes, by default you only have access to the /config subfolder, remember we need to access the /ssl/rsync/id_pub.rsa subfolder.

So it’s very simple: go to the module’s configuration and uncheck Enforce Basepath. Then restart the module.

disable the enforce basepath option in the file editor

Now open the file editor interface and navigate to the file. Then simply press ctrl-c and you’re done! 👌

disable the enforce basepath option in the file editor

NAS Configuration

Heading to the NAS, I’m assuming you’re familiar with this wonderful content management software! Let’s go! Go to the Users tab > Settings and check the box for user home directories. This way, the user created Nicotest will have a directory in /export/data/Backup/Nicotest. You don’t have to create these directories automatically if you don’t want to, and you can create them automatically. However, don’t forget to recursively grant the corresponding permissions to the subfolders and files. To do this, follow the steps.

ssh -p 22 xxx.xxx.xxx.xxx -l root # SSH connection to the NAS
mkdir ../../Nicotest
chown -R Nicotest:users Nicotest
Activate the personal directory in OMV

Let’s move on to the user creation step:

  • Name: Nicotest
  • Password
  • Group: be sure to select _ssh

Then confirm and verify that your user has the correct permissions for _ssh and users

End-user configuration in OMV

Now you need to copy the ssh key previously taken from Home Assistant.

Paste the SSH key for the user dan OMV

Now you can register the user.

Rsync-compatible omv user generation

That’s all you need to create on the OMV side; it’s simple, quick, and efficient. There’s no need to install the Rsync extension for OMV.

Check before launching

Let’s review the steps before the final launch of rsync for Home Assistant.

Home Assistant Backups

You must, of course, have backups in Home Assistant to verify that everything is working correctly. Yes, Home Assistant natively manages backups.

Go to Settings > System > Backups. If automatic backups are not configured, I strongly advise you to do so.

Backup available in Home Assistant

As you can see from my configuration above, I don’t have any automatic backups created. This is normal for a test installation, but I do have two backups configured.

Rsync in Home Assistant

We are now ready to run rsync a second time (as mentioned above, once the task is completed, Rsync stops; you will need to create an automation later).

Automatic deactivation of rsync after synchronization

Once the startup is complete, let’s check the logs

rsync logs all work

So let’s break down the command line

rsync --archive --recursive --compress --delete --prune-empty-dirs \
-e 'ssh -p 22 -i /ssl/rsync/id_rsa -oStrictHostKeyChecking=no' \
/backup

This command:

  1. Synchronizes /backup
  2. In full backup mode (permissions, dates, etc.)
  3. Via SSH with a private key
  4. With compression
  5. Deleting obsolete files on the destination side
  6. Cleaning up empty folders
  7. Without user interaction (ideal for a cron job)

Complete and efficient.

I connect to the NAS server via SSH and indeed everything is saved in a backup/ folder

NAS server copy checks

The files have been successfully copied and verified on the OMV side

Creating a Home Assistant Automation

Now we’re going to create an automation that will run x times per day and start the rsync add-on.

I know it’s not very practical, but because automations have been simplified in the latest versions of Home Assistant, I decided to create an animated GIF anyway.

Creating a crom task automation and launching the rsync module

Conclusion

Rsync is a simple way to offload Home Assistant backups to a NAS, much lighter than Syncthing, preferably one equipped with OpenMediaVault. Thanks Poeschl, thanks OMV, security first. 🫵

Rating:
Nico

Nico

Founding member of the Haade website, I have been passionate about home automation, computers and electronics for over 10 years. Through this blog, I try to help other Internet users to experiment with home automation, to find fun tutorials, in short to evolve.

Comments