nixos: describe both servers as NixOS systems
Start of the move from Ansible to NixOS. Services run as native NixOS services where nixpkgs has a module and as Podman containers otherwise; there is no Docker. Shared: users with their Debian UIDs, SSH with the Ansible role's crypto settings, nftables firewall, WireGuard (ops is the hub), sops-nix secrets per host, the restic backup script with its schedule and retention, mail on unit failure. asxpio-services: Xray, SillyTavern, Minecraft (Paper) and the ACME forwarder natively; XMPP stays in containers because the server is a custom build. asxpio-ops: Postgres, Garage and Forgejo natively. Roles, buckets, keys and grants move here from the storage repo's init scripts. Services that own data wait for /var/lib/.data-in-place, so an install never creates empty state where a restore is about to land. migration/*-data.sh move the Debian-era data into the new layout. hosts/rehearsal turns the two hosts into a test pair with its own keys and dummy secrets. Everything here ran there, Postgres, Garage, Forgejo, Minecraft and SillyTavern on a restored copy of production data. Three differences from the containers showed up and are handled: Garage rejects a 0440 credential file, Geyser needs libcrypto and netlink, SillyTavern ignores the module's listen flags. Production is untouched: both servers still run Debian under Ansible. Assisted-by: Claude:fable-5-2
This commit is contained in:
parent
c51522d371
commit
be68c5adef
1 changed files with 1981 additions and 0 deletions
174
nixos/modules/backup.nix
Normal file
174
nixos/modules/backup.nix
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
# Nightly restic backup of the host into two independent repositories
|
||||
# (Hetzner Object Storage, hel1 and fsn1): losing one region or one
|
||||
# repository leaves the other complete. A failed run mails its journal.
|
||||
#
|
||||
# restic-backup run hooks, then backup + forget to every repository
|
||||
# restic-backup init create the repositories that do not exist yet
|
||||
# restic-backup <repo> <args...> plain restic against one repository, e.g.
|
||||
# restic-backup hel1 snapshots
|
||||
# restic-backup fsn1 restore latest --target /tmp/r
|
||||
#
|
||||
# Restoring: RESTORE.md at the repo root.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.asxpio.backup;
|
||||
host = cfg.hostName;
|
||||
stage = "/var/backups/restic-stage";
|
||||
textfileDir = "/var/lib/node_exporter/textfile";
|
||||
|
||||
script = pkgs.writeShellScriptBin "restic-backup" ''
|
||||
set -uo pipefail
|
||||
export PATH=${
|
||||
lib.makeBinPath (
|
||||
[
|
||||
pkgs.restic
|
||||
pkgs.sqlite
|
||||
pkgs.coreutils
|
||||
pkgs.gnused
|
||||
config.virtualisation.podman.package
|
||||
]
|
||||
++ cfg.hookPackages
|
||||
)
|
||||
}:$PATH
|
||||
|
||||
set -a; . ${config.sops.templates."restic.env".path}; set +a
|
||||
export RESTIC_CACHE_DIR=/var/cache/restic
|
||||
STAGE=${stage}
|
||||
|
||||
declare -A REPOS=(
|
||||
${lib.concatStrings (lib.mapAttrsToList (n: url: " [${n}]=${lib.escapeShellArg url}\n") cfg.repositories)})
|
||||
|
||||
log() { echo "restic-backup: $*"; }
|
||||
|
||||
# Last good backup per repository, for Prometheus (node_exporter textfile)
|
||||
metric() {
|
||||
[ -d ${textfileDir} ] || return 0
|
||||
local f=${textfileDir}/restic_$1.prom
|
||||
printf '# HELP restic_backup_last_success_timestamp_seconds Last successful restic backup.\n# TYPE restic_backup_last_success_timestamp_seconds gauge\nrestic_backup_last_success_timestamp_seconds{repo="%s"} %s\n' "$1" "$(date +%s)" > "$f.tmp" && mv "$f.tmp" "$f"
|
||||
}
|
||||
|
||||
case "''${1:-}" in
|
||||
init)
|
||||
for name in "''${!REPOS[@]}"; do
|
||||
if RESTIC_REPOSITORY="''${REPOS[$name]}" restic cat config >/dev/null 2>&1; then
|
||||
log "$name: exists"
|
||||
else
|
||||
RESTIC_REPOSITORY="''${REPOS[$name]}" restic init || exit 1
|
||||
log "$name: created"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
|
||||
run)
|
||||
failed=0
|
||||
rm -rf "$STAGE"; install -d -m 700 "$STAGE"
|
||||
export STAGE
|
||||
${lib.concatImapStrings (i: hook: ''
|
||||
log "hook ${toString i}/${toString (lib.length cfg.hooks)}"
|
||||
( set -e; ${hook} ) || { log "hook failed"; failed=1; }
|
||||
'') cfg.hooks}
|
||||
for name in "''${!REPOS[@]}"; do
|
||||
export RESTIC_REPOSITORY="''${REPOS[$name]}"
|
||||
log "$name: backup"
|
||||
restic backup --host ${host} --one-file-system --exclude-caches \
|
||||
${lib.concatMapStringsSep " " (e: "--exclude ${lib.escapeShellArg e}") cfg.excludes} \
|
||||
${lib.escapeShellArgs (cfg.paths ++ [ stage ])} || { failed=1; continue; }
|
||||
|
||||
prune=""
|
||||
[ "$(date +%a)" = "Sun" ] && prune="--prune"
|
||||
log "$name: forget $prune"
|
||||
restic forget --host ${host} --keep-daily 7 --keep-weekly 4 --keep-monthly 6 $prune || failed=1
|
||||
metric "$name"
|
||||
done
|
||||
|
||||
rm -rf "$STAGE"
|
||||
exit $failed
|
||||
;;
|
||||
|
||||
*)
|
||||
[ -n "''${1:-}" ] && [ -n "''${REPOS[$1]:-}" ] || { echo "usage: restic-backup run | init | <${lib.concatStringsSep "|" (lib.attrNames cfg.repositories)}> <restic args>"; exit 2; }
|
||||
export RESTIC_REPOSITORY="''${REPOS[$1]}"; shift
|
||||
exec restic "$@"
|
||||
;;
|
||||
esac
|
||||
'';
|
||||
in
|
||||
{
|
||||
options.asxpio.backup = {
|
||||
hostName = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "Name the snapshots are filed under; the path inside the buckets.";
|
||||
};
|
||||
repositories = lib.mkOption {
|
||||
type = lib.types.attrsOf lib.types.str;
|
||||
default = {
|
||||
hel1 = "s3:https://hel1.your-objectstorage.com/asxpio-backups-main/restic/${host}";
|
||||
fsn1 = "s3:https://fsn1.your-objectstorage.com/asxpio-backups-repl/restic/${host}";
|
||||
};
|
||||
};
|
||||
paths = lib.mkOption { type = lib.types.listOf lib.types.str; };
|
||||
excludes = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
};
|
||||
hooks = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = ''
|
||||
Shell lines run in order before the backup, with $STAGE set; what
|
||||
they write there is backed up too. A failing hook marks the run
|
||||
as failed, but the backup still runs.
|
||||
'';
|
||||
};
|
||||
hookPackages = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.package;
|
||||
default = [ ];
|
||||
};
|
||||
schedule = lib.mkOption { type = lib.types.str; };
|
||||
};
|
||||
|
||||
config = {
|
||||
sops.secrets."restic/password" = { };
|
||||
sops.secrets."s3/access_key" = { };
|
||||
sops.secrets."s3/secret_key" = { };
|
||||
sops.templates."restic.env".content = ''
|
||||
RESTIC_PASSWORD=${config.sops.placeholder."restic/password"}
|
||||
AWS_ACCESS_KEY_ID=${config.sops.placeholder."s3/access_key"}
|
||||
AWS_SECRET_ACCESS_KEY=${config.sops.placeholder."s3/secret_key"}
|
||||
'';
|
||||
|
||||
environment.systemPackages = [ script ];
|
||||
|
||||
# Nothing to back up, and nothing to overwrite in the repositories,
|
||||
# before the host has its data
|
||||
asxpio.waitForData = [ "restic-backup" ];
|
||||
|
||||
systemd.services.restic-backup = {
|
||||
description = "restic backup (${lib.concatStringsSep ", " (lib.attrNames cfg.repositories)})";
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
onFailure = [ "alert-mail@%n.service" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${script}/bin/restic-backup run";
|
||||
Nice = 10;
|
||||
IOSchedulingClass = "idle";
|
||||
TimeoutStartSec = "6h";
|
||||
};
|
||||
};
|
||||
systemd.timers.restic-backup = {
|
||||
description = "Nightly restic backup";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = cfg.schedule;
|
||||
RandomizedDelaySec = "10m";
|
||||
Persistent = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue