Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-26-2014, 12:12 PM   #1
LQ Newbie
Registered: Aug 2012
Posts: 13

Rep: Reputation: Disabled
Rsync gives error but only when called via bash script

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.

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
#link directory
#target directory
#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
Old 02-26-2014, 12:18 PM   #2
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,559

Rep: Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116
Remove the quotes in your rsync call. Quotes break rsync's ability to parse the individual arguments.

For example:
-aAXv --delete
Should expand to
two separate arguments inside rsync. But by putting quotes around it you're forcing rsync to treat it as ONE argument, which is invalid.

If you want to play around with how arguments will expand given your quoting style, put this into a new code:
for i in "$@"; do
   echo $i
and then replace "rsync" in your call with this new script name and see what it prints

For example
$ ./script a b c
$ ./script "a b" c
a b
And using your call:
$ HOST=host
$ NAME=name
$ TODAY=today
$ LAST=last
$ SRC="$MNT"
$ LNK="/backup/$HOST/$NAME/$LAST/"
$ TRG="/backup/$HOST/$NAME/$TODAY/"
$ OPT1="-aAXv --delete --progress --link-dest=$LNK"
$ ./script "$OPT1 $SRC $TRG"
-aAXv --delete --progress --link-dest=/backup/host/name/last/ /backup/host/name/today/
when it SHOULD look like
$./script $OPT1 $SRC $TRG
You should inject some spaces into your host, name, today, last, and src variables to make sure everything is handled properly.

Also, variable names in all caps are generally reserved for environment variables rather than local script variables. It improves readability later on.

Last edited by suicidaleggroll; 02-26-2014 at 12:33 PM.
Old 02-26-2014, 12:24 PM   #3
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 3,615

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Originally Posted by KroniK View Post
rsync "$OPT1 $SRC $TRG" > /var/log/backup/backup.rsync.log 2>&1
The way you have the entire set of arguments quoted it is being presented to rsync as a single argument, and since that begins with '-' it is interpreted as a (mostly illegal) set of options. You can leave OPT1 unquoted and quote the SRC and TRG arguments individually.
rsync $OPT1 "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1
That will have a problem if $LNK contains white space. You might keep that out of OPT1 and include it explicitly.
rsync $OPT1 --link-dest="$LNK" "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1
[EDIT] There's an echo in here.

Last edited by rknichols; 02-26-2014 at 12:25 PM.
1 members found this post helpful.
Old 02-26-2014, 01:41 PM   #4
LQ Newbie
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Essentially these replies were correct, so thank you! I ended up putting all the arguments in an array and then calling the array.

Here is the modified code:

mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
#link directory
#target directory
#rsync options
OPT1=( "-aAXv" "--delete" "--progress" "$LNK" )

#run the rsync command
echo "rsync ${OPT1[@]} $SRC $TRG"
rsync "${OPT1[@]}" "$SRC" "$TRG" > /var/log/backup/backup.rsync.log 2>&1
The above setup fixed all problems and it works fine now


bash, error, rsync

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading env into bash script called by cron grob115 Programming 1 03-19-2010 12:35 AM
can expect be called from within a bash script? johnpaulodonnell Programming 4 06-21-2007 10:42 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 01:50 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration