forked from NixOS/nixpkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileBackup.nix
More file actions
225 lines (191 loc) · 6.31 KB
/
Copy pathfileBackup.nix
File metadata and controls
225 lines (191 loc) · 6.31 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{ lib, ... }:
let
inherit (lib) mkOption;
inherit (lib.types)
str
path
nonEmptyListOf
listOf
submodule
;
in
{
contracts.fileBackup = {
meta = {
maintainers = [ lib.maintainers.ibizaman ];
description = ''
File backup contract where a directory containing
regular files is to be backed up.
'';
};
input = {
options.user = mkOption {
description = ''
Unix user doing the backups.
'';
type = str;
example = "vaultwarden";
};
options.sourceDirectories = mkOption {
description = "Directories to back up.";
type = nonEmptyListOf str;
example = "/var/lib/vaultwarden";
};
options.excludePatterns = mkOption {
description = "File patterns to exclude.";
type = listOf str;
default = [ ];
};
options.hooks = mkOption {
description = "Hooks to run around the backup.";
default = { };
type = submodule {
options = {
beforeBackup = mkOption {
description = "Hooks to run before backup.";
type = listOf path;
default = [ ];
};
afterBackup = mkOption {
description = "Hooks to run after backup.";
type = listOf path;
default = [ ];
};
};
};
};
};
output = output: {
options.restoreScript = mkOption {
description = ''
Name of script that can restore the database.
One can then list snapshots with:
```bash
$ ${output.options.restoreScript.value} snapshots
```
And restore the database with:
```bash
$ ${output.options.restoreScript.value} restore latest
```
'';
type = path;
};
options.backupService = mkOption {
description = ''
Name of service backing up the database.
This script can be ran manually to back up the database:
```bash
$ systemctl start ${output.options.backupService.value}
```
'';
type = str;
};
};
behaviorTest =
{
providerRoot,
extraModules ? [ ],
}:
{
nodes.machine =
{ config, ... }:
{
imports = extraModules;
options.test = {
repository = mkOption {
type = str;
default = "/opt/repository";
};
username = mkOption {
type = str;
default = "me";
};
sourceDirectories = mkOption {
type = listOf str;
default = [
"/opt/files/A"
"/opt/files/B"
];
};
};
config = lib.mkMerge [
(lib.setAttrByPath providerRoot {
consumer.input = {
inherit (config.test) sourceDirectories;
user = config.test.username;
};
})
(lib.mkIf (config.test.username != "root") {
users.users.${config.test.username} = {
isSystemUser = true;
group = config.test.username;
};
users.groups.${config.test.username} = { };
})
];
};
extraPythonPackages = p: [ p.dictdiffer ];
testScript =
{ nodes, ... }:
let
cfg = nodes.machine;
inherit (lib.getAttrFromPath providerRoot nodes.machine) output;
in
''
from dictdiffer import diff # type: ignore
username = "${cfg.test.username}"
sourceDirectories = [ ${lib.concatMapStringsSep ", " (x: ''"${x}"'') cfg.test.sourceDirectories} ]
def list_files(dir):
files_and_content = {}
files = machine.succeed(f"""find {dir} -type f""").split("\n")[:-1]
for f in files:
content = machine.succeed(f"""cat {f}""").strip()
files_and_content[f] = content
return files_and_content
def assert_files(dir, files):
result = list(diff(list_files(dir), files))
if len(result) > 0:
raise Exception("Unexpected files:", result)
with subtest("Create initial content"):
for path in sourceDirectories:
machine.succeed(f"""
mkdir -p {path}
echo repo_fileA_1 > {path}/fileA
echo repo_fileB_1 > {path}/fileB
chown {username}: -R {path}
chmod go-rwx -R {path}
""")
for path in sourceDirectories:
assert_files(path, {
f'{path}/fileA': 'repo_fileA_1',
f'{path}/fileB': 'repo_fileB_1',
})
with subtest("First backup in repo"):
print(machine.succeed("systemctl cat ${output.backupService}"))
machine.succeed("systemctl start ${output.backupService}")
with subtest("New content"):
for path in sourceDirectories:
machine.succeed(f"""
echo repo_fileA_2 > {path}/fileA
echo repo_fileB_2 > {path}/fileB
""")
assert_files(path, {
f'{path}/fileA': 'repo_fileA_2',
f'{path}/fileB': 'repo_fileB_2',
})
with subtest("Delete content"):
for path in sourceDirectories:
machine.succeed(f"""rm -r {path}/*""")
assert_files(path, {})
with subtest("Restore initial content from repo"):
machine.succeed("""${output.restoreScript} restore latest""")
for path in sourceDirectories:
assert_files(path, {
f'{path}/fileA': 'repo_fileA_1',
f'{path}/fileB': 'repo_fileB_1',
})
'';
};
};
meta.buildDocsInSandbox = false;
}