From 579ba2916c1960852969233f32ff85c7ad147574 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 8 Jan 2025 14:41:02 +0100 Subject: [PATCH] services.mysqlBackup: apply suggested refactoring --- .../modules/services/backup/mysql-backup.nix | 96 +++++++++---------- 1 file changed, 43 insertions(+), 53 deletions(-) diff --git a/nixos/modules/services/backup/mysql-backup.nix b/nixos/modules/services/backup/mysql-backup.nix index ef6e94ddd1d58..acdab2dd892e2 100644 --- a/nixos/modules/services/backup/mysql-backup.nix +++ b/nixos/modules/services/backup/mysql-backup.nix @@ -8,40 +8,42 @@ let cfg = config.services.mysqlBackup; defaultUser = "mysqlbackup"; - compressionPkg = - { - gzip = pkgs.gzip; - xz = pkgs.xz; - zstd = pkgs.zstd; - } - .${cfg.compressionAlg}; - - fileExt = - { - gzip = ".gz"; - zstd = ".zst"; - xz = ".xz"; - } - .${cfg.compressionAlg}; - - validCompressionLevels = { - zstd = range: 1 <= range && range <= 19; - xz = range: 0 <= range && range <= 9; - gzip = range: 1 <= range && range <= 9; + compressionAlgs = { + gzip = rec { + pkg = pkgs.gzip; + ext = ".gz"; + minLevel = 1; + maxLevel = 9; + cmd = compressionLevelFlag: "${pkg}/bin/gzip -c ${cfg.gzipOptions} ${compressionLevelFlag}"; + validLevel = level: minLevel <= level && level <= maxLevel; + rangeMsg = "gzip compression level must be between ${toString minLevel} and ${toString maxLevel}"; + }; + xz = rec { + pkg = pkgs.xz; + ext = ".xz"; + minLevel = 0; + maxLevel = 9; + cmd = compressionLevelFlag: "${pkg}/bin/xz -z -c ${compressionLevelFlag} -"; + validLevel = level: minLevel <= level && level <= maxLevel; + rangeMsg = "xz compression level must be between ${toString minLevel} and ${toString maxLevel}"; + }; + zstd = rec { + pkg = pkgs.zstd; + ext = ".zst"; + minLevel = 1; + maxLevel = 19; + cmd = compressionLevelFlag: "${pkg}/bin/zstd ${compressionLevelFlag} -"; + validLevel = level: minLevel <= level && level <= maxLevel; + rangeMsg = "zstd compression level must be between ${toString minLevel} and ${toString maxLevel}"; + }; }; - compressionCmd = - let - compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) ( - "-" + toString cfg.compressionLevel - ); - in - { - gzip = "${pkgs.gzip}/bin/gzip -c ${cfg.gzipOptions} ${compressionLevelFlag}"; - xz = "${pkgs.xz}/bin/xz -z -c ${compressionLevelFlag} -"; - zstd = "${pkgs.zstd}/bin/zstd ${compressionLevelFlag} -"; - } - .${cfg.compressionAlg}; + compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) ( + "-" + toString cfg.compressionLevel + ); + + selectedAlg = compressionAlgs.${cfg.compressionAlg}; + compressionCmd = selectedAlg.cmd compressionLevelFlag; backupScript = '' set -o pipefail @@ -54,7 +56,7 @@ let ''; backupDatabaseScript = db: '' - dest="${cfg.location}/${db}${fileExt}" + dest="${cfg.location}/${db}${selectedAlg.ext}" if ${pkgs.mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${compressionCmd} > $dest.tmp; then mv $dest.tmp $dest echo "Backed up to $dest" @@ -80,11 +82,7 @@ in }; compressionAlg = lib.mkOption { - type = lib.types.enum [ - "gzip" - "zstd" - "xz" - ]; + type = lib.types.enum (lib.attrNames compressionAlgs); default = "gzip"; description = '' Compression algorithm to use for database dumps. @@ -96,9 +94,10 @@ in default = null; description = '' Compression level to use for gzip, zstd or xz. - For gzip: 1-9 (note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten) - For zstd: 1-19 - For xz: 0-9 + For gzip: ${toString compressionAlgs.gzip.minLevel}-${toString compressionAlgs.gzip.maxLevel} + For zstd: ${toString compressionAlgs.zstd.minLevel}-${toString compressionAlgs.zstd.maxLevel} + For xz: ${toString compressionAlgs.xz.minLevel}-${toString compressionAlgs.xz.maxLevel} + (note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten) ''; }; @@ -150,17 +149,8 @@ in # assert config to be correct assertions = [ { - assertion = - cfg.compressionLevel == null || validCompressionLevels.${cfg.compressionAlg} cfg.compressionLevel; - message = - let - rangeMsg = { - "zstd" = "zstd compression level must be between 1 and 19"; - "xz" = "xz compression level must be between 0 and 9"; - "gzip" = "gzip compression level must be between 1 and 9"; - }; - in - rangeMsg.${cfg.compressionAlg}; + assertion = cfg.compressionLevel == null || selectedAlg.validLevel cfg.compressionLevel; + message = selectedAlg.rangeMsg; } ]; @@ -175,7 +165,7 @@ in }; # add the compression tool to the system environment. - environment.systemPackages = [ compressionPkg ]; + environment.systemPackages = [ selectedAlg.pkg ]; # ensure database user to be used to perform backup exist. services.mysql.ensureUsers = [