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