1 { pkgs, lib, config, hostName, ... }:
4 systemd.services.sanoid.serviceConfig.SupplementaryGroups = [
5 config.users.groups."disk".name
9 extraArgs = [ "--verbose" ];
11 "${hostName}/root" = {
21 "${hostName}/root/nix" = {
24 "${hostName}/root/var/cache" = {
27 "${hostName}/root/var/log" = {
30 "${hostName}/root/home/julm/.cache" = {
33 "${hostName}/root/home/julm/.local" = {
37 "${hostName}/root/home/julm/.mozilla" = {
41 "off2/julm/backup/${hostName}" = {
52 programs.bash.interactiveShellInit = ''
58 then dst=aubergine.sp:
60 sudo syncoid --sshkey ~julm/.ssh/id_ed25519 \
61 --create-bookmark --no-sync-snap --no-privilege-elevation \
62 --preserve-properties --preserve-recordsize \
63 --recursive --sendoptions=w --recvoptions=u \
64 --exclude pumpkin/root/nix \
65 --exclude pumpkin/root/var/cache \
66 --exclude pumpkin/root/var/log \
67 --exclude pumpkin/root/home/julm/.cache \
69 ''${dst}off2/julm/backup/pumpkin
70 zfs-fix-bookmarks pumpkin 2>/dev/null
76 zpool status "$zpool" 2>/dev/null ||
77 sudo zpool import -d /dev/disk/by-id/ "$zpool"
78 trap "sudo zpool export $zpool" EXIT
79 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
80 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
81 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
85 # Trigger backups when disks are plugged
86 services.udev.extraRules = ''
87 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"
88 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
89 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
91 # Show what's happening to the user
92 systemd.services."zfs-term@" = {
93 description = "ZFS terminal for: %I";
94 unitConfig.StopWhenUnneeded = false;
95 environment.DISPLAY = ":0";
96 environment.XAUTHORITY = "/home/julm/.Xauthority";
97 after = [ "graphical.target" ];
98 bindsTo = [ "sys-subsystem-usb-%i.device" ];
102 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
105 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
106 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
107 -u zfs-force-import@$DESTPOOL \
108 -u zfs-local-backup-home@$DESTPOOL"
112 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
113 systemd.services."zfs-force-import@" = {
114 description = "ZFS force import: %I";
117 StartLimitInterval = 200;
118 StopWhenUnneeded = true;
120 wants = [ "zfs-term@%i.service" ];
121 bindsTo = [ "sys-subsystem-usb-%i.device" ];
122 path = lib.mkBefore [ "/run/booted-system/sw" ];
125 RemainAfterExit = true;
127 SyslogIdentifier = "zfs-force-import@%i";
128 Restart = "on-failure";
129 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
132 # Import the zpool, using stable paths
133 zpool import -d /dev/disk/by-id/ || true
134 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
135 zpool reopen "$DESTPOOL" ||
136 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
137 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";
157 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
160 if zpool status "$DESTPOOL"; then
161 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
162 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
163 zpool scrub -p "$DESTPOOL" || true
166 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
169 # sanoid is quite conservative:
170 # by setting hourly=24, a snapshot must be >24 hours old
171 # and there must been >24 total hourly snapshots,
172 # or nothing is pruned.
173 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
177 process_children_only=false
179 [$DESTPOOL/${User}/backup/${hostName}/home]
187 ${pkgs.sanoid}/bin/sanoid \
188 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
189 --configdir /tmp/sanoid \
191 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
194 for dataset in ${hostName}/home; do
195 ${pkgs.sanoid}/bin/syncoid \
197 --exclude "home/room" \
199 --no-privilege-elevation \
206 "$DESTPOOL"/${User}/backup/"$dataset"
209 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
212 # Only if the zpool still exists to avoid uninterruptible hanging
213 if zpool status -v "$DESTPOOL"; then
214 # Scrub the zpool 1 minute (in the background)
215 zpool scrub "$DESTPOOL"
218 while zpool status -v "$DESTPOOL"; do
219 zpool scrub -p "$DESTPOOL" || true
221 # Export the zpool (to avoid a forced import later on)
222 zpool export "$DESTPOOL" || true
224 systemctl --no-block stop zfs-term@"$DESTPOOL"
228 programs.bash.shellAliases = {
229 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";