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