vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / restic.nix
blob49631d27ca801be0f4d67568ae23c07555e9efca
1 import ./make-test-python.nix (
2   { pkgs, ... }:
4   let
5     remoteRepository = "/root/restic-backup";
6     remoteFromFileRepository = "/root/restic-backup-from-file";
7     remoteInhibitTestRepository = "/root/restic-backup-inhibit-test";
8     remoteNoInitRepository = "/root/restic-backup-no-init";
9     rcloneRepository = "rclone:local:/root/restic-rclone-backup";
11     backupPrepareCommand = ''
12       touch /root/backupPrepareCommand
13       test ! -e /root/backupCleanupCommand
14     '';
16     backupCleanupCommand = ''
17       rm /root/backupPrepareCommand
18       touch /root/backupCleanupCommand
19     '';
21     testDir = pkgs.stdenvNoCC.mkDerivation {
22       name = "test-files-to-backup";
23       unpackPhase = "true";
24       installPhase = ''
25         mkdir $out
26         echo some_file > $out/some_file
27         echo some_other_file > $out/some_other_file
28         mkdir $out/a_dir
29         echo a_file > $out/a_dir/a_file
30         echo a_file_2 > $out/a_dir/a_file_2
31       '';
32     };
34     passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
35     paths = [ "/opt" ];
36     exclude = [ "/opt/excluded_file_*" ];
37     pruneOpts = [
38       "--keep-daily 2"
39       "--keep-weekly 1"
40       "--keep-monthly 1"
41       "--keep-yearly 99"
42     ];
43   in
44   {
45     name = "restic";
47     meta = with pkgs.lib.maintainers; {
48       maintainers = [ bbigras i077 ];
49     };
51     nodes = {
52       server =
53         { pkgs, ... }:
54         {
55           services.restic.backups = {
56             remotebackup = {
57               inherit passwordFile paths exclude pruneOpts backupPrepareCommand backupCleanupCommand;
58               repository = remoteRepository;
59               initialize = true;
60               timerConfig = null; # has no effect here, just checking that it doesn't break the service
61             };
62             remote-from-file-backup = {
63               inherit passwordFile exclude pruneOpts;
64               initialize = true;
65               repositoryFile = pkgs.writeText "repositoryFile" remoteFromFileRepository;
66               paths = [
67                 "/opt/a_dir/a_file"
68                 "/opt/a_dir/a_file_2"
69               ];
70               dynamicFilesFrom = ''
71                 find /opt -mindepth 1 -maxdepth 1 ! -name a_dir # all files in /opt except for a_dir
72               '';
73             };
74             inhibit-test = {
75               inherit passwordFile paths exclude pruneOpts;
76               repository = remoteInhibitTestRepository;
77               initialize = true;
78               inhibitsSleep = true;
79             };
80             remote-noinit-backup = {
81               inherit passwordFile exclude pruneOpts paths;
82               initialize = false;
83               repository = remoteNoInitRepository;
84             };
85             rclonebackup = {
86               inherit passwordFile paths exclude pruneOpts;
87               initialize = true;
88               repository = rcloneRepository;
89               rcloneConfig = {
90                 type = "local";
91                 one_file_system = true;
92               };
94               # This gets overridden by rcloneConfig.type
95               rcloneConfigFile = pkgs.writeText "rclone.conf" ''
96                 [local]
97                 type=ftp
98               '';
99             };
100             remoteprune = {
101               inherit passwordFile;
102               repository = remoteRepository;
103               pruneOpts = [ "--keep-last 1" ];
104             };
105             custompackage = {
106               inherit passwordFile paths;
107               repository = "some-fake-repository";
108               package = pkgs.writeShellScriptBin "restic" ''
109                 echo "$@" >> /root/fake-restic.log;
110               '';
112               pruneOpts = [ "--keep-last 1" ];
113               checkOpts = [ "--some-check-option" ];
114             };
115           };
117           environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local";
118         };
119     };
121     testScript = ''
122       server.start()
123       server.wait_for_unit("dbus.socket")
124       server.fail(
125           "restic-remotebackup snapshots",
126           'restic-remote-from-file-backup snapshots"',
127           "restic-rclonebackup snapshots",
128           "grep 'backup.* /opt' /root/fake-restic.log",
129       )
130       server.succeed(
131           # set up
132           "cp -rT ${testDir} /opt",
133           "touch /opt/excluded_file_1 /opt/excluded_file_2",
134           "mkdir -p /root/restic-rclone-backup",
135           "restic-remote-noinit-backup init",
137           # test that remotebackup runs custom commands and produces a snapshot
138           "timedatectl set-time '2016-12-13 13:45'",
139           "systemctl start restic-backups-remotebackup.service",
140           "rm /root/backupCleanupCommand",
141           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
143           # test that restoring that snapshot produces the same directory
144           "mkdir /tmp/restore-1",
145           "restic-remotebackup restore latest -t /tmp/restore-1",
146           "diff -ru ${testDir} /tmp/restore-1/opt",
148           # test that remote-from-file-backup produces a snapshot
149           "systemctl start restic-backups-remote-from-file-backup.service",
150           'restic-remote-from-file-backup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
151           "mkdir /tmp/restore-2",
152           "restic-remote-from-file-backup restore latest -t /tmp/restore-2",
153           "diff -ru ${testDir} /tmp/restore-2/opt",
155           # test that remote-noinit-backup produces a snapshot
156           "systemctl start restic-backups-remote-noinit-backup.service",
157           'restic-remote-noinit-backup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
159           # test that restoring that snapshot produces the same directory
160           "mkdir /tmp/restore-3",
161           "${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} restore latest -t /tmp/restore-3",
162           "diff -ru ${testDir} /tmp/restore-3/opt",
164           # test that rclonebackup produces a snapshot
165           "systemctl start restic-backups-rclonebackup.service",
166           'restic-rclonebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
168           # test that custompackage runs both `restic backup` and `restic check` with reasonable commandlines
169           "systemctl start restic-backups-custompackage.service",
170           "grep 'backup' /root/fake-restic.log",
171           "grep 'check.* --some-check-option' /root/fake-restic.log",
173           # test that we can create four snapshots in remotebackup and rclonebackup
174           "timedatectl set-time '2017-12-13 13:45'",
175           "systemctl start restic-backups-remotebackup.service",
176           "rm /root/backupCleanupCommand",
177           "systemctl start restic-backups-rclonebackup.service",
179           "timedatectl set-time '2018-12-13 13:45'",
180           "systemctl start restic-backups-remotebackup.service",
181           "rm /root/backupCleanupCommand",
182           "systemctl start restic-backups-rclonebackup.service",
184           "timedatectl set-time '2018-12-14 13:45'",
185           "systemctl start restic-backups-remotebackup.service",
186           "rm /root/backupCleanupCommand",
187           "systemctl start restic-backups-rclonebackup.service",
189           "timedatectl set-time '2018-12-15 13:45'",
190           "systemctl start restic-backups-remotebackup.service",
191           "rm /root/backupCleanupCommand",
192           "systemctl start restic-backups-rclonebackup.service",
194           "timedatectl set-time '2018-12-16 13:45'",
195           "systemctl start restic-backups-remotebackup.service",
196           "rm /root/backupCleanupCommand",
197           "systemctl start restic-backups-rclonebackup.service",
199           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
200           'restic-rclonebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
202           # test that remoteprune brings us back to 1 snapshot in remotebackup
203           "systemctl start restic-backups-remoteprune.service",
204           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
206       )
208       # test that the inhibit option is working
209       server.systemctl("start --no-block restic-backups-inhibit-test.service")
210       server.wait_until_succeeds(
211           "systemd-inhibit --no-legend --no-pager | grep -q restic",
212           5
213       )
214     '';
215   }