-
-
Save adrianparvino/f9145b21db03b920bcf4ec95dd80b494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ pkgs, config, lib, options, ... }: | |
with lib; | |
let | |
gcfg = config.services.duplicity-backup; | |
in | |
{ | |
imports = [ ./duplicity-backup-common.nix ]; | |
options = { | |
services.duplicity-backup.enableBackup = mkEnableOption "periodic duplicity backups backup tools"; | |
services.duplicity-backup.archives = mkOption { | |
type = types.attrsOf (types.submodule ({ name, config, ... }: { | |
options = { | |
backup-script = mkOption { | |
type = types.path; | |
}; | |
}; | |
config.backup-script = pkgs.writeScriptBin "duplicity-${name}" ('' | |
for i in ${gcfg.envDir}/*; do | |
source $i | |
done | |
mkdir -p ${gcfg.cacheDir} | |
chmod 0700 ${gcfg.cacheDir} | |
${pkgs.duplicity}/bin/duplicity \ | |
--archive-dir ${gcfg.cacheDir} \ | |
--name ${name} \ | |
--gpg-options "--homedir=${gcfg.pgpDir}" \ | |
--full-if-older-than 1M \ | |
'' + optionalString (config.allowSourceMismatch) ''--allow-source-mismatch \ | |
'' + optionalString (!gcfg.usePassphrase) ''--encrypt-key "Duplicity Backup" \ | |
'' + '' | |
${concatStringsSep " " (map (v: "--exclude ${v}") config.excludes)} \ | |
${concatStringsSep " " (map (v: "--include ${v}") config.includes)} \ | |
${config.directory} \ | |
${config.destination} | |
''); | |
})); | |
}; | |
}; | |
config = mkIf (gcfg.enable && gcfg.enableBackup) { | |
systemd.services."duplicity@" = { | |
environment.ARCHIVE = "duplicity-%i"; | |
path = mapAttrsToList (_: cfg: cfg.backup-script ) gcfg.archives; | |
script = '' | |
exec ''${!ARCHIVE} | |
''; | |
}; | |
# Note: the timer must be Persistent=true, so that systemd will start it even | |
# if e.g. your laptop was asleep while the latest interval occurred. | |
systemd.timers = mapAttrs' (name: cfg: nameValuePair "duplicity@${name}" | |
{ timerConfig.OnCalendar = cfg.period; | |
timerConfig.Persistent = "true"; | |
wantedBy = [ "timers.target" ]; | |
}) gcfg.archives; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment