pastel: enable on essential profile
[julm/julm-nix.git] / hosts / pumpkin / backup.nix
index fbcc7937c8c9b254baee047dea9851100149b39a..ceef330a6e129599d25a7361c2f18f398de0ba41 100644 (file)
@@ -1,7 +1,15 @@
-{ pkgs, lib, hostName, ... }:
+{
+  pkgs,
+  lib,
+  config,
+  hostName,
+  ...
+}:
 with builtins;
 {
-  # Take regular snapshots, and prune old ones
+  systemd.services.sanoid.serviceConfig.SupplementaryGroups = [
+    config.users.groups."disk".name
+  ];
   services.sanoid = {
     enable = true;
     extraArgs = [ "--verbose" ];
@@ -14,28 +22,40 @@ with builtins;
         daily = 7;
         monthly = 0;
         yearly = 0;
-        recursive = "zfs";
+        recursive = true;
       };
-      "off2/julm/backup/oignon" = {
+      "${hostName}/root/nix" = {
         autosnap = false;
-        autoprune = true;
+      };
+      "${hostName}/root/var/cache" = {
+        autosnap = false;
+      };
+      "${hostName}/root/var/log" = {
+        autosnap = false;
+      };
+      "${hostName}/root/home/julm/.cache" = {
+        autosnap = false;
+      };
+      "${hostName}/root/home/julm/.local" = {
         hourly = 0;
-        daily = 7;
+        daily = 1;
+      };
+      "${hostName}/root/home/julm/.mozilla" = {
+        hourly = 0;
+        daily = 1;
+      };
+      "off2/julm/backup/${hostName}" = {
+        autosnap = false;
+        autoprune = true;
+        hourly = 12;
+        daily = 14;
         monthly = 3;
         yearly = 0;
-        recursive = "zfs";
       };
     };
   };
+
   programs.bash.interactiveShellInit = ''
-    backup-pumpkin () {
-      sudo syncoid --sshkey ~julm/.ssh/id_ed25519 \
-        --create-bookmark --no-sync-snap --no-privilege-elevation \
-        --sendoptions=Rw --recvoptions=u \
-        --exclude pumpkin/root/nix \
-        pumpkin/root \
-        aubergine.sp:off2/julm/backup/pumpkin
-    }
     mount-zfs-backup () {
       (
       set -eux
@@ -66,14 +86,16 @@ with builtins;
     serviceConfig = {
       Type = "simple";
       PrivateTmp = true;
-      ExecStart = pkgs.writeShellScript "zfs-force-import" ''
-        DESTPOOL=$1
-        set -eux
-        ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
-          -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
-          -u zfs-force-import@$DESTPOOL \
-          -u zfs-local-backup-home@$DESTPOOL"
-      '' + " %I";
+      ExecStart =
+        pkgs.writeShellScript "zfs-force-import" ''
+          DESTPOOL=$1
+          set -eux
+          ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
+            -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
+            -u zfs-force-import@$DESTPOOL \
+            -u zfs-local-backup-home@$DESTPOOL"
+        ''
+        + " %I";
     };
   };
   # Force zpool import, even if the disk has not been exported, or has been imported on another computer
@@ -93,16 +115,18 @@ with builtins;
       PrivateTmp = true;
       SyslogIdentifier = "zfs-force-import@%i";
       Restart = "on-failure";
-      ExecStart = pkgs.writeShellScript "zfs-force-import" ''
-        DESTPOOL=$1
-        set -eux
-        # Import the zpool, using stable paths
-        zpool import -d /dev/disk/by-id/ || true
-        zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
-        zpool reopen "$DESTPOOL" ||
-        zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
-        zpool clear -nFX "$DESTPOOL"
-      '' + " %I";
+      ExecStart =
+        pkgs.writeShellScript "zfs-force-import" ''
+          DESTPOOL=$1
+          set -eux
+          # Import the zpool, using stable paths
+          zpool import -d /dev/disk/by-id/ || true
+          zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
+          zpool reopen "$DESTPOOL" ||
+          zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
+          zpool clear -nFX "$DESTPOOL"
+        ''
+        + " %I";
     };
   };
   # Prune old snapshots on the backup and send new ones
@@ -121,75 +145,83 @@ with builtins;
       User = "julm";
       Group = "users";
       SyslogIdentifier = "zfs-local-backup-home@%i";
