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 "off2/julm/backup/${hostName}" = {
58 programs.bash.interactiveShellInit = ''
63 zpool status "$zpool" 2>/dev/null ||
64 sudo zpool import -d /dev/disk/by-id/ "$zpool"
65 trap "sudo zpool export $zpool" EXIT
66 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
67 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
68 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
72 # Trigger backups when disks are plugged
73 services.udev.extraRules = ''
74 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"
75 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
76 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
78 # Show what's happening to the user
79 systemd.services."zfs-term@" = {
80 description = "ZFS terminal for: %I";
81 unitConfig.StopWhenUnneeded = false;
82 environment.DISPLAY = ":0";
83 environment.XAUTHORITY = "/home/julm/.Xauthority";
84 after = [ "graphical.target" ];
85 bindsTo = [ "sys-subsystem-usb-%i.device" ];
90 pkgs.writeShellScript "zfs-force-import" ''
93 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
94 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
95 -u zfs-force-import@$DESTPOOL \
96 -u zfs-local-backup-home@$DESTPOOL"
101 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
102 systemd.services."zfs-force-import@" = {
103 description = "ZFS force import: %I";
106 StartLimitInterval = 200;
107 StopWhenUnneeded = true;
109 wants = [ "zfs-term@%i.service" ];
110 bindsTo = [ "sys-subsystem-usb-%i.device" ];
111 path = lib.mkBefore [ "/run/booted-system/sw" ];
114 RemainAfterExit = true;
116 SyslogIdentifier = "zfs-force-import@%i";
117 Restart = "on-failure";
119 pkgs.writeShellScript "zfs-force-import" ''
122 # Import the zpool, using stable paths
123 zpool import -d /dev/disk/by-id/ || true
124 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
125 zpool reopen "$DESTPOOL" ||
126 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
127 zpool clear -nFX "$DESTPOOL"
132 # Prune old snapshots on the backup and send new ones
133 systemd.services."zfs-local-backup-home@" = {
134 description = "ZFS backup home, on: %I";
135 wants = [ "zfs-term@%i.service" ];
136 after = [ "zfs-force-import@%i.service" ];
137 requires = [ "zfs-force-import@%i.service" ];
138 bindsTo = [ "sys-subsystem-usb-%i.device" ];
139 path = lib.mkBefore [ "/run/booted-system/sw" ];
140 serviceConfig = rec {
143 CacheDirectory = [ "zfs-usb-backup/%I" ];
144 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
147 SyslogIdentifier = "zfs-local-backup-home@%i";
150 + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
153 if zpool status "$DESTPOOL"; then
154 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
155 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
156 zpool scrub -p "$DESTPOOL" || true
161 pkgs.writeShellScript "zfs-local-backup-home" ''
164 # sanoid is quite conservative:
165 # by setting hourly=24, a snapshot must be >24 hours old
166 # and there must been >24 total hourly snapshots,
167 # or nothing is pruned.
168 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
172 process_children_only=false
174 [$DESTPOOL/${User}/backup/${hostName}/home]
182 ${pkgs.sanoid}/bin/sanoid \
183 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
184 --configdir /tmp/sanoid \
186 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
189 for dataset in ${hostName}/home; do
190 ${pkgs.sanoid}/bin/syncoid \
192 --exclude "home/room" \
194 --no-privilege-elevation \
201 "$DESTPOOL"/${User}/backup/"$dataset"
207 + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
210 # Only if the zpool still exists to avoid uninterruptible hanging
211 if zpool status -v "$DESTPOOL"; then
212 # Scrub the zpool 1 minute (in the background)
213 zpool scrub "$DESTPOOL"
216 while zpool status -v "$DESTPOOL"; do
217 zpool scrub -p "$DESTPOOL" || true
219 # Export the zpool (to avoid a forced import later on)
220 zpool export "$DESTPOOL" || true
222 systemctl --no-block stop zfs-term@"$DESTPOOL"
227 programs.bash.shellAliases = {
228 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";