nix: update home-manager input
[julm/julm-nix.git] / hosts / patate / backup.nix
index 055403976f42fbfcf859076bc563626796e69e11..837563ef631fe33c09999fed2017ce099c3fcd59 100644 (file)
-{ pkgs, lib, config, hostName, ... }:
+{ hostName, ... }:
 with builtins;
 {
-# Take regular snapshots, and prune old ones
-services.sanoid = {
-  enable = true;
-  extraArgs = [ "--verbose" ];
-  datasets = {
-    "${hostName}/home" = {
-      autosnap = true;
-      autoprune = true;
-      hourly = 12;
-      daily = 0;
-      monthly = 0;
-      yearly = 0;
-      recursive = true;
+  imports = [
+    backup/zfs-backup.nix
+    backup/rsync-backup.nix
+  ];
+  # Take regular snapshots, and prune old ones
+  services.sanoid = {
+    enable = true;
+    extraArgs = [ "--verbose" ];
+    datasets = {
+      "${hostName}/home" = {
+        autosnap = true;
+        autoprune = true;
+        hourly = 24;
+        daily = 3;
+        monthly = 0;
+        yearly = 0;
+        recursive = true;
+      };
+      "${hostName}/home/Documents" = {
+        autosnap = true;
+        autoprune = true;
+        hourly = 24;
+        daily = 31;
+        monthly = 0;
+        yearly = 0;
+        recursive = true;
+      };
+      "${hostName}/home/Downloads" = {
+        autosnap = false;
+        autoprune = false;
+      };
+      "${hostName}/home/Videos" = {
+        autosnap = false;
+        autoprune = false;
+      };
     };
-    "${hostName}/home/Documents" = {
-      autosnap = true;
-      autoprune = true;
-      hourly = 12;
-      daily = 31;
-      monthly = 0;
-      yearly = 0;
-      recursive = true;
-    };
-  };
-};
-# Tigger backups when disks are plugged
-services.udev.extraRules = ''
-  ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ATTRS{size}=="1953525168", ENV{SYSTEMD_WANTS}+="zfs-local-backup-home@WD10JPVT.service"
-'';
-# Force zpool import, even if the disk has not been exported, or has been imported on another computer
-systemd.services."zfs-force-import@" = {
-  description = "ZFS force import: %I";
-  serviceConfig = {
-    Type = "oneshot";
-    RemainAfterExit = true;
-    PrivateTmp = true;
-    ExecStartPre = [
-      # Scan the zpools, using stable paths
-      "/run/booted-system/sw/bin/zpool import -d /dev/disk/by-id/"
-    ];
-    ExecStart = ''
-      /bin/sh -xc '/run/booted-system/sw/bin/zpool import -lFd /dev/disk/by-id/ %I || \
-      /run/booted-system/sw/bin/zpool reopen %I || \
-      /run/booted-system/sw/bin/zpool clear -nFX %I'
-    '';
   };
-};
-# Run the backup
-systemd.services."zfs-local-backup-home@" = {
-  description = "ZFS backup home, on: %I";
-  after = [ "zfs-force-import@%i.service" ];
-  wants = [ "zfs-force-import@%i.service" ];
-  serviceConfig = rec {
-    Type = "oneshot";
-    RemainAfterExit = true;
-    PrivateTmp = true;
-    CacheDirectory = [ "zfs-usb-backup-%I" ];
-    RuntimeDirectory = [ "zfs-usb-backup-%I" ];
-    User = "sevy";
-    Group = "users";
-    ExecStartPre = [
-      "+/run/booted-system/sw/bin/zfs allow ${User} bookmark,hold,mount,send ${hostName}/home"
-      "+/run/booted-system/sw/bin/zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot %I/${User}"
-    ];
-    ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
-      set -eu
-      DESTPOOL=$1
-      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]
-        daily=31
-        monthly=0
-        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
+  # Trigger backups when disks are plugged
+  services.udev.extraRules = ''
+    ACTION=="add",    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", ENV{SYSTEMD_WANTS}+="zfs-local-backup-home@WD10JPVT.service", ENV{SYSTEMD_ALIAS}="/sys/subsystem/usb/WD10JPVT"
+    # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
+    ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
 
-      for dataset in ${hostName}/home; do
-        ${pkgs.sanoid}/bin/syncoid \
-          --create-bookmark \
-          --exclude "home/Downloads" \
-          --exclude "home/Videos" \
-          --force-delete \
-          --no-privilege-elevation \
-          --no-sync-snap \
-          --recursive \
-          --recvoptions "" \
-          --sendoptions raw  \
-          --skip-parent \
-          "$dataset" \
-          "$DESTPOOL"/${User}/backup/"$dataset"
-      done
-    '' + " %I";
-    # Scrub the zpool (in the background)
-    ExecStartPost = "+/run/booted-system/sw/bin/zpool scrub %I";
+    ACTION=="add",    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", ENV{SYSTEMD_USER_WANTS}+="rsync-backup@GE_SP.service", ENV{SYSTEMD_ALIAS}="/sys/subsystem/usb/GE_SP"
+    ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", TAG+="systemd"
+  '';
+  programs.bash.shellAliases = {
+    mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
   };
-};
 }