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