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.
This commit is contained in:
David D Lowe
2013-10-30 13:45:48 +00:00
parent 1b0b115dd0
commit 396b42d483
+20 -12
View File
@@ -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