Skip to content

Add option so mysql::backup to dump each database to its own file #253

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

Merged
merged 1 commit into from
Aug 27, 2013
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
2 changes: 2 additions & 0 deletions manifests/backup.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -34,6 +35,7 @@
$backuprotate = 30,
$delete_before_dump = false,
$backupdatabases = [],
$file_per_database = false,
$ensure = 'present'
) {

Expand Down
28 changes: 28 additions & 0 deletions spec/classes/mysql_backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,32 @@
# ])
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
77 changes: 77 additions & 0 deletions spec/system/mysql_backup_spec.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions templates/mysqlbackup.sh.erb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down