LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-26-2014, 12:12 PM   #1
KroniK
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:

Code:
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:

Code:
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:

Code:
 + 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.

Code:
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
 
Old 02-26-2014, 12:18 PM   #2
suicidaleggroll
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:
Code:
-aAXv --delete
Should expand to
Code:
-aAXv
and
Code:
--delete
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:
Code:
#!/bin/bash
for i in "$@"; do
   echo $i
done
and then replace "rsync" in your call with this new script name and see what it prints

For example
Code:
$ ./script a b c
a
b
c
$ ./script "a b" c
a b
c
And using your call:
Code:
$ 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
Code:
$./script $OPT1 $SRC $TRG
-aAXv
--delete
--progress
--link-dest=/backup/host/name/last/
/backup/host/name/today/
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
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,615

Rep: Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577Reputation: 1577
Quote:
Originally Posted by KroniK View Post
Code:
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.
Code:
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.
Code:
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
KroniK
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:

Code:
mkdir -p "/backup/$HOST/$NAME/$TODAY"
#source directory
SRC="$MNT"
#link directory
LNK="--link-dest=/backup/$HOST/$NAME/$LAST/"
#target directory
TRG="/backup/$HOST/$NAME/$TODAY/"
#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
 
  


Reply

Tags
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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