carotte: try to fix build
[julm/julm-nix.git] / hosts / oignon / backup.nix
index da8df5f9abf71e15ffd2f3bbb337275844d15d11..7b08652012c0ca9e0af4582ddfd70a2d549acbd1 100644 (file)
@@ -10,16 +10,20 @@ services.sanoid = {
       autosnap = true;
       autoprune = true;
       hourly = 12;
-      daily = 7;
+      daily = 3;
       monthly = 0;
       yearly = 0;
       recursive = true;
     };
+    "${hostName}/home/room" = {
+      autosnap = false;
+      autoprune = false;
+    };
     "${hostName}/var" = {
       autosnap = true;
       autoprune = true;
       hourly = 12;
-      daily = 7;
+      daily = 1;
       monthly = 0;
       yearly = 0;
       recursive = true;
@@ -82,7 +86,7 @@ systemd.services."zfs-force-import@" = {
     '' + " %I";
   };
 };
-# Run the backup
+# Prune old snapshots on the backup and send new ones
 systemd.services."zfs-local-backup-home@" = {
   description = "ZFS backup home, on: %I";
   wants = [ "zfs-term@%i.service" ];
@@ -93,8 +97,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";
@@ -110,6 +114,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
@@ -117,17 +125,18 @@ systemd.services."zfs-local-backup-home@" = {
         process_children_only=false
 
         [$DESTPOOL/${User}/backup/${hostName}/home]
+        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
@@ -149,19 +158,36 @@ 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";
   };
 };
+programs.bash.interactiveShellInit = ''
+  mount-zfs-backup () {
+    (
+    set -eux
+    zpool="$1"
+    zpool status "$zpool" 2>/dev/null ||
+    sudo zpool import -d /dev/disk/by-id/ "$zpool"
+    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-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
+    )
+  }
+'';
+programs.bash.shellAliases = {
+  mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
+};
 }