summaryrefslogtreecommitdiff
path: root/modules/backup-server.nix
blob: 96e49406814d626255b255f4df0735b579ea929a (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
{ 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";
  };

  dragon-tmpl = repo: {
    inherit repo;
    paths = [ "/home" "/opt" "/var" "/storage" ];
    exclude = [ "/var/lib/lxd" "/var/lib/borg" ];
    doInit = true;
    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";
    extraCreateArgs = "--stats";
  };
  backups.dragon-break = dragon-tmpl "borg@www.breakds.org:.";
  backups.dragon-trygve = dragon-tmpl "orbekk@backup.osl.trygveandre.net:/home/orbekk/repository";

  clientJobs = {
    ${config.networking.hostName} = backups.${config.networking.hostName};
  };

  serverJobs = {
    dragon-break = backups.dragon-break;
    dragon-trygve = backups.dragon-trygve;
  };

  backupJobs =
    if config.networking.hostName == "dragon" then serverJobs else clientJobs;
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" ];
      };
      breakds = {
        authorizedKeys = [ (builtins.readFile ../data/breakds.pub) ];
        path = [ "/var/lib/borg/breakds" ];
      };
      pincer = {
        authorizedKeys =
          [ (builtins.readFile ../secrets/pincer-borg-ssh-key.pub) ];
        path = [ "/var/lib/borg-pincer" ];
      };
    };

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