Register
It is currently Sat Nov 01, 2014 1:35 am

rsync script - variable substitution problem


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Mar 23, 2010 4:02 am   

Joined: Tue Mar 23, 2010 3:15 am
Posts: 2
Hi folks,

I am trying to automate part of my backup routines and have brickwalled myself with what appears to be a quoting problem. You will see that I have a "workaround": I create a temporary script and execute it. However, if I try to do the more intuitive "execution in place", it seems that rsync complains.

Any idea why I am not able to execute the command without creating the temporary script.

Code:
#!/bin/bash

# binary executables

RSYNC="/usr/bin/rsync"
RSSH="/usr/bin/ssh"

# rsync

RSYNC_OPTS="-avzPO"
RSYNC_LOG="--log-file=/home/ubundom/log/rsync.log"
RSYNC_SOURCE="/home/ubundom/test\ source/"
RSYNC_TARGET="/home/ubundom/test\ source"

# backup server

        SLUG_HOST="slug"
   SLUG_PORT="22"
        USER="ubundom"

   
   RSYNC_RSH="-e \"$RSSH -p$SLUG_PORT -C\""
         
   # Executing exactly the same string at the command prompt does not work:
   $RSYNC   $RSYNC_OPTS   $RSYNC_RSH   $RSYNC_LOG   $RSYNC_SOURCE   $USER@$SLUG_HOST:\"$RSYNC_TARGET\"
      # error message
      #  rsync: -p22: unknown option
      #  rsync error: syntax or usage error (code 1) at main.c(1440) [client=3.0.6]

   # Workaround: Create and execute a script file:
   echo $RSYNC   $RSYNC_OPTS   $RSYNC_LOG   $RSYNC_RSH   $RSYNC_SOURCE   $USER@$SLUG_HOST:\"$RSYNC_TARGET\" > /tmp/rsync$$.sh
   chmod +x /tmp/rsync$$.sh ; /tmp/rsync$$.sh ; rm /tmp/rsync$$.sh


Thanks for the help on this site.


Top
 Profile  
 PostPosted: Mon Oct 31, 2011 3:14 am   

Joined: Mon Oct 31, 2011 1:55 am
Posts: 1
I have the same sort of thing with my script. If I run the output of the echo command it works, but the same command as part of this script doesn't and I get the following error:
rsync: -rptgoDLKv --safe-links --exclude-from=/home/admin/etc/rsync-exclude: unknown option

I'm spooked.


script with error:
DOMAINS=/home/admin/etc/webserver.conf
STRIPPED=/tmp/webserver
PREFIX="`date` WebServer |"
ARGS="-rptgoDLKv --safe-links --exclude-from=/home/admin/etc/rsync-exclude"

# strip the conf file of comments
egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' $DOMAINS > $STRIPPED
# set the file separator
IFS="|"
for file in $STRIPPED
do
if [ -e "$file" ]
then
while read -r USER DOMAIN
do
echo "rsync $ARGS ${USER}@${DOMAIN}:/home/${USER}/ $TARGET/webserver/${DOMAIN}/" # this works as a terminal command!
rsync $ARGS $USER@$DOMAIN:/home/$USER/ $TARGET/webserver/${DOMAIN}/ # this generates the unknown option
if [ "$?" = "0" ]
then
echo "$PREFIX Backup of $DOMAIN successfull" >> $LOGFILE
else
MSG="$PREFIX ERROR ($?) backing up $DOMAIN" >> $LOGFILE
echo $MSG >> $LOGFILE
echo $MSG | mailx -s "Webserver: Error daily syncing etc" $EMAIL
fi
done < "$file"
fi
done
unset IFS

short script which does works:
DOMAIN=mydomain.com
SOURCE="myuser@$DOMAIN:/home/myuser/"
if rsync $ARGS $SOURCE /media/nas/admin/webserver/$DOMAIN/
then
echo "$PREFIX Backup of $DOMAIN successfull" >> $LOGFILE
else
MSG="$PREFIX ERROR ($?) backing up $DOMAIN" >> $LOGFILE
echo $MSG >> $LOGFILE
echo $MSG | mailx -s "Webserver: Error daily syncing etc" $EMAIL
fi


Top
 Profile  
 PostPosted: Mon Oct 31, 2011 4:11 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
ubundom wrote:
Any idea why I am not able to execute the command without creating the temporary script.

Yes, it is a quoting problem. Try adding -xv to your shebang line to see what you really tell the shell to execute.


Top
 Profile  
 PostPosted: Sat Nov 05, 2011 8:57 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You both have to use an array to store the arguments. It fails because of wordsplitting.

Code:
[...]
RSYNC_ARGS=("$RSYNC_OPTS" "$RSYNC_RSH" "$RSYNC_LOG" "$RSYNC_SOURCE" "$USER@SLUG_HOST:\"$RSYNC_TARGET\"")
rsync "${RSYNC_ARGS[@]}"
[...]


Also, you don't need to explicitly write where the binaries are. Just adjust your PATH. (and I hope it contains /usr/bin, heh...)

----

Code:
ARGS=(-rptgoDLKv --safe-links --exclude-from=/home/admin/etc/rsync-exclude)
[...]
rsync "${ARGS[@]}" "$USER@$DOMAIN:/home/$USER/$TARGET/webserver/$DOMAIN/"


----

And don't use uppercase variables. They're meant to be variables of the shell environment, per convention, so you could overwrite one not on purpose.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP