Skip to content

Backups

Every machine built so far now gets a backup that runs without you, a second copy that leaves the house, and a restore you have actually tried. The NAS of chapter 11 is the destination.

What How Where When
Home Assistant (configuration, history, pairings) Its own encrypted backup home VM disk, then backup-ha Daily 02:30, copied 03:30
home VM: /srv (Scrypted, Matter, the broker, the stack) and /etc restic backup-home Daily 03:30
infra VM: /srv, /etc, Grafana, Caddy’s certificate authority, the Insights Plus database dump restic backup-infra Daily 03:30
The Mac: your account, this repository, the VM images of step 12.9 Time Machine, encrypted timemachine Hourly
UniFi configuration Auto Backup on the UDM Pro and in your UI account UDM Pro, UniFi cloud Daily
Every secret 1Password 1Password’s servers and each of your devices Always
The NAS itself Hyper Backup, encrypted, to a USB disk Two disks, one always out of the house Weekly

What is not backed up, on purpose: Prometheus metrics and Loki logs (history you can lose), AdGuard’s query log, HomeKit Secure Video recordings (they live in iCloud), and the standby VM (it is a copy of the remote site by nature, and J8 keeps it away from the NAS).

Two tools do the work on the VMs. Home Assistant’s backup is the one format its own restore screen accepts on any machine. restic covers the rest: it encrypts before anything leaves the VM, so the unpatched NAS only ever stores ciphertext, it sends only what changed, and it keeps a history you can browse.

Why. A full backup restores configuration, history, integrations and the HomeKit Bridge pairing in one upload. Home Assistant makes a consistent copy of its database, which a file copy of a running database is not.

In Home Assistant, Settings > System > Backups > Set up backups:

  1. When the screen shows the encryption key, stop and do step 12.2 before you continue.
  2. Backup schedule: Daily, custom time 02:30.
  3. Backups to keep: 7.
  4. Data to include: history on, media on.
  5. Locations: This system. The container install has no network storage screen, so step 12.3 carries the files to the NAS.

Then Back up now > Automatic backup, and wait for it to finish.

Verify.

Terminal window
ssh home 'ls -lh /srv/homeassistant/config/backups | tail -3' # one .tar dated today

Rollback. Settings > System > Backups > Settings: turn the schedule off.

Why. Without this key a Home Assistant backup is noise. It must live outside Home Assistant, because the day you need it is the day Home Assistant is gone.

On the screen of step 12.1, copy the key (or later: Settings > System > Backups > Settings > Encryption key > Show). Then on the Mac:

Terminal window
op item get "Home Assistant backup encryption key" --vault Infrastructure >/dev/null 2>&1 || \
op item create --vault Infrastructure --category password --title "Home Assistant backup encryption key" \
password="$(pbpaste)" >/dev/null
pbcopy </dev/null

Restore path: if the old installation already made encrypted backups, the key moved with it. Store the one the screen shows.

Verify. op read "op://Infrastructure/Home Assistant backup encryption key/password" | wc -c prints about 35 (seven groups of four characters and their dashes), and the value matches the screen.

Rollback. Delete the item.

Do steps 12.3 to 12.5 twice: once with VM=home, once with VM=infra.

Why. One script, the same on both VMs, with a small settings file per VM (vm/home/backup.conf, vm/infra/backup.conf) that says what to copy. The share is mounted only while the job runs: for the other 23 hours and 50 minutes, nothing on the VM can read or delete its own backups.

Mac

Terminal window
VM=home # then: infra
$INFRA/scripts/vm-push.sh $VM
scp -r $INFRA/vm/common/backup $VM:/tmp/backup
ssh $VM 'sudo apt-get install -y restic cifs-utils rsync jq \
&& sudo install -m 755 /tmp/backup/nas-backup /usr/local/sbin/nas-backup \
&& sudo install -m 644 /tmp/backup/nas-backup.service /tmp/backup/nas-backup.timer /etc/systemd/system/ \
&& sudo install -d -m 700 /etc/nas-backup && sudo systemctl daemon-reload'

Then the two secrets, straight from 1Password into root-only files, never through the repository:

Terminal window
printf 'username=backup\npassword=%s\n' "$(op read 'op://Infrastructure/NAS backup user/password')" \
| ssh $VM 'sudo install -m 600 /dev/stdin /etc/nas-backup/smb-credentials'
op read "op://Infrastructure/restic $VM/password" \
| ssh $VM 'sudo install -m 600 /dev/stdin /etc/nas-backup/restic-password'

