]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/pumpkin/backup.nix
nix: format with nixfmt-rfc-style
[julm/julm-nix.git] / hosts / pumpkin / backup.nix
1 {
2 pkgs,
3 lib,
4 config,
5 hostName,
6 ...
7 }:
8 with builtins;
9 {
10 systemd.services.sanoid.serviceConfig.SupplementaryGroups = [
11 config.users.groups."disk".name
12 ];
13 services.sanoid = {
14 enable = true;
15 extraArgs = [ "--verbose" ];
16 datasets = {
17 "${hostName}/root" = {
18 autosnap = true;
19 autoprune = true;
20 frequently = 0;
21 hourly = 12;
22 daily = 7;
23 monthly = 0;
24 yearly = 0;
25 recursive = true;
26 };
27 "${hostName}/root/nix" = {
28 autosnap = false;
29 };
30 "${hostName}/root/var/cache" = {
31 autosnap = false;
32 };
33 "${hostName}/root/var/log" = {
34 autosnap = false;
35 };
36 "${hostName}/root/home/julm/.cache" = {
37 autosnap = false;
38 };
39 "${hostName}/root/home/julm/.local" = {
40 hourly = 0;
41 daily = 1;
42 };
43 "${hostName}/root/home/julm/.mozilla" = {
44 hourly = 0;
45 daily = 1;
46 };
47 "off2/julm/backup/${hostName}" = {
48 autosnap = false;
49 autoprune = true;
50 hourly = 12;
51 daily = 14;
52 monthly = 3;
53 yearly = 0;
54 };
55 };
56 };
57
58 programs.bash.interactiveShellInit = ''
59 backup-pumpkin () {
60 local -
61 set -x
62 dst=
63 if ! zpool list off2
64 then dst=aubergine.sp:
65 fi
66 sudo syncoid --sshkey ~julm/.ssh/id_ed25519 \
67 --create-bookmark --no-sync-snap --no-privilege-elevation \
68 --preserve-properties --preserve-recordsize \
69 --recursive --sendoptions=w --recvoptions=u \
70 --exclude pumpkin/root/nix \
71 --exclude pumpkin/root/var/cache \
72 --exclude pumpkin/root/var/log \
73 --exclude pumpkin/root/home/julm/.cache \
74 pumpkin/root \
75 ''${dst}off2/julm/backup/pumpkin
76 zfs-fix-bookmarks pumpkin 2>/dev/null
77 }
78 mount-zfs-backup () {
79 (
80 set -eux
81 zpool="$1"
82 zpool status "$zpool" 2>/dev/null ||
83 sudo zpool import -d /dev/disk/by-id/ "$zpool"
84 trap "sudo zpool export $zpool" EXIT
85 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
86 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
87 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
88 )
89 }
90 '';
91 # Trigger backups when disks are plugged
92 services.udev.extraRules = ''
93 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"
94 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
95 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
96 '';
97 # Show what's happening to the user
98 systemd.services."zfs-term@" = {
99 description = "ZFS terminal for: %I";
100 unitConfig.StopWhenUnneeded = false;
101 environment.DISPLAY = ":0";
102 environment.XAUTHORITY = "/home/julm/.Xauthority";
103 after = [ "graphical.target" ];
104 bindsTo = [ "sys-subsystem-usb-%i.device" ];
105 serviceConfig = {
106 Type = "simple";
107 PrivateTmp = true;
108 ExecStart =
109 pkgs.writeShellScript "zfs-force-import" ''
110 DESTPOOL=$1
111 set -eux
112 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
113 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
114 -u zfs-force-import@$DESTPOOL \
115 -u zfs-local-backup-home@$DESTPOOL"
116 ''
117 + " %I";
118 };
119 };
120 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
121 systemd.services."zfs-force-import@" = {
122 description = "ZFS force import: %I";
123 unitConfig = {
124 StartLimitBurst = 5;
125 StartLimitInterval = 200;
126 StopWhenUnneeded = true;
127 };
128 wants = [ "zfs-term@%i.service" ];
129 bindsTo = [ "sys-subsystem-usb-%i.device" ];
130 path = lib.mkBefore [ "/run/booted-system/sw" ];
131 serviceConfig = {
132 Type = "oneshot";
133 RemainAfterExit = true;
134 PrivateTmp = true;
135 SyslogIdentifier = "zfs-force-import@%i";
136 Restart = "on-failure";
137 ExecStart =
138 pkgs.writeShellScript "zfs-force-import" ''
139 DESTPOOL=$1
140 set -eux
141 # Import the zpool, using stable paths
142 zpool import -d /dev/disk/by-id/ || true
143 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
144 zpool reopen "$DESTPOOL" ||
145 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
146 zpool clear -nFX "$DESTPOOL"
147 ''
148 + " %I";
149 };
150 };
151 # Prune old snapshots on the backup and send new ones
152 systemd.services."zfs-local-backup-home@" = {
153 description = "ZFS backup home, on: %I";
154 wants = [ "zfs-term@%i.service" ];
155 after = [ "zfs-force-import@%i.service" ];
156 requires = [ "zfs-force-import@%i.service" ];
157 bindsTo = [ "sys-subsystem-usb-%i.device" ];
158 path = lib.mkBefore [ "/run/booted-system/sw" ];
159 serviceConfig = rec {
160 Type = "oneshot";
161 PrivateTmp = true;
162 CacheDirectory = [ "zfs-usb-backup/%I" ];
163 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
164 User = "julm";
165 Group = "users";
166 SyslogIdentifier = "zfs-local-backup-home@%i";
167 ExecStartPre =
168 "+"
169 + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
170 DESTPOOL=$1
171 set -eux
172 if zpool status "$DESTPOOL"; then
173 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
174 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
175 zpool scrub -p "$DESTPOOL" || true
176 fi
177 ''
178 + " %I";
179 ExecStart =
180 pkgs.writeShellScript "zfs-local-backup-home" ''
181 set -eu
182 DESTPOOL=$1
183 # sanoid is quite conservative:
184 # by setting hourly=24, a snapshot must be >24 hours old
185 # and there must been >24 total hourly snapshots,
186 # or nothing is pruned.
187 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
188 [template_remote]
189 autoprune=true
190 autosnap=false
191 process_children_only=false
192
193 [$DESTPOOL/${User}/backup/${hostName}/home]
194 hourly=6
195 daily=31
196 monthly=3
197 recursive=true
198 use_template=remote
199 EOF
200 set -x
201 ${pkgs.sanoid}/bin/sanoid \
202 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
203 --configdir /tmp/sanoid \
204 --prune-snapshots \
205 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
206 --verbose
207
208 for dataset in ${hostName}/home; do
209 ${pkgs.sanoid}/bin/syncoid \
210 --create-bookmark \
211 --exclude "home/room" \
212 --force-delete \
213 --no-privilege-elevation \
214 --no-sync-snap \
215 --recursive \
216 --recvoptions "" \
217 --sendoptions raw \
218 --skip-parent \
219 "$dataset" \
220 "$DESTPOOL"/${User}/backup/"$dataset"
221 done
222 ''
223 + " %I";
224 ExecStartPost =
225 "+"
226 + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
227 DESTPOOL=$1
228 set -eux
229 # Only if the zpool still exists to avoid uninterruptible hanging
230 if zpool status -v "$DESTPOOL"; then
231 # Scrub the zpool 1 minute (in the background)
232 zpool scrub "$DESTPOOL"
233 sleep 60
234 fi
235 while zpool status -v "$DESTPOOL"; do
236 zpool scrub -p "$DESTPOOL" || true
237 sleep 20
238 # Export the zpool (to avoid a forced import later on)
239 zpool export "$DESTPOOL" || true
240 done
241 systemctl --no-block stop zfs-term@"$DESTPOOL"
242 ''
243 + " %I";
244 };
245 };
246 programs.bash.shellAliases = {
247 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
248 };
249 }