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