]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/backup.nix
oignon: add x2goclient
[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 };
18 "${hostName}/home/room" = {
19 autosnap = false;
20 autoprune = false;
21 };
22 "${hostName}/var" = {
23 autosnap = true;
24 autoprune = true;
25 hourly = 12;
26 daily = 1;
27 monthly = 0;
28 yearly = 0;
29 recursive = true;
30 };
31 };
32 };
33 # Trigger backups when disks are plugged
34 services.udev.extraRules = ''
35 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"
36 # See https://github.com/systemd/systemd/issues/7587#issuecomment-381428545
37 ACTION=="remove", SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="WDC_WD10JPVT-22A1YT0_WD-WX21AC2F3987", TAG+="systemd"
38 '';
39 # Show what's happening to the user
40 systemd.services."zfs-term@" = {
41 description = "ZFS terminal for: %I";
42 unitConfig.StopWhenUnneeded = false;
43 environment.DISPLAY = ":0";
44 environment.XAUTHORITY = "/home/julm/.Xauthority";
45 after = [ "graphical.target" ];
46 bindsTo = [ "sys-subsystem-usb-%i.device" ];
47 serviceConfig = {
48 Type = "simple";
49 PrivateTmp = true;
50 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
51 DESTPOOL=$1
52 set -eux
53 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
54 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
55 -u zfs-force-import@$DESTPOOL \
56 -u zfs-local-backup-home@$DESTPOOL"
57 '' + " %I";
58 };
59 };
60 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
61 systemd.services."zfs-force-import@" = {
62 description = "ZFS force import: %I";
63 unitConfig = {
64 StartLimitBurst = 5;
65 StartLimitInterval = 200;
66 StopWhenUnneeded = true;
67 };
68 wants = [ "zfs-term@%i.service" ];
69 bindsTo = [ "sys-subsystem-usb-%i.device" ];
70 path = lib.mkBefore [ "/run/booted-system/sw" ];
71 serviceConfig = {
72 Type = "oneshot";
73 RemainAfterExit = true;
74 PrivateTmp = true;
75 SyslogIdentifier = "zfs-force-import@%i";
76 Restart = "on-failure";
77 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
78 DESTPOOL=$1
79 set -eux
80 # Import the zpool, using stable paths
81 zpool import -d /dev/disk/by-id/ || true
82 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
83 zpool reopen "$DESTPOOL" ||
84 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
85 zpool clear -nFX "$DESTPOOL"
86 '' + " %I";
87 };
88 };
89 # Prune old snapshots on the backup and send new ones
90 systemd.services."zfs-local-backup-home@" = {
91 description = "ZFS backup home, on: %I";
92 wants = [ "zfs-term@%i.service" ];
93 after = [ "zfs-force-import@%i.service" ];
94 requires = [ "zfs-force-import@%i.service" ];
95 bindsTo = [ "sys-subsystem-usb-%i.device" ];
96 path = lib.mkBefore [ "/run/booted-system/sw" ];
97 serviceConfig = rec {
98 Type = "oneshot";
99 PrivateTmp = true;
100 CacheDirectory = [ "zfs-usb-backup-%I" ];
101 RuntimeDirectory = [ "zfs-usb-backup-%I" ];
102 User = "julm";
103 Group = "users";
104 SyslogIdentifier = "zfs-local-backup-home@%i";
105 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
106 DESTPOOL=$1
107 set -eux
108 if zpool status "$DESTPOOL"; then
109 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
110 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
111 zpool scrub -p "$DESTPOOL" || true
112 fi
113 '' + " %I";
114 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
115 set -eu
116 DESTPOOL=$1
117 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
118 [template_remote]
119 autoprune=true
120 autosnap=false
121 process_children_only=false
122
123 [$DESTPOOL/${User}/backup/${hostName}/home]
124 hourly=12
125 daily=31
126 monthly=0
127 recursive=true
128 use_template=remote
129 EOF
130 set -x
131 ${pkgs.sanoid}/bin/sanoid \
132 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
133 --configdir /tmp/sanoid \
134 --prune-snapshots \
135 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
136 --verbose
137
138 for dataset in ${hostName}/home; do
139 ${pkgs.sanoid}/bin/syncoid \
140 --create-bookmark \
141 --exclude "home/room" \
142 --force-delete \
143 --no-privilege-elevation \
144 --no-sync-snap \
145 --recursive \
146 --recvoptions "" \
147 --sendoptions raw \
148 --skip-parent \
149 "$dataset" \
150 "$DESTPOOL"/${User}/backup/"$dataset"
151 done
152 '' + " %I";
153 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
154 DESTPOOL=$1
155 set -eux
156 # Only if the zpool still exists to avoid uninterruptible hanging
157 if zpool status "$DESTPOOL"; then
158 # Scrub the zpool 1 minute (in the background)
159 zpool scrub "$DESTPOOL"
160 sleep 60
161 fi
162 if zpool status "$DESTPOOL"; then
163 zpool scrub -p "$DESTPOOL" || true
164 sleep 20
165 # Export the zpool (to avoid a forced import later on)
166 zpool export "$DESTPOOL"
167 fi
168 systemctl --no-block stop zfs-term@"$DESTPOOL"
169 '' + " %I";
170 };
171 };
172 programs.bash.interactiveShellInit = ''
173 mount-zfs-backup () {
174 (
175 set -eux
176 zpool="$1"
177 zpool status "$zpool" 2>/dev/null ||
178 sudo zpool import -d /dev/disk/by-id/ "$zpool"
179 trap "sudo zpool export $zpool" EXIT
180 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
181 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
182 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
183 )
184 }
185 '';
186 programs.bash.shellAliases = {
187 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
188 };
189 }