]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/patate/backup.nix
backup: try to fix zfs-force-import
[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 # 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 = "sevy";
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/Downloads" \
105 --exclude "home/Videos" \
106 --force-delete \
107 --no-privilege-elevation \
108 --no-sync-snap \
109 --recursive \
110 --recvoptions "" \
111 --sendoptions raw \
112 --skip-parent \
113 "$dataset" \
114 "$DESTPOOL"/${User}/backup/"$dataset"
115 done
116 '' + " %I";
117 ExecStartPost = [
118 # Scrub the zpool 1 minute (in the background)
119 "+/run/booted-system/sw/bin/zpool scrub %I"
120 "${pkgs.coreutils}/bin/sleep 60"
121 "+/bin/sh -xc '/run/booted-system/sw/bin/zpool scrub -p %I || true'"
122 # Export the zpool (to avoid a forced import later on)
123 "+/run/booted-system/sw/bin/zpool export %I"
124 ];
125 };
126 };
127 }