Add option to mysql::backup to set the backup script to perform a mysqldump on each database to its own file

This commit is contained in:
Trey Dockendorf 2013-08-27 15:15:29 -05:00
parent 40db3e801d
commit 627699fc49
4 changed files with 115 additions and 0 deletions

View file

@ -9,6 +9,7 @@
# [*backupcompress*] - Boolean to compress backup with bzip2.
# [*backuprotate*] - Number of backups to keep. Default 30
# [*backupdatabases*] - Specify databases to back up as array (default all)
# [*file_per_database*] - Boolean to dump each database to its own file.
# [*delete_before_dump*] - Clean existing backups before creating new
#
# Actions:
@ -34,6 +35,7 @@ class mysql::backup (
$backuprotate = 30,
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$ensure = 'present'
) {

View file

@ -81,4 +81,32 @@ describe 'mysql::backup' do
# ])
end
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 \\',
' ${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

View file

@ -0,0 +1,77 @@
require 'spec_helper_system'
describe 'mysql::backup class' do
context 'should work with no errors' do
pp = <<-EOS
class { 'mysql::server': config_hash => { 'root_password' => 'foo' } }
mysql::db { 'backup1':
user => 'backup',
password => 'secret',
}
class { 'mysql::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
}
EOS
context puppet_apply(pp) do
its(:stderr) { should be_empty }
its(:exit_code) { should_not == 1 }
its(:refresh) { should be_nil }
its(:stderr) { should be_empty }
its(:exit_code) { should be_zero }
end
context 'should run mysqlbackup.sh with no errors' do
context shell("/usr/local/sbin/mysqlbackup.sh") do
its(:exit_code) { should be_zero }
end
end
context 'should dump all databases to single file' do
describe command('ls /tmp/backups/ | grep -c "mysql_backup_[0-9][0-9]*-[0-9][0-9]*.sql.bz2"') do
it { should return_stdout /1/ }
it { should return_exit_status 0 }
end
end
end
context 'should create one file per database' do
pp = <<-EOS
class { 'mysql::server': config_hash => { 'root_password' => 'foo' } }
mysql::db { 'backup1':
user => 'backup',
password => 'secret',
}
class { 'mysql::backup':
backupuser => 'myuser',
backuppassword => 'mypassword',
backupdir => '/tmp/backups',
backupcompress => true,
file_per_database => true,
}
EOS
context puppet_apply(pp) do
its(:stderr) { should be_empty }
its(:exit_code) { should_not == 1 }
its(:refresh) { should be_nil }
its(:stderr) { should be_empty }
its(:exit_code) { should be_zero }
end
context shell("/usr/local/sbin/mysqlbackup.sh") do
its(:exit_code) { should be_zero }
end
describe command('ls /tmp/backups/ | grep -c "mysql_backup_backup1_[0-9][0-9]*-[0-9][0-9]*.sql.bz2"') do
it { should return_stdout /1/ }
it { should return_exit_status 0 }
end
end
end

8
templates/mysqlbackup.sh.erb Normal file → Executable file
View file

@ -32,8 +32,16 @@ cleanup
<% end -%>
<% if @backupdatabases.empty? -%>
<% if @file_per_database -%>
mysql -s -r -N -e 'SHOW DATABASES' | while read dbname
do
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
${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 \
--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 \