about summary refs log tree commit diff
path: root/modules/services/backup/default.nix
blob: 3481f3f70d70d45f312201a24cbcf5c7ba75d4d4 (plain) (blame)
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
{ config, pkgs, lib, ... }:
let
  cfg = config.my.services.backup;
  excludeArg = with builtins;
    with pkgs;
    "--exclude-file="
    + (writeText "excludes.txt" (concatStringsSep "\n" cfg.exclude));
in
{
  options.my.services.backup = with lib; {
    enable = mkEnableOption "Enable backups for this host";

    repository = mkOption {
      type = types.str;
      example = "/data/slow/backups/hosts";
      description = "The repository to back up to";
    };

    passwordFile = mkOption {
      type = types.path;
      example = "/var/lib/restic/password.txt";
      description = "Read the repository's password from this path";
    };

    extraOptions = mkOption {
      type = with types; listOf str;
      default = [ ];
      example = [ "sftp.command='ssh ....'" ];
      description = "Extra options to restic";
    };

    paths = mkOption {
      type = with types; listOf str;
      default = [ ];
      example = [ "/var/lib" "/home" ];
      description = "Paths to backup";
    };

    exclude = mkOption {
      type = with types; listOf str;
      default = [ ];
      example = [
        # very large paths
        "/var/lib/docker"
        "/var/lib/systemd"
        "/var/lib/libvirt"

        # temporary files created by `cargo` and `go build`
        "**/target"
        "/home/*/go/bin"
        "/home/*/go/pkg"
      ];
      description = "Paths to exclude from backup";
    };

    pruneOpts = mkOption {
      type = with types; listOf str;
      default = [
        "--keep-last 10"
        "--keep-daily 7"
        "--keep-weekly 4"
        "--keep-monthly 8"
        "--keep-yearly 2"
      ];
      example = [ "--keep-last 5" "--keep-weekly 2" ];
      description = ''
        List of options to give to the `forget` subcommand after a backup.
      '';
    };

    timerConfig = mkOption {
      # NOTE: I do not know how to cleanly set the type
      default = { OnCalendar = "daily"; };
      example = {
        OnCalendar = "00:05";
        RandomizedDelaySec = "5h";
      };
      description = ''
        When to run the backup. See man systemd.timer for details.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    services.restic.backups.host = {
      # Take care of included and excluded files
      paths = cfg.paths;
      extraBackupArgs = [ "--verbose=1" "--compression=max" ]
        ++ lib.optional (builtins.length cfg.exclude != 0) excludeArg;
      # Take care of creating the repository if it doesn't exist
      initialize = true;
      inherit (cfg) passwordFile pruneOpts timerConfig repository extraOptions;
    };
  };
}