2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 16:41:38 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-10-07 12:55:36 +01:00
|
|
|
|
|
|
|
let
|
2010-09-13 16:41:38 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
inherit (pkgs) mysql gzip;
|
|
|
|
|
2010-11-04 08:13:57 +00:00
|
|
|
cfg = config.services.mysqlBackup ;
|
|
|
|
location = cfg.location ;
|
2009-10-07 12:55:36 +01:00
|
|
|
mysqlBackupCron = db : ''
|
2010-11-04 08:13:57 +00: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 19:20:50 +01:00
|
|
|
'';
|
2009-10-07 12:55:36 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01: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 17:57:37 +00:00
|
|
|
user = mkOption {
|
|
|
|
default = "mysql";
|
|
|
|
description = ''
|
|
|
|
User to be used to perform backup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of database names to dump.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
location = mkOption {
|
|
|
|
default = "/var/backup/mysql";
|
|
|
|
description = ''
|
2009-10-07 12:58:23 +01:00
|
|
|
Location to put the gzipped MySQL database dumps.
|
2009-10-07 12:55:36 +01:00
|
|
|
'';
|
|
|
|
};
|
2010-11-04 08:13:57 +00:00
|
|
|
|
|
|
|
singleTransaction = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to create database dump in a single transaction
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 12:55:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.mysqlBackup.enable {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2010-09-13 16:41:38 +01:00
|
|
|
services.cron.systemCronJobs = map mysqlBackupCron config.services.mysqlBackup.databases;
|
2009-10-07 12:55:36 +01:00
|
|
|
|
2012-03-15 14:21:17 +00:00
|
|
|
system.activationScripts.mysqlBackup = stringAfter [ "stdio" "users" ]
|
2010-09-13 16:41:38 +01:00
|
|
|
''
|
|
|
|
mkdir -m 0700 -p ${config.services.mysqlBackup.location}
|
|
|
|
chown ${config.services.mysqlBackup.user} ${config.services.mysqlBackup.location}
|
|
|
|
'';
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
}
|