From 58508b772a5407b7916933c7fafb26a123f54390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 27 Jan 2015 22:08:37 +0100 Subject: [PATCH] Use backup providers Add MySQL Enterprise Backup and Percona XtraBackup --- README.md | 28 +++++++++ manifests/backup/mysqlbackup.pp | 103 ++++++++++++++++++++++++++++++++ manifests/backup/mysqldump.pp | 62 +++++++++++++++++++ manifests/backup/xtrabackup.pp | 59 ++++++++++++++++++ manifests/server/backup.pp | 90 +++++++++++----------------- templates/meb.cnf.erb | 18 ++++++ 6 files changed, 306 insertions(+), 54 deletions(-) create mode 100644 manifests/backup/mysqlbackup.pp create mode 100644 manifests/backup/mysqldump.pp create mode 100644 manifests/backup/xtrabackup.pp create mode 100644 templates/meb.cnf.erb diff --git a/README.md b/README.md index 56be157ad..d2d6acdfe 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,34 @@ An array of two elements to set the backup time. Allows ['23', '5'] or ['3', '4 A script that is executed at when the backup is finished. This could be used to (r)sync the backup to a central store. This script can be either a single line that is directly executed or a number of lines, when supplied as an array. It could also be one or more externally managed (executable) files. +#####`provider` + +Set backup implementation + +* `mysqldump` +* `mysqlbackup`: MySQL Enterprise Backup +* `xtrabackup`: Percona XtraBackup + +####mysql::backup::mysqldump + +Implements mysql::server::backup with mysqldump + +Backup type: Logical + +####mysql::backup::mysqlbackup + +Implements mysql::server::backup with MySQL Enterprise Backup from Oracle + +Backup type: Physical + +For this you need the meb package, which is available in RPM and TAR format from Oracle. For Ubuntu you can use [meb-deb](https://github.com/dveeden/meb-deb) to create a package from an official tarball. + +####mysql::backup::xtrabackup + +Implements mysql::server::backup with XtraBackup from Percona + +Backup type: Physical + ####mysql::server::monitor #####`mysql_monitor_username` diff --git a/manifests/backup/mysqlbackup.pp b/manifests/backup/mysqlbackup.pp new file mode 100644 index 000000000..b2ef286b3 --- /dev/null +++ b/manifests/backup/mysqlbackup.pp @@ -0,0 +1,103 @@ +# See README.me for usage. +class mysql::backup::mysqlbackup ( + $backupuser, + $backuppassword, + $backupdir, + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = 'root', + $backupcompress = true, + $backuprotate = 30, + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $ensure = 'present', + $time = ['23', '5'], + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', +) { + + mysql_user { "${backupuser}@localhost": + ensure => $ensure, + password_hash => mysql_password($backuppassword), + provider => 'mysql', + require => Class['mysql::server::root_password'], + } + + package { 'meb': + ensure => $ensure, + } + + # http://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/mysqlbackup.privileges.html + mysql_grant { "${backupuser}@localhost/*.*": + ensure => $ensure, + user => "${backupuser}@localhost", + table => '*.*', + privileges => [ 'RELOAD', 'SUPER', 'REPLICATION CLIENT' ], + require => Mysql_user["${backupuser}@localhost"], + } + + mysql_grant { "${backupuser}@localhost/mysql.backup_progress": + ensure => $ensure, + user => "${backupuser}@localhost", + table => 'mysql.backup_progress', + privileges => [ 'CREATE', 'INSERT', 'DROP', 'UPDATE' ], + require => Mysql_user["${backupuser}@localhost"], + } + + mysql_grant { "${backupuser}@localhost/mysql.backup_history": + ensure => $ensure, + user => "${backupuser}@localhost", + table => 'mysql.backup_history', + privileges => [ 'CREATE', 'INSERT', 'SELECT', 'DROP', 'UPDATE' ], + require => Mysql_user["${backupuser}@localhost"], + } + + cron { 'mysqlbackup-weekly': + ensure => $ensure, + command => 'mysqlbackup backup', + user => 'root', + hour => $time[0], + minute => $time[1], + weekday => 0, + require => Package['meb'], + } + + cron { 'mysqlbackup-daily': + ensure => $ensure, + command => 'mysqlbackup --incremental backup', + user => 'root', + hour => $time[0], + minute => $time[1], + weekday => 1-6, + require => Package['meb'], + } + + $default_options = { + 'mysqlbackup' => { + 'backup-dir' => $backupdir, + 'with-timestamp' => true, + 'incremental_base' => 'history:last_backup', + 'incremental_backup_dir' => $backupdir, + 'user' => $backupuser, + 'password' => $backuppassword, + } + } + $options = mysql_deepmerge($default_options, $override_options) + + file { 'mysqlbackup-config-file': + path => '/etc/mysql/conf.d/meb.cnf', + content => template('mysql/meb.cnf.erb'), + mode => '0600', + } + + file { 'mysqlbackupdir': + ensure => 'directory', + path => $backupdir, + mode => $backupdirmode, + owner => $backupdirowner, + group => $backupdirgroup, + } + +} diff --git a/manifests/backup/mysqldump.pp b/manifests/backup/mysqldump.pp new file mode 100644 index 000000000..89ec32f1e --- /dev/null +++ b/manifests/backup/mysqldump.pp @@ -0,0 +1,62 @@ +# See README.me for usage. +class mysql::backup::mysqldump ( + $backupuser, + $backuppassword, + $backupdir, + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = 'root', + $backupcompress = true, + $backuprotate = 30, + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $ensure = 'present', + $time = ['23', '5'], + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', +) { + + mysql_user { "${backupuser}@localhost": + ensure => $ensure, + password_hash => mysql_password($backuppassword), + provider => 'mysql', + require => Class['mysql::server::root_password'], + } + + mysql_grant { "${backupuser}@localhost/*.*": + ensure => $ensure, + user => "${backupuser}@localhost", + table => '*.*', + privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ], + require => Mysql_user["${backupuser}@localhost"], + } + + cron { 'mysql-backup': + ensure => $ensure, + command => '/usr/local/sbin/mysqlbackup.sh', + user => 'root', + hour => $time[0], + minute => $time[1], + require => File['mysqlbackup.sh'], + } + + file { 'mysqlbackup.sh': + ensure => $ensure, + path => '/usr/local/sbin/mysqlbackup.sh', + mode => '0700', + owner => 'root', + group => 'root', + content => template('mysql/mysqlbackup.sh.erb'), + } + + file { 'mysqlbackupdir': + ensure => 'directory', + path => $backupdir, + mode => $backupdirmode, + owner => $backupdirowner, + group => $backupdirgroup, + } + +} diff --git a/manifests/backup/xtrabackup.pp b/manifests/backup/xtrabackup.pp new file mode 100644 index 000000000..37eb1a7af --- /dev/null +++ b/manifests/backup/xtrabackup.pp @@ -0,0 +1,59 @@ +# See README.me for usage. +class mysql::backup::xtrabackup ( + $backupuser, + $backuppassword, + $backupdir, + $backupmethod = 'mysqldump', + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = 'root', + $backupcompress = true, + $backuprotate = 30, + $ignore_events = true, + $delete_before_dump = false, + $backupdatabases = [], + $file_per_database = false, + $ensure = 'present', + $time = ['23', '5'], + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', +) { + + mysql_user { "${backupuser}@localhost": + ensure => $ensure, + password_hash => mysql_password($backuppassword), + provider => 'mysql', + require => Class['mysql::server::root_password'], + } + + package{ 'percona-xtrabackup': + ensure => $ensure, + } + cron { 'xtrabackup-weekly': + ensure => $ensure, + command => 'innobackupex $backupdir', + user => 'root', + hour => $time[0], + minute => $time[1], + weekday => 0, + require => Package['percona-xtrabackup'], + } + cron { 'xtrabackup-daily': + ensure => $ensure, + command => 'innobackupex --incremental $backupdir', + user => 'root', + hour => $time[0], + minute => $time[1], + weekday => 1-6, + require => Package['percona-xtrabackup'], + } + + file { 'mysqlbackupdir': + ensure => 'directory', + path => $backupdir, + mode => $backupdirmode, + owner => $backupdirowner, + group => $backupdirgroup, + } + +} diff --git a/manifests/server/backup.pp b/manifests/server/backup.pp index f599cae92..9d9dd5bcc 100644 --- a/manifests/server/backup.pp +++ b/manifests/server/backup.pp @@ -1,61 +1,43 @@ # See README.me for usage. class mysql::server::backup ( - $backupuser, - $backuppassword, - $backupdir, - $backupdirmode = '0700', - $backupdirowner = 'root', - $backupdirgroup = 'root', - $backupcompress = true, - $backuprotate = 30, - $ignore_events = true, + $backupuser = undef, + $backuppassword = undef, + $backupdir = undef, + $backupdirmode = '0700', + $backupdirowner = 'root', + $backupdirgroup = 'root', + $backupcompress = true, + $backuprotate = 30, + $ignore_events = true, $delete_before_dump = false, - $backupdatabases = [], - $file_per_database = false, - $ensure = 'present', - $time = ['23', '5'], - $postscript = false, - $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', + $backupdatabases = [], + $file_per_database = false, + $ensure = 'present', + $time = ['23', '5'], + $postscript = false, + $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', + $provider = 'mysqldump', ) { - mysql_user { "${backupuser}@localhost": - ensure => $ensure, - password_hash => mysql_password($backuppassword), - require => Class['mysql::server::root_password'], - } - - mysql_grant { "${backupuser}@localhost/*.*": - ensure => $ensure, - user => "${backupuser}@localhost", - table => '*.*', - privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ], - require => Mysql_user["${backupuser}@localhost"], - } - - cron { 'mysql-backup': - ensure => $ensure, - command => '/usr/local/sbin/mysqlbackup.sh', - user => 'root', - hour => $time[0], - minute => $time[1], - require => File['mysqlbackup.sh'], - } - - file { 'mysqlbackup.sh': - ensure => $ensure, - path => '/usr/local/sbin/mysqlbackup.sh', - mode => '0700', - owner => 'root', - group => 'root', - content => template('mysql/mysqlbackup.sh.erb'), - } - - file { 'mysqlbackupdir': - ensure => 'directory', - path => $backupdir, - mode => $backupdirmode, - owner => $backupdirowner, - group => $backupdirgroup, - } + create_resources("class", { + "mysql::backup::${provider}" => { + 'backupuser' => $backupuser, + 'backuppassword' => $backuppassword, + 'backupdir' => $backupdir, + 'backupdirmode' => $backupdirmode, + 'backupdirowner' => $backupdirowner, + 'backupdirgroup' => $backupdirgroup, + 'backupcompress' => $backupcompress, + 'backuprotate' => $backuprotate, + 'ignore_events' => $ignore_events, + 'delete_before_dump' => $delete_before_dump, + 'backupdatabases' => $backupdatabases, + 'file_per_database' => $file_per_database, + 'ensure' => $ensure, + 'time' => $time, + 'postscript' => $postscript, + 'execpath' => $execpath, + } + }) } diff --git a/templates/meb.cnf.erb b/templates/meb.cnf.erb new file mode 100644 index 000000000..d157af99a --- /dev/null +++ b/templates/meb.cnf.erb @@ -0,0 +1,18 @@ +### MANAGED BY PUPPET ### + +<% @options.sort.map do |k,v| -%> +<% if v.is_a?(Hash) -%> +[<%= k %>] +<% v.sort.map do |ki, vi| -%> +<% if vi == true or v == '' -%> +<%= ki %> +<% elsif vi.is_a?(Array) -%> +<% vi.each do |vii| -%> +<%= ki %> = <%= vii %> +<% end -%> +<% elsif ![nil, '', :undef].include?(vi) -%> +<%= ki %> = <%= vi %> +<% end -%> +<% end -%> +<% end %> +<% end -%>