1 { pkgs, lib, config, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
17 settings.skip_children = ["room"];
30 # Trigger backups when disks are plugged
31 services.udev.extraRules = ''
32 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"
33 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
34 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
36 # Show what's happening to the user
37 systemd.services."zfs-term@" = {
38 description = "ZFS terminal for: %I";
39 unitConfig.StopWhenUnneeded = false;
40 environment.DISPLAY = ":0";
41 environment.XAUTHORITY = "/home/julm/.Xauthority";
42 after = [ "graphical.target" ];
43 bindsTo = [ "sys-subsystem-usb-%i.device" ];
47 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
50 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
51 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
52 -u zfs-force-import@$DESTPOOL \
53 -u zfs-local-backup-home@$DESTPOOL"
57 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
58 systemd.services."zfs-force-import@" = {
59 description = "ZFS force import: %I";
62 StartLimitInterval = 200;
63 StopWhenUnneeded = true;
65 wants = [ "zfs-term@%i.service" ];
66 bindsTo = [ "sys-subsystem-usb-%i.device" ];
67 path = lib.mkBefore [ "/run/booted-system/sw" ];
70 RemainAfterExit = true;
72 SyslogIdentifier = "zfs-force-import@%i";
73 Restart = "on-failure";
74 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
77 # Import the zpool, using stable paths
78 zpool import -d /dev/disk/by-id/ || true
79 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
80 zpool reopen "$DESTPOOL" ||
81 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
82 zpool clear -nFX "$DESTPOOL"
86 # Prune old snapshots on the backup and send new ones
87 systemd.services."zfs-local-backup-home@" = {
88 description = "ZFS backup home, on: %I";
89 wants = [ "zfs-term@%i.service" ];
90 after = [ "zfs-force-import@%i.service" ];
91 requires = [ "zfs-force-import@%i.service" ];
92 bindsTo = [ "sys-subsystem-usb-%i.device" ];
93 path = lib.mkBefore [ "/run/booted-system/sw" ];
97 CacheDirectory = [ "zfs-usb-backup-%I" ];
98 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
101 SyslogIdentifier = "zfs-local-backup-home@%i";
102 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
105 if zpool status "$DESTPOOL"; then
106 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
107 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
108 zpool scrub -p "$DESTPOOL" || true
111 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
114 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
118 process_children_only=false
120 [$DESTPOOL/${User}/backup/${hostName}/home]
128 ${pkgs.sanoid}/bin/sanoid \
129 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
130 --configdir /tmp/sanoid \
132 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
135 for dataset in ${hostName}/home; do
136 ${pkgs.sanoid}/bin/syncoid \
138 --exclude "home/room" \
140 --no-privilege-elevation \
147 "$DESTPOOL"/${User}/backup/"$dataset"
150 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
153 # Only if the zpool still exists to avoid uninterruptible hanging
154 if zpool status "$DESTPOOL"; then
155 # Scrub the zpool 1 minute (in the background)
156 zpool scrub "$DESTPOOL"
159 if zpool status "$DESTPOOL"; then
160 zpool scrub -p "$DESTPOOL" || true
162 # Export the zpool (to avoid a forced import later on)
163 zpool export "$DESTPOOL"
165 systemctl --no-block stop zfs-term@"$DESTPOOL"
169 programs.bash.interactiveShellInit = ''
170 mount-zfs-backup () {
174 zpool status "$zpool" 2>/dev/null ||
175 sudo zpool import -d /dev/disk/by-id/ "$zpool"
176 trap "sudo zpool export $zpool" EXIT
177 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
178 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
179 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
183 programs.bash.shellAliases = {
184 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";