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