summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatt McHenry <github@matt.mchenryfamily.org>2018-08-05 12:53:53 -0400
committerJörg Thalheim <joerg@thalheim.io>2020-01-30 17:00:10 +0000
commit1c9684abd6c6eca57e6fbfad01858523da355520 (patch)
tree9003c48d5a4bcbd5857b00b4665ada744814c599
parentc6994e90dcb463af57917780838cc4d0dc87a2b7 (diff)
downloadnixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar.gz
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar.bz2
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar.lz
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar.xz
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.tar.zst
nixpkgs-1c9684abd6c6eca57e6fbfad01858523da355520.zip
restic: add dynamicFilesFrom
-rw-r--r--nixos/modules/services/backup/restic.nix41
1 files changed, 37 insertions, 4 deletions
diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix
index a0af74f6699..4d35ed447f5 100644
--- a/nixos/modules/services/backup/restic.nix
+++ b/nixos/modules/services/backup/restic.nix
@@ -120,6 +120,17 @@ in
             "--keep-yearly 75"
           ];
         };
+
+        dynamicFilesFrom = mkOption {
+          type = with types; nullOr str;
+          default = null;
+          description = ''
+            A script that produces a list of files to back up.  The
+            results of this command are given to the '--files-from'
+            option.
+          '';
+          example = "find /home/matt/git -type d -name .git";
+        };
       };
     }));
     default = {};
@@ -151,6 +162,25 @@ in
         let
           extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
           resticCmd = "${pkgs.restic}/bin/restic${extraOptions}";
+          filesFromTmpFile = "/run/restic-backups-${name}/includes";
+          preStartInit = if backup.initialize
+                         then "${resticCmd} snapshots || ${resticCmd} init"
+                         else "";
+          dynamicFilesFromScript = pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom;
+          preStartFiles = if backup.dynamicFilesFrom != null
+                          then "${dynamicFilesFromScript} > ${filesFromTmpFile}"
+                          else "";
+          preStartAttr = if (backup.initialize || backup.dynamicFilesFrom != null)
+                         then {
+                           preStart = ''
+                             ${preStartInit}
+                             ${preStartFiles}
+                           '';
+                         }
+                         else {};
+          backupPaths = if (backup.dynamicFilesFrom == null)
+                        then concatStringsSep " " backup.paths
+                        else "--files-from ${filesFromTmpFile}";
           pruneCmd = if (builtins.length backup.pruneOpts > 0)
                      then [ ( resticCmd + " forget --prune " +
                               (concatStringsSep " " backup.pruneOpts) )
@@ -167,14 +197,17 @@ in
           restartIfChanged = false;
           serviceConfig = {
             Type = "oneshot";
-            ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}" ] ++ pruneCmd;
+            ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd;
             User = backup.user;
+            RuntimeDirectory = "restic-backups-${name}";
           } // optionalAttrs (backup.s3CredentialsFile != null) {
             EnvironmentFile = backup.s3CredentialsFile;
           };
-        } // optionalAttrs backup.initialize {
-          preStart = ''
-            ${resticCmd} snapshots || ${resticCmd} init
+        }
+        // preStartAttr
+        // optionalAttrs (backup.dynamicFilesFrom != null) {
+          postStart = ''
+            rm ${filesFromTmpFile}
           '';
         })
       ) config.services.restic.backups;