1 { pkgs, lib, config, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
29 # Trigger backups when disks are plugged
30 services.udev.extraRules = ''
31 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"
32 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
33 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
35 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
36 systemd.services."zfs-force-import@" = {
37 description = "ZFS force import: %I";
38 unitConfig.StopWhenUnneeded = true;
39 bindsTo = [ "sys-subsystem-usb-%i.device" ];
40 path = lib.mkBefore [ "/run/booted-system/sw" ];
43 RemainAfterExit = true;
45 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
48 # Import the zpool, using stable paths
49 zpool import -d /dev/disk/by-id/ || true
50 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
51 zpool reopen "$DESTPOOL" ||
52 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
53 zpool clear -nFX "$DESTPOOL"
55 ExecStartPost = "+" + pkgs.writeShellScript "zfs-force-import-startPost" ''
58 # Do not block on unplug/failure
59 zpool set failmode=continue "$DESTPOOL"
64 systemd.services."zfs-local-backup-home@" = {
65 description = "ZFS backup home, on: %I";
66 after = [ "zfs-force-import@%i.service" ];
67 bindsTo = [ "zfs-force-import@%i.service" ];
68 path = lib.mkBefore [ "/run/booted-system/sw" ];
71 RemainAfterExit = true;
73 CacheDirectory = [ "zfs-usb-backup-%I" ];
74 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
77 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
80 if zpool status "$DESTPOOL"; then
81 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
82 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
83 zpool scrub -p "$DESTPOOL" || true
86 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
89 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
93 process_children_only=false
95 [$DESTPOOL/${User}/backup/${hostName}/home]
102 ${pkgs.sanoid}/bin/sanoid \
103 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
104 --configdir /tmp/sanoid \
106 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
109 for dataset in ${hostName}/home; do
110 ${pkgs.sanoid}/bin/syncoid \
112 --exclude "home/room" \
114 --no-privilege-elevation \
121 "$DESTPOOL"/${User}/backup/"$dataset"
124 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
127 # Only if the zpool still exists to avoid uninterruptible hanging
128 if zpool status "$DESTPOOL"; then
129 # Scrub the zpool 1 minute (in the background)
130 zpool scrub "$DESTPOOL"
133 if zpool status "$DESTPOOL"; then
134 zpool scrub -p "$DESTPOOL" || true
135 # Export the zpool (to avoid a forced import later on)
136 zpool export "$DESTPOOL"