nixos/services.mysqlBackup: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-28 21:18:53 +02:00 committed by Jörg Thalheim
parent 5e67f3e4c8
commit 02145eb692

View file

@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
inherit (pkgs) mariadb gzip; inherit (pkgs) mariadb gzip;
@ -12,7 +9,7 @@ let
backupScript = '' backupScript = ''
set -o pipefail set -o pipefail
failed="" failed=""
${concatMapStringsSep "\n" backupDatabaseScript cfg.databases} ${lib.concatMapStringsSep "\n" backupDatabaseScript cfg.databases}
if [ -n "$failed" ]; then if [ -n "$failed" ]; then
echo "Backup of database(s) failed:$failed" echo "Backup of database(s) failed:$failed"
exit 1 exit 1
@ -20,7 +17,7 @@ let
''; '';
backupDatabaseScript = db: '' backupDatabaseScript = db: ''
dest="${cfg.location}/${db}.gz" dest="${cfg.location}/${db}.gz"
if ${mariadb}/bin/mysqldump ${optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${gzip}/bin/gzip -c ${cfg.gzipOptions} > $dest.tmp; then if ${mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${gzip}/bin/gzip -c ${cfg.gzipOptions} > $dest.tmp; then
mv $dest.tmp $dest mv $dest.tmp $dest
echo "Backed up to $dest" echo "Backed up to $dest"
else else
@ -37,51 +34,51 @@ in
services.mysqlBackup = { services.mysqlBackup = {
enable = mkEnableOption "MySQL backups"; enable = lib.mkEnableOption "MySQL backups";
calendar = mkOption { calendar = lib.mkOption {
type = types.str; type = lib.types.str;
default = "01:15:00"; default = "01:15:00";
description = '' description = ''
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second). Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
''; '';
}; };
user = mkOption { user = lib.mkOption {
type = types.str; type = lib.types.str;
default = defaultUser; default = defaultUser;
description = '' description = ''
User to be used to perform backup. User to be used to perform backup.
''; '';
}; };
databases = mkOption { databases = lib.mkOption {
default = []; default = [];
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
description = '' description = ''
List of database names to dump. List of database names to dump.
''; '';
}; };
location = mkOption { location = lib.mkOption {
type = types.path; type = lib.types.path;
default = "/var/backup/mysql"; default = "/var/backup/mysql";
description = '' description = ''
Location to put the gzipped MySQL database dumps. Location to put the gzipped MySQL database dumps.
''; '';
}; };
singleTransaction = mkOption { singleTransaction = lib.mkOption {
default = false; default = false;
type = types.bool; type = lib.types.bool;
description = '' description = ''
Whether to create database dump in a single transaction Whether to create database dump in a single transaction
''; '';
}; };
gzipOptions = mkOption { gzipOptions = lib.mkOption {
default = "--no-name --rsyncable"; default = "--no-name --rsyncable";
type = types.str; type = lib.types.str;
description = '' description = ''
Command line options to use when invoking `gzip`. Command line options to use when invoking `gzip`.
''; '';
@ -90,8 +87,8 @@ in
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
users.users = optionalAttrs (cfg.user == defaultUser) { users.users = lib.optionalAttrs (cfg.user == defaultUser) {
${defaultUser} = { ${defaultUser} = {
isSystemUser = true; isSystemUser = true;
createHome = false; createHome = false;
@ -105,9 +102,9 @@ in
ensurePermissions = with lib; ensurePermissions = with lib;
let let
privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES"; privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
grant = db: nameValuePair "${db}.*" privs; grant = db: lib.nameValuePair "${db}.*" privs;
in in
listToAttrs (map grant cfg.databases); lib.listToAttrs (map grant cfg.databases);
}]; }];
systemd = { systemd = {