]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/backup.nix
oignon: try to fix backup over USB (again)
[julm/julm-nix.git] / hosts / oignon / 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 = 7;
14 monthly = 0;
15 yearly = 0;
16 recursive = true;
17 };
18 "${hostName}/var" = {
19 autosnap = true;
20 autoprune = true;
21 hourly = 12;
22 daily = 7;
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 = true;
39 environment.DISPLAY = ":0";
40 environment.XAUTHORITY = "/home/julm/.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 cat \
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 after = [ "zfs-force-import@%i.service" ];
89 bindsTo = [ "zfs-force-import@%i.service" ];
90 path = lib.mkBefore [ "/run/booted-system/sw" ];
91 serviceConfig = rec {
92 Type = "oneshot";
93 RemainAfterExit = true;
94 PrivateTmp = true;
95 CacheDirectory = [ "zfs-usb-backup-%I" ];
96 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
97 User = "julm";
98 Group = "users";
99 SyslogIdentifier = "zfs-local-backup-home@%i";
100 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
101 DESTPOOL=$1
102 set -eux
103 if zpool status "$DESTPOOL"; then
104 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
105 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
106 zpool scrub -p "$DESTPOOL" || true
107 fi
108 '' + " %I";
109 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
110 set -eu
111 DESTPOOL=$1
112 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
113 [template_remote]
114 autoprune=true
115 autosnap=false
116 process_children_only=false
117
118 [$DESTPOOL/${User}/backup/${hostName}/home]
119 daily=31
120 monthly=0
121 recursive=true
122 use_template=remote
123 EOF
124 set -x
125 ${pkgs.sanoid}/bin/sanoid \
126 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
127 --configdir /tmp/sanoid \
128 --prune-snapshots \
129 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
130 --verbose
131
132 for dataset in ${hostName}/home; do
133 ${pkgs.sanoid}/bin/syncoid \
134 --create-bookmark \
135 --exclude "home/room" \
136 --force-delete \
137 --no-privilege-elevation \
138 --no-sync-snap \
139 --recursive \
140 --recvoptions "" \
141 --sendoptions raw \
142 --skip-parent \
143 "$dataset" \
144 "$DESTPOOL"/${User}/backup/"$dataset"
145 done
146 '' + " %I";
147 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
148 DESTPOOL=$1
149 set -eux
150 # Only if the zpool still exists to avoid uninterruptible hanging
151 if zpool status "$DESTPOOL"; then
152 # Scrub the zpool 1 minute (in the background)
153 zpool scrub "$DESTPOOL"
154 sleep 60
155 fi
156 if zpool status "$DESTPOOL"; then
157 zpool scrub -p "$DESTPOOL" || true
158 sleep 10
159 # Export the zpool (to avoid a forced import later on)
160 zpool export "$DESTPOOL"
161 fi
162 '' + " %I";
163 };
164 };
165 }