summaryrefslogtreecommitdiff
path: root/custom
diff options
context:
space:
mode:
Diffstat (limited to 'custom')
-rw-r--r--custom/borgbackup.nix101
-rw-r--r--custom/btrfs-snapshots.nix2
2 files changed, 103 insertions, 0 deletions
diff --git a/custom/borgbackup.nix b/custom/borgbackup.nix
new file mode 100644
index 00000000..49da1975
--- /dev/null
+++ b/custom/borgbackup.nix
@@ -0,0 +1,101 @@
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.borgbackup;
7
8 targetOptions = {
9 options = {
10 repo = mkOption {
11 type = types.str;
12 };
13
14 paths = mkOption {
15 type = types.listOf types.path;
16 default = [];
17 };
18
19 interval = mkOption {
20 type = types.str;
21 default = "6h";
22 };
23
24 lock = mkOption {
25 type = types.nullOr types.str;
26 default = "backup";
27 };
28
29 network = mkOption {
30 type = types.bool;
31 default = true;
32 };
33 };
34 };
35in {
36 options = {
37 services.borgbackup = {
38 snapshots = mkOption {
39 type = types.nullOr (types.enum ["btrfs"]);
40 default = null;
41 };
42
43 targets = mkOption {
44 type = types.attrsOf (types.submodule targetOptions);
45 default = {};
46 };
47
48 prefix = mkOption {
49 type = types.str;
50 };
51 };
52 };
53
54 config = mkIf (any (t: t.paths != []) cfg.targets) {
55 services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true;
56
57 systemd.timers = listToAttrs (map (target: path: nameValuePair "borgbackup-${target}@${path}" {
58 wantedBy = [ "timers.target" ];
59
60 timerConfig = {
61 Persistent = true;
62 OnUnitInactiveSec = "6h";
63 };
64 }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path; }) tCfg.paths;) cfg.targets)));
65
66 systemd.services = map (target: nameValuePair "borgbackup-${target}@" (let
67 deps = flatten [
68 optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service"
69 optional network "network-online.target"
70 ];
71 in {
72 bindsTo = deps;
73 after = deps;
74
75 path = with pkgs; [borgbackup];
76
77 script = ''
78 borg create \
79 --stats \
80 --list \
81 --filter 'AME' \
82 --exclude-caches \
83 --keep-exclude-tags \
84 --patterns-from .backup \
85 --one-file-system \
86 --compression auto,lzma \
87 ${target}::${prefix}$1-{utcnow}
88 '';
89 scriptArgs = "%i";
90
91 serviceConfig = {
92 Type = "oneshot";
93 WorkingDirectory = if (cfg.snapshots == null) then "%p" else "/mnt/snapshot-%i";
94 Nice = 15;
95 IOSchedulingClass = 2;
96 IOSchedulingPriority = 7;
97 SuccessExitStatus = [1 2];
98 };
99 })) (attrNames cfg.targets);
100 };
101};
diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix
index 03b17d23..7114e9d9 100644
--- a/custom/btrfs-snapshots.nix
+++ b/custom/btrfs-snapshots.nix
@@ -13,11 +13,13 @@ in {
13 enable = mkEnableOption "a systemd unit for btrfs snapshots"; 13 enable = mkEnableOption "a systemd unit for btrfs snapshots";
14 14
15 mountPoint = mkOption { 15 mountPoint = mkOption {
16 readOnly = true;
16 type = types.path; 17 type = types.path;
17 default = "/mnt"; 18 default = "/mnt";
18 }; 19 };
19 20
20 mountPrefix = mkOption { 21 mountPrefix = mkOption {
22 readOnly = true;
21 type = types.str; 23 type = types.str;
22 default = "snapshot-"; 24 default = "snapshot-";
23 }; 25 };