Merge pull request #11 from Flimm/master

Deal with arguments with special characters better
This commit is contained in:
Laurent
2013-11-07 01:06:50 -08:00
+20 -12
View File
@@ -19,6 +19,13 @@ SRC_FOLDER=${1%/}
DEST_FOLDER=${2%/} DEST_FOLDER=${2%/}
EXCLUSION_FILE=$3 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 # 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 "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 "If it is indeed a backup folder, you may add the marker file by running the following command:"
echo "" echo ""
echo "touch \"$DEST_MARKER_FILE\"" echo "touch -- \"$DEST_MARKER_FILE\""
echo "" echo ""
exit 1 exit 1
fi fi
@@ -39,7 +46,7 @@ fi
NOW=$(date +"%Y-%m-%d-%H%M%S") NOW=$(date +"%Y-%m-%d-%H%M%S")
DEST=$DEST_FOLDER/$NOW 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 PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME
INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress
@@ -51,11 +58,11 @@ if [ -f "$INPROGRESS_FILE" ]; then
if [ "$LAST_TIME" != "" ]; then if [ "$LAST_TIME" != "" ]; then
# - Last backup is moved to current backup folder so that it can be resumed. # - Last backup is moved to current backup folder so that it can be resumed.
# - 2nd to last backup becomes last backup. # - 2nd to last backup becomes last backup.
echo "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." 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=$(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 mv -- "$PREVIOUS_DEST" "$DEST"
if [ "$LINE_COUNT" -gt 1 ]; then 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 LAST_TIME=$SECOND_LAST_TIME
else else
LAST_TIME="" LAST_TIME=""
@@ -74,7 +81,7 @@ if [ "$LAST_TIME" == "" ]; then
else else
# If the path is relative, it needs to be relative to the destination. To keep # 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 # 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" echo "Previous backup found - doing incremental backup from $PREVIOUS_DEST"
LINK_DEST_OPTION="--link-dest=$PREVIOUS_DEST" LINK_DEST_OPTION="--link-dest=$PREVIOUS_DEST"
fi fi
@@ -85,7 +92,7 @@ fi
if [ ! -d "$DEST" ]; then if [ ! -d "$DEST" ]; then
echo "Creating destination $DEST" echo "Creating destination $DEST"
mkdir -p $DEST mkdir -p -- "$DEST"
fi fi
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@@ -107,19 +114,20 @@ CMD="$CMD --one-file-system"
CMD="$CMD --archive" CMD="$CMD --archive"
CMD="$CMD --progress" CMD="$CMD --progress"
if [ "$EXCLUSION_FILE" != "" ]; then 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 fi
CMD="$CMD $LINK_DEST_OPTION $SRC_FOLDER/ $DEST/" CMD="$CMD -- '$LINK_DEST_OPTION' '$SRC_FOLDER/' '$DEST/'"
CMD="$CMD | grep -E '^deleting|[^/]$'" CMD="$CMD | grep -E '^deleting|[^/]$'"
echo "Running command:" echo "Running command:"
echo $CMD echo $CMD
touch $INPROGRESS_FILE touch -- "$INPROGRESS_FILE"
eval $CMD eval $CMD
EXIT_CODE=$? EXIT_CODE=$?
if [ "$EXIT_CODE" == "0" ]; then if [ "$EXIT_CODE" == "0" ]; then
rm $INPROGRESS_FILE rm -- "$INPROGRESS_FILE"
else else
echo "Error: Exited with error code $EXIT_CODE" echo "Error: Exited with error code $EXIT_CODE"
fi fi