-
Notifications
You must be signed in to change notification settings - Fork 1
/
duplicity-backup.nix
297 lines (257 loc) · 9.16 KB
/
duplicity-backup.nix
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
{ config, lib, pkgs, utils, ... }:
with lib;
let
gcfg = config.services.duplicity-backup;
in
{
options = {
services.duplicity-backup = {
enable = mkEnableOption "periodic duplicity backups";
sshIdentityFile = mkOption {
type = types.path;
default = /var/empty;
description = ''
'';
};
knownHostsFile = mkOption {
type = types.path;
default = /var/empty;
description = ''
'';
};
keyring = mkOption {
type = types.path;
default = /var/empty;
description = ''
'';
};
keyId = mkOption {
type = types.string;
default = "00000000";
description = ''
'';
};
pgpKeyFile = mkOption {
type = types.path;
default = /var/empty;
description = ''
'';
};
passphraseFile = mkOption {
type = types.path;
default = /var/empty;
description = ''
'';
};
archives = mkOption {
type = types.attrsOf (types.submodule ({ config, ... }:
{
options = {
requiredNixopsKeys = mkOption {
type = types.listOf types.string;
default = [ ];
example = [ "my-passphrase" "my-ssh-id" ];
description = ''
A list of nixops keys on which to depend
(will create the necessary <keyname>-key.service
systemd dependencies)
'';
};
sshIdentityFile = mkOption {
type = types.path;
default = gcfg.sshIdentityFile;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
knownHostsFile = mkOption {
type = types.path;
default = gcfg.knownHostsFile;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
keyring = mkOption {
type = types.path;
default = gcfg.keyring;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
pgpKeyFile = mkOption {
type = types.path;
default = gcfg.gpgKeyFile;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
passphraseFile = mkOption {
type = types.path;
default = gcfg.passphraseFile;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
keyId = mkOption {
type = types.string;
default = gcfg.keyId;
description = ''
Set a specific ___ for this archive. This defaults to
if left unspecified.
'';
};
cachedir = mkOption {
type = types.path;
default = "/var/cache/duplicity/";
description = ''
The cache allows duplicity to identify previously stored data
blocks, reducing archival time and bandwidth usage.
'';
};
destination = mkOption {
type = types.string;
default = "";
example = "rsync://user@example.com:/home/user";
description = ''
'';
};
period = mkOption {
type = types.str;
default = "01:15";
example = "hourly";
description = ''
Create archive at this interval.
The format is described in
<citerefentry><refentrytitle>systemd.time</refentrytitle>
<manvolnum>7</manvolnum></citerefentry>.
'';
};
directories = mkOption {
type = types.listOf types.path;
default = [];
description = "List of filesystem paths to archive.";
};
excludes = mkOption {
type = types.listOf types.str;
default = [];
description = ''
Exclude files and directories matching these patterns.
'';
};
includes = mkOption {
type = types.listOf types.str;
default = [];
description = ''
Include only files and directories matching these
patterns (the empty list includes everything).
Exclusions have precedence over inclusions.
'';
};
maxbw = mkOption {
type = types.nullOr types.int;
default = null;
description = ''
Abort archival if upstream bandwidth usage in bytes
exceeds this threshold.
'';
};
maxbwRateUp = mkOption {
type = types.nullOr types.int;
default = null;
example = literalExample "25 * 1000";
description = ''
Upload bandwidth rate limit in bytes.
'';
};
maxbwRateDown = mkOption {
type = types.nullOr types.int;
default = null;
example = literalExample "50 * 1000";
description = ''
Download bandwidth rate limit in bytes.
'';
};
};
}
));
default = {};
example = literalExample ''
{
nixos =
{ directories = [ "/home" "/root/ssl" ];
};
gamedata =
{ directories = [ "/var/lib/virtualMail" ];
period = "*:30";
};
}
'';
description = ''
Duplicity backup configurations. Each attribute names a backup
to be created at a given time interval, according to the options
associated with it.
For each member of the set is created a timer which triggers the
instanced <literal>duplicity-backup-name</literal> service unit. You may use
<command>systemctl start duplicity-backup-name</command> to
manually trigger creation of <literal>backup-name</literal> at
any time.
'';
};
};
};
config = mkIf gcfg.enable {
assertions =
(mapAttrsToList (name: cfg:
{ assertion = cfg.directories != [];
message = "Must specify paths for duplicity to back up";
}) gcfg.archives);
systemd.services =
mapAttrs' (name: cfg: nameValuePair "duplicity-${name}" {
description = "Duplicity archive '${name}'";
requires = [ "network-online.target" ]
++ (map (k: k + "-key.service") cfg.requiredNixopsKeys);
after = [ "network-online.target" ]
++ (map (k: k + "-key.service") cfg.requiredNixopsKeys);
path = with pkgs; [ iputils duplicity openssh gnupg utillinux ];
# make sure that the backup server is reachable
#preStart = ''
# while ! ping -q -c 1 ${findawaytoextracttheaddressmaybe} &> /dev/null; do sleep 3; done
#'';
script = ''
mkdir -p ${cfg.cachedir}
chmod 0700 ${cfg.cachedir}
gpg --import ${cfg.pgpKeyFile} # FIXME
export PASSPHRASE=$(cat ${cfg.passphraseFile})
duplicity \
--archive-dir ${cfg.cachedir} \
--name ${name} \
--ssh-options "-i '${cfg.sshIdentityFile}' -oUserKnownHostsFile='${cfg.knownHostsFile}'" \
# --gpg-options "--no-default-keyring --keyring ${cfg.keyring}" \
--encrypt-sign-key ${cfg.keyId} \
${concatStringsSep " " (map (v: "--exclude ${v}") cfg.excludes)} \
${concatStringsSep " " (map (v: "--include ${v}") cfg.includes)} \
${concatStringsSep " " cfg.directories} \
${cfg.destination}
'';
serviceConfig = {
Type = "oneshot";
IOSchedulingClass = "idle";
NoNewPrivileges = "true";
CapabilityBoundingSet = [ "CAP_DAC_READ_SEARCH" ];
PermissionsStartOnly = "true";
};
}) gcfg.archives;
# Note: the timer must be Persistent=true, so that systemd will start it even
# if e.g. your laptop was asleep while the latest interval occurred.
systemd.timers = mapAttrs' (name: cfg: nameValuePair "duplicity-${name}"
{ timerConfig.OnCalendar = cfg.period;
timerConfig.Persistent = "true";
wantedBy = [ "timers.target" ];
}) gcfg.archives;
};
}