podl: tweak names
[julm/julm-nix.git] / hosts / oignon / backup.nix
index 8017af67b08165e70fc5c92dcc0efee2f8e76bbe..3425e52432d3c1e62a08aa9d2d114c08f5a15b73 100644 (file)
@@ -1,6 +1,8 @@
 { pkgs, lib, config, hostName, ... }:
 with builtins;
 {
+# syncoid --create-bookmark --no-privilege-elevation --no-sync-snap --recvoptions '' --sendoptions raw --recursive oignon/home off2/julm/backup/oignon/home
+# zfs list -t snapshot -o name | grep ^oignon/home | while read -r snap; do zfs bookmark "$snap" "${snap//@/#}"; done
 # Take regular snapshots, and prune old ones
 services.sanoid = {
   enable = true;
@@ -15,10 +17,6 @@ services.sanoid = {
       yearly = 0;
       recursive = true;
     };
-    "${hostName}/home/room" = {
-      autosnap = false;
-      autoprune = false;
-    };
     "${hostName}/var" = {
       autosnap = true;
       autoprune = true;
@@ -28,6 +26,15 @@ services.sanoid = {
       yearly = 0;
       recursive = true;
     };
+    "off2/julm/backup/oignon" = {
+      autosnap = false;
+      autoprune = true;
+      hourly = 0;
+      daily = 7;
+      monthly = 3;
+      yearly = 0;
+      recursive = true;
+    };
   };
 };
 # Trigger backups when disks are plugged
@@ -97,8 +104,8 @@ systemd.services."zfs-local-backup-home@" = {
   serviceConfig = rec {
     Type = "oneshot";
     PrivateTmp = true;
-    CacheDirectory = [ "zfs-usb-backup-%I" ];
-    RuntimeDirectory = [ "zfs-usb-backup-%I" ];
+    CacheDirectory = [ "zfs-usb-backup/%I" ];
+    RuntimeDirectory = [ "zfs-usb-backup/%I" ];
     User = "julm";
     Group = "users";
     SyslogIdentifier = "zfs-local-backup-home@%i";
@@ -114,6 +121,10 @@ systemd.services."zfs-local-backup-home@" = {
     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
@@ -121,18 +132,18 @@ systemd.services."zfs-local-backup-home@" = {
         process_children_only=false
 
         [$DESTPOOL/${User}/backup/${hostName}/home]
-        hourly=12
+        hourly=6
         daily=31
-        monthly=0
+        monthly=3
         recursive=true
         use_template=remote
       EOF
       set -x
       ${pkgs.sanoid}/bin/sanoid \
-        --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
+        --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
         --configdir /tmp/sanoid \
         --prune-snapshots \
-        --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
+        --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
         --verbose
 
       for dataset in ${hostName}/home; do
@@ -154,17 +165,17 @@ systemd.services."zfs-local-backup-home@" = {
       DESTPOOL=$1
       set -eux
       # Only if the zpool still exists to avoid uninterruptible hanging
-      if zpool status "$DESTPOOL"; then
+      if zpool status -v "$DESTPOOL"; then
         # Scrub the zpool 1 minute (in the background)
         zpool scrub "$DESTPOOL"
         sleep 60
       fi
-      if zpool status "$DESTPOOL"; then
+      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"
-      fi
+        zpool export "$DESTPOOL" || true
+      done
       systemctl --no-block stop zfs-term@"$DESTPOOL"
     '' + " %I";
   };
@@ -179,7 +190,7 @@ programs.bash.interactiveShellInit = ''
     trap "sudo zpool export $zpool" EXIT
     zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
     grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
-    ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
+    ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
     )
   }
 '';