blob: b292f6f296b3a86550610179278cfa591663e4ae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
{ config, lib, utils, pkgs, ... }:
with utils;
with lib;
let
cfg = config.services.borgbackup;
lvmPath = {
options = {
LV = mkOption {
type = types.str;
};
VG = mkOption {
type = types.str;
};
};
};
pathType = if cfg.snapshots == "lvm" then types.submodule lvmPath else types.path;
systemdPath = path: escapeSystemdPath (if cfg.snapshots == "lvm" then "${path.VG}-${path.LV}" else path);
withSuffix = path: path + (if cfg.snapshots == "btrfs" then config.services.btrfs-snapshots.mountSuffix else config.services.lvm-snapshots.mountSuffix);
mountPoint = if cfg.snapshots == "lvm" then config.services.lvm-snapshots.mountPoint else "";
targetOptions = {
options = {
repo = mkOption {
type = types.str;
};
paths = mkOption {
type = types.listOf pathType;
default = [];
};
prune = mkOption {
type = types.attrsOf (types.listOf types.str);
default = {};
};
interval = mkOption {
type = types.str;
default = "6h";
};
jitter = mkOption {
type = with types; nullOr str;
default = "6h";
};
lock = mkOption {
type = types.nullOr types.str;
default = "backup";
};
network = mkOption {
type = types.bool;
default = true;
};
lockWait = mkOption {
type = types.int;
default = 600;
};
};
};
in {
options = {
services.borgbackup = {
snapshots = mkOption {
type = types.nullOr (types.enum ["btrfs" "lvm"]);
default = null;
};
targets = mkOption {
type = types.attrsOf (types.submodule targetOptions);
default = {};
};
prefix = mkOption {
type = types.str;
};
};
};
imports =
[ ./lvm-snapshots.nix
./btrfs-snapshots.nix
];
config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) {
services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true;
services.lvm-snapshots.snapshots = mkIf (cfg.snapshots == "lvm") (listToAttrs (map (path: nameValuePair (path.VG + "-" + path.LV) {
inherit (path) LV VG;
mountName = withSuffix (path.VG + "-" + path.LV);
}) (unique (flatten (mapAttrsToList (target: tCfg: tCfg.paths) cfg.targets)))));
systemd.targets."timers-borg" = {
wantedBy = [ "timers.target" ];
};
systemd.slices."system-borgbackup" = {};
systemd.timers = (listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${systemdPath path}" {
requiredBy = [ "timers-borg.target" ];
timerConfig = {
Persistent = false;
OnBootSec = tCfg.interval;
OnUnitActiveSec = tCfg.interval;
RandomizedDelaySec = mkIf (tCfg.jitter != null) tCfg.jitter;
};
}) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets)))) // (mapAttrs' (target: tCfg: nameValuePair "borgbackup-prune-${target}" {
enable = tCfg.prune != {};
requiredBy = [ "timers-borg.target" ];
timerConfig = {
Persistent = false;
OnBootSec = tCfg.interval;
OnUnitActiveSec = tCfg.interval;
RandomizedDelaySec = mkIf (tCfg.jitter != null) tCfg.jitter;
};
}) cfg.targets);
systemd.services = (mapAttrs' (target: tCfg: nameValuePair "borgbackup-${target}@" (let
deps = flatten [
(optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service")
(optional tCfg.network "network-online.target")
];
in {
bindsTo = deps;
after = deps;
path = with pkgs; [borgbackup] ++ optional (tCfg.lock != null) utillinux;
script = let
borgCmd = ''
borg create \
--lock-wait ${toString tCfg.lockWait} \
--stats \
--list \
--filter 'AME' \
--exclude-caches \
--keep-exclude-tags \
--patterns-from .backup-${target} \
--one-file-system \
--compression auto,lzma \
${tCfg.repo}::${cfg.prefix}$1-{utcnow}
'';
in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}";
scriptArgs = if cfg.snapshots == "lvm" then "%I" else "%i";
unitConfig = {
AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock";
DefaultDependencies = false;
RequiresMountsFor = mkIf (cfg.snapshots == "lvm") [ "${mountPoint}/${withSuffix "%I"}" ];
};
serviceConfig = {
Type = "oneshot";
WorkingDirectory = if (cfg.snapshots == null) then "%I" else (if (cfg.snapshots == "lvm") then "${withSuffix "%I"}" else "${withSuffix "%f"}");
Nice = 15;
IOSchedulingClass = 2;
IOSchedulingPriority = 7;
SuccessExitStatus = [1 2];
Slice = "system-borgbackup.slice";
};
})) cfg.targets) // (mapAttrs' (target: tCfg: nameValuePair "borgbackup-prune-${target}" {
enable = tCfg.prune != {};
bindsTo = ["network-online.target"];
after = ["network-online.target"];
path = with pkgs; [borgbackup];
script = concatStringsSep "\n" (mapAttrsToList (path: args: ''
borg prune \
--lock-wait ${toString tCfg.lockWait} \
--list \
--stats \
--prefix ${escapeShellArg "${cfg.prefix}${path}"} \
${escapeShellArgs args} \
${tCfg.repo}
'') tCfg.prune);
serviceConfig = {
Type = "oneshot";
Slice = "system-borgbackup.slice";
};
}) cfg.targets);
};
}
|