···
inherit (pkgs) mysql gzip;
+
cfg = config.services.mysqlBackup;
+
defaultUser = "mysqlbackup";
+
${concatMapStringsSep "\n" backupDatabaseScript cfg.databases}
+
if [ -n "$failed" ]; then
+
echo "Backup of database(s) failed:$failed"
+
backupDatabaseScript = db: ''
+
dest="${cfg.location}/${db}.gz"
+
if ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > $dest.tmp; then
+
echo "Backed up to $dest"
+
echo "Failed to back up to $dest"
···
+
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
User to be used to perform backup.
···
+
config = mkIf cfg.enable {
+
users.extraUsers = optionalAttrs (cfg.user == defaultUser) (singleton
+
services.mysql.ensureUsers = [{
+
ensurePermissions = with lib;
+
privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
+
grant = db: nameValuePair "${db}.*" privs;
+
listToAttrs (map grant cfg.databases);
+
timers."mysql-backup" = {
+
description = "Mysql backup timer";
+
wantedBy = [ "timers.target" ];
+
OnCalendar = cfg.calendar;
+
Unit = "mysql-backup.service";
+
services."mysql-backup" = {
+
description = "Mysql backup service";
+
PermissionsStartOnly = true;
+
mkdir -m 0700 -p ${cfg.location}
+
chown -R ${cfg.user} ${cfg.location}