On infra only, confirm the database name the dump uses, once the Insights Plus container runs:

Terminal window
ssh infra "docker exec unifi-log-insight psql -U unifi -d unifi_logs -c 'select count(*) from logs' | sed -n 3p"

A number means the names in vm/infra/backup.conf are right. An error such as role "unifi" does not exist means this version names them differently: run ssh infra 'docker exec unifi-log-insight env | grep -i postgres', correct the pg_dump line in vm/infra/backup.conf, and push again.

Verify.

Terminal window
ssh $VM 'sudo ls -l /etc/nas-backup; test -f /srv/stack/backup.conf && echo CONF-OK'

Two files, both -rw-------, owner root, then CONF-OK.

Rollback.

Terminal window
ssh $VM 'sudo rm -rf /etc/nas-backup /usr/local/sbin/nas-backup /etc/systemd/system/nas-backup.*; sudo systemctl daemon-reload'

Why. The first run creates the encrypted repository and copies everything, so it is the long one and the one that shows any mistake in the share, the password or the firewall.

Mac

Terminal window
ssh -t $VM 'sudo /usr/local/sbin/nas-backup'

It ends with nas-backup: finished, 1 snapshots kept. A few minutes for infra, a little more for home.

If it stops at mount error(13): Permission denied, the backup account or its password is wrong (step 11.5). mount error(112): Host is down or a timeout means a firewall: DSM rules 4 and 5 of step 11.8, and policy A3 of step 11.10 for home. mount error(95) means the NAS refused encryption: check Transport encryption mode in step 11.6.

Verify.

