]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/patate/backup/zfs-backup.nix
rofi: use --scope
[julm/julm-nix.git] / hosts / patate / backup / zfs-backup.nix
1 { pkgs, lib, hostName, ... }:
2 with builtins;
3 {
4 # Show what's happening to the user
5 systemd.services."zfs-term@" = {
6 description = "ZFS terminal for: %I";
7 unitConfig.StopWhenUnneeded = false;
8 environment.DISPLAY = ":0";
9 environment.XAUTHORITY = "/home/sevy/.Xauthority";
10 after = [ "graphical.target" ];
11 serviceConfig = {
12 Type = "simple";
13 PrivateTmp = true;
14 ExecStart = pkgs.writeShellScript "zfs-term" ''
15 DESTPOOL=$1
16 set -eux
17 ${pkgs.xterm}/bin/xterm -fg white -bg black -fa Monospace -fs 6 \
18 -title "ZFS backup to: $DESTPOOL" -e "journalctl -f -o short \
19 -u zfs-force-import@$DESTPOOL \
20 -u zfs-local-backup-home@$DESTPOOL"
21 '' + " %I";
22 };
23 };
24 # Force zpool import, even if the disk has not been exported, or has been imported on another computer
25 systemd.services."zfs-force-import@" = {
26 description = "ZFS force import: %I";
27 unitConfig = {
28 StartLimitBurst = 5;
29 StartLimitInterval = 200;
30 StopWhenUnneeded = true;
31 };
32 wants = [ "zfs-term@%i.service" ];
33 bindsTo = [ "sys-subsystem-usb-%i.device" ];
34 path = lib.mkBefore [ "/run/booted-system/sw" ];
35 serviceConfig = {
36 Type = "oneshot";
37 RemainAfterExit = true;
38 PrivateTmp = true;
39 SyslogIdentifier = "zfs-force-import@%i";
40 Restart = "on-failure";
41 ExecStart = pkgs.writeShellScript "zfs-force-import" ''
42 DESTPOOL=$1
43 set -eux
44 # Import the zpool, using stable paths
45 zpool import -d /dev/disk/by-id/ || true
46 zpool import -lFd /dev/disk/by-id/ "$DESTPOOL" ||
47 zpool reopen "$DESTPOOL" ||
48 zpool import -f -d /dev/disk/by-id/ "$DESTPOOL" ||
49 zpool clear -nFX "$DESTPOOL"
50 '' + " %I";
51 };
52 };
53 # Prune old snapshots on the backup and send new ones
54 systemd.services."zfs-local-backup-home@" = {
55 description = "ZFS backup home, on: %I";
56 wants = [ "zfs-term@%i.service" ];
57 after = [ "zfs-force-import@%i.service" ];
58 requires = [ "zfs-force-import@%i.service" ];
59 bindsTo = [ "sys-subsystem-usb-%i.device" ];
60 path = lib.mkBefore [ "/run/booted-system/sw" ];
61 serviceConfig = rec {
62 Type = "oneshot";
63 PrivateTmp = true;
64 CacheDirectory = [ "zfs-usb-backup/%I" ];
65 RuntimeDirectory = [ "zfs-usb-backup/%I" ];
66 User = "sevy";
67 Group = "users";
68 SyslogIdentifier = "zfs-local-backup-home@%i";
69 ExecStartPre = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPre" ''
70 DESTPOOL=$1
71 set -eux
72 if zpool status "$DESTPOOL"; then
73 zfs allow ${User} bookmark,hold,mount,send ${hostName}/home
74 zfs allow ${User} bookmark,create,destroy,load-key,mount,mountpoint,receive,rollback,snapshot "$DESTPOOL"/${User}
75 zpool scrub -p "$DESTPOOL" || true
76 fi
77 '' + " %I";
78 ExecStart = pkgs.writeShellScript "zfs-local-backup-home" ''
79 set -eu
80 DESTPOOL=$1
81 # sanoid is quite conservative:
82 # by setting hourly=24, a snapshot must be >24 hours old
83 # and there must been >24 total hourly snapshots,
84 # or nothing is pruned.
85 install -D -m 400 /dev/stdin /tmp/sanoid/sanoid.conf <<EOF
86 [template_remote]
87 autoprune=true
88 autosnap=false
89 process_children_only=false
90
91 [$DESTPOOL/${User}/backup/${hostName}/home]
92 hourly=12
93 daily=31
94 monthly=6
95 recursive=true
96 use_template=remote
97 EOF
98 set -x
99 ${pkgs.sanoid}/bin/sanoid \
100 --cache-dir /var/cache/zfs-usb-backup/"$DESTPOOL" \
101 --configdir /tmp/sanoid \
102 --prune-snapshots \
103 --run-dir /run/zfs-usb-backup/"$DESTPOOL" \
104 --verbose
105
106 for dataset in ${hostName}/home; do
107 ${pkgs.sanoid}/bin/syncoid \
108 --create-bookmark \
109 --exclude "home/Downloads" \
110 --exclude "home/Videos" \
111 --force-delete \
112 --no-privilege-elevation \
113 --no-sync-snap \
114 --recursive \
115 --recvoptions "" \
116 --sendoptions raw \
117 --skip-parent \
118 "$dataset" \
119 "$DESTPOOL"/${User}/backup/"$dataset"
120 done
121 '' + " %I";
122 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
123 DESTPOOL=$1
124 set -eux
125 # Only if the zpool still exists to avoid uninterruptible hanging
126 if zpool status -v "$DESTPOOL"; then
127 # Scrub the zpool 1 minute (in the background)
128 zpool scrub "$DESTPOOL"
129 sleep 60
130 fi
131 while zpool status -v "$DESTPOOL"; do
132 zpool scrub -p "$DESTPOOL" || true
133 sleep 20
134 # Export the zpool (to avoid a forced import later on)
135 zpool export "$DESTPOOL" || true
136 done
137 systemctl --no-block stop zfs-term@"$DESTPOOL"
138 '' + " %I";
139 };
140 };
141 programs.bash.interactiveShellInit = ''
142 mount-zfs-backup () {
143 (
144 set -eux
145 zpool="$1"
146 zpool status "$zpool" 2>/dev/null ||
147 sudo zpool import -d /dev/disk/by-id/ "$zpool"
148 trap "sudo zpool export $zpool" EXIT
149 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
150 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
151 ${pkgs.mate.caja-with-extensions}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
152 )
153 }
154 '';
155 }