-      ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
-        DESTPOOL=$1
-        set -eux
-        if zpool status "$DESTPOOL"; then
-          zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
-          zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
-          zpool scrub -p "$DESTPOOL" || true
-        fi
-      '' + " %I";
-      ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
-        set -eu
-        DESTPOOL=$1
-        # sanoid is quite conservative:
-        # by setting hourly=24, a snapshot must be >24 hours old
-        # and there must been >24 total hourly snapshots,
-        # or nothing is pruned.
-        install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
-          [template_remote]
-          autoprune=true
-          autosnap=false
-          process_children_only=false
+      ExecStartPre =
+        "+"
+        + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
+          DESTPOOL=$1
+          set -eux
+          if zpool status "$DESTPOOL"; then
+            zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
+            zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
+            zpool scrub -p "$DESTPOOL" || true
+          fi
+        ''
+        + " %I";
+      ExecStart =
+        pkgs.writeShellScript "zfs-local-backup-home" ''
+          set -eu
+          DESTPOOL=$1
+          # sanoid is quite conservative:
+          # by setting hourly=24, a snapshot must be >24 hours old
+          # and there must been >24 total hourly snapshots,
+          # or nothing is pruned.
+          install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
+            [template_remote]
+            autoprune=true
+            autosnap=false
+            process_children_only=false
 
-          [$DESTPOOL/${User}/backup/${hostName}/home]
-          hourly=6
-          daily=31
-          monthly=3
-          recursive=true
-          use_template=remote
-        EOF
-        set -x
-        ${pkgs.sanoid}/bin/sanoid \
-          --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
-          --configdir /tmp/sanoid \
-          --prune-snapshots \
-          --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
-          --verbose
+            [$DESTPOOL/${User}/backup/${hostName}/home]
+            hourly=6
+            daily=31
+            monthly=3
+            recursive=true
+            use_template=remote
+          EOF
+          set -x
+          ${pkgs.sanoid}/bin/sanoid \
+            --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
+            --configdir /tmp/sanoid \
+            --prune-snapshots \
+            --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
+            --verbose
 
-        for dataset in ${hostName}/home; do
-          ${pkgs.sanoid}/bin/syncoid \
-            --create-bookmark \
-            --exclude "home/room" \
-            --force-delete \
-            --no-privilege-elevation \
-            --no-sync-snap \
-            --recursive \
-            --recvoptions "" \
-            --sendoptions raw  \
-            --skip-parent \
-            "$dataset" \
-            "$DESTPOOL"/${User}/backup/"$dataset"
-        done
-      '' + " %I";
-      ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
-        DESTPOOL=$1
-        set -eux
-        # Only if the zpool still exists to avoid uninterruptible hanging
-        if zpool status -v "$DESTPOOL"; then
-          # Scrub the zpool 1 minute (in the background)
-          zpool scrub "$DESTPOOL"
-          sleep 60
-        fi
-        while zpool status -v "$DESTPOOL"; do
-          zpool scrub -p "$DESTPOOL" || true
-          sleep 20
-          # Export the zpool (to avoid a forced import later on)
-          zpool export "$DESTPOOL" || true
-        done
-        systemctl --no-block stop zfs-term@"$DESTPOOL"
-      '' + " %I";
+          for dataset in ${hostName}/home; do
+            ${pkgs.sanoid}/bin/syncoid \
+              --create-bookmark \
+              --exclude "home/room" \
+              --force-delete \
+              --no-privilege-elevation \
+              --no-sync-snap \
+              --recursive \
+              --recvoptions "" \
+              --sendoptions raw  \
+              --skip-parent \
+              "$dataset" \
+              "$DESTPOOL"/${User}/backup/"$dataset"
+          done
+        ''
+        + " %I";
+      ExecStartPost =
+        "+"
+        + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
+          DESTPOOL=$1
+          set -eux
+          # Only if the zpool still exists to avoid uninterruptible hanging
+          if zpool status -v "$DESTPOOL"; then
+            # Scrub the zpool 1 minute (in the background)
+            zpool scrub "$DESTPOOL"
+            sleep 60
+          fi
+          while zpool status -v "$DESTPOOL"; do
+            zpool scrub -p "$DESTPOOL" || true
+            sleep 20
+            # Export the zpool (to avoid a forced import later on)
+            zpool export "$DESTPOOL" || true
+          done
+          systemctl --no-block stop zfs-term@"$DESTPOOL"
+        ''
+        + " %I";
     };
   };
   programs.bash.shellAliases = {