Skip to content

Added support to back up specified databases only with 'mysqlbackup' #244

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 2 commits into from
Aug 27, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions manifests/backup.pp
Original file line number Diff line number Diff line change
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this [] by default? I am trying to avoid magical type shifting variables as they make validation harder (and I'm guilty of doing this all the time, using a false and then setting things to hashes and arrays)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved now. Thanks for making me do it right.

$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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