10 systemd.services.sanoid.serviceConfig.SupplementaryGroups = [
11 config.users.groups."disk".name
15 extraArgs = [ "--verbose" ];
17 "${hostName}/root" = {
27 "${hostName}/root/nix" = {
30 "${hostName}/root/var/cache" = {
33 "${hostName}/root/var/log" = {
36 "${hostName}/root/home/julm/.cache" = {
39 "${hostName}/root/home/julm/.local" = {
43 "${hostName}/root/home/julm/.mozilla" = {
47 "${hostName}/virt" = {
56 "off2/julm/backup/${hostName}" = {
67 programs.bash.interactiveShellInit = ''
72 zpool status "$zpool" 2>/dev/null ||
73 sudo zpool import -d /dev/disk/by-id/ "$zpool"
74 trap "sudo zpool export $zpool" EXIT
75 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
76 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
77 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
81 # Trigger backups when disks are plugged
82 services.udev.extraRules = ''
83 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"
84 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
85 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
87 # Show what's happening to the user
88 systemd.services."zfs-term@" = {
89 description = "ZFS terminal for: %I";
90 unitConfig.StopWhenUnneeded = false;
91 environment.DISPLAY = ":0";
92 environment.XAUTHORITY = "/home/julm/.Xauthority";
93 after = [ "graphical.target" ];
94 bindsTo = [ "sys-subsystem-usb-%i.device" ];
99 pkgs.writeShellScript "zfs-force-import" ''
102 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
103 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
104 -u zfs-force-import@$DESTPOOL \
105 -u zfs-local-backup-home@$DESTPOOL"
110 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
111 systemd.services."zfs-force-import@" = {
112 description = "ZFS force import: %I";
115 StartLimitInterval = 200;
116 StopWhenUnneeded = true;
118 wants = [ "zfs-term@%i.service" ];
119 bindsTo = [ "sys-subsystem-usb-%i.device" ];
120 path = lib.mkBefore [ "/run/booted-system/sw" ];
123 RemainAfterExit = true;
125 SyslogIdentifier = "zfs-force-import@%i";
126 Restart = "on-failure";
128 pkgs.writeShellScript "zfs-force-import" ''
131 # Import the zpool, using stable paths
132 zpool import -d /dev/disk/by-id/ || true
133 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
134 zpool reopen "$DESTPOOL" ||
135 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
136 zpool clear -nFX "$DESTPOOL"
141 # Prune old snapshots on the backup and send new ones
142 systemd.services."zfs-local-backup-home@" = {
143 description = "ZFS backup home, on: %I";
144 wants = [ "zfs-term@%i.service" ];
145 after = [ "zfs-force-import@%i.service" ];
146 requires = [ "zfs-force-import@%i.service" ];
147 bindsTo = [ "sys-subsystem-usb-%i.device" ];
148 path = lib.mkBefore [ "/run/booted-system/sw" ];
149 serviceConfig = rec {
152 CacheDirectory = [ "zfs-usb-backup/%I" ];
153 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
156 SyslogIdentifier = "zfs-local-backup-home@%i";
159 + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
162 if zpool status "$DESTPOOL"; then
163 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
164 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
165 zpool scrub -p "$DESTPOOL" || true
170 pkgs.writeShellScript "zfs-local-backup-home" ''
173 # sanoid is quite conservative:
174 # by setting hourly=24, a snapshot must be >24 hours old
175 # and there must been >24 total hourly snapshots,
176 # or nothing is pruned.
177 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
181 process_children_only=false
183 [$DESTPOOL/${User}/backup/${hostName}/home]
191 ${pkgs.sanoid}/bin/sanoid \
192 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
193 --configdir /tmp/sanoid \
195 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
198 for dataset in ${hostName}/home; do
199 ${pkgs.sanoid}/bin/syncoid \
201 --exclude "home/room" \
203 --no-privilege-elevation \
210 "$DESTPOOL"/${User}/backup/"$dataset"
216 + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
219 # Only if the zpool still exists to avoid uninterruptible hanging
220 if zpool status -v "$DESTPOOL"; then
221 # Scrub the zpool 1 minute (in the background)
222 zpool scrub "$DESTPOOL"
225 while zpool status -v "$DESTPOOL"; do
226 zpool scrub -p "$DESTPOOL" || true
228 # Export the zpool (to avoid a forced import later on)
229 zpool export "$DESTPOOL" || true
231 systemctl --no-block stop zfs-term@"$DESTPOOL"
236 programs.bash.shellAliases = {
237 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";