Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to ignore mysql.events #435

Merged
merged 1 commit into from
Feb 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions manifests/server/backup.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
$backupdirgroup = 'root',
$backupcompress = true,
$backuprotate = 30,
$ignore_events = true,
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
Expand Down
250 changes: 137 additions & 113 deletions spec/classes/mysql_server_backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,154 @@

describe 'mysql::server::backup' do

let(:default_params) {
{ 'backupuser' => 'testuser',
'backuppassword' => 'testpass',
'backupdir' => '/tmp',
'backuprotate' => '25',
'delete_before_dump' => true,
let(:default_params) {
{ 'backupuser' => 'testuser',
'backuppassword' => 'testpass',
'backupdir' => '/tmp',
'backuprotate' => '25',
'delete_before_dump' => true,
}
}
}
context 'standard conditions' do
let(:params) { default_params }

it { should contain_mysql_user('testuser@localhost').with(
:require => 'Class[Mysql::Server::Root_password]'
)}

it { should contain_mysql_grant('testuser@localhost/*.*').with(
:privileges => ["SELECT", "RELOAD", "LOCK TABLES", "SHOW VIEW"]
)}

it { should contain_cron('mysql-backup').with(
:command => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
)}

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory'
)}

it 'should have compression by default' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
])
context 'standard conditions' do
let(:params) { default_params }

it { should contain_mysql_user('testuser@localhost').with(
:require => 'Class[Mysql::Server::Root_password]'
)}

it { should contain_mysql_grant('testuser@localhost/*.*').with(
:privileges => ["SELECT", "RELOAD", "LOCK TABLES", "SHOW VIEW"]
)}

it { should contain_cron('mysql-backup').with(
:command => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
)}

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory'
)}

it 'should have compression by default' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
])
end
it 'should skip backing up events table by default' do
verify_contents(subject, 'mysqlbackup.sh', [
'EVENTS="--ignore-table=mysql.event"',
])
end

it 'should have 25 days of rotation' do
# MySQL counts from 0 I guess.
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
end
end

it 'should have 25 days of rotation' do
# MySQL counts from 0 I guess.
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
context 'custom ownership and mode for backupdir' do
let(:params) do
{ :backupdirmode => '0750',
:backupdirowner => 'testuser',
:backupdirgroup => 'testgrp',
}.merge(default_params)
end

it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory',
:mode => '0750',
:owner => 'testuser',
:group => 'testgrp'
) }
end
end

context 'custom ownership and mode for backupdir' do
let(:params) do
{ :backupdirmode => '0750',
:backupdirowner => 'testuser',
:backupdirgroup => 'testgrp',
}.merge(default_params)

context 'with compression disabled' do
let(:params) do
{ :backupcompress => false }.merge(default_params)
end

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it 'should be able to disable compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
])
end
end

it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory',
:mode => '0750',
:owner => 'testuser',
:group => 'testgrp'
) }
end

context 'with compression disabled' do
let(:params) do
{ :backupcompress => false }.merge(default_params)
context 'with mysql.events backedup' do
let(:params) do
{ :ignore_events => false }.merge(default_params)
end

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it 'should be able to backup events table' do
verify_contents(subject, 'mysqlbackup.sh', [
'EVENTS="--events"',
])
end
end

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it 'should be able to disable compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
])
end
end
context 'with database list specified' do
let(:params) do
{ :backupdatabases => ['mysql'] }.merge(default_params)
end

context 'with database list specified' do
let(:params) do
{ :backupdatabases => ['mysql'] }.merge(default_params)
end
it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }

it 'should have a backup file for each database' do
content = subject.resource('file','mysqlbackup.sh').send(:parameters)[:content]
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
# verify_contents(subject, 'mysqlbackup.sh', [
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
# ])
end
end

context 'with file per database' do
let(:params) do
default_params.merge({ :file_per_database => true })
it 'should have a backup file for each database' do
content = subject.resource('file','mysqlbackup.sh').send(:parameters)[:content]
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
# verify_contents(subject, 'mysqlbackup.sh', [
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
# ])
end
end

it 'should loop through backup all databases' do
verify_contents(subject, 'mysqlbackup.sh', [
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
'do',
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
'done',
])
end

context 'with compression disabled' do
let(:params) do
default_params.merge({ :file_per_database => true, :backupcompress => false })
end

it 'should loop through backup all databases without compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
])
end

context 'with file per database' do
let(:params) do
default_params.merge({ :file_per_database => true })
end

it 'should loop through backup all databases' do
verify_contents(subject, 'mysqlbackup.sh', [
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
'do',
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
' ${EVENTS} \\',
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
'done',
])
end

context 'with compression disabled' do
let(:params) do
default_params.merge({ :file_per_database => true, :backupcompress => false })
end

it 'should loop through backup all databases without compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
])
end
end
end
end
end
12 changes: 11 additions & 1 deletion templates/mysqlbackup.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ DIR=<%= @backupdir %>
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>

PREFIX=mysql_backup_
<% if @ignore_events %>
EVENTS="--ignore-table=mysql.event"
<% else %>
EVENTS="--events"
<% end %>

##### STOP CONFIG ####################################################
PATH=/usr/bin:/usr/sbin:/bin:/sbin



set -o pipefail

cleanup()
Expand All @@ -36,15 +43,18 @@ cleanup
mysql -s -r -N -e 'SHOW DATABASES' | while read dbname
do
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${EVENTS} \
${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} \
--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 \
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${EVENTS} \
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% end -%>
Expand Down