From 704e56667a96e03c9bb6bca53135ff4ce7387cdb Mon Sep 17 00:00:00 2001 From: Rob Vermaas Date: Wed, 7 Oct 2009 11:55:36 +0000 Subject: [PATCH] added nixos modules to perform simple backup scheduling for directories, postgresql and mysql database dumps svn path=/nixos/trunk/; revision=17690 --- modules/module-list.nix | 3 + modules/services/backup/mysql-backup.nix | 71 ++++++++++++ modules/services/backup/postgresql-backup.nix | 71 ++++++++++++ modules/services/backup/sitecopy-backup.nix | 109 ++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 modules/services/backup/mysql-backup.nix create mode 100644 modules/services/backup/postgresql-backup.nix create mode 100644 modules/services/backup/sitecopy-backup.nix diff --git a/modules/module-list.nix b/modules/module-list.nix index f152c2382f5..b443a41a64e 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -36,6 +36,9 @@ ./security/sudo.nix ./services/audio/alsa.nix ./services/audio/pulseaudio.nix + ./services/backup/mysql-backup.nix + ./services/backup/postgresql-backup.nix + ./services/backup/sitecopy-backup.nix ./services/databases/mysql.nix ./services/databases/postgresql.nix ./services/hardware/acpid.nix diff --git a/modules/services/backup/mysql-backup.nix b/modules/services/backup/mysql-backup.nix new file mode 100644 index 00000000000..ca749fa9e42 --- /dev/null +++ b/modules/services/backup/mysql-backup.nix @@ -0,0 +1,71 @@ +{pkgs, config, ...}: + +let + inherit (pkgs.lib) mkOption mkIf singleton concatStrings; + inherit (pkgs) mysql gzip; + + location = config.services.mysqlBackup.location ; + + mysqlBackupCron = db : '' + ${config.services.mysqlBackup.period} mysql ${mysql}/bin/mysqldump ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz + ''; + +in + +{ + + ###### interface + + 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. + ''; + }; + + databases = mkOption { + default = []; + description = '' + List of database names to dump. + ''; + }; + + location = mkOption { + default = "/var/backup/mysql"; + description = '' + Location to put the gzipped PostgreSQL database dumps. + ''; + }; + }; + + }; + + ###### implementation + config = mkIf config.services.mysqlBackup.enable { + services.cron = { + systemCronJobs = + pkgs.lib.optional + config.services.mysqlBackup.enable + (concatStrings (map mysqlBackupCron config.services.mysqlBackup.databases)); + }; + + system.activationScripts.mysqlBackup = pkgs.stringsWithDeps.noDepEntry '' + mkdir -m 0700 -p ${config.services.mysqlBackup.location} + chown mysql ${config.services.mysqlBackup.location} + ''; + }; + +} diff --git a/modules/services/backup/postgresql-backup.nix b/modules/services/backup/postgresql-backup.nix new file mode 100644 index 00000000000..3ef758d595b --- /dev/null +++ b/modules/services/backup/postgresql-backup.nix @@ -0,0 +1,71 @@ +{pkgs, config, ...}: + +let + inherit (pkgs.lib) mkOption mkIf singleton concatStrings; + inherit (pkgs) postgresql gzip; + + location = config.services.postgresqlBackup.location ; + + postgresqlBackupCron = db : '' + ${config.services.postgresqlBackup.period} root ${postgresql}/bin/pg_dump ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz + ''; + +in + +{ + + ###### interface + + options = { + + services.postgresqlBackup = { + + enable = mkOption { + default = false; + description = '' + Whether to enable PostgreSQL dumps. + ''; + }; + + 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. + ''; + }; + + databases = mkOption { + default = []; + description = '' + List of database names to dump. + ''; + }; + + location = mkOption { + default = "/var/backup/postgresql"; + description = '' + Location to put the gzipped PostgreSQL database dumps. + ''; + }; + }; + + }; + + ###### implementation + config = mkIf config.services.postgresqlBackup.enable { + services.cron = { + systemCronJobs = + pkgs.lib.optional + config.services.postgresqlBackup.enable + (concatStrings (map postgresqlBackupCron config.services.postgresqlBackup.databases)); + }; + + system.activationScripts.postgresqlBackup = pkgs.stringsWithDeps.noDepEntry '' + mkdir -m 0700 -p ${config.services.postgresqlBackup.location} + chown root ${config.services.postgresqlBackup.location} + ''; + }; + +} diff --git a/modules/services/backup/sitecopy-backup.nix b/modules/services/backup/sitecopy-backup.nix new file mode 100644 index 00000000000..5ff16b62157 --- /dev/null +++ b/modules/services/backup/sitecopy-backup.nix @@ -0,0 +1,109 @@ +{pkgs, config, ...}: + +let + inherit (pkgs.lib) mkOption mkIf singleton concatStrings; + inherit (pkgs) sitecopy; + + stateDir = "/var/spool/sitecopy"; + + sitecopyCron = backup : '' + ${if backup ? period then backup.period else config.services.sitecopy.period} root ${sitecopy}/bin/sitecopy --storepath=${stateDir} --rcfile=${stateDir}/${backup.name}.conf --update ${backup.name} + ''; +in + +{ + + ###### interface + + options = { + + services.sitecopy = { + + enable = mkOption { + default = false; + description = '' + Whether to enable sitecopy backups of specified directories. + ''; + }; + + period = mkOption { + default = "15 04 * * *"; + description = '' + This option defines (in the format used by cron) when the + sitecopy backup are being run. + The default is to update at 04:15 (at night) every day. + ''; + }; + + backups = mkOption { + default = []; + description = '' + List of attributesets describing the backups. + E.g. { name = "test"; + local = "/tmp/backup"; + remote = "/staff-groups/ewi/st/strategoxt/backup/test"; + server = "webdata.tudelft.nl"; + protocol = "webdav"; + https = true ; + }; + Username/password are extracted from ${stateDir}/sitecopy.secrets at activation + time. The secrets file lines should have the following structure: + + + + ''; + }; + + }; + + }; + + + ###### implementation + + config = mkIf config.services.sitecopy.enable { + environment.systemPackages = [ sitecopy ]; + + services.cron = { + systemCronJobs = pkgs.lib.optional + config.services.sitecopy.enable + (concatStrings (map sitecopyCron config.services.sitecopy.backups)); + }; + + + system.activationScripts.postgresqlBackup = + pkgs.stringsWithDeps.noDepEntry '' + mkdir -m 0700 -p ${stateDir} + chown root ${stateDir} + touch ${stateDir}/sitecopy.secrets + chown root ${stateDir}/sitecopy.secrets + + ${pkgs.lib.concatStrings (map ( b: '' + unset secrets + unset secret + secrets=`grep '^${b.server}' ${stateDir}/sitecopy.secrets | head -1` + secret=($secrets) + cat > ${stateDir}/${b.name}.conf << EOF + site ${b.name} + server ${b.server} + protocol ${b.protocol} + username ''${secret[1]} + password ''${secret[2]} + local ${b.local} + remote ${b.remote} + ${if b.https then "http secure" else ""} + EOF + chmod 0600 ${stateDir}/${b.name}.conf + if ! test -e ${stateDir}/${b.name} ; then + echo " * Initializing sitecopy '${b.name}'" + ${sitecopy}/bin/sitecopy --storepath=${stateDir} --rcfile=${stateDir}/${b.name}.conf --initialize ${b.name} + else + echo " * Sitecopy '${b.name}' already initialized" + fi + '' ) config.services.sitecopy.backups + )} + + ''; + }; + +}