From 086035d82b049e8d8ca47c5cd5d3d2220833d4bc Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 17 Dec 2017 04:33:21 +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 | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix index 28dbba16..47714e83 100644 --- a/custom/borgbackup.nix +++ b/custom/borgbackup.nix @@ -54,14 +54,15 @@ 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 }: nameValuePair "borgbackup-${target}@${path}" { + systemd.timers = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" { wantedBy = [ "timers.target" ]; timerConfig = { - Persistent = true; - OnUnitInactiveSec = "6h"; + Persistent = false; + OnBootSec = tCfg.interval; + OnUnitInactiveSec = tCfg.interval; }; - }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path; }) tCfg.paths) cfg.targets))); + }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); systemd.services = mapAttrs' (target: tCfg: nameValuePair "borgbackup-${target}@" (let deps = flatten [ @@ -74,20 +75,26 @@ in { path = with pkgs; [borgbackup]; - script = '' - borg create \ - --stats \ - --list \ - --filter 'AME' \ - --exclude-caches \ - --keep-exclude-tags \ - --patterns-from .backup \ - --one-file-system \ - --compression auto,lzma \ - ${target}::${cfg.prefix}$1-{utcnow} - ''; + script = let + borgCmd = '' + borg create \ + --stats \ + --list \ + --filter 'AME' \ + --exclude-caches \ + --keep-exclude-tags \ + --patterns-from .backup \ + --one-file-system \ + --compression auto,lzma \ + ${target}::${cfg.prefix}$1-{utcnow} + ''; + in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; scriptArgs = "%i"; + unitConfig = { + AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; + }; + serviceConfig = { Type = "oneshot"; WorkingDirectory = if (cfg.snapshots == null) then "%p" else "/mnt/snapshot-%i"; -- cgit v1.2.3