summaryrefslogtreecommitdiff
path: root/custom/borgbackup.nix
blob: 49da19752db46706544489d5f3fc2c140aa2d9c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.borgbackup;

  targetOptions = {
    options = {
      repo = mkOption {
        type = types.str;
      };
    
      paths = mkOption {
        type = types.listOf types.path;
        default = [];
      };

      interval = mkOption {
        type = types.str;
        default = "6h";
      };

      lock = mkOption {
        type = types.nullOr types.str;
        default = "backup";
      };

      network = mkOption {
        type = types.bool;
        default = true;
      };
    };
  };
in {
  options = {
    services.borgbackup = {
      snapshots = mkOption {
        type = types.nullOr (types.enum ["btrfs"]);
        default = null;
      };

      targets = mkOption {
        type = types.attrsOf (types.submodule targetOptions);
        default = {};
      };

      prefix = mkOption {
        type = types.str;
      };
    };
  };

  config = mkIf (any (t: t.paths != []) cfg.targets) {
    services.btrfs-snapshots.enable = mkIf (cfg.snapshots == "btrfs") true;
  
    systemd.timers = listToAttrs (map (target: path: nameValuePair "borgbackup-${target}@${path}" {
      wantedBy = [ "timers.target" ];

      timerConfig = {
        Persistent = true;
        OnUnitInactiveSec = "6h";
      };
    }) (flatten (mapAttrsToList (target: tCfg: map (path: { inherit target path; }) tCfg.paths;) cfg.targets)));

    systemd.services = map (target: nameValuePair "borgbackup-${target}@" (let
      deps = flatten [
        optional (cfg.snapshots == "btrfs") "btrfs-snapshot@%i.service"
        optional network "network-online.target"
      ];
    in {
      bindsTo = deps;
      after = deps;

      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}::${prefix}$1-{utcnow}
      '';
      scriptArgs = "%i";

      serviceConfig = {
        Type = "oneshot";
        WorkingDirectory = if (cfg.snapshots == null) then "%p" else "/mnt/snapshot-%i";
        Nice = 15;
        IOSchedulingClass = 2;
        IOSchedulingPriority = 7;
        SuccessExitStatus = [1 2];
      };
    })) (attrNames cfg.targets);
  };
};