What backup strategy when running Home Assistant in Docker?

I’m running HA under Docker on a Raspberry Pi (4B) along with a bunch of other stuff. I recently had a problem when the SD card went bad and I had to rebuild the system on a new card. Fortunately the HA directory on the bad card was readable and I was able to rsync it onto the new card and everything came up just fine when the new HA container got pulled.
But what if the card had been worse corrupted? I see references to snapshots of the HA environment, and I’d like to do that, and transfer these to my NAS for backup. But I can’t find the GUI options (“Supervisor → Snapshots”) on my installation. Am I just missing it, or what?
FWIW I’m using the homeassistant/raspberrypi4-homeassistant:latest container.

Can you find the supervisor even though you can’t find snapshots? The URL is https://homeassistant.local/hassio/snapshots. Could you post a screenshot?

There’s no Supervisor menu anywhere, and when I try the /hassio URL I get a 404. Screenshot of the menu I see when I go to my Pi at port 8123/config attached.

OK, I found this: https://community.home-assistant.io/t/where-is-supervisor-hassio-from-home-assistant-installed-via-docker/172853 which explains that the Docker container I’m using doesn’t included the Supervisor functions. I need to see if there are any alternative containers I could use instead. No way am I doing a direct install of HA on the Pi.

1 Like

You don’t need to change anything at all if ytou want to continue running your current installation method.

Just copy your entire config directory (including all of the hidden files - .cloud, .storage, etc) to a back up location.

I run a non-supervised HA install in docker and that’s all I do.

4 Likes

Great, thanks. So if I write a little script to archive the entire config directory and ship it over to the NAS, and run that as a cron job, I should be good?

1 Like

I don’t see why not. I just copy stuff over manually right before I update to the newest HA version an every so often in between when I make a significant number of changes.

But just to re-emphasize - make you get the entire contents of that folder, hidden stuff and all.

1 Like

Thanks. There’s a bit of an issue with the file /.storage/auth, which is owned by root, so my regular uid isn’t able to add it to the archive. Everything else zips up nicely.

I just run a chown on my docker config directory to switch it away from root to my current user (finity) and it all still works fine. And I can back everything up including that auth file.

So the way I backup seems overly involved, but it works.

1- Use Portainer to stop the Home Assistant container - if Home Assitant is running I’ve found files skip and don’t copy over

2- Sudo nautilus which pulls up the root file explorer and gets around the permission issues.

3- Copy the entire config directory to my NAS drive and backup folder

4- Start the container again in portainer after copying is complete

I mean, this works, but I really would like to automate the process if possible. I just haven’t figured out a good way to do that though.

Update - I think I figured out a way to automate. I wrote a script to backup and run it nightly through the crontab. Generally following the directions here - Using rsync and cron to automate incremental backups

First I created a script file called hass_backup.sh that stops the container, does the backup using rsync, then starts the home assistant docker container back up

docker stop homeassistant
rsync -ab --backup-dir=old_`date +%F` --delete --exclude=old_* /home/mwav3/homeassistant /media/mwav3/MyPassport/backups/hassrsync
docker start homeassistant

To make sure that all the root files copy, I did this in the global crontab by running sudo gedit /etc/crontab

I then added commands to run the script nightly at 2 AM as root. I also then added another command to the crontab to start the container at 2:15 AM in case anything happens where the first script fails at some point so Home Assistant doesn’t stay offline.

0 2    * * *    root    sh /home/mwav3/hass_backup.sh
15 2   * * *    mwav3     docker start homeassistant

Seems to be working. I’m sure there’s lots of other ways to do this.

8 Likes

I just switched from a supervised Pi4 setup to running on a laptop with Ubuntu and docker and faced the same questions. After some research (for those who also found this link as I did), I settled on Duplicati:
https://hub.docker.com/r/linuxserver/duplicati
Runs in docker, and as long as you provide it your docker root folder as a volume, it can back up all your containers.
(supports GDrive, OneDrive, local (NFS), and tons more destinations)

3 Likes

The latest version 2022.4 now allows you to manually create backups through the UI for home assistant container pretty easily.

