Sub topic 3
Checking that the repository is healthy
This chapter assumes you have a working restic or Borg repository from the earlier chapters, can edit systemd units, and can run commands as root. Examples use restic 0.16 or later and Borg 1.2 or later.
Checking repository integrity
A repository can be damaged by a full disk, an interrupted upload, or a failing disk. Run a check after each prune and at least weekly.
restic checkBy default, restic check reads the index and metadata only. To read every stored data block, add --read-data. This is slow on large repositories, so run it on a schedule rather than after every backup.
restic check --read-dataFor Borg, borg check verifies the repository structure, and borg check --verify-data reads and verifies the data chunks.
borg check
borg check --verify-dataBacking up databases consistently
Copying a live PostgreSQL data directory can produce a backup that does not start. Dump the database first, then stream the dump into the repository:
sudo -u postgres pg_dumpall | restic backup --stdin --stdin-filename pg_dumpall.sqlScheduling backups with systemd
A timer with Persistent=true runs a missed job after the server comes back up. Confirm that the timer is active and see when it next runs:
systemctl list-timers restic-backup.timerProtecting passphrases and keys
A repository cannot be read without its passphrase. Keep the passphrase file readable only by root:
chmod 600 /etc/restic/passwordFor Borg, export the repository key and store the copy off the server:
borg key export :: /root/borg-key-backup.txt> Keep a copy of the passphrase and key somewhere other than this server. If the server is lost, the backups are useless without them.
Write that as a callout instead:
Knowing when a backup fails
A timer that runs without errors is not proof that backups work. Add OnFailure= to the backup service so systemd starts an alert unit when the backup fails:
# In /etc/systemd/system/restic-backup.service, under [Unit]
OnFailure=notify-backup-failed.service# /etc/systemd/system/notify-backup-failed.service
[Unit]
Description=Log a failed backup
[Service]
Type=oneshot
ExecStart=/usr/bin/logger -p user.crit "restic-backup failed on %H"Read the cause in the journal:
journalctl -u restic-backup.service -n 50Practicing a full recovery
A backup only counts once a restore works. Restore a path into a scratch directory and compare it with the live files:
restic restore latest --target /tmp/drill --include /etc/nginx
diff -r /etc/nginx /tmp/drill/etc/nginxExpect differences only in files that changed after the snapshot. Time the drill and compare the result with your recovery time goal.
Exercise
Exercise
After a prune, you want to confirm that every stored data block in a restic repository is readable. Which command do you run, and why is it slower than a plain check?
Show answer
Run restic check --read-data. A plain check reads only the index and metadata. The --read-data option reads and verifies every data pack in the repository, so it takes longer and uses more bandwidth.