From b115168c15be41055343dba3a6e37d0e4ffd4590 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 17 Dec 2017 14:22:10 +0100 Subject: =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom/borgbackup.nix | 23 +++++++++++++++++------ hel.nix | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 222b1999..c08168ed 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -16,6 +16,11 @@ let default = []; }; + prune = mkOption { + type = types.attrsOf (types.listOf types.str); + default = {}; + }; + interval = mkOption { type = types.str; default = "6h"; @@ -54,7 +59,7 @@ in { config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; - systemd.timers = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" { + systemd.timers = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}-${path}" { wantedBy = [ "timers.target" ]; timerConfig = { @@ -64,9 +69,9 @@ in { }; }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); - systemd.services = mapAttrs' (target: tCfg: nameValuePair "borgbackup-${target}@" (let + systemd.services = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" (let deps = flatten [ - (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service") + (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%p.service") (optional tCfg.network "network-online.target") ]; in { @@ -87,10 +92,16 @@ in { --patterns-from .backup \ --one-file-system \ --compression auto,lzma \ - ${tCfg.repo}::${cfg.prefix}$1-{utcnow} + ${tCfg.repo}::${cfg.prefix}${path}-{utcnow} ''; in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; - scriptArgs = "%i"; + + preStop = mkIf (hasAttr path tCfg.prune) '' + borg prune \ + --prefix "${tcfg.prefix}${path}" \ + ${concatStringsSep " " tCfg.prune."${path}"} \ + ${tCfg.repo} + ''; unitConfig = { AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; @@ -104,6 +115,6 @@ in { IOSchedulingPriority = 7; SuccessExitStatus = [1 2]; }; - })) cfg.targets; + }))) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); }; } diff --git a/hel.nix b/hel.nix index 68d6df08..1165e4fa 100644 --- a/hel.nix +++ b/hel.nix @@ -532,10 +532,26 @@ "odin" = { repo = "borg.odin:/srv/backup/borg"; paths = [ "home-gkleen" ]; + prune = { + "home-gkleen" = + [ "--keep-within 24H" + "--keep-daily 31" + "--keep-monthly 12" + "--keep-yearly -1" + ]; + }; }; "munin" = { repo = "borg.munin:borg"; paths = [ "home-gkleen" ]; + prune = { + "home-gkleen" = + [ "--keep-within 24H" + "--keep-daily 31" + "--keep-monthly 12" + "--keep-yearly -1" + ]; + }; }; }; }; -- cgit v1.2.3