1 { pkgs, lib, config, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
18 "${hostName}/home/Documents" = {
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 # Show what's happening to the user
36 systemd.services."zfs-term@" = {
37 description = "ZFS terminal for: %I";
38 unitConfig.StopWhenUnneeded = false;
39 environment.DISPLAY = ":0";
40 environment.XAUTHORITY = "/home/sevy/.Xauthority";
41 after = [ "graphical.target" ];
42 bindsTo = [ "sys-subsystem-usb-%i.device" ];
46 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
49 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
50 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
51 -u zfs-force-import@$DESTPOOL \
52 -u zfs-local-backup-home@$DESTPOOL"
56 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
57 systemd.services."zfs-force-import@" = {
58 description = "ZFS force import: %I";
61 StartLimitInterval = 200;
62 StopWhenUnneeded = true;
64 wants = [ "zfs-term@%i.service" ];
65 bindsTo = [ "sys-subsystem-usb-%i.device" ];
66 path = lib.mkBefore [ "/run/booted-system/sw" ];
69 RemainAfterExit = true;
71 SyslogIdentifier = "zfs-force-import@%i";
72 Restart = "on-failure";
73 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
76 # Import the zpool, using stable paths
77 zpool import -d /dev/disk/by-id/ || true
78 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
79 zpool reopen "$DESTPOOL" ||
80 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
81 zpool clear -nFX "$DESTPOOL"
86 systemd.services."zfs-local-backup-home@" = {
87 description = "ZFS backup home, on: %I";
88 wants = [ "zfs-term@%i.service" ];
89 after = [ "zfs-force-import@%i.service" ];
90 requires = [ "zfs-force-import@%i.service" ];
91 bindsTo = [ "sys-subsystem-usb-%i.device" ];
92 path = lib.mkBefore [ "/run/booted-system/sw" ];
96 CacheDirectory = [ "zfs-usb-backup-%I" ];
97 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
100 SyslogIdentifier = "zfs-local-backup-home@%i";
101 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
104 if zpool status "$DESTPOOL"; then
105 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
106 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
107 zpool scrub -p "$DESTPOOL" || true
110 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
113 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
117 process_children_only=false
119 [$DESTPOOL/${User}/backup/${hostName}/home]
126 ${pkgs.sanoid}/bin/sanoid \
127 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
128 --configdir /tmp/sanoid \
130 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
133 for dataset in ${hostName}/home; do
134 ${pkgs.sanoid}/bin/syncoid \
136 --exclude "home/Downloads" \
137 --exclude "home/Videos" \
139 --no-privilege-elevation \
146 "$DESTPOOL"/${User}/backup/"$dataset"
149 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
152 # Only if the zpool still exists to avoid uninterruptible hanging
153 if zpool status "$DESTPOOL"; then
154 # Scrub the zpool 1 minute (in the background)
155 zpool scrub "$DESTPOOL"
158 if zpool status "$DESTPOOL"; then
159 zpool scrub -p "$DESTPOOL" || true
161 # Export the zpool (to avoid a forced import later on)
162 zpool export "$DESTPOOL"
164 systemctl --no-block stop zfs-term@"$DESTPOOL"