]> Git — Sourcephile - julm/julm-nix.git/blob - hosts/patate/backup/zfs-backup.nix
update nixpkgs to recent master
[julm/julm-nix.git] / hosts / patate / backup / zfs-backup.nix
1 { pkgs, lib, config, 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 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 hourly=12
89 daily=31
90 monthly=0
91 recursive=true
92 use_template=remote
93 EOF
94 set -x
95 ${pkgs.sanoid}/bin/sanoid \
96 --cache-dir /var/cache/zfs-usb-backup-"$DESTPOOL" \
97 --configdir /tmp/sanoid \
98 --prune-snapshots \
99 --run-dir /run/zfs-usb-backup-"$DESTPOOL" \
100 --verbose
101
102 for dataset in ${hostName}/home; do
103 ${pkgs.sanoid}/bin/syncoid \
104 --create-bookmark \
105 --exclude "home/Downloads" \
106 --exclude "home/Videos" \
107 --force-delete \
108 --no-privilege-elevation \
109 --no-sync-snap \
110 --recursive \
111 --recvoptions "" \
112 --sendoptions raw \
113 --skip-parent \
114 "$dataset" \
115 "$DESTPOOL"/${User}/backup/"$dataset"
116 done
117 '' + " %I";
118 ExecStartPost = "+" + pkgs.writeShellScript "zfs-local-backup-home-startPost" ''
119 DESTPOOL=$1
120 set -eux
121 # Only if the zpool still exists to avoid uninterruptible hanging
122 if zpool status "$DESTPOOL"; then
123 # Scrub the zpool 1 minute (in the background)
124 zpool scrub "$DESTPOOL"
125 sleep 60
126 fi
127 if zpool status "$DESTPOOL"; then
128 zpool scrub -p "$DESTPOOL" || true
129 sleep 20
130 # Export the zpool (to avoid a forced import later on)
131 zpool export "$DESTPOOL"
132 fi
133 systemctl --no-block stop zfs-term@"$DESTPOOL"
134 '' + " %I";
135 };
136 };
137 programs.bash.interactiveShellInit = ''
138 mount-zfs-backup () {
139 (
140 set -eux
141 zpool="$1"
142 zpool status "$zpool" 2>/dev/null ||
143 sudo zpool import -d /dev/disk/by-id/ "$zpool"
144 trap "sudo zpool export $zpool" EXIT
145 zfs list -rH -t filesystem -o mounted,mountpoint,name "$zpool"/"$USER"/backup |
146 grep "^no\\s*/" | cut -f 3 | xargs -ortL1 sudo zfs mount -Olv || true
147 ${pkgs.mate.caja}/bin/caja --browser /mnt/"$zpool"/"$USER"/backup
148 )
149 }
150 '';
151 }