Added options for including/excluding triggers and routines, and fixed a permission problem that was preventing triggers from being backed up

This commit is contained in:
Steven C. Saliman 2015-04-29 07:52:37 -06:00 committed by Steven C. Saliman
parent cc5d937b83
commit ec14b87a81
8 changed files with 244 additions and 12 deletions

View file

@ -377,6 +377,14 @@ Specify an array of databases to back up.
Whether a separate file be used per database. Valid values are 'true', 'false'. Defaults to 'false'.
#####`include_routines`
Whether or not to include routines for each database when doing a `file_per_database` backup. Defaults to `false`.
#####`include_triggers`
Whether or not to include triggers for a each database when doing a `file_per_database` backup. Defaults to `true`.
#####`ensure`
Allows you to remove the backup scripts. Valid values are 'present', 'absent'. Defaults to 'present'.

View file

@ -12,6 +12,8 @@ class mysql::backup::mysqlbackup (
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$include_triggers = true,
$include_routines = false,
$ensure = 'present',
$time = ['23', '5'],
$postscript = false,

View file

@ -12,6 +12,8 @@ class mysql::backup::mysqldump (
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$include_triggers = true,
$include_routines = false,
$ensure = 'present',
$time = ['23', '5'],
$postscript = false,
@ -28,7 +30,7 @@ class mysql::backup::mysqldump (
ensure => $ensure,
user => "${backupuser}@localhost",
table => '*.*',
privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ],
privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER' ],
require => Mysql_user["${backupuser}@localhost"],
}

View file

@ -13,6 +13,8 @@ class mysql::backup::xtrabackup (
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$include_triggers = true,
$include_routines = false,
$ensure = 'present',
$time = ['23', '5'],
$postscript = false,

View file

@ -12,6 +12,8 @@ class mysql::server::backup (
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$include_routines = false,
$include_triggers = true,
$ensure = 'present',
$time = ['23', '5'],
$postscript = false,
@ -33,6 +35,8 @@ class mysql::server::backup (
'delete_before_dump' => $delete_before_dump,
'backupdatabases' => $backupdatabases,
'file_per_database' => $file_per_database,
'include_routines' => $include_routines,
'include_triggers' => $include_triggers,
'ensure' => $ensure,
'time' => $time,
'postscript' => $postscript,

View file

@ -129,4 +129,46 @@ describe 'mysql::server::backup class' do
end
end
end
context 'with triggers and routines' do
it 'when configuring mysql backups with triggers and routines' do
pp = <<-EOS
class { 'mysql::server': root_password => 'password' }
mysql::db { [
'backup1',
'backup2'
]:
user => 'backup',
password => 'secret',
}
package { 'bzip2':
ensure => present,
}
class { 'mysql::server::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
file_per_database => true,
include_triggers => true,
include_routines => true,
postscript => [
'rm -rf /var/tmp/mysqlbackups',
'rm -f /var/tmp/mysqlbackups.done',
'cp -r /tmp/backups /var/tmp/mysqlbackups',
'touch /var/tmp/mysqlbackups.done',
],
execpath => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
require => Package['bzip2'],
}
EOS
apply_manifest(pp, :catch_failures => true)
end
it 'should run mysqlbackup.sh with no errors' do
shell("/usr/local/sbin/mysqlbackup.sh") do |r|
expect(r.stderr).to eq("")
end
end
end
end

View file

@ -24,7 +24,7 @@ describe 'mysql::server::backup' do
:require => 'Class[Mysql::Server::Root_password]') }
it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with(
:privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS']
:privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER']
).that_requires('Mysql_user[testuser@localhost]') }
it { is_expected.to contain_cron('mysql-backup').with(
@ -43,13 +43,26 @@ describe 'mysql::server::backup' do
)}
it 'should have compression by default' do
is_expected.to contain_file('mysqlbackup.sh').with(
:content => /bzcat -zc/
is_expected.to contain_file('mysqlbackup.sh').with_content(
/bzcat -zc/
)
end
it 'should skip backing up events table by default' do
is_expected.to contain_file('mysqlbackup.sh').with(
:content => /EVENTS="--ignore-table=mysql.event"/
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="--ignore-table=mysql.event"/
)
end
it 'should not mention triggers by default because file_per_database is false' do
is_expected.to contain_file('mysqlbackup.sh').without_content(
/.*triggers.*/
)
end
it 'should not mention routines by default because file_per_database is false' do
is_expected.to contain_file('mysqlbackup.sh').without_content(
/.*routines.*/
)
end
@ -109,7 +122,7 @@ describe 'mysql::server::backup' do
it 'should be able to backup events table' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/EVENTS="--events"/
/ADDITIONAL_OPTIONS="--events"/
)
end
end
@ -130,6 +143,78 @@ describe 'mysql::server::backup' do
/mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date'/
)
end
it 'should backup triggers by default' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
)
end
it 'should skip backing up routines by default' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
)
end
context 'with include_triggers set to true' do
let(:params) do
default_params.merge({
:backupdatabases => ['mysql'],
:include_triggers => true
})
end
it 'should backup triggers when asked' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
)
end
end
context 'with include_triggers set to false' do
let(:params) do
default_params.merge({
:backupdatabases => ['mysql'],
:include_triggers => false
})
end
it 'should skip backing up triggers when asked to skip' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
)
end
end
context 'with include_routines set to true' do
let(:params) do
default_params.merge({
:backupdatabases => ['mysql'],
:include_routines => true
})
end
it 'should backup routines when asked' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/
)
end
end
context 'with include_routines set to false' do
let(:params) do
default_params.merge({
:backupdatabases => ['mysql'],
:include_triggers => true
})
end
it 'should skip backing up routines when asked to skip' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
)
end
end
end
context 'with file per database' do
@ -155,6 +240,78 @@ describe 'mysql::server::backup' do
)
end
end
it 'should backup triggers by default' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
)
end
it 'should skip backing up routines by default' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
)
end
context 'with include_triggers set to true' do
let(:params) do
default_params.merge({
:file_per_database => true,
:include_triggers => true
})
end
it 'should backup triggers when asked' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/
)
end
end
context 'with include_triggers set to false' do
let(:params) do
default_params.merge({
:file_per_database => true,
:include_triggers => false
})
end
it 'should skip backing up triggers when asked to skip' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/
)
end
end
context 'with include_routines set to true' do
let(:params) do
default_params.merge({
:file_per_database => true,
:include_routines => true
})
end
it 'should backup routines when asked' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/
)
end
end
context 'with include_routines set to false' do
let(:params) do
default_params.merge({
:file_per_database => true,
:include_triggers => true
})
end
it 'should skip backing up routines when asked to skip' do
is_expected.to contain_file('mysqlbackup.sh').with_content(
/ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/
)
end
end
end
context 'with postscript' do

View file

@ -21,10 +21,25 @@ ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
PREFIX=mysql_backup_
<% if @ignore_events %>
EVENTS="--ignore-table=mysql.event"
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
<% else %>
EVENTS="--events"
ADDITIONAL_OPTIONS="--events"
<% end %>
<%# Only include routines or triggers if we're doing a file per database -%>
<%# backup. This happens if we named databases, or if we explicitly set -%>
<%# file per database mode -%>
<% if !@backupdatabases.empty? || @file_per_database -%>
<% if @include_triggers -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers"
<% else -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers"
<% end -%>
<% if @include_routines -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines"
<% else -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines"
<% end -%>
<% end -%>
##### STOP CONFIG ####################################################
PATH=<%= @execpath %>
@ -49,18 +64,18 @@ cleanup
mysql -u${USER} -p${PASS} -s -r -N -e 'SHOW DATABASES' | while read dbname
do
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${EVENTS} \
${ADDITIONAL_OPTIONS} \
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
done
<% else -%>
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${EVENTS} \
${ADDITIONAL_OPTIONS} \
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% else -%>
<% @backupdatabases.each do |db| -%>
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${EVENTS} \
${ADDITIONAL_OPTIONS} \
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% end -%>