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