1 { pkgs, lib, hostName, ... }:
4 # Take regular snapshots, and prune old ones
7 extraArgs = [ "--verbose" ];
19 "${hostName}/root/nix" = {
22 "${hostName}/root/var/cache" = {
25 "${hostName}/root/home/julm/.cache" = {
28 "off2/julm/backup/${hostName}" = {
38 programs.bash.interactiveShellInit = ''
40 sudo syncoid --sshkey ~julm/.ssh/id_ed25519 \
41 --create-bookmark --no-sync-snap --no-privilege-elevation \
42 --preserve-properties --preserve-recordsize \
43 --recursive --sendoptions=w --recvoptions=u \
44 --exclude pumpkin/root/nix \
45 --exclude pumpkin/root/var/cache \
46 --exclude pumpkin/root/home/julm/.cache \
48 aubergine.sp:off2/julm/backup/pumpkin
54 zpool status "$zpool" 2>/dev/null ||
55 sudo zpool import -d /dev/disk/by-id/ "$zpool"
56 trap "sudo zpool export $zpool" EXIT
57 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
58 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
59 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
63 # Trigger backups when disks are plugged
64 services.udev.extraRules = ''
65 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"
66 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
67 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
69 # Show what's happening to the user
70 systemd.services."zfs-term@" = {
71 description = "ZFS terminal for: %I";
72 unitConfig.StopWhenUnneeded = false;
73 environment.DISPLAY = ":0";
74 environment.XAUTHORITY = "/home/julm/.Xauthority";
75 after = [ "graphical.target" ];
76 bindsTo = [ "sys-subsystem-usb-%i.device" ];
80 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
83 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
84 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
85 -u zfs-force-import@$DESTPOOL \
86 -u zfs-local-backup-home@$DESTPOOL"
90 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
91 systemd.services."zfs-force-import@" = {
92 description = "ZFS force import: %I";
95 StartLimitInterval = 200;
96 StopWhenUnneeded = true;
98 wants = [ "zfs-term@%i.service" ];
99 bindsTo = [ "sys-subsystem-usb-%i.device" ];
100 path = lib.mkBefore [ "/run/booted-system/sw" ];
103 RemainAfterExit = true;
105 SyslogIdentifier = "zfs-force-import@%i";
106 Restart = "on-failure";
107 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
110 # Import the zpool, using stable paths
111 zpool import -d /dev/disk/by-id/ || true
112 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
113 zpool reopen "$DESTPOOL" ||
114 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
115 zpool clear -nFX "$DESTPOOL"
119 # Prune old snapshots on the backup and send new ones
120 systemd.services."zfs-local-backup-home@" = {
121 description = "ZFS backup home, on: %I";
122 wants = [ "zfs-term@%i.service" ];
123 after = [ "zfs-force-import@%i.service" ];
124 requires = [ "zfs-force-import@%i.service" ];
125 bindsTo = [ "sys-subsystem-usb-%i.device" ];
126 path = lib.mkBefore [ "/run/booted-system/sw" ];
127 serviceConfig = rec {
130 CacheDirectory = [ "zfs-usb-backup/%I" ];
131 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
134 SyslogIdentifier = "zfs-local-backup-home@%i";
135 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
138 if zpool status "$DESTPOOL"; then
139 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
140 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
141 zpool scrub -p "$DESTPOOL" || true
144 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
147 # sanoid is quite conservative:
148 # by setting hourly=24, a snapshot must be >24 hours old
149 # and there must been >24 total hourly snapshots,
150 # or nothing is pruned.
151 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
155 process_children_only=false
157 [$DESTPOOL/${User}/backup/${hostName}/home]
165 ${pkgs.sanoid}/bin/sanoid \
166 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
167 --configdir /tmp/sanoid \
169 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
172 for dataset in ${hostName}/home; do
173 ${pkgs.sanoid}/bin/syncoid \
175 --exclude "home/room" \
177 --no-privilege-elevation \
184 "$DESTPOOL"/${User}/backup/"$dataset"
187 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
190 # Only if the zpool still exists to avoid uninterruptible hanging
191 if zpool status -v "$DESTPOOL"; then
192 # Scrub the zpool 1 minute (in the background)
193 zpool scrub "$DESTPOOL"
196 while zpool status -v "$DESTPOOL"; do
197 zpool scrub -p "$DESTPOOL" || true
199 # Export the zpool (to avoid a forced import later on)
200 zpool export "$DESTPOOL" || true
202 systemctl --no-block stop zfs-term@"$DESTPOOL"
206 programs.bash.shellAliases = {
207 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";