From d9e3b1fafe3c1509a886fb8795b3438c7caa07f5 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Thu, 2 Jun 2022 13:43:03 -0300 Subject: [PATCH] nixos/restic: add backup{Prepare,Cleanup}Command options The backupPrepareCommand and backupCleanupCommand options offer a way to run a script to prepare for backup and then cleanup it once finish. Signed-off-by: Otavio Salvador --- nixos/modules/services/backup/restic.nix | 30 +++++++++++++++++++++--- nixos/tests/restic.nix | 18 +++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix index f4bcca395813..333fdd494e3b 100644 --- a/nixos/modules/services/backup/restic.nix +++ b/nixos/modules/services/backup/restic.nix @@ -206,6 +206,22 @@ in ''; example = "find /home/matt/git -type d -name .git"; }; + + backupPrepareCommand = mkOption { + type = with types; nullOr str; + default = null; + description = '' + A script that must run before starting the backup process. + ''; + }; + + backupCleanupCommand = mkOption { + type = with types; nullOr str; + default = null; + description = '' + A script that must run after finishing the backup process. + ''; + }; }; })); default = { }; @@ -283,8 +299,11 @@ in } // optionalAttrs (backup.environmentFile != null) { EnvironmentFile = backup.environmentFile; }; - } // optionalAttrs (backup.initialize || backup.dynamicFilesFrom != null) { + } // optionalAttrs (backup.initialize || backup.dynamicFilesFrom != null || backup.backupPrepareCommand != null) { preStart = '' + ${optionalString (backup.backupPrepareCommand != null) '' + ${pkgs.writeScript "backupPrepareCommand" backup.backupPrepareCommand} + ''} ${optionalString (backup.initialize) '' ${resticCmd} snapshots || ${resticCmd} init ''} @@ -292,9 +311,14 @@ in ${pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom} > ${filesFromTmpFile} ''} ''; - } // optionalAttrs (backup.dynamicFilesFrom != null) { + } // optionalAttrs (backup.dynamicFilesFrom != null || backup.backupCleanupCommand != null) { postStart = '' - rm ${filesFromTmpFile} + ${optionalString (backup.backupCleanupCommand != null) '' + ${pkgs.writeScript "backupCleanupCommand" backup.backupCleanupCommand} + ''} + ${optionalString (backup.dynamicFilesFrom != null) '' + rm ${filesFromTmpFile} + ''} ''; }) ) diff --git a/nixos/tests/restic.nix b/nixos/tests/restic.nix index 16d7edde0c1c..7523d5e5ed5d 100644 --- a/nixos/tests/restic.nix +++ b/nixos/tests/restic.nix @@ -7,6 +7,16 @@ import ./make-test-python.nix ( 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" ]; @@ -30,7 +40,7 @@ import ./make-test-python.nix ( { services.restic.backups = { remotebackup = { - inherit repository passwordFile initialize paths pruneOpts; + inherit repository passwordFile initialize paths pruneOpts backupPrepareCommand backupCleanupCommand; }; remotebackup-from-file = { inherit repositoryFile passwordFile initialize paths pruneOpts; @@ -73,6 +83,7 @@ import ./make-test-python.nix ( "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"', @@ -80,18 +91,23 @@ import ./make-test-python.nix ( '${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"', "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"',