Rsync syntax error when run from bash script

11,192

Solution 1

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.

Solution 2

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

eval rsync "$OPT1 $SRC $TRG"

Solution 3

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}
Share:
11,192
KroniK907
Author by

KroniK907

Updated on June 04, 2022

Comments

  • KroniK907
    KroniK907 almost 2 years

    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