Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Created July 17, 2019 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianparvino/f9145b21db03b920bcf4ec95dd80b494 to your computer and use it in GitHub Desktop.
Save adrianparvino/f9145b21db03b920bcf4ec95dd80b494 to your computer and use it in GitHub Desktop.
{ 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