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