1import ./make-test-python.nix (
2 { pkgs, ... }:
3
4 let
5 remoteRepository = "rest:http://restic_rest_server:8001/";
6
7 backupPrepareCommand = ''
8 touch /root/backupPrepareCommand
9 test ! -e /root/backupCleanupCommand
10 '';
11
12 backupCleanupCommand = ''
13 rm /root/backupPrepareCommand
14 touch /root/backupCleanupCommand
15 '';
16
17 testDir = pkgs.stdenvNoCC.mkDerivation {
18 name = "test-files-to-backup";
19 unpackPhase = "true";
20 installPhase = ''
21 mkdir $out
22 echo some_file > $out/some_file
23 echo some_other_file > $out/some_other_file
24 mkdir $out/a_dir
25 echo a_file > $out/a_dir/a_file
26 '';
27 };
28
29 passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
30 paths = [ "/opt" ];
31 exclude = [ "/opt/excluded_file_*" ];
32 pruneOpts = [
33 "--keep-daily 2"
34 "--keep-weekly 1"
35 "--keep-monthly 1"
36 "--keep-yearly 99"
37 ];
38 in
39 {
40 name = "restic-rest-server";
41
42 nodes = {
43 restic_rest_server = {
44 services.restic.server = {
45 enable = true;
46 extraFlags = [ "--no-auth" ];
47 listenAddress = "8001";
48 };
49 networking.firewall.allowedTCPPorts = [ 8001 ];
50 };
51 server = {
52 services.restic.backups = {
53 remotebackup = {
54 inherit
55 passwordFile
56 paths
57 exclude
58 pruneOpts
59 backupPrepareCommand
60 backupCleanupCommand
61 ;
62 repository = remoteRepository;
63 initialize = true;
64 timerConfig = null; # has no effect here, just checking that it doesn't break the service
65 };
66 remoteprune = {
67 inherit passwordFile;
68 repository = remoteRepository;
69 pruneOpts = [ "--keep-last 1" ];
70 };
71 };
72 };
73 };
74
75 testScript = ''
76 restic_rest_server.start()
77 server.start()
78 restic_rest_server.wait_for_unit("restic-rest-server.socket")
79 restic_rest_server.wait_for_open_port(8001)
80 server.wait_for_unit("dbus.socket")
81 server.fail(
82 "restic-remotebackup snapshots",
83 )
84 server.succeed(
85 # set up
86 "cp -rT ${testDir} /opt",
87 "touch /opt/excluded_file_1 /opt/excluded_file_2",
88
89 # test that remotebackup runs custom commands and produces a snapshot
90 "timedatectl set-time '2016-12-13 13:45'",
91 "systemctl start restic-backups-remotebackup.service",
92 "rm /root/backupCleanupCommand",
93 'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
94
95 # test that restoring that snapshot produces the same directory
96 "mkdir /tmp/restore-1",
97 "restic-remotebackup restore latest -t /tmp/restore-1",
98 "diff -ru ${testDir} /tmp/restore-1/opt",
99
100 # test that we can create four snapshots in remotebackup and rclonebackup
101 "timedatectl set-time '2017-12-13 13:45'",
102 "systemctl start restic-backups-remotebackup.service",
103 "rm /root/backupCleanupCommand",
104
105 "timedatectl set-time '2018-12-13 13:45'",
106 "systemctl start restic-backups-remotebackup.service",
107 "rm /root/backupCleanupCommand",
108
109 "timedatectl set-time '2018-12-14 13:45'",
110 "systemctl start restic-backups-remotebackup.service",
111 "rm /root/backupCleanupCommand",
112
113 "timedatectl set-time '2018-12-15 13:45'",
114 "systemctl start restic-backups-remotebackup.service",
115 "rm /root/backupCleanupCommand",
116
117 "timedatectl set-time '2018-12-16 13:45'",
118 "systemctl start restic-backups-remotebackup.service",
119 "rm /root/backupCleanupCommand",
120
121 'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
122
123 # test that remoteprune brings us back to 1 snapshot in remotebackup
124 "systemctl start restic-backups-remoteprune.service",
125 'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
126 )
127 '';
128 }
129)