]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/pumpkin/backup.nix
pumpkin: sanoid: no longer use recursive=zfs
[julm/julm-nix.git] / hosts / pumpkin / backup.nix
1 { pkgs, lib, hostName, ... }:
2 with builtins;
3 {
4 # Take regular snapshots, and prune old ones
5 services.sanoid = {
6 enable = true;
7 extraArgs = [ "--verbose" ];
8 datasets = {
9 "${hostName}/root" = {
10 autosnap = true;
11 autoprune = true;
12 frequently = 1;
13 hourly = 12;
14 daily = 7;
15 monthly = 0;
16 yearly = 0;
17 recursive = true;
18 };
19 "${hostName}/root/nix" = {
20 autosnap = false;
21 };
22 "${hostName}/root/var/cache" = {
23 autosnap = false;
24 };
25 "${hostName}/root/home/julm/.cache" = {
26 autosnap = false;
27 };
28 "off2/julm/backup/${hostName}" = {
29 autosnap = false;
30 autoprune = true;
31 hourly = 12;
32 daily = 14;
33 monthly = 3;
34 yearly = 0;
35 };
36 };
37 };
38 programs.bash.interactiveShellInit = ''
39 backup-pumpkin () {
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 \
47 pumpkin/root \
48 aubergine.sp:off2/julm/backup/pumpkin
49 }
50 mount-zfs-backup () {
51 (
52 set -eux
53 zpool="$1"
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
60 )
61 }
62 '';
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"
68 '';
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" ];
77 serviceConfig = {
78 Type = "simple";
79 PrivateTmp = true;
80 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
81 DESTPOOL=$1
82 set -eux
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"
87 '' + " %I";
88 };
89 };
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";
93 unitConfig = {
94 StartLimitBurst = 5;
95 StartLimitInterval = 200;
96 StopWhenUnneeded = true;
97 };
98 wants = [ "zfs-term@%i.service" ];
99 bindsTo = [ "sys-subsystem-usb-%i.device" ];
100 path = lib.mkBefore [ "/run/booted-system/sw" ];
101 serviceConfig = {
102 Type = "oneshot";
103 RemainAfterExit = true;
104 PrivateTmp = true;
105 SyslogIdentifier = "zfs-force-import@%i";
106 Restart = "on-failure";
107 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
108 DESTPOOL=$1
109 set -eux
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"
116 '' + " %I";
117 };
118 };
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 {
128 Type = "oneshot";
129 PrivateTmp = true;
130 CacheDirectory = [ "zfs-usb-backup/%I" ];
131 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
132 User = "julm";
133 Group = "users";
134 SyslogIdentifier = "zfs-local-backup-home@%i";
135 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
136 DESTPOOL=$1
137 set -eux
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
142 fi
143 '' + " %I";
144 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
145 set -eu
146 DESTPOOL=$1
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
152 [template_remote]
153 autoprune=true
154 autosnap=false
155 process_children_only=false
156
157 [$DESTPOOL/${User}/backup/${hostName}/home]
158 hourly=6
159 daily=31
160 monthly=3
161 recursive=true
162 use_template=remote
163 EOF
164 set -x
165 ${pkgs.sanoid}/bin/sanoid \
166 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
167 --configdir /tmp/sanoid \
168 --prune-snapshots \
169 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
170 --verbose
171
172 for dataset in ${hostName}/home; do
173 ${pkgs.sanoid}/bin/syncoid \
174 --create-bookmark \
175 --exclude "home/room" \
176 --force-delete \
177 --no-privilege-elevation \
178 --no-sync-snap \
179 --recursive \
180 --recvoptions "" \
181 --sendoptions raw \
182 --skip-parent \
183 "$dataset" \
184 "$DESTPOOL"/${User}/backup/"$dataset"
185 done
186 '' + " %I";
187 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
188 DESTPOOL=$1
189 set -eux
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"
194 sleep 60
195 fi
196 while zpool status -v "$DESTPOOL"; do
197 zpool scrub -p "$DESTPOOL" || true
198 sleep 20
199 # Export the zpool (to avoid a forced import later on)
200 zpool export "$DESTPOOL" || true
201 done
202 systemctl --no-block stop zfs-term@"$DESTPOOL"
203 '' + " %I";
204 };
205 };
206 programs.bash.shellAliases = {
207 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
208 };
209 }