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