diff options
Diffstat (limited to 'custom')
-rw-r--r-- | custom/borgbackup.nix | 23 |
1 files changed, 17 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 | |||
16 | default = []; | 16 | default = []; |
17 | }; | 17 | }; |
18 | 18 | ||
19 | prune = mkOption { | ||
20 | type = types.attrsOf (types.listOf types.str); | ||
21 | default = {}; | ||
22 | }; | ||
23 | |||
19 | interval = mkOption { | 24 | interval = mkOption { |
20 | type = types.str; | 25 | type = types.str; |
21 | default = "6h"; | 26 | default = "6h"; |
@@ -54,7 +59,7 @@ in { | |||
54 | config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { | 59 | config = mkIf (any (t: t.paths != []) (attrValues cfg.targets)) { |
55 | services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; | 60 | services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true; |
56 | 61 | ||
57 | systemd.timers = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" { | 62 | systemd.timers = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}-${path}" { |
58 | wantedBy = [ "timers.target" ]; | 63 | wantedBy = [ "timers.target" ]; |
59 | 64 | ||
60 | timerConfig = { | 65 | timerConfig = { |
@@ -64,9 +69,9 @@ in { | |||
64 | }; | 69 | }; |
65 | }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); | 70 | }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); |
66 | 71 | ||
67 | systemd.services = mapAttrs' (target: tCfg: nameValuePair "borgbackup-${target}@" (let | 72 | systemd.services = listToAttrs (map ({ target, path, tCfg }: nameValuePair "borgbackup-${target}@${path}" (let |
68 | deps = flatten [ | 73 | deps = flatten [ |
69 | (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service") | 74 | (optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%p.service") |
70 | (optional tCfg.network "network-online.target") | 75 | (optional tCfg.network "network-online.target") |
71 | ]; | 76 | ]; |
72 | in { | 77 | in { |
@@ -87,10 +92,16 @@ in { | |||
87 | --patterns-from .backup \ | 92 | --patterns-from .backup \ |
88 | --one-file-system \ | 93 | --one-file-system \ |
89 | --compression auto,lzma \ | 94 | --compression auto,lzma \ |
90 | ${tCfg.repo}::${cfg.prefix}$1-{utcnow} | 95 | ${tCfg.repo}::${cfg.prefix}${path}-{utcnow} |
91 | ''; | 96 | ''; |
92 | in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; | 97 | in if tCfg.lock == null then borgCmd else "flock -xo /var/lock/${tCfg.lock} ${borgCmd}"; |
93 | scriptArgs = "%i"; | 98 | |
99 | preStop = mkIf (hasAttr path tCfg.prune) '' | ||
100 | borg prune \ | ||
101 | --prefix "${tcfg.prefix}${path}" \ | ||
102 | ${concatStringsSep " " tCfg.prune."${path}"} \ | ||
103 | ${tCfg.repo} | ||
104 | ''; | ||
94 | 105 | ||
95 | unitConfig = { | 106 | unitConfig = { |
96 | AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; | 107 | AssertPathIsDirectory = mkIf (tCfg.lock != null) "/var/lock"; |
@@ -104,6 +115,6 @@ in { | |||
104 | IOSchedulingPriority = 7; | 115 | IOSchedulingPriority = 7; |
105 | SuccessExitStatus = [1 2]; | 116 | SuccessExitStatus = [1 2]; |
106 | }; | 117 | }; |
107 | })) cfg.targets; | 118 | }))) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path tCfg; }) tCfg.paths) cfg.targets))); |
108 | }; | 119 | }; |
109 | } | 120 | } |