From 1c9684abd6c6eca57e6fbfad01858523da355520 Mon Sep 17 00:00:00 2001 From: Matt McHenry Date: Sun, 5 Aug 2018 12:53:53 -0400 Subject: [PATCH] restic: add dynamicFilesFrom --- nixos/modules/services/backup/restic.nix | 41 +++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix index a0af74f66997..4d35ed447f55 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;