1 { pkgs, lib, config, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
17 settings.skip_children = ["Downloads" "Videos"];
21 # Trigger backups when disks are plugged
22 services.udev.extraRules = ''
23 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"
24 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
25 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
27 # Show what's happening to the user
28 systemd.services."zfs-term@" = {
29 description = "ZFS terminal for: %I";
30 unitConfig.StopWhenUnneeded = false;
31 environment.DISPLAY = ":0";
32 environment.XAUTHORITY = "/home/sevy/.Xauthority";
33 after = [ "graphical.target" ];
34 bindsTo = [ "sys-subsystem-usb-%i.device" ];
38 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
41 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
42 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
43 -u zfs-force-import@$DESTPOOL \
44 -u zfs-local-backup-home@$DESTPOOL"
48 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
49 systemd.services."zfs-force-import@" = {
50 description = "ZFS force import: %I";
53 StartLimitInterval = 200;
54 StopWhenUnneeded = true;
56 wants = [ "zfs-term@%i.service" ];
57 bindsTo = [ "sys-subsystem-usb-%i.device" ];
58 path = lib.mkBefore [ "/run/booted-system/sw" ];
61 RemainAfterExit = true;
63 SyslogIdentifier = "zfs-force-import@%i";
64 Restart = "on-failure";
65 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
68 # Import the zpool, using stable paths
69 zpool import -d /dev/disk/by-id/ || true
70 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
71 zpool reopen "$DESTPOOL" ||
72 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
73 zpool clear -nFX "$DESTPOOL"
77 # Prune old snapshots on the backup and send new ones
78 systemd.services."zfs-local-backup-home@" = {
79 description = "ZFS backup home, on: %I";
80 wants = [ "zfs-term@%i.service" ];
81 after = [ "zfs-force-import@%i.service" ];
82 requires = [ "zfs-force-import@%i.service" ];
83 bindsTo = [ "sys-subsystem-usb-%i.device" ];
84 path = lib.mkBefore [ "/run/booted-system/sw" ];
88 CacheDirectory = [ "zfs-usb-backup-%I" ];
89 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
92 SyslogIdentifier = "zfs-local-backup-home@%i";
93 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
96 if zpool status "$DESTPOOL"; then
97 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
98 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
99 zpool scrub -p "$DESTPOOL" || true
102 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
105 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
109 process_children_only=false
111 [$DESTPOOL/${User}/backup/${hostName}/home]
119 ${pkgs.sanoid}/bin/sanoid \
120 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
121 --configdir /tmp/sanoid \
123 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
126 for dataset in ${hostName}/home; do
127 ${pkgs.sanoid}/bin/syncoid \
129 --exclude "home/Downloads" \
130 --exclude "home/Videos" \
132 --no-privilege-elevation \
139 "$DESTPOOL"/${User}/backup/"$dataset"
142 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
145 # Only if the zpool still exists to avoid uninterruptible hanging
146 if zpool status "$DESTPOOL"; then
147 # Scrub the zpool 1 minute (in the background)
148 zpool scrub "$DESTPOOL"
151 if zpool status "$DESTPOOL"; then
152 zpool scrub -p "$DESTPOOL" || true
154 # Export the zpool (to avoid a forced import later on)
155 zpool export "$DESTPOOL"
157 systemctl --no-block stop zfs-term@"$DESTPOOL"
161 programs.bash.interactiveShellInit = ''
162 mount-zfs-backup () {
166 zpool status "$zpool" 2>/dev/null ||
167 sudo zpool import -d /dev/disk/by-id/ "$zpool"
168 trap "sudo zpool export $zpool" EXIT
169 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
170 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
171 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
175 programs.bash.shellAliases = {
176 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";