Implement fn_expire_backups to reduce code duplication.

This commit is contained in:
Robert Bruce Park
2013-11-13 10:17:41 -08:00
parent 32e71ce9aa
commit a67efdd0f1
+18 -14
View File
@@ -122,6 +122,11 @@ if [ -f "$INPROGRESS_FILE" ]; then
fi fi
fi fi
fn_expire_backup() {
fn_log_info "Expiring $1"
echo rm -rf -- "$1"
}
# Run in a loop to handle the "No space left on device" logic. # Run in a loop to handle the "No space left on device" logic.
while [ "1" ]; do while [ "1" ]; do
@@ -165,11 +170,11 @@ while [ "1" ]; do
elif [ $stamp -ge $KEEP_DAILIES_DATE ]; then elif [ $stamp -ge $KEEP_DAILIES_DATE ]; then
# Delete all but the most recent of each day. # Delete all but the most recent of each day.
[ ${date:8:2} -eq ${prev:8:2} ] && echo rm -rf -- "$DEST_FOLDER/$date" [ ${date:8:2} -eq ${prev:8:2} ] && fn_expire_backup "$fname"
else else
# Delete all but the most recent of each month. # Delete all but the most recent of each month.
[ ${date:5:2} -eq ${prev:5:2} ] && echo rm -rf -- "$DEST_FOLDER/$date" [ ${date:5:2} -eq ${prev:5:2} ] && fn_expire_backup "$fname"
fi fi
prev=$date prev=$date
@@ -225,9 +230,9 @@ while [ "1" ]; do
grep --quiet "Result too large (34)" "$LOG_FILE" grep --quiet "Result too large (34)" "$LOG_FILE"
NO_SPACE_LEFT="$?" NO_SPACE_LEFT="$?"
fi fi
rm -- "$LOG_FILE" rm -- "$LOG_FILE"
if [ "$NO_SPACE_LEFT" == "0" ]; then if [ "$NO_SPACE_LEFT" == "0" ]; then
# TODO: -y flag # TODO: -y flag
read -p "It looks like there is no space left on the destination. Delete old backup? (Y/n) " yn read -p "It looks like there is no space left on the destination. Delete old backup? (Y/n) " yn
@@ -236,29 +241,28 @@ while [ "1" ]; do
esac esac
fn_log_warn "No space left on device - removing oldest backup and resuming." fn_log_warn "No space left on device - removing oldest backup and resuming."
BACKUP_FOLDER_COUNT=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | wc -l) BACKUP_FOLDER_COUNT=$(fn_find_backups | wc -l)
if [ "$BACKUP_FOLDER_COUNT" -lt "2" ]; then if [ "$BACKUP_FOLDER_COUNT" -lt "2" ]; then
fn_log_error "No space left on device, and no old backup to delete." fn_log_error "No space left on device, and no old backup to delete."
exit 1 exit 1
fi fi
OLD_BACKUP_PATH=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | head -n 1) OLD_BACKUP_PATH=$(fn_find_backups | head -n 1)
if [ "$OLD_BACKUP_PATH" == "" ]; then if [ "$OLD_BACKUP_PATH" == "" ]; then
fn_log_error "No space left on device, and cannot get path to oldest backup to delete." fn_log_error "No space left on device, and cannot get path to oldest backup to delete."
exit 1 exit 1
fi fi
# Double-check that we're on a backup destination to be completely sure we're deleting the right folder # Double-check that we're on a backup destination to be completely sure we're deleting the right folder
OLD_BACKUP_PARENT_PATH=$(dirname -- "$OLD_BACKUP_PATH") OLD_BACKUP_PARENT_PATH=$(dirname -- "$OLD_BACKUP_PATH")
if [ "$(fn_is_backup_destination $OLD_BACKUP_PARENT_PATH)" != "1" ]; then if [ "$(fn_is_backup_destination $OLD_BACKUP_PARENT_PATH)" != "1" ]; then
fn_log_error "'$OLD_BACKUP_PATH' is not on a backup destination - aborting." fn_log_error "'$OLD_BACKUP_PATH' is not on a backup destination - aborting."
exit 1 exit 1
fi fi
fn_log_info "Deleting '$OLD_BACKUP_PATH'..." fn_expire_backup "$OLD_BACKUP_PATH"
rm -rf -- "$OLD_BACKUP_PATH"
# Resume backup # Resume backup
continue continue
fi fi
@@ -267,7 +271,7 @@ while [ "1" ]; do
fn_log_error "Exited with error code $RSYNC_EXIT_CODE" fn_log_error "Exited with error code $RSYNC_EXIT_CODE"
exit $RSYNC_EXIT_CODE exit $RSYNC_EXIT_CODE
fi fi
rm -- "$INPROGRESS_FILE" rm -- "$INPROGRESS_FILE"
# TODO: grep for "^rsync error:.*$" in log # TODO: grep for "^rsync error:.*$" in log
fn_log_info "Backup completed without errors." fn_log_info "Backup completed without errors."