From d425a05528718d4d788ca1bd0d33f1c54d4fedfe Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 00:01:17 -0800 Subject: Initial implementation of Time Machine style backup pruning. Within 24 hours, all backups are kept. Within 1 month, only the most recent backup for each day is kept. For all previous backups, only the most recent of each month is kept. This is not *quite* the same as Time Machine, but this implementation is a lot easier to do since it is based on string comparisons of the dates and doesn't require any "hard" date logic. Also this commit just 'echo's what will be deleted, and does not actually delete anything yet because I am still testing it. --- README.md | 2 -- rsync_tmbackup.sh | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81ed1d4..6bbb554 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,6 @@ An optional exclude file can be provided as a third parameter. It should be comp * Check if there's enough space in the destination before doing the backup. Also automatically delete old backups. -* Manage the backups in a way similar to Time Machine - hourly backups for the past 24 hours; daily backups for the past month; weekly backups for the previous months. - # LICENSE [MIT](http://opensource.org/licenses/MIT) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 2364deb..100ce9f 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -31,6 +31,10 @@ trap 'fn_terminate_script' SIGINT # Source and destination information # ----------------------------------------------------------------------------- +fn_parse_date() { + date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s +} + SRC_FOLDER=${1%/} DEST_FOLDER=${2%/} EXCLUSION_FILE=$3 @@ -82,6 +86,8 @@ DEST=$DEST_FOLDER/$NOW LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 1) PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress +KEEP_ALL_DATE=$(date -d '-1 day' +%s) +KEEP_DAILIES_DATE=$(date -d '-1 month' +%s) # ----------------------------------------------------------------------------- # Create profile folder if it doesn't exist @@ -140,6 +146,32 @@ while [ "1" ]; do mkdir -p -- "$DEST" fi + # ----------------------------------------------------------------------------- + # Purge certain old backups before beginning new backup. + # ----------------------------------------------------------------------------- + + for date in $(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | sort -r); do + stamp=$(fn_parse_date $date) + + # Skip if failed to parse date... + [ -n "$stamp" ] || continue + + if [ $stamp -ge $KEEP_ALL_DATE ]; then + true + + 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 + + else + # Delete all but the most recent of each month. + [ ${date:5:2} -eq ${prev:5:2} ] && echo rm -rf $DEST_FOLDER/$date + fi + + prev=$date + done + + # ----------------------------------------------------------------------------- # Start backup # ----------------------------------------------------------------------------- -- cgit v1.2.3 From fe0bfde041ded29812a56228f3b4f4c96178462b Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 09:12:50 -0800 Subject: More robust path handling. --- rsync_tmbackup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 100ce9f..a3a42fa 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -161,11 +161,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} ] && echo rm -rf -- "$DEST_FOLDER/$date" 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} ] && echo rm -rf -- "$DEST_FOLDER/$date" fi prev=$date -- cgit v1.2.3 From 32e71ce9aac4276097b4e90148be5a72c2d983dc Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:16:27 -0800 Subject: 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... -- cgit v1.2.3 From a67efdd0f1ee524ccecc083939afe84eb8c9256a Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:17:41 -0800 Subject: Implement fn_expire_backups to reduce code duplication. --- rsync_tmbackup.sh | 32 ++++++++++++++++++-------------- 1 file 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." -- cgit v1.2.3 From 8bb5b3f2650282330e400cf88839be02e6830029 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:21:47 -0800 Subject: Rearrange function definitions. --- rsync_tmbackup.sh | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 00fab0d..1413db8 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -28,13 +28,26 @@ fn_terminate_script() { trap 'fn_terminate_script' SIGINT # ----------------------------------------------------------------------------- -# Source and destination information +# Small utility functions for reducing code duplication # ----------------------------------------------------------------------------- fn_parse_date() { date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s } +fn_find_backups() { + find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune +} + +fn_expire_backup() { + fn_log_info "Expiring $1" + echo rm -rf -- "$1" +} + +# ----------------------------------------------------------------------------- +# Source and destination information +# ----------------------------------------------------------------------------- + SRC_FOLDER=${1%/} DEST_FOLDER=${2%/} EXCLUSION_FILE=$3 @@ -78,10 +91,6 @@ 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") @@ -122,11 +131,6 @@ 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 -- cgit v1.2.3 From 19446c24b9821cd8771bff8992ac8770dae599bd Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:34:47 -0800 Subject: Take the training wheels off ;-) --- rsync_tmbackup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 1413db8..67d22d1 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -41,7 +41,7 @@ fn_find_backups() { fn_expire_backup() { fn_log_info "Expiring $1" - echo rm -rf -- "$1" + rm -rf -- "$1" } # ----------------------------------------------------------------------------- -- cgit v1.2.3 From 584e3a9b4bed54d671174555a184ef3f40ff8dff Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 10:53:13 -0800 Subject: Drop BACKUP_FOLDER_PATTERN since it's now only used in one place. --- rsync_tmbackup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 67d22d1..7b0f7a6 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -32,11 +32,12 @@ trap 'fn_terminate_script' SIGINT # ----------------------------------------------------------------------------- fn_parse_date() { + # Converts YYYY-MM-DD-HHMMSS to YYYY-MM-DD HH:MM:SS and then to Unix Epoch. date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s } fn_find_backups() { - find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune + find "$DEST_FOLDER" -type d -name "????-??-??-??????" -prune } fn_expire_backup() { @@ -92,7 +93,6 @@ fi # ----------------------------------------------------------------------------- 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" -- cgit v1.2.3 From 4250423d98de817154bbf653f205e1377e66fc10 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Thu, 14 Nov 2013 18:50:34 -0800 Subject: Make fn_expire_backup more robust. --- rsync_tmbackup.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 7b0f7a6..5e12eda 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -41,6 +41,13 @@ fn_find_backups() { } fn_expire_backup() { + # Double-check that we're on a backup destination to be completely + # sure we're deleting the right folder + if [ "$(fn_is_backup_destination $(dirname -- "$1"))" != "1" ]; then + fn_log_error "$1 is not on a backup destination - aborting." + exit 1 + fi + fn_log_info "Expiring $1" rm -rf -- "$1" } @@ -258,13 +265,6 @@ while [ "1" ]; do 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_expire_backup "$OLD_BACKUP_PATH" # Resume backup -- cgit v1.2.3 From 855c77a83f1db1da041b1d14bb114378bed64855 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Thu, 14 Nov 2013 19:03:26 -0800 Subject: Simplify fn_is_backup_destination implementation. --- rsync_tmbackup.sh | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 5e12eda..883415c 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -43,7 +43,7 @@ fn_find_backups() { fn_expire_backup() { # Double-check that we're on a backup destination to be completely # sure we're deleting the right folder - if [ "$(fn_is_backup_destination $(dirname -- "$1"))" != "1" ]; then + if [ -z "$(fn_is_backup_destination "$(dirname -- "$1")")" ]; then fn_log_error "$1 is not on a backup destination - aborting." exit 1 fi @@ -78,15 +78,10 @@ fn_backup_marker_path() { } fn_is_backup_destination() { - DEST_MARKER_FILE="$(fn_backup_marker_path $1)" - if [ -f "$DEST_MARKER_FILE" ]; then - echo "1" - else - echo "0" - fi + find "$(fn_backup_marker_path "$1")" 2>/dev/null } -if [ "$(fn_is_backup_destination $DEST_FOLDER)" != "1" ]; then +if [ -z "$(fn_is_backup_destination $DEST_FOLDER)" ]; then fn_log_info "Safety check failed - the destination does not appear to be a backup folder or drive (marker file not found)." fn_log_info "If it is indeed a backup folder, you may add the marker file by running the following command:" fn_log_info "" -- cgit v1.2.3 From 12deac25d69b63523507855218d3279ce29ab6eb Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Thu, 14 Nov 2013 19:31:22 -0800 Subject: Correctly handle most recent backup if it's older than 24hs. Code was assuming that you'd run backups hourly, eg, so you'd have many backups within the last 24 hours. Code assumed that by the time you found a backup more than 24h old, you'd already have a value for $prev set. In the event that your most recent backup is more than 24 hours old, $prev would not be set and bash would get an error "unary operator expected" when it tried to compare the day of the newest backup to the empty $prev value. I fix this by setting a default value for $prev, one that cannot be mistaken for an existing backup because it does not represent a valid date. This ensures that the most recent backup is correctly preserved regardless of it's age. --- rsync_tmbackup.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 883415c..d260115 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -164,6 +164,8 @@ while [ "1" ]; do # Purge certain old backups before beginning new backup. # ----------------------------------------------------------------------------- + # Default value for $prev ensures that the most recent backup is never deleted. + prev="0000-00-00-000000" for fname in $(fn_find_backups | sort -r); do date=$(basename "$fname") stamp=$(fn_parse_date $date) -- cgit v1.2.3 From 324a3ffc9a81d39e0a5455e58c26dc88c496e177 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Thu, 14 Nov 2013 19:36:32 -0800 Subject: Whitespace fix, oops. --- rsync_tmbackup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index d260115..a0687dc 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -164,7 +164,7 @@ while [ "1" ]; do # Purge certain old backups before beginning new backup. # ----------------------------------------------------------------------------- - # Default value for $prev ensures that the most recent backup is never deleted. + # Default value for $prev ensures that the most recent backup is never deleted. prev="0000-00-00-000000" for fname in $(fn_find_backups | sort -r); do date=$(basename "$fname") -- cgit v1.2.3 From ae7998025a80d1119361d5b0a948063856bc380d Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Fri, 15 Nov 2013 10:24:52 -0800 Subject: Add MacOSX compatibility. --- rsync_tmbackup.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index a0687dc..1bad059 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -33,7 +33,10 @@ trap 'fn_terminate_script' SIGINT fn_parse_date() { # Converts YYYY-MM-DD-HHMMSS to YYYY-MM-DD HH:MM:SS and then to Unix Epoch. - date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s + case "$OSTYPE" in + linux*) date -d "${1:0:10} ${1:11:2}:${1:13:2}:${1:15:2}" +%s ;; + darwin*) date -j -f "%Y-%m-%d-%H%M%S" "$1" "+%s" ;; + esac } fn_find_backups() { @@ -101,8 +104,17 @@ LOG_FILE="$PROFILE_FOLDER/$NOW.log" DEST=$DEST_FOLDER/$NOW 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) + +case "$OSTYPE" in + linux*) + KEEP_ALL_DATE=$(date -d '-1 day' +%s) + KEEP_DAILIES_DATE=$(date -d '-1 month' +%s) + ;; + darwin*) + KEEP_ALL_DATE=$(date -j -f "%a %b %d %T %Z %Y" "`date -v -1d`" "+%s") + KEEP_DAILIES_DATE=$(date -j -f "%a %b %d %T %Z %Y" "`date -v -1m`" "+%s") + ;; +esac # ----------------------------------------------------------------------------- # Create profile folder if it doesn't exist @@ -188,7 +200,6 @@ while [ "1" ]; do prev=$date done - # ----------------------------------------------------------------------------- # Start backup # ----------------------------------------------------------------------------- -- cgit v1.2.3 From 9b9a564447cc6bf1aa99b65e57c3ecafefe9aca4 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Fri, 15 Nov 2013 10:35:04 -0800 Subject: More robust date handling. Previously the date handling expiry logic had a bug where if you had backups that were a year apart to the day, it wouldn't notice the difference in year and only notice that the month was the same, and expire the older one (eg, if you had a backup on 2012-04-01 and another on 2013-04-01, it'd delete the one from 2012. This commit makes it compare the full date string instead of just the month, so that it more robustly keeps older backups. --- rsync_tmbackup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 1bad059..10ec4f7 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -190,11 +190,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} ] && fn_expire_backup "$fname" + [ "${date:0:10}" == "${prev:0:10}" ] && fn_expire_backup "$fname" else # Delete all but the most recent of each month. - [ ${date:5:2} -eq ${prev:5:2} ] && fn_expire_backup "$fname" + [ "${date:0:7}" == "${prev:0:7}" ] && fn_expire_backup "$fname" fi prev=$date -- cgit v1.2.3 From f011e1a7ab0f2dc4fc6de85b7a62e65d6f80fa4b Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Fri, 15 Nov 2013 11:08:28 -0800 Subject: More elegant cross-platform date logic. --- rsync_tmbackup.sh | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 10ec4f7..3aa0712 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -97,25 +97,20 @@ fi # Setup additional variables # ----------------------------------------------------------------------------- -export IFS=$'\n' # Better for handling spaces in filenames. +# Date logic NOW=$(date +"%Y-%m-%d-%H%M%S") +EPOCH=$(date "+%s") +KEEP_ALL_DATE=$(($EPOCH - 86400)) # 1 day ago +KEEP_DAILIES_DATE=$(($EPOCH - 2678400)) # 31 days ago + + +export IFS=$'\n' # Better for handling spaces in filenames. PROFILE_FOLDER="$HOME/.rsync_tmbackup" LOG_FILE="$PROFILE_FOLDER/$NOW.log" DEST=$DEST_FOLDER/$NOW PREVIOUS_DEST=$(fn_find_backups | sort | tail -n 1) INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress -case "$OSTYPE" in - linux*) - KEEP_ALL_DATE=$(date -d '-1 day' +%s) - KEEP_DAILIES_DATE=$(date -d '-1 month' +%s) - ;; - darwin*) - KEEP_ALL_DATE=$(date -j -f "%a %b %d %T %Z %Y" "`date -v -1d`" "+%s") - KEEP_DAILIES_DATE=$(date -j -f "%a %b %d %T %Z %Y" "`date -v -1m`" "+%s") - ;; -esac - # ----------------------------------------------------------------------------- # Create profile folder if it doesn't exist # ----------------------------------------------------------------------------- -- cgit v1.2.3 From 28735b181586606e107a471163e8c6b109016446 Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Fri, 15 Nov 2013 12:01:52 -0800 Subject: Create "latest" symlink pointing at the most recent completed backup. --- rsync_tmbackup.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 3aa0712..66eab2e 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -279,6 +279,10 @@ while [ "1" ]; do exit $RSYNC_EXIT_CODE fi + # Force creation of 'latest' symlink pointing at the new $DEST. + rm -rf -- "$DEST_FOLDER/latest" + ln -vs "$(basename "$DEST")" "$DEST_FOLDER/latest" + rm -- "$INPROGRESS_FILE" # TODO: grep for "^rsync error:.*$" in log fn_log_info "Backup completed without errors." -- cgit v1.2.3 From a474f139f57261cdca8110cabc1b9c607a31c1de Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Fri, 15 Nov 2013 23:02:24 -0800 Subject: Stop creating "latest" symlink (implemented in a different branch). This reverts commit 28735b181586606e107a471163e8c6b109016446. --- rsync_tmbackup.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 66eab2e..3aa0712 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -279,10 +279,6 @@ while [ "1" ]; do exit $RSYNC_EXIT_CODE fi - # Force creation of 'latest' symlink pointing at the new $DEST. - rm -rf -- "$DEST_FOLDER/latest" - ln -vs "$(basename "$DEST")" "$DEST_FOLDER/latest" - rm -- "$INPROGRESS_FILE" # TODO: grep for "^rsync error:.*$" in log fn_log_info "Backup completed without errors." -- cgit v1.2.3