3

I have been working on a backup script that uses rsync to do an incremental backup.

I have tested the following rsync command manually, and it runs and completes a backup without error:

rsync -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/

however when I run that same command in my backup script it gives me the following error:

rsync: -aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

I ran bash -x on my script to figure out exactly what is sent to the console and here is what was printed:

+ rsync '-aAXv --delete --progress --link-dest=/backup/Uyuk/Uyuk-backup-part1/2014-02-24/ /mnt/backup/ /backup/Uyuk/Uyuk-backup-part1/2014-02-25/'

Does anyone see what is wrong? I cant find anything that would cause the syntax error.

EDIT: Here is the actual code I have in the script, and this is a pretty large script so yes some variables are not defined here, but you get the idea.

mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPT1="-aAXv --delete --progress --link-dest=$LNK"

#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "$OPT1 $SRC $TRG" > /var/log/backup/backup.rsync.log 2>&1
4
  • Why there is single quotes around the rsync options in the bash -x output of your script? Can you include in your question the acutally statements that you use to call rsync? Commented Feb 26, 2014 at 2:24
  • 3
    typically a leading + sign and single-quoted strings are the way that set -x displays the line with all var expansions and string quoting, where all "..." are now "boiled down" to '...' (single-quoted strings). I think you're right that something is missing, I would guess O.P. is doing something like rOpts="-aXV --delete..."; rsync "$rOpts". Hence the error msg unknown option, Good luck to all. Commented Feb 26, 2014 at 3:01
  • If @shellter is right, your first stop should be BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail! Commented Feb 26, 2014 at 6:53
  • I updated and included the code im using for running the rsync command. Commented Feb 26, 2014 at 17:08

3 Answers 3

5

You are passing your option list as a single argument, when it needs to be passed as a list of arguments. In general, you should use an array in bash to hold your arguments, in case any of them contain whitespace. Try the following:

mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#rsync options
OPTS=( "-aAXv" "--delete" "--progress" "--link-dest=$LNK" )

#run the rsync command
echo "rsync $OPT1 $SRC $TRG"
rsync "${OPTS[@]}" "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1

An array expansion ${OPTS[@]}, when quoted, is treated specially as a sequence of arguments, each of which is quoted individually to preserve any whitespace or special characters in the individual elements. If arr=("a b" c d), then echo "${arr[@]}" is the same as

echo "a b" "c" "d"

rather than

echo "a b c d"

This will not work in a shell that doesn't support arrays, but then, arrays were invented because there wasn't a safe way (that is, without using eval) to handle this use case without them.

Sign up to request clarification or add additional context in comments.

Comments

1

This:

rsync "$OPT1 $SRC $TRG"

passes all your intended arguments lumped together as one argument, which rsync doesn't know how to deal with.

Try this instead:

rsync ${OPT1} ${SRC} ${TRG}

Comments

1

The approach suggested by @chepner didn't work on my Mac OS X (10.9.4), but eval did.

eval rsync "$OPT1 $SRC $TRG"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.