From 396b42d4832f6d248230bb5eb36db747e3503028 Mon Sep 17 00:00:00 2001 From: David D Lowe Date: Wed, 30 Oct 2013 13:45:48 +0000 Subject: [PATCH 1/2] Deal with arguments with special characters better Script now handles filenames with special characters (such as spaces, hard tabs, newlines, double quotes and a dash prefix). The only exceptions are filenames with single quotes. In this case, the script will fail early with a clear error message. --- rsync_tmbackup.sh | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index bb961c2..8a22549 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -19,6 +19,13 @@ SRC_FOLDER=${1%/} DEST_FOLDER=${2%/} EXCLUSION_FILE=$3 +for arg in "$SRC_FOLDER" "$DEST_FOLDER" "$EXCLUSION_FILE"; do + if [[ "$arg" == *"'"* ]] ; then + echo 'Arguments may not have any single quote characters!' + exit 1 + fi +done + # ----------------------------------------------------------------------------- # Check that the destination drive is a backup drive # ----------------------------------------------------------------------------- @@ -28,7 +35,7 @@ if [ ! -f "$DEST_MARKER_FILE" ]; then echo "Safety check failed - the destination does not appear to be a backup folder or drive (marker file not found)." echo "If it is indeed a backup folder, you may add the marker file by running the following command:" echo "" - echo "touch \"$DEST_MARKER_FILE\"" + echo "touch -- \"$DEST_MARKER_FILE\"" echo "" exit 1 fi @@ -39,7 +46,7 @@ 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) +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 INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress @@ -51,11 +58,11 @@ if [ -f "$INPROGRESS_FILE" ]; then if [ "$LAST_TIME" != "" ]; 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) - mv $PREVIOUS_DEST $DEST + 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) + 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=$(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) LAST_TIME=$SECOND_LAST_TIME else LAST_TIME="" @@ -74,7 +81,7 @@ if [ "$LAST_TIME" == "" ]; then else # If the path is relative, it needs to be relative to the destination. To keep # it simple, just use an absolute path. See http://serverfault.com/a/210058/118679 - PREVIOUS_DEST=`cd \`dirname "$PREVIOUS_DEST"\`; pwd`"/"`basename "$PREVIOUS_DEST"` + PREVIOUS_DEST=`cd \`dirname -- "$PREVIOUS_DEST"\`; pwd`"/"`basename -- "$PREVIOUS_DEST"` echo "Previous backup found - doing incremental backup from $PREVIOUS_DEST" LINK_DEST_OPTION="--link-dest=$PREVIOUS_DEST" fi @@ -85,7 +92,7 @@ fi if [ ! -d "$DEST" ]; then echo "Creating destination $DEST" - mkdir -p $DEST + mkdir -p -- $DEST fi # ----------------------------------------------------------------------------- @@ -107,19 +114,20 @@ CMD="$CMD --one-file-system" CMD="$CMD --archive" CMD="$CMD --progress" if [ "$EXCLUSION_FILE" != "" ]; then - CMD="$CMD --exclude-from \"$EXCLUSION_FILE\"" + # We've already checked that $EXCLUSION_FILE doesn't contain a single quote + CMD="$CMD --exclude-from '$EXCLUSION_FILE'" fi -CMD="$CMD $LINK_DEST_OPTION $SRC_FOLDER/ $DEST/" +CMD="$CMD -- '$LINK_DEST_OPTION' '$SRC_FOLDER/' '$DEST/'" CMD="$CMD | grep -E '^deleting|[^/]$'" echo "Running command:" echo $CMD -touch $INPROGRESS_FILE +touch -- $INPROGRESS_FILE eval $CMD EXIT_CODE=$? if [ "$EXIT_CODE" == "0" ]; then - rm $INPROGRESS_FILE + rm -- $INPROGRESS_FILE else echo "Error: Exited with error code $EXIT_CODE" fi From 24540f33ee667ec81be4610d6807f5dff3e8ae65 Mon Sep 17 00:00:00 2001 From: David D Lowe Date: Thu, 7 Nov 2013 08:51:12 +0000 Subject: [PATCH 2/2] Handle source and dest arguments with spaces --- rsync_tmbackup.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh index 8a22549..f4c71a8 100644 --- a/rsync_tmbackup.sh +++ b/rsync_tmbackup.sh @@ -46,7 +46,7 @@ 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) +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 INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress @@ -59,10 +59,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) - mv -- $PREVIOUS_DEST $DEST + 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) + 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=$(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) LAST_TIME=$SECOND_LAST_TIME else LAST_TIME="" @@ -92,7 +92,7 @@ fi if [ ! -d "$DEST" ]; then echo "Creating destination $DEST" - mkdir -p -- $DEST + mkdir -p -- "$DEST" fi # ----------------------------------------------------------------------------- @@ -123,11 +123,11 @@ CMD="$CMD | grep -E '^deleting|[^/]$'" echo "Running command:" echo $CMD -touch -- $INPROGRESS_FILE +touch -- "$INPROGRESS_FILE" eval $CMD EXIT_CODE=$? if [ "$EXIT_CODE" == "0" ]; then - rm -- $INPROGRESS_FILE + rm -- "$INPROGRESS_FILE" else echo "Error: Exited with error code $EXIT_CODE" fi