]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/backup.nix
oignon: try to fix backup over USB
[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 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
36 systemd.services."zfs-force-import@" = {
37 description = "ZFS force import: %I";
38 unitConfig.StopWhenUnneeded = true;
39 bindsTo = [ "sys-subsystem-usb-%i.device" ];
40 path = lib.mkBefore [ "/run/booted-system/sw" ];
41 serviceConfig = {
42 Type = "oneshot";
43 RemainAfterExit = true;
44 PrivateTmp = true;
45 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
46 DESTPOOL=$1
47 set -eux
48 # Import the zpool, using stable paths
49 zpool import -d /dev/disk/by-id/ || true
50 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
51 zpool reopen "$DESTPOOL" ||
52 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
53 zpool clear -nFX "$DESTPOOL"
54 '' + " %I";
55 ExecStartPost = "+" + pkgs.writeShellScript "zfs-force-import-startPost" ''
56 DESTPOOL=$1
57 set -eux
58 # Do not block on unplug/failure
59 zpool set failmode=continue "$DESTPOOL"
60 '' + " %I";
61 };
62 };
63 # Run the backup
64 systemd.services."zfs-local-backup-home@" = {
65 description = "ZFS backup home, on: %I";
66 after = [ "zfs-force-import@%i.service" ];
67 bindsTo = [ "zfs-force-import@%i.service" ];
68 path = lib.mkBefore [ "/run/booted-system/sw" ];
69 serviceConfig = rec {
70 Type = "oneshot";
71 RemainAfterExit = true;
72 PrivateTmp = true;
73 CacheDirectory = [ "zfs-usb-backup-%I" ];
74 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
75 User = "julm";
76 Group = "users";
77 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
78 DESTPOOL=$1
79 set -eux
80 if zpool status "$DESTPOOL"; then
81 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
82 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
83 zpool scrub -p "$DESTPOOL" || true
84 fi
85 '' + " %I";
86 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
87 set -eu
88 DESTPOOL=$1
89 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
90 [template_remote]
91 autoprune=true
92 autosnap=false
93 process_children_only=false
94
95 [$DESTPOOL/${User}/backup/${hostName}/home]
96 daily=31
97 monthly=0
98 recursive=true
99 use_template=remote
100 EOF
101 set -x
102 ${pkgs.sanoid}/bin/sanoid \
103 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
104 --configdir /tmp/sanoid \
105 --prune-snapshots \
106 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
107 --verbose
108
109 for dataset in ${hostName}/home; do
110 ${pkgs.sanoid}/bin/syncoid \
111 --create-bookmark \
112 --exclude "home/room" \
113 --force-delete \
114 --no-privilege-elevation \
115 --no-sync-snap \
116 --recursive \
117 --recvoptions "" \
118 --sendoptions raw \
119 --skip-parent \
120 "$dataset" \
121 "$DESTPOOL"/${User}/backup/"$dataset"
122 done
123 '' + " %I";
124 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
125 DESTPOOL=$1
126 set -eux
127 # Only if the zpool still exists to avoid uninterruptible hanging
128 if zpool status "$DESTPOOL"; then
129 # Scrub the zpool 1 minute (in the background)
130 zpool scrub "$DESTPOOL"
131 sleep 60
132 fi
133 if zpool status "$DESTPOOL"; then
134 zpool scrub -p "$DESTPOOL" || true
135 # Export the zpool (to avoid a forced import later on)
136 zpool export "$DESTPOOL"
137 fi
138 '' + " %I";
139 };
140 };
141 }