summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatt McHenry <github@matt.mchenryfamily.org>2018-07-21 22:24:19 -0400
committerJörg Thalheim <joerg@thalheim.io>2020-01-30 16:59:34 +0000
commitc6994e90dcb463af57917780838cc4d0dc87a2b7 (patch)
treeff7231d0047a48b5741d84f374450633176e9a47
parent6f7c3fbff756dfafc86a33afc6e318e8d73319de (diff)
downloadnixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar.gz
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar.bz2
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar.lz
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar.xz
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.tar.zst
nixpkgs-c6994e90dcb463af57917780838cc4d0dc87a2b7.zip
restic: add support for pruning
-rw-r--r--nixos/modules/services/backup/restic.nix24
1 files changed, 23 insertions, 1 deletions
diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix
index 7e8e91e4b9c..a0af74f6699 100644
--- a/nixos/modules/services/backup/restic.nix
+++ b/nixos/modules/services/backup/restic.nix
@@ -103,6 +103,23 @@ in
             Create the repository if it doesn't exist.
           '';
         };
+
+        pruneOpts = mkOption {
+          type = types.listOf types.str;
+          default = [];
+          description = ''
+            A list of options (--keep-* et al.) for 'restic forget
+            --prune', to automatically prune old snapshots.  The
+            'forget' command is run *after* the 'backup' command, so
+            keep that in mind when constructing the --keep-* options.
+          '';
+          example = [
+            "--keep-daily 7"
+            "--keep-weekly 5"
+            "--keep-monthly 12"
+            "--keep-yearly 75"
+          ];
+        };
       };
     }));
     default = {};
@@ -134,6 +151,11 @@ in
         let
           extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
           resticCmd = "${pkgs.restic}/bin/restic${extraOptions}";
+          pruneCmd = if (builtins.length backup.pruneOpts > 0)
+                     then [ ( resticCmd + " forget --prune " +
+                              (concatStringsSep " " backup.pruneOpts) )
+                            ( resticCmd + " check" ) ]
+                     else [];
         in nameValuePair "restic-backups-${name}" ({
           environment = {
             RESTIC_PASSWORD_FILE = backup.passwordFile;
@@ -145,7 +167,7 @@ in
           restartIfChanged = false;
           serviceConfig = {
             Type = "oneshot";
-            ExecStart = "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}";
+            ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}" ] ++ pruneCmd;
             User = backup.user;
           } // optionalAttrs (backup.s3CredentialsFile != null) {
             EnvironmentFile = backup.s3CredentialsFile;