|
8 | 8 | cfg = config.services.mysqlBackup;
|
9 | 9 | defaultUser = "mysqlbackup";
|
10 | 10 |
|
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 | + }; |
31 | 36 | };
|
32 | 37 |
|
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; |
45 | 44 |
|
46 | 45 | backupScript = ''
|
47 | 46 | set -o pipefail
|
|
54 | 53 | '';
|
55 | 54 |
|
56 | 55 | backupDatabaseScript = db: ''
|
57 |
| - dest="${cfg.location}/${db}${fileExt}" |
| 56 | + dest="${cfg.location}/${db}${selectedAlg.ext}" |
58 | 57 | if ${pkgs.mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${compressionCmd} > $dest.tmp; then
|
59 | 58 | mv $dest.tmp $dest
|
60 | 59 | echo "Backed up to $dest"
|
|
80 | 79 | };
|
81 | 80 |
|
82 | 81 | compressionAlg = lib.mkOption {
|
83 |
| - type = lib.types.enum [ |
84 |
| - "gzip" |
85 |
| - "zstd" |
86 |
| - "xz" |
87 |
| - ]; |
| 82 | + type = lib.types.enum (lib.attrNames compressionAlgs); |
88 | 83 | default = "gzip";
|
89 | 84 | description = ''
|
90 | 85 | Compression algorithm to use for database dumps.
|
|
95 | 90 | type = lib.types.nullOr lib.types.int;
|
96 | 91 | default = null;
|
97 | 92 | 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) |
102 | 100 | '';
|
103 | 101 | };
|
104 | 102 |
|
|
150 | 148 | # assert config to be correct
|
151 | 149 | assertions = [
|
152 | 150 | {
|
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}"; |
164 | 153 | }
|
165 | 154 | ];
|
166 | 155 |
|
|
175 | 164 | };
|
176 | 165 |
|
177 | 166 | # add the compression tool to the system environment.
|
178 |
| - environment.systemPackages = [ compressionPkg ]; |
| 167 | + environment.systemPackages = [ selectedAlg.pkg ]; |
179 | 168 |
|
180 | 169 | # ensure database user to be used to perform backup exist.
|
181 | 170 | services.mysql.ensureUsers = [
|
|
0 commit comments