···
inherit (pkgs) mysql gzip;
9
-
cfg = config.services.mysqlBackup ;
10
-
location = cfg.location ;
11
-
mysqlBackupCron = db : ''
12
-
${cfg.period} ${cfg.user} ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
9
+
cfg = config.services.mysqlBackup;
10
+
defaultUser = "mysqlbackup";
15
+
${concatMapStringsSep "\n" backupDatabaseScript cfg.databases}
16
+
if [ -n "$failed" ]; then
17
+
echo "Backup of database(s) failed:$failed"
21
+
backupDatabaseScript = db: ''
22
+
dest="${cfg.location}/${db}.gz"
23
+
if ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > $dest.tmp; then
25
+
echo "Backed up to $dest"
27
+
echo "Failed to back up to $dest"
29
+
failed="$failed ${db}"
···
30
-
default = "15 01 * * *";
47
+
calendar = mkOption {
49
+
default = "01:15:00";
32
-
This option defines (in the format used by cron) when the
33
-
databases should be dumped.
34
-
The default is to update at 01:15 (at night) every day.
51
+
Configured when to run the backup service systemd unit (DayOfWeek Year-Month-Day Hour:Minute:Second).
56
+
default = defaultUser;
User to be used to perform backup.
···
69
-
config = mkIf config.services.mysqlBackup.enable {
71
-
services.cron.systemCronJobs = map mysqlBackupCron config.services.mysqlBackup.databases;
86
+
config = mkIf cfg.enable {
87
+
users.extraUsers = optionalAttrs (cfg.user == defaultUser) (singleton
88
+
{ name = defaultUser;
89
+
isSystemUser = true;
91
+
home = cfg.location;
73
-
system.activationScripts.mysqlBackup = stringAfter [ "stdio" "users" ]
75
-
mkdir -m 0700 -p ${config.services.mysqlBackup.location}
76
-
chown ${config.services.mysqlBackup.user} ${config.services.mysqlBackup.location}
95
+
services.mysql.ensureUsers = [{
97
+
ensurePermissions = with lib;
99
+
privs = "SELECT, SHOW VIEW, TRIGGER, LOCK TABLES";
100
+
grant = db: nameValuePair "${db}.*" privs;
102
+
listToAttrs (map grant cfg.databases);
106
+
timers."mysql-backup" = {
107
+
description = "Mysql backup timer";
108
+
wantedBy = [ "timers.target" ];
110
+
OnCalendar = cfg.calendar;
111
+
AccuracySec = "5m";
112
+
Unit = "mysql-backup.service";
115
+
services."mysql-backup" = {
116
+
description = "Mysql backup service";
120
+
PermissionsStartOnly = true;
123
+
mkdir -m 0700 -p ${cfg.location}
124
+
chown -R ${cfg.user} ${cfg.location}
126
+
script = backupScript;