1 { pkgs, lib, config, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
18 "${hostName}/home/room" = {
33 # Trigger backups when disks are plugged
34 services.udev.extraRules = ''
35 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"
36 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
37 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
39 # Show what's happening to the user
40 systemd.services."zfs-term@" = {
41 description = "ZFS terminal for: %I";
42 unitConfig.StopWhenUnneeded = false;
43 environment.DISPLAY = ":0";
44 environment.XAUTHORITY = "/home/julm/.Xauthority";
45 after = [ "graphical.target" ];
46 bindsTo = [ "sys-subsystem-usb-%i.device" ];
50 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
53 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
54 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
55 -u zfs-force-import@$DESTPOOL \
56 -u zfs-local-backup-home@$DESTPOOL"
60 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
61 systemd.services."zfs-force-import@" = {
62 description = "ZFS force import: %I";
65 StartLimitInterval = 200;
66 StopWhenUnneeded = true;
68 wants = [ "zfs-term@%i.service" ];
69 bindsTo = [ "sys-subsystem-usb-%i.device" ];
70 path = lib.mkBefore [ "/run/booted-system/sw" ];
73 RemainAfterExit = true;
75 SyslogIdentifier = "zfs-force-import@%i";
76 Restart = "on-failure";
77 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
80 # Import the zpool, using stable paths
81 zpool import -d /dev/disk/by-id/ || true
82 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
83 zpool reopen "$DESTPOOL" ||
84 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
85 zpool clear -nFX "$DESTPOOL"
89 # Prune old snapshots on the backup and send new ones
90 systemd.services."zfs-local-backup-home@" = {
91 description = "ZFS backup home, on: %I";
92 wants = [ "zfs-term@%i.service" ];
93 after = [ "zfs-force-import@%i.service" ];
94 requires = [ "zfs-force-import@%i.service" ];
95 bindsTo = [ "sys-subsystem-usb-%i.device" ];
96 path = lib.mkBefore [ "/run/booted-system/sw" ];
100 CacheDirectory = [ "zfs-usb-backup/%I" ];
101 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
104 SyslogIdentifier = "zfs-local-backup-home@%i";
105 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
108 if zpool status "$DESTPOOL"; then
109 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
110 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
111 zpool scrub -p "$DESTPOOL" || true
114 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
117 # sanoid is quite conservative:
118 # by setting hourly=24, a snapshot must be >24 hours old
119 # and there must been >24 total hourly snapshots,
120 # or nothing is pruned.
121 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
125 process_children_only=false
127 [$DESTPOOL/${User}/backup/${hostName}/home]
135 ${pkgs.sanoid}/bin/sanoid \
136 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
137 --configdir /tmp/sanoid \
139 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
142 for dataset in ${hostName}/home; do
143 ${pkgs.sanoid}/bin/syncoid \
145 --exclude "home/room" \
147 --no-privilege-elevation \
154 "$DESTPOOL"/${User}/backup/"$dataset"
157 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
160 # Only if the zpool still exists to avoid uninterruptible hanging
161 if zpool status -v "$DESTPOOL"; then
162 # Scrub the zpool 1 minute (in the background)
163 zpool scrub "$DESTPOOL"
166 while zpool status -v "$DESTPOOL"; do
167 zpool scrub -p "$DESTPOOL" || true
169 # Export the zpool (to avoid a forced import later on)
170 zpool export "$DESTPOOL" || true
172 systemctl --no-block stop zfs-term@"$DESTPOOL"
176 programs.bash.interactiveShellInit = ''
177 mount-zfs-backup () {
181 zpool status "$zpool" 2>/dev/null ||
182 sudo zpool import -d /dev/disk/by-id/ "$zpool"
183 trap "sudo zpool export $zpool" EXIT
184 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
185 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
186 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
190 programs.bash.shellAliases = {
191 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";