about summary refs log tree commit diff
path: root/modules/services/backup/rsync.nix
blob: d58dfe94926237124683b041fb3e5f971d8b66a1 (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
{ config, pkgs, lib, ... }:
let
  cfg = config.my.services.backup.rsync;
  secrets = config.age.secrets;
  ssh-key-path = secrets."rsync.net/ssh-key".path;
in
{
  options.my.services.backup.rsync = with lib; {
    enable = mkEnableOption "rsync backup service";

    sourceDir = mkOption {
      type = types.path;
      example = "/data/slow/backups";
      description = "The directory to synchronize";
    };

    destination = mkOption {
      type = types.str;
      example = "de2664@de2664.rsync.net:backups/";
      description = "The destination";
    };

    timerConfig = mkOption {
      default = { OnCalendar = "daily"; };
      example = {
        OnCalendar = "00:05";
        RandomizedDelaySec = "5h";
      };
      description = ''
        When to run rsync. See man systemd.timer for details.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    systemd = {
      timers.rsync-backups = {
        description = "synchronize restic repository to rsync.net";
        wantedBy = [ "timers.target" ];
        partOf = [ "rsync-backups.service" ];
        timerConfig = cfg.timerConfig;
      };
      services.rsync-backups = {
        description = "synchronize restic repository to rsync.net";
        serviceConfig = {
          Type = "oneshot";
        };
        script = ''
          exec ${pkgs.rsync}/bin/rsync \
            -azq --delete \
            -e '${pkgs.openssh}/bin/ssh -i ${ssh-key-path}' \
            ${cfg.sourceDir} ${cfg.destination}
        '';
      };
    };
  };
}