Skip to content

Commit d7460d4

Browse files
committed
backup: configurable cleanup sequence
$delete_before_dump controls whether old backups to be removed before creating new one.
1 parent 1521a2e commit d7460d4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

manifests/backup.pp

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# [*backupdir*] - The target directory of the mysqldump.
99
# [*backupcompress*] - Boolean to compress backup with bzip2.
1010
# [*backuprotate*] - Number of backups to keep. Default 30
11+
# [*delete_before_dump*] - Clean existing backups before creating new
1112
#
1213
# Actions:
1314
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
@@ -30,6 +31,7 @@
3031
$backupdir,
3132
$backupcompress = true,
3233
$backuprotate = 30,
34+
$delete_before_dump = false,
3335
$ensure = 'present'
3436
) {
3537

templates/mysqlbackup.sh.erb

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,21 @@ PATH=/usr/bin:/usr/sbin:/bin:/sbin
2222

2323
set -o pipefail
2424

25+
cleanup()
26+
{
27+
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
28+
}
29+
30+
<% if @delete_before_dump -%>
31+
cleanup
32+
33+
<% end -%>
2534
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
2635
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
2736

37+
<% if not @delete_before_dump -%>
2838
if [ $? -eq 0 ] ; then
29-
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
39+
cleanup
3040
fi
41+
<% end -%>
3142

0 commit comments

Comments
 (0)