aubergine: nftables: fix DHCP renewal
[julm/julm-nix.git] / hosts / patate / backup.nix
index 649485f9b570a46a45a172c80e3b8d1fe3c24a48..9a989c1d8558f8fca62ec42d1a2c1dc687a395cd 100644 (file)
-{ pkgs, lib, config, hostName, ... }:
+{ pkgs, 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;
-    };
-  };
-};
-# 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"
-'';
-# 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";
-  unitConfig.StopWhenUnneeded = true;
-  bindsTo = [ "sys-subsystem-usb-%i.device" ];
-  path = lib.mkBefore [ "/run/booted-system/sw" ];
-  serviceConfig = {
-    Type = "oneshot";
-    RemainAfterExit = true;
-    PrivateTmp = true;
-    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";
-    ExecStartPost = "+" + pkgs.writeShellScript "zfs-force-import-startPost" ''
-      DESTPOOL=$1
-      set -eux
-      # Do not block on unplug/failure
-      zpool set failmode=continue "$DESTPOOL"
-    '' + " %I";
   };
-};
-# Run the backup
-systemd.services."zfs-local-backup-home@" = {
-  description = "ZFS backup home, on: %I";
-  after = [ "zfs-force-import@%i.service" ];
-  bindsTo = [ "zfs-force-import@%i.service" ];
-  path = lib.mkBefore [ "/run/booted-system/sw" ];
-  serviceConfig = rec {
-    Type = "oneshot";
-    RemainAfterExit = true;
-    PrivateTmp = true;
-    CacheDirectory = [ "zfs-usb-backup-%I" ];
-    RuntimeDirectory = [ "zfs-usb-backup-%I" ];
-    User = "sevy";
-    Group = "users";
-    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
-      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 = ''
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", \
+      ACTION=="add", ENV{SYSTEMD_WANTS}+="zfs-local-backup-home@WD10JPVT.service"
+    # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", \
+      ACTION=="remove", RUN+="${pkgs.systemd}/bin/systemctl stop --no-block zfs-local-backup-home@WD10JPVT.service"
 
-      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";
-    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 "$DESTPOOL"; then
-        # Scrub the zpool 1 minute (in the background)
-        zpool scrub "$DESTPOOL"
-        sleep 60
-      fi
-      if zpool status "$DESTPOOL"; then
-        zpool scrub -p "$DESTPOOL" || true
-        # Export the zpool (to avoid a forced import later on)
-        zpool export "$DESTPOOL"
-      fi
-    '' + " %I";
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", \
+      ACTION=="add", ENV{SYSTEMD_USER_WANTS}+="rsync-backup@GE_SP.service"
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_FS_LABEL}=="GE_SP", \
+      ACTION=="remove", RUN+="${pkgs.systemd}/bin/systemctl stop --no-block zfs-import@off3.service"
+  '';
+  programs.bash.shellAliases = {
+    mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
   };
-};
 }