nixos/BACKUPS.md
agent 79647356f4 Add nightly restic backups to Scaleway Object Storage
Three restic jobs share one repository:
- panel (03:30): /var/lib/containers and rootless named volumes, with the
  panel's containers stopped for a consistent copy and started again after
- system (04:00): /etc, /home, /root, /var/lib, plus consistent .dump
  copies of the Forgejo, Nextcloud, Authelia and panel SQLite databases
- maintenance (Sun 05:30): forget/prune and a 2 GB sampled check

Scaleway bills storage and egress, not requests, so objects go to One
Zone, /nix/store and images are left out, all jobs share one cache, and
pruning and checks are weekly and bounded.

Needs secrets/restic-password.age and secrets/restic-env.age to be
created before deploying; BACKUPS.md has the steps and a restore runbook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gUd5tW9VHStkEKmgqu
2026-10-27 08:36:51 +00:00

8 KiB

Backups

Nightly restic backups of the VPS to a Scaleway Object Storage bucket, configured in backups.nix.

What is backed up

One encrypted, deduplicated restic repository holds two kinds of snapshots:

Tag When (Europe/Amsterdam) Paths Notes
panel daily 03:30 /var/lib/containers (stacks, volumes, routes, panel state), ~reudy/.local/share/containers/storage/volumes The panel's running containers are stopped for the duration (usually under a minute) and started again afterwards, so databases inside them are copied consistently.
system daily 04:00 /etc, /home, /root, /var/lib, /var/backup Forgejo, Nextcloud, Authelia, the panel's database, SSH host keys, /var/lib/nixos (uid/gid map), the hello container, and so on.

Before the system snapshot, the SQLite databases (Forgejo, Nextcloud, Authelia, panel) are dumped to /var/backup/sqlite/<name>.sql. These dumps are consistent even though the services keep running. The raw .db files are backed up as well, but a copy taken mid-write can be torn, so restore from the dumps if a raw file looks broken.

Not backed up (it can all be rebuilt): /nix/store (rebuilt from this flake), container images (pulled or built again on deploy), ~/.cache, Nextcloud previews, core dumps.

Retention, applied every Sunday at 05:30 by restic-backups-maintenance: 7 daily, 4 weekly and 6 monthly snapshots, per tag. The same run prunes unreferenced data and checks the repository, reading a random 2 GB of it.

Cost notes

Scaleway doesn't charge for API requests. It charges for stored GB-hours, egress above 75 GB a month, and Glacier restores. So:

  • objects are uploaded as One Zone (ONEZONE_IA), half the price of Multi-AZ. The class is set per object at upload time (extraOptions in backups.nix), so changing it only affects new uploads;
  • the backup itself downloads almost nothing. All jobs share one local cache in /var/cache/restic, so metadata is fetched once;
  • pruning (which may download and re-pack partly-used data) runs weekly, not after every backup;
  • check reads a fixed 2 GB sample instead of the whole repository;
  • Glacier isn't used. It stores more cheaply, but every restore and every pruning repack would first need a paid, hours-long thaw.

sudo restic-system stats --mode raw-data shows how much is actually stored.

One-time setup

The two secrets are agenix files. Create them on a machine that holds one of the user keys in secrets.nix, from a checkout of this repo:

# 1. The repository password. Generate a long random one (e.g. in your password
#    manager) and keep it there: without it the backups cannot be read.
agenix -e secrets/restic-password.age

# 2. Where the repository is and how to reach it. Four lines, no quotes:
agenix -e secrets/restic-env.age
RESTIC_REPOSITORY=s3:https://s3.<region>.scw.cloud/<bucket>/vps
AWS_ACCESS_KEY_ID=SCW...
AWS_SECRET_ACCESS_KEY=...
AWS_DEFAULT_REGION=<region>

<region> is the bucket's region: fr-par, nl-ams or pl-waw. Use an API key from a dedicated Scaleway IAM application whose policy only grants Object Storage in the bucket's project, not your personal key.

Store the password, the access key pair, the bucket name and the region in your password manager too. The .age files can be decrypted with your SSH keys, but the canonical copy of this repo lives on the server you'd be recovering.

Then commit the two .age files, deploy, and run the first backups by hand:

sudo nixos-rebuild switch --flake .#vps

