restic: allow prune without backup

fixes #97820

authored by

Matt McHenry and committed by helbling.dev 13bee29b ef2ac476

+14 -5
+8 -5
nixos/modules/services/backup/restic.nix
··· 93 93 }; 94 94 95 95 paths = mkOption { 96 - type = types.listOf types.str; 97 - default = []; 96 + type = types.nullOr (types.listOf types.str); 97 + default = null; 98 98 description = '' 99 - Which paths to backup. 99 + Which paths to backup. If null or an empty array, no 100 + backup command will be run. This can be used to create a 101 + prune-only job. 100 102 ''; 101 103 example = [ 102 104 "/var/lib/postgresql" ··· 217 219 resticCmd = "${pkgs.restic}/bin/restic${extraOptions}"; 218 220 filesFromTmpFile = "/run/restic-backups-${name}/includes"; 219 221 backupPaths = if (backup.dynamicFilesFrom == null) 220 - then concatStringsSep " " backup.paths 222 + then if (backup.paths != null) then concatStringsSep " " backup.paths else "" 221 223 else "--files-from ${filesFromTmpFile}"; 222 224 pruneCmd = optionals (builtins.length backup.pruneOpts > 0) [ 223 225 ( resticCmd + " forget --prune " + (concatStringsSep " " backup.pruneOpts) ) ··· 243 245 restartIfChanged = false; 244 246 serviceConfig = { 245 247 Type = "oneshot"; 246 - ExecStart = [ "${resticCmd} backup --cache-dir=%C/restic-backups-${name} ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd; 248 + ExecStart = (optionals (backupPaths != "") [ "${resticCmd} backup --cache-dir=%C/restic-backups-${name} ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ]) 249 + ++ pruneCmd; 247 250 User = backup.user; 248 251 RuntimeDirectory = "restic-backups-${name}"; 249 252 CacheDirectory = "restic-backups-${name}";
+6
nixos/tests/restic.nix
··· 45 45 ''; 46 46 inherit passwordFile initialize paths pruneOpts; 47 47 }; 48 + remoteprune = { 49 + inherit repository passwordFile; 50 + pruneOpts = [ "--keep-last 1" ]; 51 + }; 48 52 }; 49 53 50 54 environment.sessionVariables.RCLONE_CONFIG_LOCAL_TYPE = "local"; ··· 84 88 "systemctl start restic-backups-rclonebackup.service", 85 89 '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^4 snapshot"', 86 90 '${pkgs.restic}/bin/restic -r ${rcloneRepository} -p ${passwordFile} snapshots -c | grep -e "^4 snapshot"', 91 + "systemctl start restic-backups-remoteprune.service", 92 + '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"', 87 93 ) 88 94 ''; 89 95 }