From d425a05528718d4d788ca1bd0d33f1c54d4fedfe Mon Sep 17 00:00:00 2001 From: Robert Bruce Park Date: Wed, 13 Nov 2013 00:01:17 -0800 Subject: [PATCH] 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 # -----------------------------------------------------------------------------