]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/oignon/backup.nix
bash: add riseup alias
[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 # sanoid is quite conservative:
118 # by setting hourly=24, a snapshot must be >24 hours old
119 # and there must been >24 total hourly snapshots,
120 # or nothing is pruned.
121 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
122 [template_remote]
123 autoprune=true
124 autosnap=false
125 process_children_only=false
126
127 [$DESTPOOL/${User}/backup/${hostName}/home]
128 hourly=6
129 daily=31
130 monthly=3
131 recursive=true
132 use_template=remote
133 EOF
134 set -x
135 ${pkgs.sanoid}/bin/sanoid \
136 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
137 --configdir /tmp/sanoid \
138 --prune-snapshots \
139 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
140 --verbose
141
142 for dataset in ${hostName}/home; do
143 ${pkgs.sanoid}/bin/syncoid \
144 --create-bookmark \
145 --exclude "home/room" \
146 --force-delete \
147 --no-privilege-elevation \
148 --no-sync-snap \
149 --recursive \
150 --recvoptions "" \
151 --sendoptions raw \
152 --skip-parent \
153 "$dataset" \
154 "$DESTPOOL"/${User}/backup/"$dataset"
155 done
156 '' + " %I";
157 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
158 DESTPOOL=$1
159 set -eux
160 # Only if the zpool still exists to avoid uninterruptible hanging
161 if zpool status -v "$DESTPOOL"; then
162 # Scrub the zpool 1 minute (in the background)
163 zpool scrub "$DESTPOOL"
164 sleep 60
165 fi
166 while zpool status -v "$DESTPOOL"; do
167 zpool scrub -p "$DESTPOOL" || true
168 sleep 20
169 # Export the zpool (to avoid a forced import later on)
170 zpool export "$DESTPOOL" || true
171 done
172 systemctl --no-block stop zfs-term@"$DESTPOOL"
173 '' + " %I";
174 };
175 };
176 programs.bash.interactiveShellInit = ''
177 mount-zfs-backup () {
178 (
179 set -eux
180 zpool="$1"
181 zpool status "$zpool" 2>/dev/null ||
182 sudo zpool import -d /dev/disk/by-id/ "$zpool"
183 trap "sudo zpool export $zpool" EXIT
184 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
185 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
186 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
187 )
188 }
189 '';
190 programs.bash.shellAliases = {
191 mount-backup-WD10JPVT = "mount-zfs-backup WD10JPVT";
192 };
193 }