Skip to content

Commit

Permalink
Added support to back up specified databases only with 'mysqlbackup'
Browse files Browse the repository at this point in the history
parameter.  Each database is backed up separately to a named file.
  • Loading branch information
cfeskens committed Aug 13, 2013
1 parent e0aed9f commit 359d881
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 8 additions & 5 deletions manifests/backup.pp
Expand Up @@ -3,11 +3,12 @@
# This module handles ...
#
# Parameters:
# [*backupuser*] - The name of the mysql backup user.
# [*backuppassword*] - The password of the mysql backup user.
# [*backupdir*] - The target directory of the mysqldump.
# [*backupcompress*] - Boolean to compress backup with bzip2.
# [*backuprotate*] - Number of backups to keep. Default 30
# [*backupuser*] - The name of the mysql backup user.
# [*backuppassword*] - The password of the mysql backup user.
# [*backupdir*] - The target directory of the mysqldump.
# [*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)
# [*delete_before_dump*] - Clean existing backups before creating new
#
# Actions:
Expand All @@ -32,6 +33,7 @@
$backupcompress = true,
$backuprotate = 30,
$delete_before_dump = false,
$backupdatabases = false,
$ensure = 'present'
) {

Expand Down Expand Up @@ -72,4 +74,5 @@
owner => 'root',
group => 'root',
}

}
19 changes: 19 additions & 0 deletions spec/classes/mysql_backup_spec.rb
Expand Up @@ -62,4 +62,23 @@
])
end
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 have a backup file for each database' do
content = catalogue.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
end
7 changes: 7 additions & 0 deletions templates/mysqlbackup.sh.erb
Expand Up @@ -31,8 +31,15 @@ cleanup()
cleanup

<% end -%>
<% if @backupdatabases -%>
<% @backupdatabases.each do |db| -%>
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<% end -%>
<% 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 -%>

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

0 comments on commit 359d881

Please sign in to comment.