Intro

The September 2022 update for Home Assistant just dropped, so it’s time to upgrade. Before jumping to upgrading, however, it’s always a good idea to pause and create a backup first.

Let’s check out a few options for backing up Home Assistant Container.

Home Assistant Backup Integration

A first, and obvious, choice is using the built-in backup integration.

Unless you removed the default_config: entry from your configuration.yaml, the backup integration is enabled by default. If you did remove this line, you can selectively enable the backup integration by adding backup: to the HA configuration.

With this integration enabled, taking a backup is a simple 3-step process:

  1. In Home Assistant, go to Settings > System > Backups.
  2. Click the + Create Backup button on the bottom-right and Create in the pop-up window.
  3. Once the backup is created, download it to your local system by clicking the button to the right of the created backup.

And you’re done :) In case an upgrade fails, we can restore Home Assistant using the backup file to rollback the changes.

Create a Home Assistant backup
Creating a Home Assistant Backup

Backing up Docker volumes

The most important data within Home Assistant is stored within the /config folder inside the container. This folder is exposed to our Docker volume at /opt/homeassistant/config. So a backup of this folder, would keep our HA data safe.

Because a running Home Assistant could cause inconsistencies in the backed up data, it’s a good idea to temporarily stop the HA Container while copying the data. We can achieve this using a small script, which we could even run periodically.

We use rsync to copy the data, as it has some nice features which are ideal for doing periodic automated backups. You may need to install it on your system first.

sudo apt update
sudo apt install rsync

Next we write our script to perform the backup.

sudo nano /opt/homeassistant/backup.sh

# Copy/paste the lines between the '---' and save with Ctrl+X
# Also copy the 'shebang' starting with #!

# ---
#!/bin/bash
readonly SOURCE_DIR="/opt/homeassistant/config"
readonly BACKUP_DIR="/mnt/backup/homeassistant"
readonly BACKUP_PATH="${BACKUP_DIR}/$(date '+%F')"
readonly LATEST_LINK="${BACKUP_DIR}/latest"

mkdir -p "${BACKUP_DIR}"

docker stop homeassistant

rsync -a --delete \
  "${SOURCE_DIR}/" \
  --link-dest "${LATEST_LINK}" \
  "${BACKUP_PATH}"

rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"

docker start homeassistant
# ---

# Make file executable
sudo chmod u+x /opt/homeassistant/homeassistant/backup.sh

The content of the Home Assistant configuration directory will be compared to our latest backup directory, which actually is a symbolic link to the most recent back-up folder. New and modified files existing in the source directory will be copied to the backup directory, which is timestamped. Files deleted in the source directory will also be removed in the backup directory (--delete). Finally, files that didn’t change between two back-ups will be visible in the back-up directory through hards links pointing to the originally backed up file (--link-dest).

On the first run, we won’t have a latest directory to compare against, so rsync will do a full backup.

Once rsync has finished backing up, we remove the latest link and replace it with a link to the backup we just completed.

Backup and latest link
Our first backup and the ’latest’ link

Automatic backups

We can now have this backup run automatically using a cronjob.

sudo crontab -e

# Add the following lines at the end of the file
30 23 * * 6 /opt/homeassistant/homeassistant/backup.sh > /dev/null
45 23 * * 6 docker start homeassistant > /dev/null

The commands above will run the backup every Saturday at 23:30 (11:30 PM). We also add a fail-safe that will start the Home Assistant Docker container at 23:45 should the backup script have failed somehow.
The output of the scripts is sent to the void of /dev/null, however error messages will be logged.

Backing up via Duplicati

Duplicati is a nice tool to do scheduled backups. It not only allows you to store backups on a local or attached drive, but even to many differen cloud storage locations. And backups can be restored on a file-level, so you don’t have to overwrite everything if you ever need to restore a system. Duplicati even gives you the option to encrypt your backups. Useful for when you backup to a remote system.

docker-compose.yaml

Duplicati is a Docker container, so we again add an entry to our docker-compose.yaml.

services:
  [...]

  duplicati:
    container_name: duplicati
    image: duplicati/duplicati
    restart: unless-stopped
    ports:
      - "8200:8200/tcp"
    environment:
      - TZ=Europe/Brussels
    volumes:
      - /opt/duplicati/data:/data
      - /mnt/backup/duplicati:/backups
      - /opt:/source

Running docker-compose up -d launches the Duplicati container which will host a web portal at http://<ip.of.our.box>:8200.

Home Assistant sidebar

We’ve done it a few times before. Add an entry for Duplicati to the sidebar of our Home Assistant dashboard using the panel-iframe.

Add the following lines to configuration.yaml (via the terminal or using Configurator):

panel_iframe:
  portainer:     # part 3
    [...]
  nodered:       # part 5
    [...]
  configurator:  # part 6
    [...]
  duplicati:
    title: Duplicati
    icon: mdi:backup-restore
    url: http://192.168.10.106:8200
    require_admin: true

After restarting Home Assistant, we’re able to open Duplicati from within the Home Assistant dashboard.

Configure backup

We can now open the Duplicati web portal via http://<ip.of.our.box>:8200 or via the Home Assistant sidebar and add a new backup.

Enter the details if your backup. For example:

  1. General
    • Name: Home Assistant
    • Encryption: No encryption
  2. Destination
    • Storage Type: Local folder or drive
    • Manually type path: /backups/homeassistant/
      Test connection and create the directory.
  3. Source Data
    • Source data: /source/homeassistant/
  4. Schedule
    • Automaticall run backups
    • Next time: next Wednesday at 23:30
    • Run again every: 1 weeks
    • Allowed days: Wed
  5. Options
    • Backup retention: Smart backup retention

Once you saved the configuration, you can Run now to make a first backup and confirm everything works as expected.

Duplicati dashboard
Backing up Home Assistant

Upgrade

We now have 3 methods of backing up our Home Assistant install. So we should be safe to update Home Assistant to the latest version.