restic: add dynamicFilesFrom

This commit is contained in:
Matt McHenry 2018-08-05 12:53:53 -04:00 committed by Jörg Thalheim
parent c6994e90dc
commit 1c9684abd6
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92

View File

@ -120,6 +120,17 @@ in
"--keep-yearly 75" "--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 = {}; default = {};
@ -151,6 +162,25 @@ in
let let
extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions; extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
resticCmd = "${pkgs.restic}/bin/restic${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) pruneCmd = if (builtins.length backup.pruneOpts > 0)
then [ ( resticCmd + " forget --prune " + then [ ( resticCmd + " forget --prune " +
(concatStringsSep " " backup.pruneOpts) ) (concatStringsSep " " backup.pruneOpts) )
@ -167,14 +197,17 @@ in
restartIfChanged = false; restartIfChanged = false;
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}" ] ++ pruneCmd; ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd;
User = backup.user; User = backup.user;
RuntimeDirectory = "restic-backups-${name}";
} // optionalAttrs (backup.s3CredentialsFile != null) { } // optionalAttrs (backup.s3CredentialsFile != null) {
EnvironmentFile = backup.s3CredentialsFile; EnvironmentFile = backup.s3CredentialsFile;
}; };
} // optionalAttrs backup.initialize { }
preStart = '' // preStartAttr
${resticCmd} snapshots || ${resticCmd} init // optionalAttrs (backup.dynamicFilesFrom != null) {
postStart = ''
rm ${filesFromTmpFile}
''; '';
}) })
) config.services.restic.backups; ) config.services.restic.backups;