make genepi backups atomic

This commit is contained in:
2025-01-31 14:07:54 +01:00
parent b0766743b5
commit bf67b71237

View File

@@ -7,6 +7,7 @@
let let
storagebox-user = "u422292-sub1"; storagebox-user = "u422292-sub1";
storagebox-host = "${storagebox-user}.your-storagebox.de"; storagebox-host = "${storagebox-user}.your-storagebox.de";
storagebox-nightly-backup-name = "storagebox-nightly";
in in
{ {
environment.systemPackages = [ environment.systemPackages = [
@@ -20,12 +21,14 @@ in
"${storagebox-host}".publicKey = keys.hosts.storagebox-rsa; "${storagebox-host}".publicKey = keys.hosts.storagebox-rsa;
}; };
services.restic.backups = { services.restic.backups."${storagebox-nightly-backup-name}" = {
storagebox-nightly = {
initialize = true; initialize = true;
paths = [ paths = [
"/persist" "/persist"
]; ];
exclude = [
"/persist/@backup-snapshot"
];
passwordFile = config.age.secrets.restic-genepi-storagebox-key.path; passwordFile = config.age.secrets.restic-genepi-storagebox-key.path;
repository = "sftp://${storagebox-user}@${storagebox-host}/"; repository = "sftp://${storagebox-user}@${storagebox-host}/";
extraOptions = [ extraOptions = [
@@ -41,6 +44,29 @@ in
"--keep-monthly 12" "--keep-monthly 12"
"--keep-yearly 10" "--keep-yearly 10"
]; ];
backupPrepareCommand = ''
set -Eeuxo pipefail
# clean old snapshot
if btrfs subvolume delete /persist/@backup-snapshot; then
echo "WARNING: previous run did not cleanly finish, removing old snapshot"
fi
btrfs subvolume snapshot -r /persist /persist/@backup-snapshot
umount /persist
mount -t btrfs -o subvol=/persist/@backup-snapshot /dev/disk/by-partlabel/disk-main-root /persist
'';
backupCleanupCommand = ''
btrfs subvolume delete /persist/@backup-snapshot
'';
}; };
systemd.services."restic-backups-${storagebox-nightly-backup-name}" = {
path = with pkgs; [
btrfs-progs
umount
mount
];
serviceConfig.privateMounts = true;
}; };
} }