2020-01-30 21:31:52 +00:00
|
|
|
import ./make-test-python.nix (
|
|
|
|
{ pkgs, ... }:
|
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
let
|
2022-11-28 13:41:10 +00:00
|
|
|
remoteRepository = "/tmp/restic-backup";
|
|
|
|
remoteFromFileRepository = "/tmp/restic-backup-from-file";
|
2022-06-02 16:38:39 +00:00
|
|
|
rcloneRepository = "rclone:local:/tmp/restic-rclone-backup";
|
2020-07-05 20:25:57 +00:00
|
|
|
|
2022-06-02 16:43:03 +00:00
|
|
|
backupPrepareCommand = ''
|
2022-12-04 18:42:58 +00:00
|
|
|
touch /tmp/backupPrepareCommand
|
|
|
|
test ! -e /tmp/backupCleanupCommand
|
2022-06-02 16:43:03 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
backupCleanupCommand = ''
|
2022-12-04 18:42:58 +00:00
|
|
|
rm /tmp/backupPrepareCommand
|
|
|
|
touch /tmp/backupCleanupCommand
|
2022-06-02 16:43:03 +00:00
|
|
|
'';
|
|
|
|
|
2022-12-04 18:42:58 +00:00
|
|
|
testDir = pkgs.stdenvNoCC.mkDerivation {
|
|
|
|
name = "test-files-to-backup";
|
|
|
|
unpackPhase = "true";
|
|
|
|
installPhase = ''
|
|
|
|
mkdir $out
|
|
|
|
touch $out/some_file
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
|
|
|
|
paths = [ "/opt" ];
|
2022-12-03 23:00:58 +00:00
|
|
|
exclude = [ "/opt/excluded_file_*" ];
|
2022-06-02 16:38:39 +00:00
|
|
|
pruneOpts = [
|
|
|
|
"--keep-daily 2"
|
|
|
|
"--keep-weekly 1"
|
|
|
|
"--keep-monthly 1"
|
|
|
|
"--keep-yearly 99"
|
|
|
|
];
|
|
|
|
in
|
|
|
|
{
|
|
|
|
name = "restic";
|
2020-01-30 21:31:52 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
meta = with pkgs.lib.maintainers; {
|
|
|
|
maintainers = [ bbigras i077 ];
|
|
|
|
};
|
2020-01-30 21:31:52 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
nodes = {
|
|
|
|
server =
|
|
|
|
{ pkgs, ... }:
|
|
|
|
{
|
|
|
|
services.restic.backups = {
|
|
|
|
remotebackup = {
|
2022-12-03 23:00:58 +00:00
|
|
|
inherit passwordFile paths exclude pruneOpts backupPrepareCommand backupCleanupCommand;
|
2022-11-28 13:41:10 +00:00
|
|
|
repository = remoteRepository;
|
|
|
|
initialize = true;
|
2022-06-02 16:38:39 +00:00
|
|
|
};
|
2022-11-28 13:41:10 +00:00
|
|
|
remote-from-file-backup = {
|
2022-12-03 23:00:58 +00:00
|
|
|
inherit passwordFile paths exclude pruneOpts;
|
2022-11-28 13:41:10 +00:00
|
|
|
initialize = true;
|
|
|
|
repositoryFile = pkgs.writeText "repositoryFile" remoteFromFileRepository;
|
2022-06-02 16:40:39 +00:00
|
|
|
};
|
2022-06-02 16:38:39 +00:00
|
|
|
rclonebackup = {
|
2022-12-03 23:00:58 +00:00
|
|
|
inherit passwordFile paths exclude pruneOpts;
|
2022-11-28 13:41:10 +00:00
|
|
|
initialize = true;
|
2022-06-02 16:38:39 +00:00
|
|
|
repository = rcloneRepository;
|
|
|
|
rcloneConfig = {
|
|
|
|
type = "local";
|
|
|
|
one_file_system = true;
|
|
|
|
};
|
2020-07-05 20:25:57 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
# This gets overridden by rcloneConfig.type
|
|
|
|
rcloneConfigFile = pkgs.writeText "rclone.conf" ''
|
|
|
|
[local]
|
|
|
|
type=ftp
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
remoteprune = {
|
2022-11-28 13:41:10 +00:00
|
|
|
inherit passwordFile;
|
|
|
|
repository = remoteRepository;
|
2022-06-02 16:38:39 +00:00
|
|
|
pruneOpts = [ "--keep-last 1" ];
|
|
|
|
};
|
2022-07-27 17:47:41 +00:00
|
|
|
custompackage = {
|
2022-11-28 13:41:10 +00:00
|
|
|
inherit passwordFile paths;
|
|
|
|
repository = "some-fake-repository";
|
2022-07-27 17:47:41 +00:00
|
|
|
package = pkgs.writeShellScriptBin "restic" ''
|
|
|
|
echo "$@" >> /tmp/fake-restic.log;
|
|
|
|
'';
|
2022-08-29 23:40:28 +00:00
|
|
|
|
|
|
|
pruneOpts = [ "--keep-last 1" ];
|
|
|
|
checkOpts = [ "--some-check-option" ];
|
2022-07-27 17:47:41 +00:00
|
|
|
};
|
2022-06-02 16:38:39 +00:00
|
|
|
};
|
2020-07-05 20:25:57 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local";
|
2020-01-30 21:31:52 +00:00
|
|
|
};
|
2022-06-02 16:38:39 +00:00
|
|
|
};
|
2020-01-30 21:31:52 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
testScript = ''
|
|
|
|
server.start()
|
|
|
|
server.wait_for_unit("dbus.socket")
|
|
|
|
server.fail(
|
2022-11-28 13:41:10 +00:00
|
|
|
"${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} snapshots",
|
|
|
|
'${pkgs.restic}/bin/restic -r ${remoteFromFileRepository} -p ${passwordFile} snapshots"',
|
2022-06-02 16:38:39 +00:00
|
|
|
"${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots",
|
2023-03-11 11:23:24 +00:00
|
|
|
"grep 'backup.* /opt' /tmp/fake-restic.log",
|
2022-06-02 16:38:39 +00:00
|
|
|
)
|
|
|
|
server.succeed(
|
2022-11-28 12:43:14 +00:00
|
|
|
# set up
|
2022-12-04 18:42:58 +00:00
|
|
|
"cp -rT ${testDir} /opt",
|
2022-12-03 23:00:58 +00:00
|
|
|
"touch /opt/excluded_file_1 /opt/excluded_file_2",
|
2022-06-02 16:38:39 +00:00
|
|
|
"mkdir -p /tmp/restic-rclone-backup",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
|
|
|
# test that remotebackup runs custom commands and produces a snapshot
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2016-12-13 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-11-28 13:52:29 +00:00
|
|
|
'${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-12-04 18:42:58 +00:00
|
|
|
# test that restoring that snapshot produces the same directory
|
|
|
|
"mkdir /tmp/restore-1",
|
|
|
|
"${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} restore latest -t /tmp/restore-1",
|
|
|
|
"diff -ru ${testDir} /tmp/restore-1/opt",
|
|
|
|
|
2022-11-28 13:41:10 +00:00
|
|
|
# test that remote-from-file-backup produces a snapshot
|
|
|
|
"systemctl start restic-backups-remote-from-file-backup.service",
|
2022-11-28 13:52:29 +00:00
|
|
|
'${pkgs.restic}/bin/restic -r ${remoteFromFileRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
|
2022-11-28 12:43:14 +00:00
|
|
|
|
|
|
|
# test that rclonebackup produces a snapshot
|
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 13:52:29 +00:00
|
|
|
'${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
|
2022-11-28 12:43:14 +00:00
|
|
|
|
|
|
|
# test that custompackage runs both `restic backup` and `restic check` with reasonable commandlines
|
2022-07-27 17:47:41 +00:00
|
|
|
"systemctl start restic-backups-custompackage.service",
|
2023-03-11 11:23:24 +00:00
|
|
|
"grep 'backup.* /opt' /tmp/fake-restic.log",
|
|
|
|
"grep 'check.* --some-check-option' /tmp/fake-restic.log",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
|
|
|
# test that we can create four snapshots in remotebackup and rclonebackup
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2017-12-13 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2018-12-13 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2018-12-14 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2018-12-15 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
"timedatectl set-time '2018-12-16 13:45'",
|
|
|
|
"systemctl start restic-backups-remotebackup.service",
|
2022-12-04 18:42:58 +00:00
|
|
|
"rm /tmp/backupCleanupCommand",
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-rclonebackup.service",
|
2022-11-28 12:43:14 +00:00
|
|
|
|
2022-11-28 13:52:29 +00:00
|
|
|
'${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
|
|
|
|
'${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
|
2022-11-28 12:43:14 +00:00
|
|
|
|
|
|
|
# test that remoteprune brings us back to 1 snapshot in remotebackup
|
2022-06-02 16:38:39 +00:00
|
|
|
"systemctl start restic-backups-remoteprune.service",
|
2022-11-28 13:52:29 +00:00
|
|
|
'${pkgs.restic}/bin/restic -r ${remoteRepository} -p ${passwordFile} snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
|
|
|
|
|
2022-06-02 16:38:39 +00:00
|
|
|
)
|
|
|
|
'';
|
|
|
|
}
|
2020-01-30 21:31:52 +00:00
|
|
|
)
|