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