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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
{ config, pkgs, lib, ... }:
let
inherit (lib) mkOption mkIf mkDefault mapAttrsToList flatten hasPrefix
concatMapStringsSep concatStringsSep optionalString filterAttrs
splitString removeSuffix;
inherit (lib.types) bool str int path either enum nullOr listOf attrsOf submodule;
inherit (builtins) isString isBool isInt isList isPath length;
cfg = config.nixsap.apps.mysqlbackup;
privateDir = "/run/mysqlbackup";
mysql = "${pkgs.mariadb.client.bin}/bin/mysql";
mysqldump = "${pkgs.mariadb.client.bin}/bin/mysqldump";
s3cmd = "${pkgs.s3cmd}/bin/s3cmd ${optionalString (cfg.s3cfg != null) "-c '${cfg.s3cfg}'"}";
gpgPubKeys = cfg.encrypt;
gpg = "${pkgs.gpg}/bin/gpg2";
pubring = pkgs.runCommand "pubring.kbx" {} ''
${gpg} --homedir . --import ${toString gpgPubKeys}
cp pubring.kbx $out
'';
default = d: t: mkOption { type = t; default = d; };
explicit = filterAttrs (n: v: n != "_module" && v != null);
mandatory = type: mkOption { inherit type; };
optional = type: mkOption { type = nullOr type; default = null; };
sub = options: submodule { inherit options; } ;
connection = mkOption {
description = "Connection options used by mysqlbackup";
type = sub {
compress = default true bool;
host = mandatory str;
max-allowed-packet = optional int;
password-file = optional path;
port = optional int;
socket = optional path;
ssl = optional bool;
ssl-ca = optional path;
ssl-cert = optional path;
ssl-key = optional path;
ssl-verify-server-cert = optional bool;
user = optional str;
};
};
databases = mkOption {
description = "What to dump and what to ignore";
default = {};
type = sub {
like = mkOption {
description = ''
Databases to dump. MySQL wildcards (_ and %) are supported.
Logical OR is applied to all entries.
'';
type = listOf str;
default = [ "%" ];
example = [ "%\\_live\\_%" ];
};
not-like = mkOption {
description = ''
Databases to skip. MySQL wildcards (_ and %) are supported.
You don't need to specify `performance_schema` or `information_schema`
here, they are always ignored. Logical AND is applied to all entries.
'';
type = listOf str;
default = [];
example = [ "tmp\\_%" "snap\\_%" ];
};
empty-tables-like = mkOption {
description = ''
Tables to ignore. MySQL wildcards (_ and %) are supported.
Note that the schemas of these tables will be dumped anyway.
Each table template can be prefixed with a database template.
In that case it will be applied to matching databases only,
instead of all databases'';
type = listOf str;
default = [];
example = [ "bob%.alice\\_message" ];
};
skip-tables-like = mkOption {
description = ''
Tables to ignore. MySQL wildcards (_ and %) are supported.
Each table template can be prefixed with a database template.
In that case it will be applied to matching databases only,
instead of all databases'';
type = listOf str;
default = [];
example = [ "tmp%" "%\\_backup" ];
};
};
};
server = submodule ({ name, ... }:
{
options = { inherit connection databases; };
config.connection.host = mkDefault name;
}
);
connectionKeys = flatten (mapAttrsToList (_: s: with s.connection; [ password-file ssl-key ]) cfg.servers);
keys = connectionKeys ++ [ cfg.s3cfg ];
showDatabases = name: server: pkgs.writeText "show-databases-${name}.sql" ''
SHOW DATABASES WHERE `Database` NOT IN ('information_schema', 'performance_schema', 'tmp', 'innodb')
AND (${concatMapStringsSep " OR " (e: "`Database` LIKE '${e}'") server.databases.like})
${concatMapStringsSep " " (e: "AND `Database` NOT LIKE '${e}'") server.databases.not-like}
;
'';
defaultsFile = name: server:
let
inc = optionalString (server.connection.password-file != null)
"!include ${privateDir}/cnf/${name}";
show = n: v:
if isBool v then (if v then "1" else "0")
else if isInt v then toString v
else if isString v then "${v}"
else if isPath v then "'${v}'"
else abort "Unrecognized option ${n}";
in pkgs.writeText "my-${name}.cnf"
( concatStringsSep "\n" (
[ "[client]" ]
++ mapAttrsToList (k: v: "${k} = ${show k v}")
(filterAttrs (k: _: k != "password-file") (explicit server.connection))
++ [ "${inc}\n" ]
)
);
listTables = name: server: tables:
let
anyDb = s: if 1 == length (splitString "." s)
then "%.${s}" else s;
query = optionalString (0 < length tables) ''
set -euo pipefail
db="$1"
cat <<SQL | ${mysql} --defaults-file=${defaultsFile name server} -N
SELECT CONCAT(table_schema, '.', table_name) AS tables
FROM information_schema.tables HAVING tables LIKE '$db.%'
AND ( ${concatMapStringsSep " OR " (e: "tables LIKE '${e}'")
(map anyDb tables)} );
SQL
'';
in pkgs.writeBashScript "list-tables-${name}" query;
job = name: server: pkgs.writeBashScript "job-${name}" ''
set -euo pipefail
db=$(basename "$0")
cd "${cfg.dumpDir}/$DATE"
dump="$db@${name},$DATE.mysql.xz"
${if (gpgPubKeys != []) then ''
aim="$dump.gpg"
'' else ''
aim="$dump"
''}
if ! [ -r "$aim" ]; then
{
empty=()
empty+=( $(${listTables name server server.databases.empty-tables-like} "$db") )
if [ ''${#empty[@]} -gt 0 ]; then
tables=( "''${empty[@]/#*./}" )
${mysqldump} --defaults-file=${defaultsFile name server} \
--skip-comments --force --single-transaction \
--no-data "$db" "''${tables[@]}"
fi
empty+=( $(${listTables name server server.databases.skip-tables-like} "$db") )
if [ ''${#empty[@]} -gt 0 ]; then
ignoretables+=( "''${empty[@]/#/--ignore-table=}" )
fi
${mysqldump} --defaults-file=${defaultsFile name server} \
--skip-comments --force --single-transaction \
"''${ignoretables[@]:+''${ignoretables[@]}}" \
"$db"
} | ${pkgs.pxz}/bin/pxz -2 -T2 > "$dump".tmp
${pkgs.xz}/bin/xz -t -v "$dump".tmp
mv "$dump".tmp "$dump"
${optionalString (gpgPubKeys != []) ''
mapfile -t recipient < <(${gpg} --homedir '${privateDir}/gnupg' -k --with-colons --fast-list-mode | \
${pkgs.gawk}/bin/awk -F: '/^pub/{print $5}')
r=( "''${recipient[@]/#/-r}" )
${gpg} --homedir '${privateDir}/gnupg' --batch --no-tty --yes \
"''${r[@]}" --trust-model always \
--compress-algo none \
-v -e "$dump"
rm -f "$dump"
''}
else
echo "$aim exists. Not dumping." >&2
fi
${optionalString (cfg.s3uri != null) ''
remote="${removeSuffix "/" cfg.s3uri}/$DATE/$aim"
if ! ${s3cmd} ls "$remote" | ${pkgs.gnugrep}/bin/grep -qF "/$aim"; then
${s3cmd} put "$aim" "$remote"
else
echo "$remote exists. Not uploading." >&2
fi
''}
'';
mkJobs = name: server: pkgs.writeBashScript "mkjobs-${name}" ''
set -euo pipefail
mkdir -p '${privateDir}/jobs/${name}'
${mysql} --defaults-file=${defaultsFile name server} -N < ${showDatabases name server} \
| while read -r db
do
ln -svf ${job name server} "${privateDir}/jobs/${name}/$db"
done
'';
preStart = ''
mkdir --mode=0750 -p '${cfg.dumpDir}'
chown -R ${cfg.user}:${cfg.user} '${cfg.dumpDir}'
chmod -R u=rwX,g=rX,o= ${cfg.dumpDir}
rm -rf '${privateDir}'
mkdir --mode=0700 -p '${privateDir}'
chown ${cfg.user}:${cfg.user} '${privateDir}'
'';
main = pkgs.writeBashScriptBin "mysqlbackup" ''
set -euo pipefail
umask 0027
DATE=$(date --iso-8601)
HOME='${privateDir}'
PARALLEL_SHELL=${pkgs.bash}/bin/bash
export DATE
export HOME
export PARALLEL_SHELL
clean() {
${pkgs.findutils}/bin/find '${cfg.dumpDir}' -type f -name '*.tmp' -delete || true
}
listSets() {
${pkgs.findutils}/bin/find '${cfg.dumpDir}' \
-maxdepth 1 -mindepth 1 -type d -name '????-??-??' \
| sort -V
}
enoughStorage() {
local n
local used
local total
local avg
local p
n=$(listSets | wc -l)
used=$(du -x -s --block-size=1M '${cfg.dumpDir}' | cut -f1)
total=$(df --output=size --block-size=1M '${cfg.dumpDir}' | tail -n 1)
if [ "$n" -eq 0 ]; then
echo "no sets" >&2
return 0
fi
avg=$(( used / n ))
p=$(( 100 * avg * (n + 1) / total ))
printf "estimated storage: %d of %d MiB (%d%%, max ${toString cfg.storage}%%)\n" \
"$((used + avg))" "$total" "$p" >&2
if [ "$p" -le ${toString cfg.storage} ]; then
return 0
else
return 1
fi
}
clean
listSets | head -n -${toString (cfg.slots - 1)} \
| ${pkgs.findutils}/bin/xargs --no-run-if-empty rm -rfv \
|| true
while ! enoughStorage; do
listSets | head -n 1 \
| ${pkgs.findutils}/bin/xargs --no-run-if-empty rm -rfv \
|| true
done
mkdir -p "${cfg.dumpDir}/$DATE"
mkdir -p '${privateDir}/cnf'
mkdir -p '${privateDir}/jobs'
${optionalString (gpgPubKeys != []) ''
# shellcheck disable=SC2174
mkdir --mode=0700 -p '${privateDir}/gnupg'
ln -sf ${pubring} '${privateDir}/gnupg/pubring.kbx'
''}
${concatStringsSep "\n" (
mapAttrsToList (n: s: ''
printf '[client]\npassword=' > '${privateDir}/cnf/${n}'
cat '${s.connection.password-file}' >> '${privateDir}/cnf/${n}'
'') (filterAttrs (_: s: s.connection.password-file != null) cfg.servers)
)}
failedServers=0
{
cat <<'LIST'
${concatStringsSep "\n" (mapAttrsToList (mkJobs) cfg.servers)}
LIST
} | ${pkgs.parallel}/bin/parallel \
--halt-on-error 0 \
--jobs 100% \
--line-buffer \
--no-notice \
--no-run-if-empty \
--retries 2 \
--shuf \
--tagstr '* {}:' \
--timeout ${toString (10 * 60)} \
|| failedServers=$?
failedJobs=0
log="${cfg.dumpDir}/$DATE/joblog.txt"
{
cd '${privateDir}/jobs' && ${pkgs.findutils}/bin/find . -type l -printf '%P\n';
} | ${pkgs.parallel}/bin/parallel \
--halt-on-error 0 \
--joblog "$log" \
--jobs '${toString cfg.jobs}' \
--line-buffer \
--no-notice \
--no-run-if-empty \
--retries 2 \
--tagstr '* {}:' \
--timeout ${toString (6 * 60 * 60)} \
'${privateDir}/jobs/{}' || failedJobs=$?
cat "$log"
clean
du -sh "${cfg.dumpDir}/$DATE" || true
if [ "$failedServers" -gt "$failedJobs" ]; then
exit "$failedServers"
else
exit "$failedJobs"
fi
'';
in {
options.nixsap.apps.mysqlbackup = {
user = mkOption {
description = "User to run as";
default = "mysqlbackup";
type = str;
};
startAt = mkOption {
description = "Time to start (systemd format)";
default = "02:00";
type = str;
};
dumpDir = mkOption {
description = "Directory to save dumps in";
default = "/mysqlbackup";
type = path;
};
slots = mkOption {
description = ''
How many backup sets should be kept locally.
However, old sets will be removed anyway if storage
constraints apply.
'';
default = 60;
type = int;
};
storage = mkOption {
description = ''
Percent of storage backups can occupy.
'';
default = 75;
type = int;
};
encrypt = mkOption {
description = "Public GPG key(s) for encrypting the dumps";
default = [ ];
type = listOf path;
};
servers = mkOption {
default = {};
type = attrsOf server;
};
jobs = mkOption {
description = ''
Number of jobs (mysqldump) to run in parallel.
In the format of GNU Parallel, e. g. "100%", -1. +3, 7, etc.
'';
default = "50%";
type = either int str;
};
s3cfg = mkOption {
description = "s3cmd config file (secret)";
type = nullOr path;
default = null;
};
s3uri = mkOption {
description = "S3 bucket URI with prefix in s3cmd format";
type = nullOr str;
default = null;
example = "s3://backups/nightly";
};
};
config = mkIf (cfg.servers != {}) {
nixsap.system.users.daemons = [ cfg.user ];
nixsap.deployment.keyrings.${cfg.user} = keys;
systemd.services.mysqlbackup = {
description = "MySQL backup";
after = [ "local-fs.target" "keys.target" "network.target" ];
wants = [ "keys.target" ];
startAt = cfg.startAt;
inherit preStart;
serviceConfig = {
ExecStart = "${main}/bin/mysqlbackup";
User = cfg.user;
PermissionsStartOnly = true;
};
};
};
}
|