Homelab

Nightly restic backups to the NAS

Every node runs restic at 04:00 into one repository on the NAS. A systemd timer starts it, and only the NAS prunes.

Proxmox’s weekly dumps restore a whole guest. Most of the time I need one file from yesterday, so every node now runs restic 0.17.3 at 04:00 into one repository on the NAS, at /srv/restic. restic deduplicates across the three nodes and encrypts everything before it leaves them.

A user and a repository

restic reaches the NAS over SFTP as a user that owns the repository and nothing else. On the NAS:

$ sudo useradd --create-home --shell /bin/sh restic
$ sudo install -d -o restic -m 700 /srv/restic

Then once, from pve-1:

$ restic -r sftp:restic@nas:/srv/restic init
enter password for new repository:
enter password again:
created restic repository 1c9f02ab7e at sftp:restic@nas:/srv/restic

The password goes into /root/.restic-password on each node, mode 600, and into the password manager. Without it the repository is noise.

The script

#!/usr/bin/env bash
set -euo pipefail

export RESTIC_REPOSITORY=sftp:restic@nas:/srv/restic
export RESTIC_PASSWORD_FILE=/root/.restic-password

restic backup --one-file-system --exclude-caches \
  --tag nightly /etc /root /var/lib/vz

--one-file-system keeps it out of /mnt/pve, where the share with the weekly dumps is mounted. Backing up the backups once was enough.

The timer

[Unit]
Description=restic backup to the NAS
OnFailure=mail-root@%n.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
IOSchedulingClass=idle
[Unit]
Description=Nightly restic backup

[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true

[Install]
WantedBy=timers.target

Persistent=true catches up on a run missed while the node was off. OnFailure= starts a unit that mails the service’s status to root, and root’s mail goes to the address in /etc/aliases.

$ sudo systemctl enable --now restic-backup.timer

Pruning on the NAS

Three nodes pruning one repository would fight over its lock, so only the NAS forgets old snapshots, on Sundays at 06:00, when the nodes are long done.

$ restic -r /srv/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

The first restore

A backup counts once a restore has worked. This one brought back yesterday’s /etc/hosts of pve-2, and diff found nothing to say:

$ restic -r sftp:restic@nas:/srv/restic restore latest --host pve-2 --include /etc/hosts --target /tmp/restore
$ diff /etc/hosts /tmp/restore/etc/hosts

The next chapter puts all four chapters into one Ansible repository.

Comments

Buy Logbuch