summaryrefslogtreecommitdiffstats
path: root/mail-server/borgbackup.nix
blob: ef83b0d324da1ba3931c77ed77b68d057bcc1098 (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
#  nixos-mailserver: a simple mail server
#  Copyright (C) 2016-2018  Robin Raymond
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. If not, see <http://www.gnu.org/licenses/>

{ config, pkgs, lib, ... }:

let
  cfg = config.mailserver.borgbackup;

  methodFragment = lib.optional (cfg.compression.method != null) cfg.compression.method;
  autoFragment =
    if cfg.compression.auto && cfg.compression.method == null
    then throw "compression.method must be set when using auto."
    else lib.optional cfg.compression.auto "auto";
  levelFragment =
    if cfg.compression.level != null && cfg.compression.method == null
    then throw "compression.method must be set when using compression.level."
    else lib.optional (cfg.compression.level != null) (toString cfg.compression.level);
  compressionFragment = lib.concatStringsSep "," (lib.flatten [autoFragment methodFragment levelFragment]);
  compression = lib.optionalString (compressionFragment != "") "--compression ${compressionFragment}";

  encryptionFragment = cfg.encryption.method;
  passphraseFile = lib.escapeShellArg cfg.encryption.passphraseFile;
  passphraseFragment = lib.optionalString (cfg.encryption.method != "none")
                         (if cfg.encryption.passphraseFile != null then ''env BORG_PASSPHRASE="$(cat ${passphraseFile})"''
                          else throw "passphraseFile must be set when using encryption.");

  locations = lib.escapeShellArgs cfg.locations;
  name = lib.escapeShellArg cfg.name;

  repoLocation = lib.escapeShellArg cfg.repoLocation;

  extraInitArgs = lib.escapeShellArgs cfg.extraArgumentsForInit;
  extraCreateArgs = lib.escapeShellArgs cfg.extraArgumentsForCreate;

  cmdPreexec = lib.optionalString (cfg.cmdPreexec != null) cfg.cmdPreexec;
  cmdPostexec = lib.optionalString (cfg.cmdPostexec != null) cfg.cmdPostexec;

  borgScript = ''
    export BORG_REPO=${repoLocation}
    ${cmdPreexec}
    ${passphraseFragment} ${pkgs.borgbackup}/bin/borg init ${extraInitArgs} --encryption ${encryptionFragment} || true
    ${passphraseFragment} ${pkgs.borgbackup}/bin/borg create ${extraCreateArgs} ${compression} ::${name} ${locations}
    ${cmdPostexec}
  '';
in {
  config = lib.mkIf (config.mailserver.enable && cfg.enable) {
    environment.systemPackages = with pkgs; [
      borgbackup
    ];

    systemd.services.borgbackup = {
      description = "borgbackup";
      unitConfig.Documentation = "man:borgbackup";
      script = borgScript;
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        CPUSchedulingPolicy = "idle";
        IOSchedulingClass = "idle";
        ProtectSystem = "full";
      };
      startAt = cfg.startAt;
    };
  };
}