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