From 32e71ce9aac4276097b4e90148be5a72c2d983dc Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:16:27 -0800 Subject: [PATCH] Implement fn_find_backups to reduce code duplication. --- rsync_tmbackup.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index a2c8bd7..49e215c 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -78,12 +78,17 @@ fi # Setup additional variables # ----------------------------------------------------------------------------- +fn_find_backups() { + find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune +} + +export IFS=$'\n' # Better for handling spaces in filenames. BACKUP_FOLDER_PATTERN=????-??-??-?????? NOW=$(date +"%Y-%m-%d-%H%M%S") PROFILE_FOLDER="$HOME/.rsync_tmbackup" LOG_FILE="$PROFILE_FOLDER/$NOW.log" DEST=$DEST_FOLDER/$NOW -PREVIOUS_DEST=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 1) +PREVIOUS_DEST=$(fn_find_backups | sort | tail -n 1) INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress KEEP_ALL_DATE=$(date -d '-1 day' +%s) KEEP_DAILIES_DATE=$(date -d '-1 month' +%s) @@ -106,10 +111,10 @@ if [ -f "$INPROGRESS_FILE" ]; then # - Last backup is moved to current backup folder so that it can be resumed. # - 2nd to last backup becomes last backup. fn_log_info "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." - LINE_COUNT=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 2 | wc -l) + LINE_COUNT=$(fn_find_backups | sort | tail -n 2 | wc -l) mv -- "$PREVIOUS_DEST" "$DEST" if [ "$LINE_COUNT" -gt 1 ]; then - PREVIOUS_PREVIOUS_DEST=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 2 | head -n 1) + PREVIOUS_PREVIOUS_DEST=$(fn_find_backups | sort | tail -n 2 | head -n 1) PREVIOUS_DEST=$PREVIOUS_PREVIOUS_DEST else PREVIOUS_DEST="" @@ -148,7 +153,8 @@ while [ "1" ]; do # Purge certain old backups before beginning new backup. # ----------------------------------------------------------------------------- - for date in $(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | sort -r); do + for fname in $(fn_find_backups | sort -r); do + date=$(basename "$fname") stamp=$(fn_parse_date $date) # Skip if failed to parse date...