Terminal window
ssh -t $VM 'sudo /usr/local/sbin/nas-backup shell'
# inside the shell that opens:
restic snapshots # one line, tagged nightly
restic stats latest | tail -3 # the size of what was saved
ls /mnt/nas/*/ # a restic folder
exit
ssh $VM 'mount | grep -c /mnt/nas' # 0: the share is unmounted again

For home, also check that Home Assistant’s file arrived in its own share: from the Mac, open smb://backup@10.0.2.2/backup-ha shows the .tar of step 12.1. Eject it afterwards.

Rollback. To start over, delete the restic folder on the share from the Mac and run the job again.

Why. 03:30 is one hour after Home Assistant has written its own backup, so tonight’s file is the one that gets copied. Persistent=true runs a missed night at the next start.

Mac

Terminal window
ssh $VM 'sudo systemctl enable --now nas-backup.timer && systemctl list-timers nas-backup.timer --no-pager | sed -n 1,2p'

Verify. The list shows the next run at about 03:30. The morning after:

Terminal window
for VM in home infra; do
ssh $VM 'printf "%s: last success %s\n" "$(hostname)" "$(date -d @"$(sudo cat /var/lib/nas-backup/last-success)" "+%F %R")"'
done

Both lines carry today’s date. A failed night shows in ssh $VM 'systemctl status nas-backup.service --no-pager | head -5; journalctl -u nas-backup -n 30 --no-pager'.

Rollback. ssh $VM 'sudo systemctl disable --now nas-backup.timer'.

Why. The nightly job copies Scrypted’s folder while Scrypted runs. That copy has always restored cleanly in practice, yet Scrypted’s own backup file is the form its developer supports, and it is the one that carries the HomeKit pairings of the cameras. One file per month, or after any change of cameras, is enough.

In Scrypted (https://10.0.5.2:10443), Settings > Backup > Download. Save the file as ~/infra-backups/scrypted/scrypted-YYYY-MM-DD.zip on the Mac. Time Machine carries it to the NAS.

Terminal window
mkdir -p ~/infra-backups/{scrypted,unifi,vm-images}
mv ~/Downloads/scrypted*.zip ~/infra-backups/scrypted/scrypted-$(date +%F).zip

Verify. unzip -l ~/infra-backups/scrypted/scrypted-*.zip | tail -1 lists files without an error.

Rollback. None needed.

Why. The Mac holds the working copy of this repository, your SSH configuration, UTM’s settings and the VM images of step 12.9. Time Machine encrypts on the Mac, so again the NAS stores ciphertext.

Mac

  1. In Finder, Go > Connect to Server: smb://backup@10.0.2.2/timemachine. Password: op read "op://Infrastructure/NAS backup user/password" | pbcopy. Tick Remember this password in my keychain.
  2. System Settings > General > Time Machine > Add Backup Disk: choose timemachine, Set Up Disk. Encrypt Backup: on, password from op read "op://Infrastructure/Time Machine encryption/password" | pbcopy. Leave the disk usage limit at None: the share’s quota of step 11.6 is the limit.
  3. Keep the running VM disks out. They change every second, a copy taken while a VM runs cannot be trusted, and each change would resend tens of gigabytes:
Terminal window
sudo tmutil addexclusion -p ~/Library/Containers/com.utmapp.UTM/Data/Documents
sudo tmutil addexclusion -p ~/vm-base
sudo tmutil addexclusion -p ~/infra-migration
tmutil startbackup --auto

~/vm-base holds the empty base images of step 5.15, which chapter 5 rebuilds in an hour. ~/infra-migration is already on the external disk of step 2.14.

Verify.

Terminal window
tmutil destinationinfo | grep -E "Name|Kind|URL" # Kind: Network, URL ends in /timemachine
tmutil status | grep -E "Running|Percent" # Running = 1 during the first backup
tmutil isexcluded ~/Library/Containers/com.utmapp.UTM/Data/Documents # [Excluded]

The first backup takes a few hours. When it is done, Time Machine in System Settings shows Latest backup: today.

Rollback. System Settings > General > Time Machine: select the disk and click −. sudo tmutil removeexclusion -p <path> for each exclusion.

Why. Step 10.12 turned on Auto Backup, yet those files sit on the UDM Pro itself. If the device dies, they die with it. The UI account copy and one file on the Mac cover that.

UniFi Settings > Control Plane > Backups: Cloud Backup on (it stores the configuration in your UI account, encrypted in transit and at rest). Then Download and:

Terminal window
mv ~/Downloads/*.unf ~/infra-backups/unifi/unifi-$(date +%F).unf 2>/dev/null || mv ~/Downloads/*.unifi ~/infra-backups/unifi/unifi-$(date +%F).unifi
ls -lh ~/infra-backups/unifi | tail -2

Verify. The backup list shows a cloud entry dated today, and the folder holds today’s file.

Rollback. Turn Cloud Backup off.

12.9 A full image of each VM, after every monthly restart

Section titled “12.9 A full image of each VM, after every monthly restart”

Why. restic brings back the data, and chapter 5 brings back the system in about an hour. A full image skips that hour: copy one file back and the VM starts as it was. A VM must be off for its image to be trustworthy, and the monthly restart of chapter 15 is the moment both are off anyway. The copy is an APFS clone, so it takes a second and no extra space until the VM changes, and Time Machine then carries it, encrypted, to the NAS.

Mac, with the VMs stopped (vm-down, chapter 15):

Terminal window
mkdir -p ~/infra-backups/vm-images
for VM in home infra; do
rm -rf ~/infra-backups/vm-images/$VM.utm
cp -Rc ~/Library/Containers/com.utmapp.UTM/Data/Documents/$VM.utm ~/infra-backups/vm-images/$VM.utm
done
du -sh ~/infra-backups/vm-images/*

The standby VM is left out: it holds another household’s data, and it is rebuilt from the remote site, not from a copy in this house.

Verify. Two folders, each the size of the VM’s used disk. After the next Time Machine run, tmutil listbackups | tail -1 is newer than the copy.

Rollback. rm -rf ~/infra-backups/vm-images.

12.10 Hyper Backup to two rotating USB disks

Section titled “12.10 Hyper Backup to two rotating USB disks”

Why. Fire, theft and a power surge take the Mac and the NAS together. The NAS has no internet (step 11.10), so the copy leaves on a disk: two of them, one plugged in, one at another address, swapped every month. Everything on the backup shares is already encrypted by its sender. Hyper Backup’s own encryption adds a layer for the disk that travels.

You need two USB disks, each at least the size of the data on the four shares.

NAS

  1. If Hyper Backup is not installed: pause policy A1 of step 11.10 for five minutes, install it from Package Center, and resume A1.
  2. Plug in the first disk. Control Panel > External Devices: format it as ext4 if it is new.
  3. Hyper Backup > + > Data backup task > Local folder & USB, Single-version off (you want versions). Destination: the USB disk, folder nas-offsite.
  4. Source: the four shares backup-ha, backup-home, backup-infra, timemachine.
  5. Schedule: weekly, Sunday 06:00. Enable integrity check: monthly. Enable client-side encryption: on, password from op read "op://Infrastructure/NAS Hyper Backup encryption/password" | pbcopy. DSM offers a key file to download: attach it to that 1Password item.
  6. Rotation: Smart Recycle, 26 versions.
  7. Back up now.
  8. Repeat steps 2 to 7 with the second disk as a second task named offsite-B. A task whose disk is absent fails quietly, which is expected for the disk that is away.

Put a monthly reminder in your calendar: swap the disks. Chapter 15 lists it in the monthly routine.

Verify. Hyper Backup shows the task Successful with today’s date. Backup Explorer (the magnifier icon) opens the version and lists the four shares.

Rollback. Delete the task. The disk keeps its last version.

A backup that has never been restored is a hope. Do these four once now, and the first two again every six months (chapter 15).

Mac

Terminal window
ssh -t infra 'sudo /usr/local/sbin/nas-backup shell'
# inside:
restic restore latest --target /tmp/restore-test --include /srv/adguard/conf/AdGuardHome.yaml
diff /tmp/restore-test/srv/adguard/conf/AdGuardHome.yaml /srv/adguard/conf/AdGuardHome.yaml && echo RESTORE-OK
rm -rf /tmp/restore-test
exit

Verify. RESTORE-OK, or a short diff if you changed a setting since last night.

12.12 Restore the Insights Plus dump into a scratch database

Section titled “12.12 Restore the Insights Plus dump into a scratch database”

Mac

Terminal window
ssh infra 'sudo cat /var/backups/nas-backup/insights.dump | docker exec -i unifi-log-insight sh -c "
createdb -U unifi restore_test && pg_restore -U unifi -d restore_test --no-owner &&
psql -U unifi -d restore_test -Atc \"select count(*) from logs\" && dropdb -U unifi restore_test"'

Verify. A number close to the count of step 12.3. The scratch database is gone: ssh infra "docker exec unifi-log-insight psql -U unifi -lqt | grep -c restore_test" prints 0.

12.13 Restore Home Assistant into a throwaway container

Section titled “12.13 Restore Home Assistant into a throwaway container”

Why. This proves the file and the key together, without touching the real Home Assistant. The test container listens on the infra VM’s loopback only, and you reach it through SSH.

Mac

Terminal window
open smb://backup@10.0.2.2/backup-ha # copy the newest .tar to ~/Downloads, then eject
ssh infra 'docker run -d --rm --name ha-restore-test -p 127.0.0.1:8124:8123 ghcr.io/home-assistant/home-assistant:stable'
ssh -N -L 8124:127.0.0.1:8124 infra &
open http://localhost:8124

On the welcome screen choose Restore from backup > Upload backup, pick the .tar, and paste the key: op read "op://Infrastructure/Home Assistant backup encryption key/password" | pbcopy. Wait for the restart and log in with your usual account.

Verify. Your dashboards and entities are there (devices show unavailable, which is right: this copy is on another network and must not talk to them). Then remove it:

Terminal window
kill %1; ssh infra 'docker stop ha-restore-test'

Do not leave it running: a second Home Assistant with the same HomeKit Bridge identity confuses the Home app if it ever reaches the IoT network.

Mac Open Time Machine from the menu bar, Browse Time Machine Backups, go back one day, select a file in ~/infra-backups, and Restore it to the Desktop.

Verify. The file opens. Delete the copy.

Lost Bring it back with
One file or folder on a VM Step 12.11 with the right path, then copy it into place
Home Assistant configuration broken Settings > System > Backups, pick a night, Restore
The home VM Its image from ~/infra-backups/vm-images (step 12.9) dropped back into UTM, then last night’s Home Assistant backup. Without an image: chapter 5, then chapter 7 from scratch with Restore from backup at the welcome screen, then restic restore latest --target / --include /srv/scrypted --include /srv/matter --include /srv/mosquitto
The infra VM Its image, or chapter 5, then scripts/vm-push.sh infra, restic restore latest --target / --include /srv --include /var/lib/docker/volumes, the stack up, and pg_restore as in step 12.12 into unifi_logs
The Mac A new Mac, chapter 4 steps 4.1 to 4.7, then Migration Assistant > From a Time Machine backup with smb://backup@10.0.2.2/timemachine. The VM images come back with it
The UDM Pro A replacement, set up with Restore from backup from the UI account or the .unf file
The NAS A new NAS, chapter 11, then Hyper Backup Restore from the USB disk. The VMs fill their shares again on the next night either way
The house The off-site USB disk, the UI account, 1Password, and this guide

On a rebuilt VM, restic needs its repository password before the first restore: repeat step 12.3, then nas-backup shell.

Each night Home Assistant and both VMs save themselves to the NAS in encrypted form, the Mac follows every hour, and once a week the NAS copies everything to a disk that leaves the house. You have restored from each of them once.