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
|
2009-10-07 13:55:36 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
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
|
|
|
|
2010-09-13 17:41:38 +02:00
|
|
|
system.activationScripts.mysqlBackup = stringAfter [ "stdio" "defaultPath" "systemConfig" "users" ]
|
|
|
|
''
|
|
|
|
mkdir -m 0700 -p ${config.services.mysqlBackup.location}
|
|
|
|
chown ${config.services.mysqlBackup.user} ${config.services.mysqlBackup.location}
|
|
|
|
'';
|
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|