summaryrefslogtreecommitdiff
path: root/modules/backup-server.nix
blob: 8d5d085043eaa40223e3e7a3129067567f035a5a (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
{ config, lib, pkgs, ... }:

let
  cfg = config.orbekk.backups;

  backups.pincer = {
    paths = [ "/etc/nixos" ];
    doInit = true;
    repo = cfg.serverLocation;
    encryption = {
      mode = "repokey-blake2";
      passCommand = "cat ${config.age.secrets.pincer-borg-repo-key.path}";
    };
    environment = { BORG_RSH = "ssh -i ${config.age.secrets.pincer-borg-ssh-key.path}"; };
    compression = "auto,lzma";
    startAt = "daily";
  };

  backups.dragon = {
    paths = [ "/etc/nixos" ];
    doInit = true;
    repo = cfg.serverLocation;
    encryption = {
      mode = "repokey-blake2";
      passCommand = "cat ${config.age.secrets.dragon-borg-repo-key.path}";
    };
    environment = { BORG_RSH = "ssh -i ${config.age.secrets.dragon-borg-ssh-key.path}"; };
    compression = "auto,lzma";
    startAt = "daily";
  };

  backupJob = {
    ${config.networking.hostName} = backups.${config.networking.hostName};
  };
in
{
  options = {
    orbekk.backups = {
      enableServer = lib.mkEnableOption "Enable backup server";
      enableClient = lib.mkEnableOption "Enable backup client";
      serverLocation = lib.mkOption {
        type = lib.types.str;
        default = "borg@localhost:.";
      };
    };
  };

  config = {
    age.secrets = lib.mkIf cfg.enableClient {
      "${config.networking.hostName}-borg-repo-key".file =
        ../secrets/${config.networking.hostName}-borg-repo-key.age;
      "${config.networking.hostName}-borg-ssh-key".file =
        ../secrets/${config.networking.hostName}-borg-ssh-key.age;
    };

    services.borgbackup.repos = lib.mkIf cfg.enableServer {
      dragon = {
        authorizedKeys = [ (builtins.readFile ../secrets/dragon-borg-ssh-key.pub) ];
        path = [ "/var/lib/dragon" ];
      };
      pincer = {
        authorizedKeys = [ (builtins.readFile ../secrets/pincer-borg-ssh-key.pub) ];
        path = [ "/var/lib/borg-pincer" ];
      };
    };

    services.borgbackup.jobs = lib.mkIf cfg.enableClient backupJob;
  };
}