inxi: add to essentials
[julm/julm-nix.git] / hosts / aubergine / backup.nix
index bf37c72239268dd13792f9eae9cd8ef8d92cebe5..26ac7737eaa570a6cca37ce15be39691a3856fc9 100644 (file)
@@ -1,6 +1,34 @@
-{ hostName, ... }:
-with builtins;
+{ pkgs, lib, config, hostName, ... }:
 {
+  users.users.backup = {
+    isSystemUser = true;
+    shell = config.users.users.root.shell;
+    group = config.users.groups.disk.name;
+    openssh.authorizedKeys.keys = [
+      (lib.readFile ../pumpkin/syncoid/ssh.key.pub)
+    ];
+  };
+  # Trigger import when disks are plugged
+  services.udev.extraRules = ''
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="Samsung_SSD_860_EVO_1TB_S3Z9NR0N508159W", \
+      ACTION=="add", ENV{SYSTEMD_WANTS}+="zfs-import@off2.service"
+    SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="Samsung_SSD_860_EVO_1TB_S3Z9NR0N508159W", \
+      ACTION=="remove", RUN+="${pkgs.systemd}/bin/systemctl stop --no-block zfs-import@off2.service"
+  '';
+  # Setup permissions on disk off2
+  systemd.services."zfs-import@".serviceConfig.ExecStartPost = pkgs.writeShellScript "zfs-allow" ''
+    set -eux
+    pool="$1"
+    case "$pool" in
+      (off2) zfs allow -u ${config.users.users.backup.name} change-key,compression,create,destroy,mount,mountpoint,receive,rollback "$pool"/julm/backup;;
+    esac
+  '' + " %I";
+  systemd.tmpfiles.rules = [
+    "z /dev/zfs 0660 - ${config.users.groups."disk".name}  -"
+  ];
+  systemd.services.sanoid.serviceConfig.SupplementaryGroups = [
+    config.users.groups."disk".name
+  ];
   services.sanoid = {
     enable = true;
     extraArgs = [ "--verbose" ];
@@ -57,9 +85,9 @@ with builtins;
         autosnap = false;
         autoprune = true;
         frequently = 1;
-        hourly = 1;
+        hourly = 12;
         daily = 7;
-        monthly = 0;
+        monthly = 3;
         yearly = 0;
         recursive = true;
       };
@@ -67,9 +95,9 @@ with builtins;
         autosnap = false;
         autoprune = true;
         frequently = 1;
-        hourly = 1;
+        hourly = 0;
         daily = 1;
-        monthly = 0;
+        monthly = 3;
         yearly = 0;
         recursive = true;
       };
@@ -102,6 +130,15 @@ with builtins;
         yearly = 0;
         recursive = true;
       };
+      "off2/julm/backup/pumpkin" = {
+        autosnap = false;
+        autoprune = true;
+        hourly = 12;
+        daily = 7;
+        monthly = 3;
+        yearly = 0;
+        recursive = true;
+      };
     };
   };
 }