From a40a733902eec7a8fa22364fa7bd741892bbab30 Mon Sep 17 00:00:00 2001 From: rastkov Date: Tue, 29 Oct 2013 12:07:30 +0100 Subject: Update rsync_tmbackup.sh Using find instead of ls to find the last and second last directory used for backup. --- rsync_tmbackup.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index bb961c2..df3413f 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -39,8 +39,9 @@ fi NOW=$(date +"%Y-%m-%d-%H%M%S") DEST=$DEST_FOLDER/$NOW -LAST_TIME=$(ls -1 $DEST_FOLDER | grep "\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d" | tail -n 1) -PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME +LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 1) +echo "############################################ $LAST_TIME" +PREVIOUS_DEST=$LAST_TIME INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress # ----------------------------------------------------------------------------- @@ -52,10 +53,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. echo "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." - LINE_COUNT=$(ls -1 $DEST_FOLDER | grep "\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d" | tail -n 2 | wc -l) + LINE_COUNT=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 2 | wc -l) mv $PREVIOUS_DEST $DEST if [ "$LINE_COUNT" -gt 1 ]; then - SECOND_LAST_TIME=$(ls -1 $DEST_FOLDER | grep "\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d" | tail -n 2 | head -n 1) + SECOND_LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 2 | head -n 1) LAST_TIME=$SECOND_LAST_TIME else LAST_TIME="" -- cgit v1.2.3 From 8318db90c5e8975e181735bf9c68b9bc0b54731e Mon Sep 17 00:00:00 2001 From: rastkov Date: Tue, 29 Oct 2013 12:15:38 +0100 Subject: Update rsync_tmbackup.sh --- rsync_tmbackup.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index df3413f..3328b83 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -40,7 +40,6 @@ fi NOW=$(date +"%Y-%m-%d-%H%M%S") DEST=$DEST_FOLDER/$NOW LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 1) -echo "############################################ $LAST_TIME" PREVIOUS_DEST=$LAST_TIME INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress -- cgit v1.2.3 From 27a0a1881f36ce41fc5d6f21de2df9f96e812339 Mon Sep 17 00:00:00 2001 From: rastkov Date: Fri, 1 Nov 2013 08:48:21 +0100 Subject: Update rsync_tmbackup.sh Using find with regex instead of glob --- rsync_tmbackup.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 3328b83..60adb6a 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -39,7 +39,7 @@ fi NOW=$(date +"%Y-%m-%d-%H%M%S") DEST=$DEST_FOLDER/$NOW -LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 1) +LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 1) PREVIOUS_DEST=$LAST_TIME INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress @@ -52,10 +52,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. echo "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." - LINE_COUNT=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 2 | wc -l) + LINE_COUNT=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 2 | wc -l) mv $PREVIOUS_DEST $DEST if [ "$LINE_COUNT" -gt 1 ]; then - SECOND_LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -iname "*-*[0-9]" | sort | tail -n 2 | head -n 1) + SECOND_LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 2 | head -n 1) LAST_TIME=$SECOND_LAST_TIME else LAST_TIME="" -- cgit v1.2.3 From 99258d80abc2411e14a4ead5cdc019842b322d6e Mon Sep 17 00:00:00 2001 From: rastkov Date: Fri, 1 Nov 2013 14:32:59 +0100 Subject: Update rsync_tmbackup.sh Set LAST_TIME, LINE_CONUT and SECOND_LAST_TIME based on `uname -s` output for Mac OS compatibility. --- rsync_tmbackup.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 60adb6a..8a0909c 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -39,7 +39,23 @@ fi NOW=$(date +"%Y-%m-%d-%H%M%S") DEST=$DEST_FOLDER/$NOW -LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 1) + +# Set LAST_TIME, LINE_CONUT and SECOND_LAST_TIME based on uname -s output for Mac OS compatibility +# I guess there might be an more elegant way to do this +case `uname -s` in + Darwin) + LAST_TIME=$(find -E $DEST_FOLDER -type d -iregex "./[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 1) + LINE_COUNT=$(find -E $DEST_FOLDER -type d -iregex "./[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 2 | wc -l) + SECOND_LAST_TIME=$(find -E $DEST_FOLDER -type d -iregex "./[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 2 | head -n 1) + ;; + + *) + LAST_TIME=$(find $DEST_FOLDER -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 1) + LINE_COUNT=$(find $DEST_FOLDER -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 2 | wc -l) + SECOND_LAST_TIME=$(find $DEST_FOLDER -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" -prune | sort | tail -n 2 | head -n 1) + ;; +esac + PREVIOUS_DEST=$LAST_TIME INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress @@ -52,10 +68,9 @@ 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. echo "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." - LINE_COUNT=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 2 | wc -l) + mv $PREVIOUS_DEST $DEST - if [ "$LINE_COUNT" -gt 1 ]; then - SECOND_LAST_TIME=$(find $DEST_FOLDER -maxdepth 1 -type d -regextype posix-extended -regex "^.*[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}" | sort | tail -n 2 | head -n 1) + if [ "$LINE_COUNT" -gt 1 ]; then LAST_TIME=$SECOND_LAST_TIME else LAST_TIME="" -- cgit v1.2.3 From 2fd6b109f02d1f1cc88c86d8a779a5c6ec601ff1 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 13 Nov 2013 17:46:20 +0800 Subject: Using find instead of ls to find the last and second last directory used for backup. --- rsync_tmbackup.sh | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 2364deb..754189f 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -74,13 +74,12 @@ fi # Setup additional variables # ----------------------------------------------------------------------------- -BACKUP_FOLDER_PATTERN="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]$" +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 -LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 1) -PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME +PREVIOUS_DEST=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 1) INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress # ----------------------------------------------------------------------------- @@ -97,19 +96,18 @@ fi # ----------------------------------------------------------------------------- if [ -f "$INPROGRESS_FILE" ]; then - if [ "$LAST_TIME" != "" ]; then + if [ "$PREVIOUS_DEST" != "" ]; 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=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | wc -l) + LINE_COUNT=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 2 | wc -l) mv -- "$PREVIOUS_DEST" "$DEST" if [ "$LINE_COUNT" -gt 1 ]; then - SECOND_LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | head -n 1) - LAST_TIME=$SECOND_LAST_TIME + PREVIOUS_PREVIOUS_DEST=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | sort | tail -n 2 | head -n 1) + PREVIOUS_DEST=$PREVIOUS_PREVIOUS_DEST else - LAST_TIME="" + PREVIOUS_DEST="" fi - PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME fi fi @@ -121,7 +119,7 @@ while [ "1" ]; do # ----------------------------------------------------------------------------- LINK_DEST_OPTION="" - if [ "$LAST_TIME" == "" ]; then + if [ "$PREVIOUS_DEST" == "" ]; then fn_log_info "No previous backup - creating new one." else # If the path is relative, it needs to be relative to the destination. To keep @@ -201,20 +199,18 @@ while [ "1" ]; do fn_log_warn "No space left on device - removing oldest backup and resuming." - BACKUP_FOLDER_COUNT=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | wc -l) + BACKUP_FOLDER_COUNT=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | 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 - OLDEST_BACKUP=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | head -n 1) - if [ "$OLDEST_BACKUP" == "" ]; then + OLD_BACKUP_PATH=$(find "$DEST_FOLDER" -type d -name "$BACKUP_FOLDER_PATTERN" -prune | 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 - - OLD_BACKUP_PATH="$DEST_FOLDER/$OLDEST_BACKUP" - + # 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 -- cgit v1.2.3 From 8a2e91813b0ccd8989870a87e36ec3b609942afa Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 13 Nov 2013 17:57:51 +0800 Subject: Update README.md Now done: "Check if there's enough space in the destination before doing the backup. Also automatically delete old backups." --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81ed1d4..e36e090 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ An optional exclude file can be provided as a third parameter. It should be comp # TODO -* 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. +* Other minor changes (see TODO comments in the source). + # LICENSE [MIT](http://opensource.org/licenses/MIT) -- cgit v1.2.3