From a7a5c66bc431898e2931d7520a434394ef495276 Mon Sep 17 00:00:00 2001 From: David Danzilio Date: Wed, 5 Aug 2015 16:15:25 -0400 Subject: [PATCH] Add support for postscript for xtrabackup provider This commit implements the 'postscript' functionality for the xtrabackup provider. It also adds a 'prescript' option to be executed before a backup. --- manifests/backup/mysqlbackup.pp | 1 + manifests/backup/mysqldump.pp | 1 + manifests/backup/xtrabackup.pp | 14 ++++++- manifests/server/backup.pp | 6 +++ spec/classes/mysql_server_backup_spec.rb | 48 ++++++++++++++++++++++++ templates/xtrabackup.sh.erb | 21 +++++++++++ 6 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 templates/xtrabackup.sh.erb diff --git a/manifests/backup/mysqlbackup.pp b/manifests/backup/mysqlbackup.pp index e7bb4d94a..3b0d94dfb 100644 --- a/manifests/backup/mysqlbackup.pp +++ b/manifests/backup/mysqlbackup.pp @@ -16,6 +16,7 @@ $include_routines = false, $ensure = 'present', $time = ['23', '5'], + $prescript = false, $postscript = false, $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', ) { diff --git a/manifests/backup/mysqldump.pp b/manifests/backup/mysqldump.pp index 77d5e39f4..543476d8b 100644 --- a/manifests/backup/mysqldump.pp +++ b/manifests/backup/mysqldump.pp @@ -16,6 +16,7 @@ $include_routines = false, $ensure = 'present', $time = ['23', '5'], + $prescript = false, $postscript = false, $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', ) { diff --git a/manifests/backup/xtrabackup.pp b/manifests/backup/xtrabackup.pp index a9c76bae1..4dc31f9cd 100644 --- a/manifests/backup/xtrabackup.pp +++ b/manifests/backup/xtrabackup.pp @@ -17,6 +17,7 @@ $include_routines = false, $ensure = 'present', $time = ['23', '5'], + $prescript = false, $postscript = false, $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', ) { @@ -27,7 +28,7 @@ cron { 'xtrabackup-weekly': ensure => $ensure, - command => "innobackupex ${backupdir}", + command => "/usr/local/sbin/xtrabackup.sh ${backupdir}", user => 'root', hour => $time[0], minute => $time[1], @@ -37,7 +38,7 @@ cron { 'xtrabackup-daily': ensure => $ensure, - command => "innobackupex --incremental ${backupdir}", + command => "/usr/local/sbin/xtrabackup.sh --incremental ${backupdir}", user => 'root', hour => $time[0], minute => $time[1], @@ -52,4 +53,13 @@ owner => $backupdirowner, group => $backupdirgroup, } + + file { 'xtrabackup.sh': + ensure => $ensure, + path => '/usr/local/sbin/xtrabackup.sh', + mode => '0700', + owner => 'root', + group => $mysql::params::root_group, + content => template('mysql/xtrabackup.sh.erb'), + } } diff --git a/manifests/server/backup.pp b/manifests/server/backup.pp index 94e7732e5..3b1f1eebe 100644 --- a/manifests/server/backup.pp +++ b/manifests/server/backup.pp @@ -16,11 +16,16 @@ $include_triggers = false, $ensure = 'present', $time = ['23', '5'], + $prescript = false, $postscript = false, $execpath = '/usr/bin:/usr/sbin:/bin:/sbin', $provider = 'mysqldump', ) { + if $prescript and $provider =~ /(mysqldump|mysqlbackup)/ { + warn("The \$prescript option is not currently implemented for the ${provider} backup provider.") + } + create_resources('class', { "mysql::backup::${provider}" => { 'backupuser' => $backupuser, @@ -39,6 +44,7 @@ 'include_triggers' => $include_triggers, 'ensure' => $ensure, 'time' => $time, + 'prescript' => $prescript, 'postscript' => $postscript, 'execpath' => $execpath, } diff --git a/spec/classes/mysql_server_backup_spec.rb b/spec/classes/mysql_server_backup_spec.rb index ce88ae376..8d199432c 100644 --- a/spec/classes/mysql_server_backup_spec.rb +++ b/spec/classes/mysql_server_backup_spec.rb @@ -349,6 +349,54 @@ ) end end + + context 'with the xtrabackup provider' do + let(:params) do + default_params.merge({:provider => 'xtrabackup'}) + end + + it 'should contain the wrapper script' do + is_expected.to contain_file('xtrabackup.sh').with_content( + /^innobackupex\s+"\$@"/ + ) + end + + context 'with prescript defined' do + let(:params) do + default_params.merge({ + :provider => 'xtrabackup', + :prescript => [ + 'rsync -a /tmp backup01.local-lan:', + 'rsync -a /tmp backup02.local-lan:', + ] + }) + end + + it 'should contain the prescript' do + is_expected.to contain_file('xtrabackup.sh').with_content( + /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ + ) + end + end + + context 'with postscript defined' do + let(:params) do + default_params.merge({ + :provider => 'xtrabackup', + :postscript => [ + 'rsync -a /tmp backup01.local-lan:', + 'rsync -a /tmp backup02.local-lan:', + ] + }) + end + + it 'should contain the prostscript' do + is_expected.to contain_file('xtrabackup.sh').with_content( + /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ + ) + end + end + end end end end diff --git a/templates/xtrabackup.sh.erb b/templates/xtrabackup.sh.erb new file mode 100644 index 000000000..14493983e --- /dev/null +++ b/templates/xtrabackup.sh.erb @@ -0,0 +1,21 @@ +<%- if @kernel == 'Linux' -%> +#!/bin/bash +<%- else -%> +#!/bin/sh +<%- end -%> +# +# A wrapper for Xtrabackup +# +<% if @prescript -%> + <%- [@prescript].flatten.compact.each do |script| %> +<%= script %> + <%- end -%> +<% end -%> + +innobackupex "$@" + +<% if @postscript -%> + <%- [@postscript].flatten.compact.each do |script| %> +<%= script %> + <%- end -%> +<% end -%>