But I would still recomend an automated backup like the cron rsync example I posted above or using Duplicati as the last poster suggested. I saw this helpful video guide to setup duplicati recently as well if you’re looking for step by step instructions.

1 Like

Duplicati seems like a perfect solution. However as stated in the video, if HA is running, database files backup will fail. He told that it’s not a probem for him, as he only want’s to backup configuration files, but for me it would be important to backup db as well. I had an incident of deleting (yes, stupid me) my whole HA docker folder and lost about half a year of data (luckily had half year old full backup).

Does anybody have a idea how to solve this issue and use duplicati to backup the whole docker folder with db-s included?

I think the key is to stop the home assistant container prior to backing up. I’m still using the shell script I posted a few posts up for backups What backup strategy when running Home Assistant in Docker? - #10 by mwav3

The shell script stops the Home Assistant container every night at 2AM. It then uses Rsync to copy the home assistant config files over to another backup folder (fully in tack because Home Assistant is stopped). After the backup the shell script starts Home Assistant again. I then use the built in backup program in Ubuntu (which I believe is just duplicity) to copy the backup from that other folder to my Google Drive automatically. Duplicati could also be used to copy the backup to create a “backup of the backup”.

I think the problem with just using Duplicati alone is you can’t use it to stop Home Assistant (at least no way I am aware of)

I’ve had issues restoring Home Assistant container using Duplicacy. I think it’s a permissions issue in respect of the /.storage/auth file (which I can see is referred to above), either the backing up of this and/or the restoring of it. I’m running Unraid. Does anyone know what I should do to avoid any permissions issue? Thanks.

Hi,
Sorry to reopen this thread but did anyone find a solution to the permission issues when doing the backup with Duplicati?

Many thanks

It’s been a while, so let me add to this for anyone else finding this now.
I have moved my DB to MariaDB. I wanted to be able to show 45 days of recorder data, and my DB is now 65GB :frowning: (As opposed to my InfluxDB with over a year’s worth of data only being 11GB). But even with the built-in DB, there are options.
I use GitHub - tiredofit/docker-db-backup: Backup multiple database types on a scheduled basis with many customizable options. This can backup many different databases. And I have an NFS mount to my NAS where it puts the database backups.
This way backups get done through SQL and not file-level.
It supports SQLite (which I think HA uses?), so it’s just a matter of mounting the db file to this container and it should be able to back that up properly.

As for the mentioned permissions issues, you can either run duplicati as root, and there shouldn’t be any issues, or make sure all containers are running as the correct PUID/GUID.

I’m surprised to how difficult it is to find a best-practice for this.

Here’s what I’ve done:

  1. Create an automation in HA to make a backup:
alias: "Backup: Create backup"
description: ""
trigger:
  - platform: time
    at: "03:00:00"
condition:
  - condition: time
    weekday:
      - mon
      - wed
      - sat
action:
  - service: backup.create
    data: {}
mode: single
  1. Install Rclone on the host machine: https://rclone.org/

  2. Create a script on the host machine

#!/bin/bash
# Backup HA
echo "Backing up Home Assistant to pCloud..."
rclone sync /opt/appdata/homeassistant/backups pCloud:/homeassistant_backup/ -P
echo "Backup of Home Assistant database to pCloud completed."
# Remove oldest HA backups
echo "Removing oldest Home Assistant backups..."
ls -t /opt/appdata/homeassistant/backups | tail -n +4 | xargs -I {} rm -f "/opt/appdata/homeassistant/backups/{}"
echo "Finnished removing old Home Assistant backups."
  1. Setup a cron job on the host to run the script each night.

Pro: It works, and by using HA’s own backup functionality, removes the risk of database issues. Also, no need to stop and start Home Assistant.
Con: Ideally, I would have liked to do also the HA backup from the script, but I haven’t managed to figure out how to do that.

Hope this helps someone.

5 Likes

Does HA backup (from UI or calling service as in your case) create also backup of data ( db)?

It helps a lot.
For creating backups from cli or script maybe you can call directly to the backup API service with curl.