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