blob: a1fba73743153e2a03d713522a495557787524d3 (
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
|
{
pkgs,
lib,
config,
mkInstanceServices,
...
}:
let
snapshotType = lib.types.submodule {
options = {
paths = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "snapshoted paths";
};
schedule = lib.mkOption {
type = lib.types.str;
default = "*-*-* 4:00:00";
description = "Snapshot schedule";
};
};
};
instanceType = lib.types.submodule {
options = {
snapshots = lib.mkOption {
type = snapshotType;
};
};
};
in
{
options.services.kopia.instances = lib.mkOption {
type = lib.types.attrsOf instanceType;
};
config = lib.mkIf config.services.kopia.enable (
let
mkSnapshotService =
# refactor with mkRepositoryArgs
name: instance:
lib.attrsets.nameValuePair "kopia-snapshot-${name}" {
description = "Kopia S3 snapshot service";
wants = [
"kopia-repository-${name}.service"
];
after = [ "kopia-repository-${name}.service" ];
script = lib.strings.concatLines (
lib.lists.forEach instance.snapshots.paths (x: ''
${pkgs.kopia}/bin/kopia snapshot create ${x} --description "Snapshot for ${name}"
'')
);
serviceConfig = {
Type = "simple";
User = "${instance.user}";
WorkingDirectory = "~";
SetLoginEnvironment = true;
# retry on failure
Restart = "on-failure";
# wait 30 seconds before restarting
RestartSec = "30";
# lower priority
Nice = "-19";
IOSchedulingClass = "idle";
};
unitConfig = {
# limit the number of restarts to 5 in 1 day
StartLimitInterval = "1d";
StartLimitBurst = "5";
};
};
mkSnapshotTimer =
name: instance:
lib.attrsets.nameValuePair "kopia-snapshot-${name}" {
description = "Kopia S3 snapshot timer";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = instance.snapshots.schedule;
};
};
in
{
# systemd service for repositories open
systemd.services = mkInstanceServices config.services.kopia.instances mkSnapshotService;
systemd.timers = mkInstanceServices config.services.kopia.instances mkSnapshotTimer;
}
);
}
|