summaryrefslogtreecommitdiffstats
path: root/lass/3modules/restic.nix
blob: c720793b1956b4a3d17c4ee61f0bb9d62c3706b7 (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
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;

{
  options.lass.restic = mkOption {
    type = types.attrsOf (types.submodule ({ config, ... }: {
      options = {
        name = mkOption {
          type = types.str;
          default = config._module.args.name;
        };
        passwordFile = mkOption {
          type = types.str;
          default = toString <secrets/restic-password>;
          description = ''
            read the repository password from a file.
          '';
          example = "/etc/nixos/restic-password";

        };
        repo = mkOption {
          type = types.str;
          default = "sftp:backup@prism.r:/backups/${config.name}";
          description = ''
            repository to backup to.
          '';
          example = "sftp:backup@192.168.1.100:/backups/${config.name}";
        };
        dirs = mkOption {
          type = types.listOf types.str;
          default = [];
          description = ''
            which directories to backup.
          '';
          example = [
            "/var/lib/postgresql"
            "/home/user/backup"
          ];
        };
        timerConfig = mkOption {
          type = types.attrsOf types.str;
          default = {
            OnCalendar = "daily";
          };
          description = ''
            When to run the backup. See man systemd.timer for details.
          '';
          example = {
            OnCalendar = "00:05";
            RandomizedDelaySec = "5h";
          };
        };
        user = mkOption {
          type = types.str;
          default = "root";
          description = ''
            As which user the backup should run.
          '';
          example = "postgresql";
        };
        extraArguments = mkOption {
          type = types.listOf types.str;
          default = [];
          description = ''
            Extra arguments to append to the restic command.
          '';
          example = [
            "sftp.command='ssh backup@192.168.1.100 -i /home/user/.ssh/id_rsa -s sftp"
          ];
        };
        initialize = mkOption {
          type = types.bool;
          default = false;
          description = ''
            Create the repository if it doesn't exist.
          '';
        };
      };
    }));
    default = {};
  };

  config = {
    systemd.services =
      mapAttrs' (_: plan:
        let
          extraArguments = concatMapStringsSep " " (arg: "-o ${arg}") plan.extraArguments;
          connectTo = elemAt (splitString ":" plan.repo) 1;
          resticCmd = "${pkgs.restic}/bin/restic ${extraArguments}";
        in nameValuePair "backup.${plan.name}" {
          environment = {
            RESTIC_PASSWORD_FILE = plan.passwordFile;
            RESTIC_REPOSITORY = plan.repo;
          };
          path = with pkgs; [
            openssh
          ];
          restartIfChanged = false;
          serviceConfig = {
            ExecStartPre = mkIf plan.initialize (pkgs.writeScript "rustic-${plan.name}-init" ''
              #! ${pkgs.bash}/bin/bash
              ${resticCmd} snapshots || ${resticCmd} init
            '');
            ExecStart = pkgs.writeDash "rustic-${plan.name}" (
              "#! ${pkgs.bash}/bin/bash\n" +
              concatMapStringsSep "\n" (dir: "${resticCmd} backup ${dir}") plan.dirs
            );
            User = plan.user;
          };
        }
      ) config.lass.restic;
    systemd.timers =
      mapAttrs' (_: plan: nameValuePair "backup.${plan.name}" {
        wantedBy = [ "timers.target" ];
        timerConfig = plan.timerConfig;
      }) config.lass.restic;
  };
}