backup: configurable cleanup sequence

$delete_before_dump controls whether old backups to be removed before
creating new one.
This commit is contained in:
Oleg Malashenko 2013-07-03 22:31:31 +10:00
parent 1521a2ea4f
commit d7460d4b02
2 changed files with 14 additions and 1 deletions

View file

@ -8,6 +8,7 @@
# [*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'
@ -30,6 +31,7 @@ class mysql::backup (
$backupdir,
$backupcompress = true,
$backuprotate = 30,
$delete_before_dump = false,
$ensure = 'present'
) {

View file

@ -22,10 +22,21 @@ PATH=/usr/bin:/usr/sbin:/bin:/sbin
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
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
cleanup
fi
<% end -%>