services.mysqlBackup: add option to change compression tool

This commit is contained in:
6543 2024-12-30 20:33:18 +01:00
parent 459a63f20a
commit ec9d4c4ae6

View file

@ -8,6 +8,41 @@ 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;
};
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};
backupScript = ''
set -o pipefail
failed=""
@ -17,9 +52,10 @@ let
exit 1
fi
'';
backupDatabaseScript = db: ''
dest="${cfg.location}/${db}.gz"
if ${pkgs.mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${pkgs.gzip}/bin/gzip -c ${cfg.gzipOptions} > $dest.tmp; then
dest="${cfg.location}/${db}${fileExt}"
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"
else
@ -43,6 +79,29 @@ in
'';
};
compressionAlg = lib.mkOption {
type = lib.types.enum [
"gzip"
"zstd"
"xz"
];
default = "gzip";
description = ''
Compression algorithm to use for database dumps.
'';
};
compressionLevel = lib.mkOption {
type = lib.types.nullOr lib.types.int;
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
'';
};
user = lib.mkOption {
type = lib.types.str;
default = defaultUser;
@ -63,7 +122,7 @@ in
type = lib.types.path;
default = "/var/backup/mysql";
description = ''
Location to put the gzipped MySQL database dumps.
Location to put the compressed MySQL database dumps.
'';
};
@ -80,13 +139,32 @@ in
type = lib.types.str;
description = ''
Command line options to use when invoking `gzip`.
Only used when compression is set to "gzip".
If compression level is specified both here and in compressionLevel, the compressionLevel value will take precedence.
'';
};
};
};
config = lib.mkIf cfg.enable {
# 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};
}
];
# ensure unix user to be used to perform backup exist.
users.users = lib.optionalAttrs (cfg.user == defaultUser) {
${defaultUser} = {
isSystemUser = true;
@ -96,6 +174,10 @@ in
};
};
# add the compression tool to the system environment.
environment.systemPackages = [ compressionPkg ];
# ensure database user to be used to perform backup exist.
services.mysql.ensureUsers = [
{
name = cfg.user;
@ -132,5 +214,4 @@ in
];
};
};
}