From 01d2927a4eb9f2753c5e2c4e5af820dca18cc9fc Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 17 Dec 2017 14:30:52 +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 | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 9047d72e..70647334 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -69,7 +69,17 @@ in { }; }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); - systemd.services = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" (let + systemd.timers = mapAttrs (target: tCfg: nameValuePair "borgbackup-prune-${target}" { + wantedBy = [ "timers.target" ]; + + timerConfig = { + Persistent = false; + OnBootSec = tCfg.interval; + OnUnitInactiveSec = tCfg.interval; + }; + }) cfg.targets; + + systemd.services = mapAttrs (target: tCfg: nameValuePair "borgbackup-${target}@" (let deps = flatten [ (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%p.service") (optional tCfg.network "network-online.target") @@ -92,16 +102,10 @@ in { --patterns-from .backup \ --one-file-system \ --compression auto,lzma \ - ${tCfg.repo}::${cfg.prefix}${path}-{utcnow} + ${tCfg.repo}::${cfg.prefix}$1-{utcnow} ''; in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; - - preStop = mkIf (hasAttr path tCfg.prune) '' - borg prune \ - --prefix "${cfg.prefix}${path}" \ - ${concatStringsSep " " tCfg.prune."${path}"} \ - ${tCfg.repo} - ''; + scriptArgs = "%i"; unitConfig = { AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; @@ -115,6 +119,22 @@ in { IOSchedulingPriority = 7; SuccessExitStatus = [1 2]; }; - })) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); + })) cfg.targets; + + systemd.services = mapAttrs (target: tCfg: nameValuePair "borgbackup-prune-${target}" { + bindsTo = "network-online.target"; + after = "network-online.target"; + + script = concatStringsSep "\n" (mapAttrsToList ('' + borg prune \ + --prefix "${cfg.prefix}${path}" \ + ${concatStringsSep " " tCfg.prune."${path}"} \ + ${tCfg.repo} + '') tCfg.prune); + + serviceConfig = { + Type = "oneshot"; + }; + }) cfg.targets; }; } -- cgit v1.2.3