From 559ffa33d039128e0cf6c677616432453065da1e Mon Sep 17 00:00:00 2001 From: ShortChanged13 Date: Mon, 4 Feb 2019 23:02:43 -0500 Subject: [PATCH 1/3] Moved the backup type to within the backup target loop. This will determine the backup type for each backup target. Also added the backup target name to the grep so the return list is limited to the specific backup target. Added a loop around the cleanup of the backups. I choose to put the existing routine inside a loop instead of adding it to the backup target loop so the call to --list-archives is only run once instead of after each backup target. Also added the backup target name to the grep so the return list is limited to the specific backup target. --- acts | 89 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/acts b/acts index fdbfaf8..38899a8 100755 --- a/acts +++ b/acts @@ -131,21 +131,6 @@ year=$(echo "$today" | cut -d_ -f1 | cut -d- -f1) month=$(echo "$today" | cut -d_ -f1 | cut -d- -f2) # day=$(echo "$today" | cut -d_ -f1 | cut -d- -f3) # unused -# Determine the archive type to create -if echo "$archives" | grep -E -q "^$hostname-yearly-$year"; then - if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month"; then - # There's a yearly and monthly backup already - archivetype="daily" - else - # There was a yearly but no monthly backup - archivetype="monthly" - fi -else - # There's no yearly backup - archivetype="yearly" -fi -log_verbose "archive-type type=$archivetype" - # Run the pre-backup script if [ -n "$prebackupscript" ]; then if [ -x "$prebackupscript" ]; then @@ -161,6 +146,20 @@ backuprc=0 # Notice any failed backups for dir in $backuptargets; do archive_starttime=$(date +%s) nicedirname=$(echo "$dir" | tr -d '/') + # Determine the archive type to create + if echo "$archives" | grep -E -q "^$hostname-yearly-$year-.+-$nicedirname"; then + if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month-.+-$nicedirname"; then + # There's a yearly and monthly backup already + archivetype="daily" + else + # There was a yearly but no monthly backup + archivetype="monthly" + fi + else + # There's no yearly backup + archivetype="yearly" + fi + log_verbose "archive-type type=$archivetype" archivename="$hostname-$archivetype-$today-$nicedirname" log_verbose "backup-start type=$archivetype dir=/$dir name=$archivename" # Uncontrolled expansion is bad, but we have little choice. See https://github.com/koalaman/shellcheck/wiki/Sc2086 @@ -180,37 +179,41 @@ if [ "$backuprc" != "0" ]; then die "acts-tarsnap-error One of the backups failed -- not deleting old backups" fi -# We don't delete any yearly backups - -# We keep 12 monthly backups -monthlybackups=$(echo "$archives" | grep "$hostname-monthly-" | cut -d_ -f1 | uniq | sort -rn) -if [ "$(echo "$monthlybackups" | wc -l)" -gt 12 ]; then - log_debug 'message="More than 12 monthly backups, deleting the oldest"' - echo "$monthlybackups" | tail -n +13 | while read -r archiveprefixtodel; do - log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\"" - echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do - log_debug "message=\"Deleting backup $archivetodel\"" - $tarsnap -d -f "$archivetodel" + +for dir in $backuptargets; do + nicedirname=$(echo "$dir" | tr -d '/') + # We don't delete any yearly backups + + # We keep 12 monthly backups + monthlybackups=$(echo "$archives" | grep "$hostname-monthly-.+-$nicedirname" | sort -rn) + if [ "$(echo "$monthlybackups" | wc -l)" -gt 12 ]; then + log_debug 'message="More than 12 monthly backups, deleting the oldest"' + echo "$monthlybackups" | tail -n +13 | while read -r archiveprefixtodel; do + log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\"" + echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do + log_debug "message=\"Deleting backup $archivetodel\"" + $tarsnap -d -f "$archivetodel" + done done - done -else - log_debug "message=\"Found $(echo "$monthlybackups" | wc -l) monthly backups, not deleting\"" -fi + else + log_debug "message=\"Found $(echo "$monthlybackups" | wc -l) monthly backups, not deleting\"" + fi -# We keep 31 daily backups -dailybackups=$(echo "$archives" | grep "$hostname-daily-" | cut -d_ -f1 | uniq | sort -rn) -if [ "$(echo "$dailybackups" | wc -l)" -gt 31 ]; then - log_debug "message=\"More than 30 daily backups, deleting the oldest\"" - echo "$dailybackups" | tail -n +32 | while read -r archiveprefixtodel; do - log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\"" - echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do - log_debug "message=\"Deleting backup $archivetodel\"" - $tarsnap -d -f "$archivetodel" + # We keep 31 daily backups + dailybackups=$(echo "$archives" | grep "$hostname-daily-.+-$nicedirname" | sort -rn) + if [ "$(echo "$dailybackups" | wc -l)" -gt 31 ]; then + log_debug "message=\"More than 30 daily backups, deleting the oldest\"" + echo "$dailybackups" | tail -n +32 | while read -r archiveprefixtodel; do + log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\"" + echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do + log_debug "message=\"Deleting backup $archivetodel\"" + $tarsnap -d -f "$archivetodel" + done done - done -else - log_debug "message=\"Found $(echo "$dailybackups" | wc -l) daily backups, not deleting any\"" -fi + else + log_debug "message=\"Found $(echo "$dailybackups" | wc -l) daily backups, not deleting any\"" + fi +done # Run the post-backup script if [ -n "$postbackupscript" ]; then From fb0a7afb77e9c578c93544b1ca677697573bc696 Mon Sep 17 00:00:00 2001 From: ShortChanged13 Date: Thu, 7 Feb 2019 19:18:00 -0500 Subject: [PATCH 2/3] Added debug logging at the start of the backup and deletion loops. Added '$' to the end of the grep statements to enhance safety Removed extra blank line --- acts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/acts b/acts index 38899a8..8b674b9 100755 --- a/acts +++ b/acts @@ -144,11 +144,12 @@ fi # PART 3: Backup backuprc=0 # Notice any failed backups for dir in $backuptargets; do + log_debug "message=\"Starting backup of $dir\"" archive_starttime=$(date +%s) nicedirname=$(echo "$dir" | tr -d '/') # Determine the archive type to create - if echo "$archives" | grep -E -q "^$hostname-yearly-$year-.+-$nicedirname"; then - if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month-.+-$nicedirname"; then + if echo "$archives" | grep -E -q "^$hostname-yearly-$year-.+-$nicedirname$"; then + if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month-.+-$nicedirname$"; then # There's a yearly and monthly backup already archivetype="daily" else @@ -179,13 +180,13 @@ if [ "$backuprc" != "0" ]; then die "acts-tarsnap-error One of the backups failed -- not deleting old backups" fi - for dir in $backuptargets; do + log_debug "message=\"Checking $dir for old backups to delete\"" nicedirname=$(echo "$dir" | tr -d '/') # We don't delete any yearly backups # We keep 12 monthly backups - monthlybackups=$(echo "$archives" | grep "$hostname-monthly-.+-$nicedirname" | sort -rn) + monthlybackups=$(echo "$archives" | grep "$hostname-monthly-.+-$nicedirname$" | sort -rn) if [ "$(echo "$monthlybackups" | wc -l)" -gt 12 ]; then log_debug 'message="More than 12 monthly backups, deleting the oldest"' echo "$monthlybackups" | tail -n +13 | while read -r archiveprefixtodel; do @@ -200,7 +201,7 @@ for dir in $backuptargets; do fi # We keep 31 daily backups - dailybackups=$(echo "$archives" | grep "$hostname-daily-.+-$nicedirname" | sort -rn) + dailybackups=$(echo "$archives" | grep "$hostname-daily-.+-$nicedirname$" | sort -rn) if [ "$(echo "$dailybackups" | wc -l)" -gt 31 ]; then log_debug "message=\"More than 30 daily backups, deleting the oldest\"" echo "$dailybackups" | tail -n +32 | while read -r archiveprefixtodel; do From 8117ab59ae0e3cd39a1a3de7a65e3ad4c2f12c13 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Wed, 13 Feb 2019 08:42:54 +1100 Subject: [PATCH 3/3] Add README note about removing backup paths --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 81e11f3..250c7d1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Notes on behaviour: - Keep the most recent 12 monthly backups, and delete any older ones. - Do not delete any yearly backups. +- If you ever remove a backup path from your config, its old backups will no longer be automatically cleaned. TODO ----