Merge pull request #170 from omalashenko/master

Harden mysqlbackup.sh script
This commit is contained in:
Ashley Penney 2013-07-09 16:06:55 -07:00
commit 926e94a4cb
3 changed files with 39 additions and 9 deletions

View file

@ -7,6 +7,8 @@
# [*backuppassword*] - The password of the mysql backup user. # [*backuppassword*] - The password of the mysql backup user.
# [*backupdir*] - The target directory of the mysqldump. # [*backupdir*] - The target directory of the mysqldump.
# [*backupcompress*] - Boolean to compress backup with bzip2. # [*backupcompress*] - Boolean to compress backup with bzip2.
# [*backuprotate*] - Number of backups to keep. Default 30
# [*delete_before_dump*] - Clean existing backups before creating new
# #
# Actions: # Actions:
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost' # GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
@ -28,6 +30,8 @@ class mysql::backup (
$backuppassword, $backuppassword,
$backupdir, $backupdir,
$backupcompress = true, $backupcompress = true,
$backuprotate = 30,
$delete_before_dump = false,
$ensure = 'present' $ensure = 'present'
) { ) {

View file

@ -6,6 +6,8 @@ describe 'mysql::backup' do
{ 'backupuser' => 'testuser', { 'backupuser' => 'testuser',
'backuppassword' => 'testpass', 'backuppassword' => 'testpass',
'backupdir' => '/tmp', 'backupdir' => '/tmp',
'backuprotate' => '25',
'delete_before_dump' => true,
} }
} }
context "standard conditions" do context "standard conditions" do
@ -34,9 +36,14 @@ describe 'mysql::backup' do
it 'should have compression by default' do it 'should have compression by default' do
verify_contents(subject, 'mysqlbackup.sh', [ verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases | bzcat -zc > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql.bz2', ' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
]) ])
end end
it 'should have 25 days of rotation' do
# MySQL counts from 0 I guess.
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
end
end end
context "with compression disabled" do context "with compression disabled" do
@ -51,7 +58,7 @@ describe 'mysql::backup' do
it 'should be able to disable compression' do it 'should be able to disable compression' do
verify_contents(subject, 'mysqlbackup.sh', [ verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql', ' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
]) ])
end end
end end

View file

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
# #
# MySQL Backup Script # MySQL Backup Script
# Dumps mysql databases to a file for another backup tool to pick up. # Dumps mysql databases to a file for another backup tool to pick up.
@ -13,11 +13,30 @@
USER=<%= @backupuser %> USER=<%= @backupuser %>
PASS=<%= @backuppassword %> PASS=<%= @backuppassword %>
DIR=<%= @backupdir %> DIR=<%= @backupdir %>
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
PREFIX=mysql_backup_
##### STOP CONFIG #################################################### ##### STOP CONFIG ####################################################
PATH=/usr/bin:/usr/sbin:/bin:/sbin PATH=/usr/bin:/usr/sbin:/bin:/sbin
find $DIR -mtime +30 -exec rm -f {} \; set -o pipefail
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> cleanup()
{
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
}
<% if @delete_before_dump -%>
cleanup
<% end -%>
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 %>
<% if not @delete_before_dump -%>
if [ $? -eq 0 ] ; then
cleanup
fi
<% end -%>