2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2009-10-07 11:55:36 +00:00
|
|
|
let
|
2010-09-13 15:41:38 +00:00
|
|
|
|
2021-06-04 14:08:41 +00:00
|
|
|
inherit (pkgs) mariadb gzip;
|
2009-10-07 11:55:36 +00:00
|
|
|
|
2017-09-23 14:58:07 +00:00
|
|
|
cfg = config.services.mysqlBackup;
|
|
|
|
defaultUser = "mysqlbackup";
|
|
|
|
|
|
|
|
backupScript = ''
|
|
|
|
set -o pipefail
|
|
|
|
failed=""
|
2024-08-28 19:18:53 +00:00
|
|
|
${lib.concatMapStringsSep "\n" backupDatabaseScript cfg.databases}
|
2017-09-23 14:58:07 +00:00
|
|
|
if [ -n "$failed" ]; then
|
|
|
|
echo "Backup of database(s) failed:$failed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
backupDatabaseScript = db: ''
|
|
|
|
dest="${cfg.location}/${db}.gz"
|
2024-08-28 19:18:53 +00:00
|
|
|
if ${mariadb}/bin/mysqldump ${lib.optionalString cfg.singleTransaction "--single-transaction"} ${db} | ${gzip}/bin/gzip -c ${cfg.gzipOptions} > $dest.tmp; then
|
2017-09-23 14:58:07 +00:00
|
|
|
mv $dest.tmp $dest
|
|
|
|
echo "Backed up to $dest"
|
|
|
|
else
|
|
|
|
echo "Failed to back up to $dest"
|
|
|
|
rm -f $dest.tmp
|
|
|
|
failed="$failed ${db}"
|
|
|
|
fi
|
2011-09-14 18:20:50 +00:00
|
|
|
'';
|
2009-10-07 11:55:36 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-07 11:55:36 +00:00
|
|
|
services.mysqlBackup = {
|
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
enable = lib.mkEnableOption "MySQL backups";
|
2009-10-07 11:55:36 +00:00
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
calendar = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2017-09-23 14:58:07 +00:00
|
|
|
default = "01:15:00";
|
2009-10-07 11:55:36 +00:00
|
|
|
description = ''
|
2017-09-23 14:58:07 +00:00
|
|
|
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
|
2009-10-07 11:55:36 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2017-09-23 14:58:07 +00:00
|
|
|
default = defaultUser;
|
2009-12-14 17:57:37 +00:00
|
|
|
description = ''
|
|
|
|
User to be used to perform backup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
databases = lib.mkOption {
|
2009-10-07 11:55:36 +00:00
|
|
|
default = [];
|
2024-08-28 19:18:53 +00:00
|
|
|
type = lib.types.listOf lib.types.str;
|
2009-10-07 11:55:36 +00:00
|
|
|
description = ''
|
|
|
|
List of database names to dump.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
location = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2009-10-07 11:55:36 +00:00
|
|
|
default = "/var/backup/mysql";
|
|
|
|
description = ''
|
2009-10-07 11:58:23 +00:00
|
|
|
Location to put the gzipped MySQL database dumps.
|
2009-10-07 11:55:36 +00:00
|
|
|
'';
|
|
|
|
};
|
2010-11-04 08:13:57 +00:00
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
singleTransaction = lib.mkOption {
|
2010-11-04 08:13:57 +00:00
|
|
|
default = false;
|
2024-08-28 19:18:53 +00:00
|
|
|
type = lib.types.bool;
|
2010-11-04 08:13:57 +00:00
|
|
|
description = ''
|
|
|
|
Whether to create database dump in a single transaction
|
|
|
|
'';
|
|
|
|
};
|
2024-03-30 09:16:19 +00:00
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
gzipOptions = lib.mkOption {
|
2024-03-30 09:16:19 +00:00
|
|
|
default = "--no-name --rsyncable";
|
2024-08-28 19:18:53 +00:00
|
|
|
type = lib.types.str;
|
2024-03-30 09:16:19 +00:00
|
|
|
description = ''
|
|
|
|
Command line options to use when invoking `gzip`.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 11:55:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:53 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
users.users = lib.optionalAttrs (cfg.user == defaultUser) {
|
2019-09-14 17:51:29 +00:00
|
|
|
${defaultUser} = {
|
2017-09-23 14:58:07 +00:00
|
|
|
isSystemUser = true;
|
|
|
|
createHome = false;
|
|
|
|
home = cfg.location;
|
|
|
|
group = "nogroup";
|
2019-09-14 17:51:29 +00:00
|
|
|
};
|
|
|
|
};
|
2017-09-23 14:58:07 +00:00
|
|
|
|
|
|
|
services.mysql.ensureUsers = [{
|
|
|
|
name = cfg.user;
|
|
|
|
ensurePermissions = with lib;
|
|
|
|
let
|
|
|
|
privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
|
2024-08-28 19:18:53 +00:00
|
|
|
grant = db: lib.nameValuePair "${db}.*" privs;
|
2017-09-23 14:58:07 +00:00
|
|
|
in
|
2024-08-28 19:18:53 +00:00
|
|
|
lib.listToAttrs (map grant cfg.databases);
|
2017-09-23 14:58:07 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
systemd = {
|
2019-08-13 21:52:01 +00:00
|
|
|
timers.mysql-backup = {
|
2017-09-23 14:58:07 +00:00
|
|
|
description = "Mysql backup timer";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.calendar;
|
|
|
|
AccuracySec = "5m";
|
|
|
|
Unit = "mysql-backup.service";
|
|
|
|
};
|
|
|
|
};
|
2019-08-13 21:52:01 +00:00
|
|
|
services.mysql-backup = {
|
2022-02-18 13:36:51 +00:00
|
|
|
description = "MySQL backup service";
|
2017-09-23 14:58:07 +00:00
|
|
|
enable = true;
|
|
|
|
serviceConfig = {
|
2022-02-18 13:36:51 +00:00
|
|
|
Type = "oneshot";
|
2017-09-23 14:58:07 +00:00
|
|
|
User = cfg.user;
|
|
|
|
};
|
|
|
|
script = backupScript;
|
|
|
|
};
|
2019-02-23 22:55:54 +00:00
|
|
|
tmpfiles.rules = [
|
|
|
|
"d ${cfg.location} 0700 ${cfg.user} - - -"
|
|
|
|
];
|
2017-09-23 14:58:07 +00:00
|
|
|
};
|
2009-10-07 11:55:36 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-07 11:55:36 +00:00
|
|
|
}
|