2010-09-13 17:41:38 +02:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
2009-10-07 13:55:36 +02:00
|
|
|
|
|
|
|
let
|
2010-09-13 17:41:38 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
inherit (pkgs) mysql gzip;
|
|
|
|
|
2010-11-04 09:13:57 +01:00
|
|
|
cfg = config.services.mysqlBackup ;
|
|
|
|
location = cfg.location ;
|
2009-10-07 13:55:36 +02:00
|
|
|
mysqlBackupCron = db : ''
|
2010-11-04 09:13:57 +01:00
|
|
|
${cfg.period} ${cfg.user} ${mysql}/bin/mysqldump ${if cfg.singleTransaction then "--single-transaction" else ""} ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
|
2011-09-14 20:20:50 +02:00
|
|
|
'';
|
2009-10-07 13:55:36 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
services.mysqlBackup = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable MySQL backups.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
period = mkOption {
|
|
|
|
default = "15 01 * * *";
|
|
|
|
description = ''
|
|
|
|
This option defines (in the format used by cron) when the
|
|
|
|
databases should be dumped.
|
|
|
|
The default is to update at 01:15 (at night) every day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-12-14 18:57:37 +01:00
|
|
|
user = mkOption {
|
|
|
|
default = "mysql";
|
|
|
|
description = ''
|
|
|
|
User to be used to perform backup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of database names to dump.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
location = mkOption {
|
|
|
|
default = "/var/backup/mysql";
|
|
|
|
description = ''
|
2009-10-07 13:58:23 +02:00
|
|
|
Location to put the gzipped MySQL database dumps.
|
2009-10-07 13:55:36 +02:00
|
|
|
'';
|
|
|
|
};
|
2010-11-04 09:13:57 +01:00
|
|
|
|
|
|
|
singleTransaction = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to create database dump in a single transaction
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 13:55:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.mysqlBackup.enable {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2010-09-13 17:41:38 +02:00
|
|
|
services.cron.systemCronJobs = map mysqlBackupCron config.services.mysqlBackup.databases;
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2012-03-15 15:21:17 +01:00
|
|
|
system.activationScripts.mysqlBackup = stringAfter [ "stdio" "users" ]
|
2010-09-13 17:41:38 +02:00
|
|
|
''
|
|
|
|
mkdir -m 0700 -p ${config.services.mysqlBackup.location}
|
|
|
|
chown ${config.services.mysqlBackup.user} ${config.services.mysqlBackup.location}
|
|
|
|
'';
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
}
|