Merge pull request #170 from omalashenko/master
Harden mysqlbackup.sh script
This commit is contained in:
commit
926e94a4cb
3 changed files with 39 additions and 9 deletions
|
@ -7,6 +7,8 @@
|
|||
# [*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
|
||||
# [*delete_before_dump*] - Clean existing backups before creating new
|
||||
#
|
||||
# Actions:
|
||||
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
|
||||
|
@ -28,6 +30,8 @@ class mysql::backup (
|
|||
$backuppassword,
|
||||
$backupdir,
|
||||
$backupcompress = true,
|
||||
$backuprotate = 30,
|
||||
$delete_before_dump = false,
|
||||
$ensure = 'present'
|
||||
) {
|
||||
|
||||
|
|
|
@ -3,9 +3,11 @@ require 'spec_helper'
|
|||
describe 'mysql::backup' do
|
||||
|
||||
let(:default_params) {
|
||||
{ 'backupuser' => 'testuser',
|
||||
'backuppassword' => 'testpass',
|
||||
'backupdir' => '/tmp',
|
||||
{ 'backupuser' => 'testuser',
|
||||
'backuppassword' => 'testpass',
|
||||
'backupdir' => '/tmp',
|
||||
'backuprotate' => '25',
|
||||
'delete_before_dump' => true,
|
||||
}
|
||||
}
|
||||
context "standard conditions" do
|
||||
|
@ -34,9 +36,14 @@ describe 'mysql::backup' do
|
|||
|
||||
it 'should have compression by default' do
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
context "with compression disabled" do
|
||||
|
@ -51,7 +58,7 @@ describe 'mysql::backup' do
|
|||
|
||||
it 'should be able to disable compression' do
|
||||
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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
#
|
||||
# MySQL Backup Script
|
||||
# Dumps mysql databases to a file for another backup tool to pick up.
|
||||
|
@ -13,11 +13,30 @@
|
|||
USER=<%= @backupuser %>
|
||||
PASS=<%= @backuppassword %>
|
||||
DIR=<%= @backupdir %>
|
||||
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
|
||||
|
||||
PREFIX=mysql_backup_
|
||||
|
||||
##### STOP CONFIG ####################################################
|
||||
PATH=/usr/bin:/usr/sbin:/bin:/sbin
|
||||
|
||||
find $DIR -mtime +30 -exec rm -f {} \;
|
||||
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 %>
|
||||
set -o pipefail
|
||||
|
||||
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 -%>
|
||||
|
||||
|
|
Loading…
Reference in a new issue