Skip to content

Commit

Permalink
services.mysqlBackup: apply suggested refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Jan 8, 2025
1 parent fc842d8 commit 579ba29
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions nixos/modules/services/backup/mysql-backup.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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.
Expand All @@ -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)
'';
};

Expand Down Expand Up @@ -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;
}
];

Expand All @@ -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 = [
Expand Down

0 comments on commit 579ba29

Please sign in to comment.