diff options
| author | Robert Bruce Park <r@robru.ca> | 2013-11-13 10:17:41 -0800 |
|---|---|---|
| committer | Robert Bruce Park <r@robru.ca> | 2013-11-13 10:17:41 -0800 |
| commit | a67efdd0f1ee524ccecc083939afe84eb8c9256a (patch) | |
| tree | f7b79c8ea6e7618570891b050dea95b450680676 | |
| parent | 32e71ce9aac4276097b4e90148be5a72c2d983dc (diff) | |
Implement fn_expire_backups to reduce code duplication.
| -rw-r--r-- | rsync_tmbackup.sh | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 49e215c..00fab0d 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -122,6 +122,11 @@ if [ -f "$INPROGRESS_FILE" ]; then 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. while [ "1" ]; do @@ -165,11 +170,11 @@ while [ "1" ]; do elif [ $stamp -ge $KEEP_DAILIES_DATE ]; then # 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 # 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 prev=$date @@ -225,9 +230,9 @@ while [ "1" ]; do grep --quiet "Result too large (34)" "$LOG_FILE" NO_SPACE_LEFT="$?" fi - + rm -- "$LOG_FILE" - + if [ "$NO_SPACE_LEFT" == "0" ]; then # TODO: -y flag 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 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 fn_log_error "No space left on device, and no old backup to delete." exit 1 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 fn_log_error "No space left on device, and cannot get path to oldest backup to delete." exit 1 fi - + # 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") if [ "$(fn_is_backup_destination $OLD_BACKUP_PARENT_PATH)" != "1" ]; then fn_log_error "'$OLD_BACKUP_PATH' is not on a backup destination - aborting." exit 1 fi - - fn_log_info "Deleting '$OLD_BACKUP_PATH'..." - rm -rf -- "$OLD_BACKUP_PATH" - + + fn_expire_backup "$OLD_BACKUP_PATH" + # Resume backup continue fi @@ -267,7 +271,7 @@ while [ "1" ]; do fn_log_error "Exited with error code $RSYNC_EXIT_CODE" exit $RSYNC_EXIT_CODE fi - + rm -- "$INPROGRESS_FILE" # TODO: grep for "^rsync error:.*$" in log fn_log_info "Backup completed without errors." |
