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 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
121 process_children_only=false
123 [$DESTPOOL/${User}/backup/${hostName}/home]
131 ${pkgs.sanoid}/bin/sanoid \
132 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
133 --configdir /tmp/sanoid \
135 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
138 for dataset in ${hostName}/home; do
139 ${pkgs.sanoid}/bin/syncoid \
141 --exclude "home/room" \
143 --no-privilege-elevation \
150 "$DESTPOOL"/${User}/backup/"$dataset"
153 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
156 # Only if the zpool still exists to avoid uninterruptible hanging
157 if zpool status "$DESTPOOL"; then
158 # Scrub the zpool 1 minute (in the background)
159 zpool scrub "$DESTPOOL"
162 if zpool status "$DESTPOOL"; then
163 zpool scrub -p "$DESTPOOL" || true
165 # Export the zpool (to avoid a forced import later on)
166 zpool export "$DESTPOOL"
168 systemctl --no-block stop zfs-term@"$DESTPOOL"
172 programs.bash.interactiveShellInit = ''
173 mount-zfs-backup () {
177 zpool status "$zpool" 2>/dev/null ||
178 sudo zpool import -d /dev/disk/by-id/ "$zpool"
179 trap "sudo zpool export $zpool" EXIT
180 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
181 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
182 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
186 programs.bash.shellAliases = {
187 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";