about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/restic.nix
blob: 75fffe9d9a84d8b115a923a8beeb29d5e6da1096 (plain) (blame)
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
import ./make-test-python.nix (
  { pkgs, ... }:

  let
    password = "some_password";
    repository = "/tmp/restic-backup";
    repositoryFile = "${pkgs.writeText "repositoryFile" "/tmp/restic-backup-from-file"}";
    rcloneRepository = "rclone:local:/tmp/restic-rclone-backup";

    backupPrepareCommand = ''
      touch /opt/backupPrepareCommand
      test ! -e /opt/backupCleanupCommand
    '';

    backupCleanupCommand = ''
      rm /opt/backupPrepareCommand
      touch /opt/backupCleanupCommand
    '';

    passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
    initialize = true;
    paths = [ "/opt" ];
    pruneOpts = [
      "--keep-daily 2"
      "--keep-weekly 1"
      "--keep-monthly 1"
      "--keep-yearly 99"
    ];
  in
  {
    name = "restic";

    meta = with pkgs.lib.maintainers; {
      maintainers = [ bbigras i077 ];
    };

    nodes = {
      server =
        { pkgs, ... }:
        {
          services.restic.backups = {
            remotebackup = {
              inherit repository passwordFile initialize paths pruneOpts backupPrepareCommand backupCleanupCommand;
            };
            remotebackup-from-file = {
              inherit repositoryFile passwordFile initialize paths pruneOpts;
            };
            rclonebackup = {
              repository = rcloneRepository;
              rcloneConfig = {
                type = "local";
                one_file_system = true;
              };

              # This gets overridden by rcloneConfig.type
              rcloneConfigFile = pkgs.writeText "rclone.conf" ''
                [local]
                type=ftp
              '';
              inherit passwordFile initialize paths pruneOpts;
            };
            remoteprune = {
              inherit repository passwordFile;
              pruneOpts = [ "--keep-last 1" ];
            };
            custompackage = {
              inherit repository passwordFile paths;
              package = pkgs.writeShellScriptBin "restic" ''
                echo "$@" >> /tmp/fake-restic.log;
              '';
            };
          };

          environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local";
        };
    };

    testScript = ''
      server.start()
      server.wait_for_unit("dbus.socket")
      server.fail(
          "${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots",
          '${pkgs.restic}/bin/restic --repository-file ${repositoryFile} -p ${passwordFile} snapshots"',
          "${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots",
          "grep 'backup .* /opt' /tmp/fake-restic.log",
      )
      server.succeed(
          "mkdir -p /opt",
          "touch /opt/some_file",
          "mkdir -p /tmp/restic-rclone-backup",
          "timedatectl set-time '2016-12-13 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-remotebackup-from-file.service",
          "systemctl start restic-backups-rclonebackup.service",
          '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"',
          '${pkgs.restic}/bin/restic --repository-file ${repositoryFile} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"',
          '${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"',
          "systemctl start restic-backups-custompackage.service",
          "grep 'backup .* /opt' /tmp/fake-restic.log",
          "timedatectl set-time '2017-12-13 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-rclonebackup.service",
          "timedatectl set-time '2018-12-13 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-rclonebackup.service",
          "timedatectl set-time '2018-12-14 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-rclonebackup.service",
          "timedatectl set-time '2018-12-15 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-rclonebackup.service",
          "timedatectl set-time '2018-12-16 13:45'",
          "systemctl start restic-backups-remotebackup.service",
          "rm /opt/backupCleanupCommand",
          "systemctl start restic-backups-rclonebackup.service",
          '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^4 snapshot"',
          '${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots -c | grep -e "^4 snapshot"',
          "systemctl start restic-backups-remoteprune.service",
          '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"',
      )
    '';
  }
)