julm: install calibre
[julm/julm-nix.git] / hosts / patate / backup / zfs-backup.nix
index 412c4980db1f2acf780672dc489e271ef10c97d7..baf699e7b3779fc051d0b5077fd6fa2e606c3b69 100644 (file)
@@ -61,8 +61,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 = "sevy";
     Group = "users";
     SyslogIdentifier = "zfs-local-backup-home@%i";
@@ -78,6 +78,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
@@ -87,16 +91,16 @@ systemd.services."zfs-local-backup-home@" = {
         [$DESTPOOL/${User}/backup/${hostName}/home]
         hourly=12
         daily=31
-        monthly=0
+        monthly=6
         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
@@ -119,17 +123,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";
   };