summaryrefslogtreecommitdiff
path: root/hosts/vidhar/borg/default.nix
blob: d6d64ec80e6aee7d7b9cc263db5e3bb2e9aa792f (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{ config, pkgs, lib, flakeInputs, utils, ... }:

with lib;

let
  sshConfig = ''
    Include /etc/ssh/ssh_config

    ControlMaster auto
    ControlPath /var/lib/borg/.borgssh-master-%r@%n:%p
    ControlPersist yes

    Host yggdrasil.borgbase
      HostName nx69hpl8.repo.borgbase.com
      User nx69hpl8
      IdentityFile ${config.sops.secrets."append.borgbase".path}
      IdentitiesOnly yes

      BatchMode yes
      ServerAliveInterval 10
      ServerAliveCountMax 30

      IPQoS cs1
  '';

  checkBorgUnit = {
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "${pkgs.borgbackup}/bin/borg ${utils.escapeSystemdExecArgs [
        "--lock-wait" "3600"
        "--log-json"
        "--progress"
        "check"
        "--verify-data"
        "--max-duration" "4500"
      ]} %I";
      Environment = [
        "BORG_BASE_DIR=/var/lib/borg"
        "BORG_CONFIG_DIR=/var/lib/borg/config"
        "BORG_CACHE_DIR=/var/lib/borg/cache"
        "BORG_SECURITY_DIR=/var/lib/borg/security"
        "BORG_KEYS_DIR=/var/lib/borg/keys"
        "BORG_HOSTNAME_IS_UNIQUE=yes"
        "BORG_RSH=\"${pkgs.openssh}/bin/ssh -F ${pkgs.writeText "config" sshConfig}\""
      ];
    };
  };
in {
  config = {
    services.borgsnap = {
      enable = true;

      target = "yggdrasil.borgbase:repo";
      inherit sshConfig;
      keyfile = config.sops.secrets."yggdrasil.borgkey".path;
    };

    services.copyborg.jotnar = {
      from = "/srv/backup/borg/jotnar";
      to = "yggdrasil.borgbase:repo";
      inherit sshConfig;
      keyfile = config.sops.secrets."yggdrasil.borgkey".path;
      timerOptions.timerConfig = {
        OnCalendar = "*-*-* 00/4:00:00 Europe/Berlin";
      };
    };

    services.borgbackup.repos.jotnar = {
      path = "/srv/backup/borg/jotnar";
      authorizedKeysAppendOnly = let
        dir = ./jotnar;
        toAuthKey = fname: ftype: if ftype != "regular" || !(hasSuffix ".pub" fname) then null else builtins.readFile (dir + "/${fname}");
      in filter (v: v != null) (mapAttrsToList toAuthKey (builtins.readDir dir));
    };

    # systemd.services."check-borg@${utils.escapeSystemdPath "/srv/backup/borg/jotnar"}" = checkBorgUnit;
    # systemd.services."check-borg@${utils.escapeSystemdPath "yggdrasil.borgbase:repo"}" = recursiveUpdate checkBorgUnit {
    #   serviceConfig = {
    #     Environment = checkBorgUnit.serviceConfig.Environment ++ [
    #       "BORG_KEY_FILE=${config.sops.secrets."yggdrasil.borgkey".path}"
    #     ];
    #   };
    # };
    # systemd.timers."check-borg@${utils.escapeSystemdPath "/srv/backup/borg/jotnar"}" = {
    #   wantedBy = [ "timers.target" ];
    #   timerConfig = {
    #     OnCalendar = "*-*-* 00:30:00 UTC";
    #   };
    # };
    # systemd.timers."check-borg@${utils.escapeSystemdPath "yggdrasil.borgbase:repo"}" = {
    #   wantedBy = [ "timers.target" ];
    #   timerConfig = {
    #     OnCalendar = "*-*-* 00:30:00 UTC";
    #   };
    # };

    boot.postBootCommands = mkBefore ''
      ${pkgs.findutils}/bin/find /srv/backup/borg -type d -empty -delete
    '';

    users.users.borg.extraGroups = ["ssh"];

    services.openssh.extraConfig = ''
      Match User borg
        ClientAliveInterval 10
        ClientAliveCountMax 30

      Match All
    '';

    sops.secrets."append.borgbase" = {
      format = "binary";
      sopsFile = ./append.borgbase;
      owner = "borg";
      group = "borg";
      mode = "0400";
    };
    sops.secrets."yggdrasil.borgkey" = {
      format = "binary";
      sopsFile = ./yggdrasil.borgkey;
      owner = "borg";
      group = "borg";
      mode = "0400";
    };
  };
}