Backup Hassio in Docker

I’m running Hassio in a Docker on a Ubuntu machine.
How and what should I backup to be able to restore my system if it crashes?

Create regular snapshots and copy them of the host machine. There are addons to help you automate this.

e.g.

I would like to have the backups automated, i.e ones a week or something.
I have a NAS and would like to use it for storing the backups.
Further would it be good if the backups are versionhandled.

Is it only the snapshots that need to be backuped?
Or do I need to backup more files, Docker setup and so on

Only the snapshots. You can redeploy the hassio docker container(s).

The way I get my snapshots onto my NAS is by using a scheduled copy using freefilesync on another always on PC. I then use this to clean up the snapshots, GitHub - tmonck/clean_up_snapshots: Service to clean up your home assistant snapshots, so you don't manually have to.

It depends how often you are updating your config but daily is probably better than weekly.

1 Like

Snapshot are all you need to restore everything setup inside HassIO.
If your system goes bang, you can install host OS (hopefully you’ve fully documented this) then just re-run the HassIO generic Linux install script. Finally, restore from your latest snapshot.


As for automated backup. I first have automation that runs at 2am each day, to generate a snapshot.

- alias: Schedulled snapshot
  description: 'Ties up with 2:25 sudo crontab script '
  trigger:
  - at: '2:01'
    platform: time
  condition: []
  action:
  - data_template:
      name: '{{now().strftime(''%d.%m.%y'')}}'
    service: hassio.snapshot_full

Then at 2:25, I have a crontab task running a dirty (thrown together) shell script to do the following:

  1. Check and mount NAS directory
  2. Rsync NAS directory with HassIO backup folder (copy the new snapshot across)
  3. Delete all backups 3 days or older, but keep all Monday backup (mimic father-son backup scheme)
  4. Delete all backups over 5 weeks old (in another words, delete the old "father"s)
    (steps 3 and 4 are done across both HassIO backup folder and NAS directory)

Throughout the script, single word log files are generated. HA can use file sensor to read in those files. I’ve been meaning to tidy this up to generate a single JSON for HA…

If you are interested, I can post my script when I get home later :slight_smile:

1 Like

I have a different setup, but you still may find this info useful.

I have proxmox on my host and run ubuntu with docker + Hass on top of that inside a VM. I connected proxmox to my NAS and setup backups in proxmox. This makes a full snapshot of the VM daily and stores it on the NAS. When my host breaks down (I hope not :rofl:), I buy a new one, install proxmox, restore the VM snapshot and I’m back where I left, no ubuntu install, no docker install.

2 Likes

wyx087, please share your script

Burningstone, if I want to use your solution.
It it easy to go from my solution today to yours?

I found it pretty easy, I think there are even some guides here on the forum for the setup. What kind of hardware are you using?

This is my setup as well, glad to see someone else doing the same.

Did you set it up to automatically take the snapshot daily? Does that stop the VM? I know when I manually do snapshots through proxmox it’ll stop the VM, but if I can automate it and do it during overnight it shouldnt be too bad.

Let’s assume you’ll be installing Raspbian as host OS and use the generic Linux install method I linked earlier.

First, you need to setup your NAS mount in fstab. Tutorials all over the place, for example.

Then, you’ll need to edit this script to match directories on your install. Change:

  • /var/ramdisk
  • LOG_* locations
  • mount location
  • rsync directories
  • file delete directories

This script first synchronises NAS mount directory to backup folder, it then reads file modification dates and delete old snapshots accordingly.
(May be better the other way? Delete locally first, then sync folders… hum… :thinking: )

Finally, remember to set the script as crontab task.

#!/bin/bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/var/ramdisk/snapshot_rsync.log 2>&1
# Everything below will go to the log file

LOG_MOUNT=/usr/share/hassio/homeassistant/sensors/snapshot_mount.log
LOG_RSYNC=/usr/share/hassio/homeassistant/sensors/snapshot_rsync.log
LOG_SIZE=/usr/share/hassio/homeassistant/sensors/snapshot_size.log
LOG_DATE=/usr/share/hassio/homeassistant/sensors/snapshot_date.log

mount="/media/timemachinehome"

if grep -qs "$mount" /proc/mounts; then
  echo "NAS is already mounted."
  echo "Success" > $LOG_MOUNT
else
  echo "NAS need to be mounted"
  mount "$mount"
  if [ $? -eq 0 ]; then
   echo "Mount success!"
   echo "Success" > $LOG_MOUNT
  else
   echo "Something went wrong with the mount..."
   echo "Failed" > $LOG_MOUNT
   exit 1
  fi
fi


echo [`date +"%Y-%m-%d %H:%M:%S"`] !! Copying files to NAS

rsync -av /usr/share/hassio/backup/ /media/timemachinehome/hassio_snapshot/
if [[ $? -gt 0 ]]
then
  echo "Failed" > $LOG_RSYNC
else
  echo "Success" > $LOG_RSYNC
fi

echo [`date +"%Y-%m-%d %H:%M:%S"`] !! Copy completed


# File sensor input
ls -hlt /media/timemachinehome/hassio_snapshot/ | head -n 2 | tail -n 1 | awk {'print $5'} > $LOG_SIZE
ls -hlt /media/timemachinehome/hassio_snapshot/ | head -n 2 | tail -n 1 | awk {'print $7, $6'} > $LOG_DATE


echo [`date +"%Y-%m-%d %H:%M:%S"`] !! Deleting old files NAS

# First delete recent old backups, but keep Monday backup
find /usr/share/hassio/backup/ -type f -mtime +3 \
  -exec sh -c 'test $(date +%u -r "$1") = 1 || rm "$1"' -- {} \;
find /media/timemachinehome/hassio_snapshot/ -type f -mtime +3 \
  -exec sh -c 'test $(date +%u -r "$1") = 1 || rm "$1"' -- {} \;

# Now go back 5 weeks and delete older files
find /usr/share/hassio/backup/ -type f -name '*.tar' -mtime +40 -exec rm -v {} \;
find /media/timemachinehome/hassio_snapshot/ -type f -name '*.tar' -mtime +40 -exec rm -v {} \;


echo [`date +"%Y-%m-%d %H:%M:%S"`] !! Delete completed

Yes I set it up automatically at 3 in the morning. I set the backup mode to snapshot and my VM never stop aa far as I see in the logs. Even if it would stop the VM, the whole backup takes less than 15 minutes.

Burningstone, I’m using a Ubuntu machine with the Docker.

I want to know what hardware you are using, a Rasperry Pi, a NUC, a Server?

okay, I use a NISE 3720E computer
http://www.nexcom.com/Products/industrial-computing-solutions/industrial-fanless-computer/core-i-performance/fanless-pc-fanless-computer-nise-3720e

Seems powerful enough for running VMs inside Proxmox.

Is it difficult to run VMs inside Promox?
I have zero experience of that.

I think it’s really easy. You can do it through the Web GUI.

Burningstone, are you using Hassio or Ha?
I’m using Hassio and would like to continue with that.
So you run a VM inside Promox. In the VM do you run Hassio/HA.
Is that correct understod?

I have multiple VMs. In the production VM I use Home Assistant in Docker, in the development VM i also use Home Assistant in Docker and in the test VM i hass.io.

Just look at a VM as a separate computer, you can install any OS (or almost any) on it.