Skip to content

Commit

Permalink
services.mysqlBackup: add option to change compression tool
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Dec 14, 2024
1 parent 67cbf84 commit 9dea91d
Showing 1 changed file with 92 additions and 14 deletions.
106 changes: 92 additions & 14 deletions nixos/modules/services/backup/mysql-backup.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,50 @@
...
}:
let

inherit (pkgs) mariadb gzip;
inherit (pkgs)
mariadb
gzip
xz
zstd
;

cfg = config.services.mysqlBackup;
defaultUser = "mysqlbackup";

compressionPkg =
{
"gzip" = pkgs.gzip;
"xz" = pkgs.xz;
"zstd" = pkgs.zstd;
}
.${cfg.compression};

fileExt =
{
gzip = ".gz";
zstd = ".zst";
xz = ".xz";
}
.${cfg.compression};

validCompressionLevels = {
zstd = range: 1 <= range && range <= 19;
xz = range: 0 <= range && range <= 9;
gzip = range: false; # gzip doesn't use compressionLevel
};

compressionCmd =
let
compressionLevelFlag =
if cfg.compressionLevel != null then "-${toString cfg.compressionLevel}" else "";
in
{
gzip = "${gzip}/bin/gzip -c ${cfg.gzipOptions}";
xz = "${xz}/bin/xz -z -c ${compressionLevelFlag} -";
zstd = "${zstd}/bin/zstd ${compressionLevelFlag} -";
}
.${cfg.compression};

backupScript = ''
set -o pipefail
failed=""
Expand All @@ -20,9 +58,10 @@ let
exit 1
fi
'';

backupDatabaseScript = db: ''
dest="${cfg.location}/${db}.gz"
if ${mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${gzip}/bin/gzip -c ${cfg.gzipOptions} > $dest.tmp; then
dest="${cfg.location}/${db}${fileExt}"
if ${mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${compressionCmd} > $dest.tmp; then
mv $dest.tmp $dest
echo "Backed up to $dest"
else
Expand All @@ -33,12 +72,9 @@ let
'';

in

{
options = {

services.mysqlBackup = {

enable = lib.mkEnableOption "MySQL backups";

calendar = lib.mkOption {
Expand All @@ -49,6 +85,30 @@ in
'';
};

compression = lib.mkOption {
type = lib.types.enum [
"gzip"
"zstd"
"xz"
];
default = "gzip";
description = ''
Compression algorithm to use for database dumps.
Available options: gzip, zstd, xz
'';
};

compressionLevel = lib.mkOption {
type = lib.types.nullOr lib.types.int;
default = null;
description = ''
Compression level to use for zstd or xz.
For zstd: 1-19
For xz: 0-9
Ignored for gzip (use gzipOptions instead)
'';
};

user = lib.mkOption {
type = lib.types.str;
default = defaultUser;
Expand All @@ -69,7 +129,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.
'';
};

Expand All @@ -86,13 +146,31 @@ in
type = lib.types.str;
description = ''
Command line options to use when invoking `gzip`.
Only used when compression is set to "gzip".
'';
};
};

};

config = lib.mkIf cfg.enable {
# assert config to be correct
assertions = [
{
assertion =
cfg.compressionLevel == null || validCompressionLevels.${cfg.compression} 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 does not support compression level option, use gzipOptions instead";
};
in
rangeMsg.${cfg.compression};
}
];

# ensure unix user to be used to perform backup exist.
users.users = lib.optionalAttrs (cfg.user == defaultUser) {
${defaultUser} = {
isSystemUser = true;
Expand All @@ -102,11 +180,14 @@ 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;
ensurePermissions =
with lib;
let
privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
grant = db: lib.nameValuePair "${db}.*" privs;
Expand Down Expand Up @@ -134,10 +215,7 @@ in
};
script = backupScript;
};
tmpfiles.rules = [
"d ${cfg.location} 0700 ${cfg.user} - - -"
];
tmpfiles.rules = [ "d ${cfg.location} 0700 ${cfg.user} - - -" ];
};
};

}

0 comments on commit 9dea91d

Please sign in to comment.