Skip to content

Commit 2a8bbe0

Browse files
committed
services.mysqlBackup: apply suggested refactoring
1 parent fc842d8 commit 2a8bbe0

File tree

1 file changed

+43
-54
lines changed

1 file changed

+43
-54
lines changed

nixos/modules/services/backup/mysql-backup.nix

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,39 @@ let
88
cfg = config.services.mysqlBackup;
99
defaultUser = "mysqlbackup";
1010

11-
compressionPkg =
12-
{
13-
gzip = pkgs.gzip;
14-
xz = pkgs.xz;
15-
zstd = pkgs.zstd;
16-
}
17-
.${cfg.compressionAlg};
18-
19-
fileExt =
20-
{
21-
gzip = ".gz";
22-
zstd = ".zst";
23-
xz = ".xz";
24-
}
25-
.${cfg.compressionAlg};
26-
27-
validCompressionLevels = {
28-
zstd = range: 1 <= range && range <= 19;
29-
xz = range: 0 <= range && range <= 9;
30-
gzip = range: 1 <= range && range <= 9;
11+
compressionAlgs = {
12+
gzip = rec {
13+
pkg = pkgs.gzip;
14+
ext = ".gz";
15+
minLevel = 1;
16+
maxLevel = 9;
17+
cmd = compressionLevelFlag: "${pkg}/bin/gzip -c ${cfg.gzipOptions} ${compressionLevelFlag}";
18+
validLevel = level: minLevel <= level && level <= maxLevel;
19+
};
20+
xz = rec {
21+
pkg = pkgs.xz;
22+
ext = ".xz";
23+
minLevel = 0;
24+
maxLevel = 9;
25+
cmd = compressionLevelFlag: "${pkg}/bin/xz -z -c ${compressionLevelFlag} -";
26+
validLevel = level: minLevel <= level && level <= maxLevel;
27+
};
28+
zstd = rec {
29+
pkg = pkgs.zstd;
30+
ext = ".zst";
31+
minLevel = 1;
32+
maxLevel = 19;
33+
cmd = compressionLevelFlag: "${pkg}/bin/zstd ${compressionLevelFlag} -";
34+
validLevel = level: minLevel <= level && level <= maxLevel;
35+
};
3136
};
3237

33-
compressionCmd =
34-
let
35-
compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) (
36-
"-" + toString cfg.compressionLevel
37-
);
38-
in
39-
{
40-
gzip = "${pkgs.gzip}/bin/gzip -c ${cfg.gzipOptions} ${compressionLevelFlag}";
41-
xz = "${pkgs.xz}/bin/xz -z -c ${compressionLevelFlag} -";
42-
zstd = "${pkgs.zstd}/bin/zstd ${compressionLevelFlag} -";
43-
}
44-
.${cfg.compressionAlg};
38+
compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) (
39+
"-" + toString cfg.compressionLevel
40+
);
41+
42+
selectedAlg = compressionAlgs.${cfg.compressionAlg};
43+
compressionCmd = selectedAlg.cmd compressionLevelFlag;
4544

4645
backupScript = ''
4746
set -o pipefail
@@ -54,7 +53,7 @@ let
5453
'';
5554

5655
backupDatabaseScript = db: ''
57-
dest="${cfg.location}/${db}${fileExt}"
56+
dest="${cfg.location}/${db}${selectedAlg.ext}"
5857
if ${pkgs.mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${compressionCmd} > $dest.tmp; then
5958
mv $dest.tmp $dest
6059
echo "Backed up to $dest"
@@ -80,11 +79,7 @@ in
8079
};
8180

8281
compressionAlg = lib.mkOption {
83-
type = lib.types.enum [
84-
"gzip"
85-
"zstd"
86-
"xz"
87-
];
82+
type = lib.types.enum (lib.attrNames compressionAlgs);
8883
default = "gzip";
8984
description = ''
9085
Compression algorithm to use for database dumps.
@@ -95,10 +90,13 @@ in
9590
type = lib.types.nullOr lib.types.int;
9691
default = null;
9792
description = ''
98-
Compression level to use for gzip, zstd or xz.
99-
For gzip: 1-9 (note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten)
100-
For zstd: 1-19
101-
For xz: 0-9
93+
Compression level to use for ${lib.concatStringsSep ", " (lib.init (lib.attrNames compressionAlgs))} or ${lib.last (lib.attrNames compressionAlgs)}.
94+
${lib.concatStringsSep "\n" (
95+
lib.mapAttrsToList (
96+
name: algo: " For ${name}: ${toString algo.minLevel}-${toString algo.maxLevel}"
97+
) compressionAlgs
98+
)}
99+
(note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten)
102100
'';
103101
};
104102

@@ -150,17 +148,8 @@ in
150148
# assert config to be correct
151149
assertions = [
152150
{
153-
assertion =
154-
cfg.compressionLevel == null || validCompressionLevels.${cfg.compressionAlg} cfg.compressionLevel;
155-
message =
156-
let
157-
rangeMsg = {
158-
"zstd" = "zstd compression level must be between 1 and 19";
159-
"xz" = "xz compression level must be between 0 and 9";
160-
"gzip" = "gzip compression level must be between 1 and 9";
161-
};
162-
in
163-
rangeMsg.${cfg.compressionAlg};
151+
assertion = cfg.compressionLevel == null || selectedAlg.validLevel cfg.compressionLevel;
152+
message = "${cfg.compressionAlg} compression level must be between ${toString selectedAlg.minLevel} and ${toString selectedAlg.maxLevel}";
164153
}
165154
];
166155

@@ -175,7 +164,7 @@ in
175164
};
176165

177166
# add the compression tool to the system environment.
178-
environment.systemPackages = [ compressionPkg ];
167+
environment.systemPackages = [ selectedAlg.pkg ];
179168

180169
# ensure database user to be used to perform backup exist.
181170
services.mysql.ensureUsers = [

0 commit comments

Comments
 (0)