# Optional: seed the panel data without stopping anything. The real run below
# then uploads only what changed, so the apps are down for seconds, not for
# the whole first upload.
sudo restic-panel init   # only if the repository doesn't exist yet
sudo restic-panel backup --tag seed /var/lib/containers

sudo systemctl start restic-backups-panel
sudo restic-panel forget --tag seed          # its data stays, shared with the real snapshot

sudo systemctl start restic-backups-system   # the first upload can take a while
journalctl -u restic-backups-system -f

Day to day

systemctl list-timers 'restic-*'                  # next / last runs
systemctl status restic-backups-{panel,system,maintenance}
journalctl -u restic-backups-system -n 100

sudo restic-system snapshots                      # restic-<job> wrappers work
sudo restic-system snapshots --tag panel          # all point at the same repo

A failed run leaves the unit in failed state. There is no alerting yet, so systemctl --failed is the place to look.

Restoring files

sudo restic-system snapshots --tag system
sudo restic-system ls latest --tag system /var/lib/forgejo
sudo restic-system restore latest --tag system \
  --include /var/lib/forgejo/data/forgejo.db --target /root/restore

Restores keep owners and permissions. Stop the affected service before copying files back into place.

A panel app

panelctl stop <app>
sudo restic-panel restore latest --tag panel \
  --include /var/lib/containers/volumes/<app> --target /
panelctl deploy <app>

A SQLite database from its dump

sudo systemctl stop forgejo
sudo -u forgejo mv /var/lib/forgejo/data/forgejo.db{,.broken}
sudo -u forgejo sqlite3 /var/lib/forgejo/data/forgejo.db < /root/restore/var/backup/sqlite/forgejo.sql
sudo systemctl start forgejo

Disaster recovery: rebuilding the server

On NixOS, "the whole server" is this flake plus the state in the backup.

  1. Get a fresh NixOS VPS (vpsAdminOS NixOS template) and SSH in as root. If the IP address changed, update the DNS records for *.reudy.net.

  2. Get restic and the credentials into the shell. Don't paste them on the command line:

    nix --extra-experimental-features 'nix-command flakes' shell nixpkgs#restic nixpkgs#git nixpkgs#rsync
    read -rs -p 'Scaleway access key: ' AWS_ACCESS_KEY_ID; echo
    read -rs -p 'Scaleway secret key: ' AWS_SECRET_ACCESS_KEY; echo
    read -rs -p 'Restic password: '     RESTIC_PASSWORD; echo
    export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY RESTIC_PASSWORD
    export AWS_DEFAULT_REGION=<region>
    export RESTIC_REPOSITORY=s3:https://s3.<region>.scw.cloud/<bucket>/vps
    restic snapshots
    
  3. Restore both latest snapshots to a staging directory:

    restic restore latest --tag system --target /restore
    restic restore latest --tag panel  --target /restore
    
  4. Put identity back first: the SSH host keys (agenix decrypts the secrets with them) and the uid/gid map (so restored files keep the right owners):

    cp -a /restore/etc/ssh/ssh_host_* /etc/ssh/
    cp -a /restore/var/lib/nixos/. /var/lib/nixos/
    
  5. Get the flake. Forgejo isn't running yet, so clone it from the restored repository, and point the panel input at the restored panel repository:

    git clone /restore/var/lib/forgejo/repositories/reudy-net/nixos.git /root/nixos
    cd /root/nixos
    nixos-rebuild boot --flake .#vps \
      --override-input panel 'git+file:///restore/var/lib/forgejo/repositories/reudy-net/panel.git?ref=main'
    
  6. Copy the state into place and boot into the new system:

    rsync -aHAX /restore/var/lib/ /var/lib/
    rsync -aHAX /restore/home/    /home/
    rsync -aHAX /restore/root/    /root/
    reboot
    

    Caddy fetches new certificates by itself. If a service won't start because its database is damaged, load the dump from /var/backup/sqlite (above). Then run passwd reudy if sudo doesn't accept the old password.

  7. Bring the panel apps back: their compose files, volumes and settings are restored, but the images are not:

    panelctl list
    panelctl deploy <app>   # for each app
    
  8. Once Forgejo is up, rebuild once more without --override-input, check systemctl --failed, then delete /restore.

Practise this once on a throwaway VPS. Until a restore has worked, the backup hasn't been tested.