Skip to content

Commit 40db3e8

Browse files
author
Ashley Penney
committed
Merge pull request #244 from willametteuniversity/master
Added support to back up specified databases only with 'mysqlbackup'
2 parents 3e7d7a2 + ad479e6 commit 40db3e8

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

manifests/backup.pp

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# This module handles ...
44
#
55
# Parameters:
6-
# [*backupuser*] - The name of the mysql backup user.
7-
# [*backuppassword*] - The password of the mysql backup user.
8-
# [*backupdir*] - The target directory of the mysqldump.
9-
# [*backupcompress*] - Boolean to compress backup with bzip2.
10-
# [*backuprotate*] - Number of backups to keep. Default 30
6+
# [*backupuser*] - The name of the mysql backup user.
7+
# [*backuppassword*] - The password of the mysql backup user.
8+
# [*backupdir*] - The target directory of the mysqldump.
9+
# [*backupcompress*] - Boolean to compress backup with bzip2.
10+
# [*backuprotate*] - Number of backups to keep. Default 30
11+
# [*backupdatabases*] - Specify databases to back up as array (default all)
1112
# [*delete_before_dump*] - Clean existing backups before creating new
1213
#
1314
# Actions:
@@ -32,6 +33,7 @@
3233
$backupcompress = true,
3334
$backuprotate = 30,
3435
$delete_before_dump = false,
36+
$backupdatabases = [],
3537
$ensure = 'present'
3638
) {
3739

@@ -72,4 +74,5 @@
7274
owner => 'root',
7375
group => 'root',
7476
}
77+
7578
}

spec/classes/mysql_backup_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,23 @@
6262
])
6363
end
6464
end
65+
66+
context 'with database list specified' do
67+
let(:params) do
68+
{ :backupdatabases => ['mysql'] }.merge(default_params)
69+
end
70+
71+
it { should contain_file('mysqlbackup.sh').with(
72+
:path => '/usr/local/sbin/mysqlbackup.sh',
73+
:ensure => 'present'
74+
) }
75+
76+
it 'should have a backup file for each database' do
77+
content = catalogue.resource('file','mysqlbackup.sh').send(:parameters)[:content]
78+
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
79+
# verify_contents(subject, 'mysqlbackup.sh', [
80+
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
81+
# ])
82+
end
83+
end
6584
end

templates/mysqlbackup.sh.erb

+7
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ cleanup()
3131
cleanup
3232

3333
<% end -%>
34+
<% if @backupdatabases.empty? -%>
3435
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
3536
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
37+
<% else -%>
38+
<% @backupdatabases.each do |db| -%>
39+
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
40+
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
41+
<% end -%>
42+
<% end -%>
3643

3744
<% unless @delete_before_dump -%>
3845
if [ $? -eq 0 ] ; then

0 commit comments

Comments
 (0)