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