What backup strategy when running Home Assistant in Docker?

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