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
This commit is contained in:
agent 2026-10-27 08:36:51 +00:00
parent 9aa395814c
commit 79647356f4
4 changed files with 391 additions and 0 deletions

176
backups.nix Normal file
View file

@ -0,0 +1,176 @@
{ config, lib, pkgs, ... }:
# Nightly restic backups to Scaleway Object Storage. BACKUPS.md covers what is
# kept, how to check on it and how to restore (including a full rebuild).
#
# Everything goes into one repository, as three jobs:
# system - host state: /etc, /home, /root, /var/lib (minus the panel)
# panel - panel apps: /var/lib/containers plus rootless named volumes,
# with the app containers stopped for a consistent copy
# maintenance - weekly forget/prune and a small check, no backup
#
# Scaleway doesn't charge for requests, only for stored GB-hours, egress over
# 75 GB a month and Glacier restores. So the settings below keep the stored size
# small and avoid downloads: no /nix/store (rebuilt from the flake), one shared
# local cache so metadata is fetched once, pruning only weekly, and checks that
# read a fixed 2 GB sample instead of the whole repository.
let
panel = config.services.reudy-panel;
# Rootless Podman keeps named volumes (and images) in the user's home.
panelPodmanStorage = "${config.users.users.${panel.user}.home}/.local/share/containers/storage";
# Consistent SQLite copies land here and are picked up by the system job.
sqliteDumpDir = "/var/backup/sqlite";
common = {
passwordFile = config.age.secrets."restic-password".path;
# RESTIC_REPOSITORY, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and
# AWS_DEFAULT_REGION; see BACKUPS.md for the format.
environmentFile = config.age.secrets."restic-env".path;
initialize = true;
extraOptions = [
# One Zone costs half as much as Multi-AZ and is enough for a copy that
# already lives away from the server. It only applies to newly uploaded
# objects, so changing it later doesn't move what's already stored.
"s3.storage-class=ONEZONE_IA"
];
};
# Copies each SQLite database with `.dump`, which reads inside a single
# transaction, so the copy is consistent even while the service writes. It
# runs as the database's owner so it can't leave root-owned -wal/-shm files
# behind that would lock the service out. A failed dump only warns: the live
# files are still backed up, and one bad database shouldn't stop the rest.
dumpSqlite = name: db: ''
if [ -f ${lib.escapeShellArg db} ]; then
owner=$(stat -c %U ${lib.escapeShellArg db})
if runuser -u "$owner" -- sqlite3 ${lib.escapeShellArg db} .dump > ${sqliteDumpDir}/${name}.sql.tmp; then
mv ${sqliteDumpDir}/${name}.sql.tmp ${sqliteDumpDir}/${name}.sql
else
echo "WARNING: dumping ${name} failed, keeping the previous dump" >&2
rm -f ${sqliteDumpDir}/${name}.sql.tmp
fi
else
echo "skipping ${name}: ${db} does not exist"
fi
'';
# Podman as the panel's user, the same way the panel itself runs it. The
# user has no fixed uid in this config, so look it up.
podmanAsPanelUser = ''
podman_panel() {
runuser -u ${panel.user} -- env XDG_RUNTIME_DIR="/run/user/$(id -u ${panel.user})" podman "$@"
}
'';
stoppedContainers = "/run/restic-backups-panel/stopped-containers";
in
{
age.secrets."restic-password".file = ./secrets/restic-password.age;
age.secrets."restic-env".file = ./secrets/restic-env.age;
systemd.tmpfiles.rules = [ "d ${sqliteDumpDir} 0700 root root -" ];
services.restic.backups = {
system = common // {
paths = [ "/etc" "/home" "/root" "/var/lib" "/var/backup" ];
exclude = [
# Backed up by the panel job, with the containers stopped.
"/var/lib/containers"
"${config.users.users.${panel.user}.home}/.local/share/containers"
# Regenerated on demand.
"/home/*/.cache"
"/root/.cache"
"/var/lib/systemd/coredump"
"${config.services.nextcloud.datadir}/data/appdata_*/preview"
];
extraBackupArgs = [ "--tag system" ];
backupPrepareCommand = ''
set -eu
${dumpSqlite "forgejo" config.services.forgejo.database.path}
${dumpSqlite "nextcloud" "${config.services.nextcloud.datadir}/data/${config.services.nextcloud.config.dbname}.db"}
${dumpSqlite "authelia" config.services.authelia.instances.main.settings.storage.local.path}
${dumpSqlite "panel" "${panel.baseDir}/state/panel/panel.db"}
'';
timerConfig = {
OnCalendar = "04:00";
RandomizedDelaySec = "10m";
Persistent = true;
};
};
panel = common // {
paths = [
panel.baseDir
"${panelPodmanStorage}/volumes"
];
exclude = [
# Rootful Podman's image store, if it is ever used. Images are pulled
# or rebuilt again on deploy, so they're not worth paying to store.
"${panel.baseDir}/storage"
"${panel.baseDir}/cache"
];
extraBackupArgs = [ "--tag panel" ];
# Stop the running app containers so databases inside them are copied
# in a consistent state. The cleanup command starts exactly these again,
# and runs even when the backup fails.
backupPrepareCommand = ''
set -eu
${podmanAsPanelUser}
podman_panel ps --quiet > ${stoppedContainers}
if [ -s ${stoppedContainers} ]; then
echo "stopping $(wc -l < ${stoppedContainers}) container(s)"
podman_panel stop --time 30 $(cat ${stoppedContainers})
fi
'';
backupCleanupCommand = ''
set -eu
${podmanAsPanelUser}
if [ -s ${stoppedContainers} ]; then
echo "starting $(wc -l < ${stoppedContainers}) container(s)"
podman_panel start $(cat ${stoppedContainers})
fi
'';
timerConfig = {
OnCalendar = "03:30";
RandomizedDelaySec = "10m";
Persistent = true;
};
};
# No paths, so this unit only forgets, prunes and checks.
maintenance = common // {
initialize = false;
pruneOpts = [
"--keep-daily 7"
"--keep-weekly 4"
"--keep-monthly 6"
];
# Without --with-cache, check re-downloads all metadata into a temp dir.
# The 2 GB sample spreads a real read of the data over the weeks while
# staying well inside the free egress.
checkOpts = [
"--with-cache"
"--read-data-subset=2G"
];
timerConfig = {
OnCalendar = "Sun 05:30";
Persistent = true;
};
};
};
systemd.services = lib.genAttrs
[ "restic-backups-system" "restic-backups-panel" "restic-backups-maintenance" ]
(_: {
# One cache for all jobs, so metadata is downloaded once instead of once
# per job. The restic-<job> wrappers pick this up from the unit too.
environment.RESTIC_CACHE_DIR = lib.mkForce "/var/cache/restic";
serviceConfig.CacheDirectory = lib.mkForce "restic";
path = [
"/run/wrappers" # newuidmap & co. for rootless Podman
config.virtualisation.podman.package
pkgs.sqlite
pkgs.util-linux
];
});
}