Skip to content

Commit 4e649d7

Browse files
author
Ashley Penney
committed
Merge pull request #253 from treydock/mysqlbackup_file_per_database
Add option so mysql::backup to dump each database to its own file
2 parents 40db3e8 + 627699f commit 4e649d7

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

manifests/backup.pp

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# [*backupcompress*] - Boolean to compress backup with bzip2.
1010
# [*backuprotate*] - Number of backups to keep. Default 30
1111
# [*backupdatabases*] - Specify databases to back up as array (default all)
12+
# [*file_per_database*] - Boolean to dump each database to its own file.
1213
# [*delete_before_dump*] - Clean existing backups before creating new
1314
#
1415
# Actions:
@@ -34,6 +35,7 @@
3435
$backuprotate = 30,
3536
$delete_before_dump = false,
3637
$backupdatabases = [],
38+
$file_per_database = false,
3739
$ensure = 'present'
3840
) {
3941

spec/classes/mysql_backup_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,32 @@
8181
# ])
8282
end
8383
end
84+
85+
context 'with file per database' do
86+
let(:params) do
87+
default_params.merge({ :file_per_database => true })
88+
end
89+
90+
it 'should loop through backup all databases' do
91+
verify_contents(subject, 'mysqlbackup.sh', [
92+
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
93+
'do',
94+
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
95+
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
96+
'done',
97+
])
98+
end
99+
100+
context 'with compression disabled' do
101+
let(:params) do
102+
default_params.merge({ :file_per_database => true, :backupcompress => false })
103+
end
104+
105+
it 'should loop through backup all databases without compression' do
106+
verify_contents(subject, 'mysqlbackup.sh', [
107+
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
108+
])
109+
end
110+
end
111+
end
84112
end

spec/system/mysql_backup_spec.rb

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'spec_helper_system'
2+
3+
describe 'mysql::backup class' do
4+
context 'should work with no errors' do
5+
pp = <<-EOS
6+
class { 'mysql::server': config_hash => { 'root_password' => 'foo' } }
7+
mysql::db { 'backup1':
8+
user => 'backup',
9+
password => 'secret',
10+
}
11+
12+
class { 'mysql::backup':
13+
backupuser => 'myuser',
14+
backuppassword => 'mypassword',
15+
backupdir => '/tmp/backups',
16+
backupcompress => true,
17+
}
18+
EOS
19+
20+
context puppet_apply(pp) do
21+
its(:stderr) { should be_empty }
22+
its(:exit_code) { should_not == 1 }
23+
its(:refresh) { should be_nil }
24+
its(:stderr) { should be_empty }
25+
its(:exit_code) { should be_zero }
26+
end
27+
28+
context 'should run mysqlbackup.sh with no errors' do
29+
context shell("/usr/local/sbin/mysqlbackup.sh") do
30+
its(:exit_code) { should be_zero }
31+
end
32+
end
33+
34+
context 'should dump all databases to single file' do
35+
describe command('ls /tmp/backups/ | grep -c "mysql_backup_[0-9][0-9]*-[0-9][0-9]*.sql.bz2"') do
36+
it { should return_stdout /1/ }
37+
it { should return_exit_status 0 }
38+
end
39+
end
40+
end
41+
42+
43+
context 'should create one file per database' do
44+
pp = <<-EOS
45+
class { 'mysql::server': config_hash => { 'root_password' => 'foo' } }
46+
mysql::db { 'backup1':
47+
user => 'backup',
48+
password => 'secret',
49+
}
50+
51+
class { 'mysql::backup':
52+
backupuser => 'myuser',
53+
backuppassword => 'mypassword',
54+
backupdir => '/tmp/backups',
55+
backupcompress => true,
56+
file_per_database => true,
57+
}
58+
EOS
59+
60+
context puppet_apply(pp) do
61+
its(:stderr) { should be_empty }
62+
its(:exit_code) { should_not == 1 }
63+
its(:refresh) { should be_nil }
64+
its(:stderr) { should be_empty }
65+
its(:exit_code) { should be_zero }
66+
end
67+
68+
context shell("/usr/local/sbin/mysqlbackup.sh") do
69+
its(:exit_code) { should be_zero }
70+
end
71+
72+
describe command('ls /tmp/backups/ | grep -c "mysql_backup_backup1_[0-9][0-9]*-[0-9][0-9]*.sql.bz2"') do
73+
it { should return_stdout /1/ }
74+
it { should return_exit_status 0 }
75+
end
76+
end
77+
end

templates/mysqlbackup.sh.erb

100644100755
+8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ cleanup
3232

3333
<% end -%>
3434
<% if @backupdatabases.empty? -%>
35+
<% if @file_per_database -%>
36+
mysql -s -r -N -e 'SHOW DATABASES' | while read dbname
37+
do
38+
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
39+
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
40+
done
41+
<% else -%>
3542
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
3643
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
44+
<% end -%>
3745
<% else -%>
3846
<% @backupdatabases.each do |db| -%>
3947
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \

0 commit comments

Comments
 (0)