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