Monday, 27 May 2013

Bash scripting rsync puzzler

Bash scripting rsync puzzler

Frustrating. The script below work for the first two targets, but not the second.
#!bin/bash
...
backitup () {
Todaysdate=`date "+%Y-%m-%dT%H_%M_%S"`
Sourcedir=$1
Destination=$2
Username=$(echo $Sourcedir | cut -f1 -d'@')
if [ "$Username" = "root" ] ; then
    RSYNCPATH="rsync"
else
    RSYNCPATH="sudo rsync"
fi

cmd="sudo rsync -vazP --rsync-path='$RSYNCPATH' --log-file=/var/log/rsync.log $Sourcedir /BACKUPS/$Destination/back-$Todaysdate"
                echo $cmd >> ~/cmd.txt
                $cmd
...
}
backitup root@HAZns01:/etc "HAZNS01/etc"
backitup root@HAZns01:/home/ftpusers "HAZNS01/ftpusers"
backitup jakadmin@HAZFAND10:/etc "HAZFAND10/etc"
backitup jakadmin@HAZFAND11:/etc "HAZFAND11/etc"
The second two calls give:
unexpected remote arg: jakadmin@HAZFAND10:/etc
If however I run then run the contents of cmd.txt manually from the commandline (bash cmd.txt) then it runs fine. How can that be? (I've got passwordless ssh all setup correctly of course)
What am I missing?

No comments:

Post a Comment