Review your favorite Linux distribution.
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, 11:12 AM   #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, 11:18 AM   #2
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,492

Rep: Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091Reputation: 2091
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 11:33 AM.
Old 02-26-2014, 11:24 AM   #3
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 3,440

Rep: Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508
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 11:25 AM.
1 members found this post helpful.
Old 02-26-2014, 12: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-18-2010 11:35 PM
can expect be called from within a bash script? johnpaulodonnell Programming 4 06-21-2007 09:42 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

All times are GMT -5. The time now is 10:29 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