1 import ./make-test-python.nix (
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
16 backupCleanupCommand = ''
17 rm /root/backupPrepareCommand
18 touch /root/backupCleanupCommand
21 testDir = pkgs.stdenvNoCC.mkDerivation {
22 name = "test-files-to-backup";
26 echo some_file > $out/some_file
27 echo some_other_file > $out/some_other_file
29 echo a_file > $out/a_dir/a_file
30 echo a_file_2 > $out/a_dir/a_file_2
34 passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
36 exclude = [ "/opt/excluded_file_*" ];
47 meta = with pkgs.lib.maintainers; {
48 maintainers = [ bbigras i077 ];
55 services.restic.backups = {
57 inherit passwordFile paths exclude pruneOpts backupPrepareCommand backupCleanupCommand;
58 repository = remoteRepository;
60 timerConfig = null; # has no effect here, just checking that it doesn't break the service
62 remote-from-file-backup = {
63 inherit passwordFile exclude pruneOpts;
65 repositoryFile = pkgs.writeText "repositoryFile" remoteFromFileRepository;
71 find /opt -mindepth 1 -maxdepth 1 ! -name a_dir # all files in /opt except for a_dir
75 inherit passwordFile paths exclude pruneOpts;
76 repository = remoteInhibitTestRepository;
80 remote-noinit-backup = {
81 inherit passwordFile exclude pruneOpts paths;
83 repository = remoteNoInitRepository;
86 inherit passwordFile paths exclude pruneOpts;
88 repository = rcloneRepository;
91 one_file_system = true;
94 # This gets overridden by rcloneConfig.type
95 rcloneConfigFile = pkgs.writeText "rclone.conf" ''
101 inherit passwordFile;
102 repository = remoteRepository;
103 pruneOpts = [ "--keep-last 1" ];
106 inherit passwordFile paths;
107 repository = "some-fake-repository";
108 package = pkgs.writeShellScriptBin "restic" ''
109 echo "$@" >> /root/fake-restic.log;
112 pruneOpts = [ "--keep-last 1" ];
113 checkOpts = [ "--some-check-option" ];
117 environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local";
123 server.wait_for_unit("dbus.socket")
125 "restic-remotebackup snapshots",
126 'restic-remote-from-file-backup snapshots"',
127 "restic-rclonebackup snapshots",
128 "grep 'backup.* /opt' /root/fake-restic.log",
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"',
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",