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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
{ pkgs, ... }:
let
inherit (import ./ssh-keys.nix pkgs)
snakeOilEd25519PrivateKey
snakeOilEd25519PublicKey
;
remoteRepository = "/root/restic-backup";
remoteFromFileRepository = "/root/restic-backup-from-file";
remoteFromCommandRepository = "/root/restic-backup-from-command";
remoteInhibitTestRepository = "/root/restic-backup-inhibit-test";
remoteNoInitRepository = "/root/restic-backup-no-init";
rcloneRepository = "rclone:local:/root/restic-rclone-backup";
sftpRepository = "sftp:alice@sftp:backups/test";
backupPrepareCommand = ''
touch /root/backupPrepareCommand
test ! -e /root/backupCleanupCommand
'';
backupCleanupCommand = ''
rm /root/backupPrepareCommand
touch /root/backupCleanupCommand
'';
testDir = pkgs.stdenvNoCC.mkDerivation {
name = "test-files-to-backup";
unpackPhase = "true";
installPhase = ''
mkdir $out
echo some_file > $out/some_file
echo some_other_file > $out/some_other_file
mkdir $out/a_dir
echo a_file > $out/a_dir/a_file
echo a_file_2 > $out/a_dir/a_file_2
'';
};
passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
paths = [ "/opt" ];
exclude = [ "/opt/excluded_file_*" ];
pruneOpts = [
"--keep-daily 2"
"--keep-weekly 1"
"--keep-monthly 1"
"--keep-yearly 99"
];
commandString = "testing";
command = [
"echo"
"-n"
commandString
];
in
{
name = "restic";
meta = with pkgs.lib.maintainers; {
maintainers = [
bbigras
i077
];
};
nodes = {
sftp =
# Copied from openssh.nix
{ pkgs, ... }:
{
services.openssh = {
enable = true;
extraConfig = ''
Match Group sftponly
ChrootDirectory /srv/sftp
ForceCommand internal-sftp
'';
};
users.groups = {
sftponly = { };
};
users.users = {
alice = {
isNormalUser = true;
createHome = false;
group = "sftponly";
shell = "/run/current-system/sw/bin/nologin";
openssh.authorizedKeys.keys = [ snakeOilEd25519PublicKey ];
};
};
};
restic =
{ pkgs, ... }:
{
services.restic.backups = {
remotebackup = {
inherit
passwordFile
paths
exclude
pruneOpts
backupPrepareCommand
backupCleanupCommand
;
repository = remoteRepository;
initialize = true;
timerConfig = null; # has no effect here, just checking that it doesn't break the service
};
remote-sftp = {
inherit
passwordFile
paths
exclude
pruneOpts
;
repository = sftpRepository;
initialize = true;
timerConfig = null; # has no effect here, just checking that it doesn't break the service
extraOptions = [
"sftp.command='ssh alice@sftp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -s sftp'"
];
};
remote-from-file-backup = {
inherit passwordFile pruneOpts;
initialize = true;
repositoryFile = pkgs.writeText "repositoryFile" remoteFromFileRepository;
paths = [
"/opt/a_dir/a_file"
"/opt/a_dir/a_file_2"
];
dynamicFilesFrom = ''
# all files in /opt except for a_dir and excluded_file_*
find /opt -mindepth 1 -maxdepth 1 ! -name a_dir ! -name excluded_file_*
'';
};
remote-from-command-backup = {
inherit
passwordFile
pruneOpts
command
;
initialize = true;
repository = remoteFromCommandRepository;
};
inhibit-test = {
inherit
passwordFile
paths
exclude
pruneOpts
;
repository = remoteInhibitTestRepository;
initialize = true;
inhibitsSleep = true;
};
remote-noinit-backup = {
inherit
passwordFile
exclude
pruneOpts
paths
;
initialize = false;
repository = remoteNoInitRepository;
};
rclonebackup = {
inherit
passwordFile
paths
exclude
pruneOpts
;
initialize = true;
repository = rcloneRepository;
rcloneConfig = {
type = "local";
one_file_system = true;
};
# This gets overridden by rcloneConfig.type
rcloneConfigFile = pkgs.writeText "rclone.conf" ''
[local]
type=ftp
'';
};
remoteprune = {
inherit passwordFile;
repository = remoteRepository;
pruneOpts = [ "--keep-last 1" ];
};
custompackage = {
inherit passwordFile paths;
repository = "some-fake-repository";
package = pkgs.writeShellScriptBin "restic" ''
echo "$@" >> /root/fake-restic.log;
'';
pruneOpts = [ "--keep-last 1" ];
checkOpts = [ "--some-check-option" ];
};
};
environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local";
};
};
testScript = ''
restic.start()
sftp.start()
restic.wait_for_unit("dbus.socket")
sftp.wait_for_unit("sshd.service")
restic.systemctl("start network-online.target")
restic.wait_for_unit("network-online.target")
sftp.succeed(
"mkdir -p /srv/sftp/backups",
"chown alice:sftponly /srv/sftp/backups",
"chmod 0755 /srv/sftp/backups",
)
restic.succeed(
"mkdir -p /root/.ssh/",
"cat ${snakeOilEd25519PrivateKey} > /root/.ssh/id_ed25519",
"chmod 0600 /root/.ssh/id_ed25519",
)
restic.fail(
"restic-remotebackup snapshots",
"restic-remote-sftp snapshots",
'restic-remote-from-file-backup snapshots"',
"restic-rclonebackup snapshots",
"grep 'backup.* /opt' /root/fake-restic.log",
)
restic.succeed(
# set up
"cp -rT ${testDir} /opt",
"touch /opt/excluded_file_1 /opt/excluded_file_2",
"mkdir -p /root/restic-rclone-backup",
)
restic.fail(
# test that noinit backup in fact does not initialize the repository
# and thus fails without a pre-initialized repository
"systemctl start restic-backups-remote-noinit-backup.service",
)
restic.succeed(
# test that remotebackup runs custom commands and produces a snapshot
"date -s '2016-12-13 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
)
restic.succeed(
# test that remotebackup runs custom commands and produces a snapshot
"date -s '2016-12-13 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
# test that restoring that snapshot produces the same directory
"mkdir /tmp/restore-1",
"restic-remotebackup restore latest -t /tmp/restore-1",
"diff -ru ${testDir} /tmp/restore-1/opt",
# test that remote-from-file-backup produces a snapshot
"systemctl start restic-backups-remote-from-file-backup.service",
'restic-remote-from-file-backup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
"mkdir /tmp/restore-2",
"restic-remote-from-file-backup restore latest -t /tmp/restore-2",
"diff -ru ${testDir} /tmp/restore-2/opt",
# test that remote-noinit-backup produces a snapshot once initialized
"restic-remote-noinit-backup init",
"systemctl start restic-backups-remote-noinit-backup.service",
'restic-remote-noinit-backup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
# test that restoring that snapshot produces the same directory
"mkdir /tmp/restore-3",
"${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} restore latest -t /tmp/restore-3",
"diff -ru ${testDir} /tmp/restore-3/opt",
# test that remote-from-command-backup produces a snapshot, with the expected contents
"systemctl start restic-backups-remote-from-command-backup.service",
'restic-remote-from-command-backup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
'[[ $(restic-remote-from-command-backup dump --path /stdin latest stdin) == ${commandString} ]]',
# test that rclonebackup produces a snapshot
"systemctl start restic-backups-rclonebackup.service",
'restic-rclonebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
# test that custompackage runs both `restic backup` and `restic check` with reasonable commandlines
"systemctl start restic-backups-custompackage.service",
"grep 'backup' /root/fake-restic.log",
"grep 'check.* --some-check-option' /root/fake-restic.log",
# test that we can create four snapshots in remotebackup and rclonebackup
"date -s '2017-12-13 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
"systemctl start restic-backups-rclonebackup.service",
"date -s '2018-12-13 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
"systemctl start restic-backups-rclonebackup.service",
"date -s '2018-12-14 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
"systemctl start restic-backups-rclonebackup.service",
"date -s '2018-12-15 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
"systemctl start restic-backups-rclonebackup.service",
"date -s '2018-12-16 13:45'",
"systemctl start restic-backups-remotebackup.service",
"rm /root/backupCleanupCommand",
"systemctl start restic-backups-rclonebackup.service",
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
'restic-rclonebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
# test that SFTP backup works by copying from the remotebackup
'restic-remote-sftp init --from-repo ${remoteRepository} --from-password-file ${passwordFile} --copy-chunker-params',
'restic-remote-sftp copy --from-repo ${remoteRepository} --from-password-file ${passwordFile}',
'restic-remote-sftp snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
# test that remoteprune brings us back to 1 snapshot in remotebackup
"systemctl start restic-backups-remoteprune.service",
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
# test that remoteprune brings us back to 1 snapshot in remotebackup
"systemctl start restic-backups-remoteprune.service",
'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
)
# test that the inhibit option is working
restic.systemctl("start --no-block restic-backups-inhibit-test.service")
restic.wait_until_succeeds(
"systemd-inhibit --no-legend --no-pager | grep -q restic",
5
)
# test that the inhibit option is working
restic.systemctl("start --no-block restic-backups-inhibit-test.service")
restic.wait_until_succeeds(
"systemd-inhibit --no-legend --no-pager | grep -q restic",
5
)
'';
}
|