mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-06-12 20:55:31 +03:00
services.mysqlBackup: apply suggested refactoring
This commit is contained in:
parent
8b4703426c
commit
085ebf0376
1 changed files with 40 additions and 54 deletions
|
@ -8,40 +8,36 @@ let
|
||||||
cfg = config.services.mysqlBackup;
|
cfg = config.services.mysqlBackup;
|
||||||
defaultUser = "mysqlbackup";
|
defaultUser = "mysqlbackup";
|
||||||
|
|
||||||
compressionPkg =
|
compressionAlgs = {
|
||||||
{
|
gzip = rec {
|
||||||
gzip = pkgs.gzip;
|
pkg = pkgs.gzip;
|
||||||
xz = pkgs.xz;
|
ext = ".gz";
|
||||||
zstd = pkgs.zstd;
|
minLevel = 1;
|
||||||
}
|
maxLevel = 9;
|
||||||
.${cfg.compressionAlg};
|
cmd = compressionLevelFlag: "${pkg}/bin/gzip -c ${cfg.gzipOptions} ${compressionLevelFlag}";
|
||||||
|
};
|
||||||
fileExt =
|
xz = rec {
|
||||||
{
|
pkg = pkgs.xz;
|
||||||
gzip = ".gz";
|
ext = ".xz";
|
||||||
zstd = ".zst";
|
minLevel = 0;
|
||||||
xz = ".xz";
|
maxLevel = 9;
|
||||||
}
|
cmd = compressionLevelFlag: "${pkg}/bin/xz -z -c ${compressionLevelFlag} -";
|
||||||
.${cfg.compressionAlg};
|
};
|
||||||
|
zstd = rec {
|
||||||
validCompressionLevels = {
|
pkg = pkgs.zstd;
|
||||||
zstd = range: 1 <= range && range <= 19;
|
ext = ".zst";
|
||||||
xz = range: 0 <= range && range <= 9;
|
minLevel = 1;
|
||||||
gzip = range: 1 <= range && range <= 9;
|
maxLevel = 19;
|
||||||
|
cmd = compressionLevelFlag: "${pkg}/bin/zstd ${compressionLevelFlag} -";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
compressionCmd =
|
compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) (
|
||||||
let
|
"-" + toString cfg.compressionLevel
|
||||||
compressionLevelFlag = lib.optionalString (cfg.compressionLevel != null) (
|
);
|
||||||
"-" + toString cfg.compressionLevel
|
|
||||||
);
|
selectedAlg = compressionAlgs.${cfg.compressionAlg};
|
||||||
in
|
compressionCmd = selectedAlg.cmd compressionLevelFlag;
|
||||||
{
|
|
||||||
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};
|
|
||||||
|
|
||||||
backupScript = ''
|
backupScript = ''
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
@ -54,7 +50,7 @@ let
|
||||||
'';
|
'';
|
||||||
|
|
||||||
backupDatabaseScript = db: ''
|
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
|
if ${pkgs.mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${compressionCmd} > $dest.tmp; then
|
||||||
mv $dest.tmp $dest
|
mv $dest.tmp $dest
|
||||||
echo "Backed up to $dest"
|
echo "Backed up to $dest"
|
||||||
|
@ -80,11 +76,7 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
compressionAlg = lib.mkOption {
|
compressionAlg = lib.mkOption {
|
||||||
type = lib.types.enum [
|
type = lib.types.enum (lib.attrNames compressionAlgs);
|
||||||
"gzip"
|
|
||||||
"zstd"
|
|
||||||
"xz"
|
|
||||||
];
|
|
||||||
default = "gzip";
|
default = "gzip";
|
||||||
description = ''
|
description = ''
|
||||||
Compression algorithm to use for database dumps.
|
Compression algorithm to use for database dumps.
|
||||||
|
@ -95,10 +87,13 @@ in
|
||||||
type = lib.types.nullOr lib.types.int;
|
type = lib.types.nullOr lib.types.int;
|
||||||
default = null;
|
default = null;
|
||||||
description = ''
|
description = ''
|
||||||
Compression level to use for gzip, zstd or xz.
|
Compression level to use for ${lib.concatStringsSep ", " (lib.init (lib.attrNames compressionAlgs))} or ${lib.last (lib.attrNames compressionAlgs)}.
|
||||||
For gzip: 1-9 (note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten)
|
${lib.concatStringsSep "\n" (
|
||||||
For zstd: 1-19
|
lib.mapAttrsToList (
|
||||||
For xz: 0-9
|
name: algo: " For ${name}: ${toString algo.minLevel}-${toString algo.maxLevel}"
|
||||||
|
) compressionAlgs
|
||||||
|
)}
|
||||||
|
(note: if compression level is also specified in gzipOptions, the gzipOptions value will be overwritten)
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -150,17 +145,8 @@ in
|
||||||
# assert config to be correct
|
# assert config to be correct
|
||||||
assertions = [
|
assertions = [
|
||||||
{
|
{
|
||||||
assertion =
|
assertion = cfg.compressionLevel == null || selectedAlg.minLevel <= cfg.compressionLevel && cfg.compressionLevel <= selectedAlg.maxLevel;
|
||||||
cfg.compressionLevel == null || validCompressionLevels.${cfg.compressionAlg} cfg.compressionLevel;
|
message = "${cfg.compressionAlg} compression level must be between ${toString selectedAlg.minLevel} and ${toString selectedAlg.maxLevel}";
|
||||||
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};
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -175,7 +161,7 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
# add the compression tool to the system environment.
|
# 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.
|
# ensure database user to be used to perform backup exist.
|
||||||
services.mysql.ensureUsers = [
|
services.mysql.ensureUsers = [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue