LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rsync ssh - how to execute shell commands on server (http://www.linuxquestions.org/questions/linux-newbie-8/rsync-ssh-how-to-execute-shell-commands-on-server-694776/)

banjer 01-03-2009 06:21 PM

rsync ssh - how to execute shell commands on server
 
Hi,
I'm relatively new to Linux and I'm trying to set up a script to backup machine A files to machine B. I have my ssh key all set up and can rsync just fine to copy files over, but I want to be able to run some additional commands on the remote host (the machine I'm connecting to) BEFORE actually copying files over. I am new to shell scripting. Here is my script so far:


Code:

HOST=server.myhost.com
SRC=/var/www/html/testd
DEST=/home/banjer/backup.0
USER=banjer

rsync -avz -e "ssh -i /home/theclient/.ssh/id_dsa 'rm -rf backup.3; mv backup.2 backup.3; mv backup.1 backup.2; cp -al backup.0 backup.1'" \
$SRC $USER@$HOST:$DEST

When I run the above script on machine A, I get the error:

Quote:

ssh: rm -rf backup.3; mv backup.2 backup.3; mv backup.1 backup.2; cp -al backup.0 backup.1: Name or service not known
Whats the best way to embed those commands within the rsync/ssh command? Also, can I put those shell commands in a variable or function to tidy things up? I may have more commands to add to that so could get messy. I appreciate the help!

Tinkster 01-03-2009 08:48 PM

Hi, and welcome to LQ!

All you need to do is to run those command separately before
you do your rsync.

This is (of course) untested. I've replaced your semi-colons with
&& so the subsequent command only gets executed if the previous
one was successful.

Code:

ssh -i /home/theclient/.ssh/id_dsa "rm -rf backup.3 && mv backup.2 backup.3 && mv backup.1 backup.2 && cp -al backup.0 backup.1"
rsync -avz -e "ssh -i /home/theclient/.ssh/id_dsa" $SRC $USER@$HOST:$DEST



Cheers,
Tink

banjer 01-04-2009 08:42 AM

That makes total sense. I had to add was $USER@$HOST to the first ssh command, but otherwise the following works great:

Code:

ssh -i /home/theclient/.ssh/id_dsa $USER@$HOST 'rm -rf backup.3 && mv backup.2 backup.3 && mv backup.1 backup.2 && cp -al backup.0 backup.1'
rsync -avz -e "ssh -i /home/theclient/.ssh/id_dsa" $SRC $USER@$HOST:$DEST

The && separator is a nice tip too. Now I can call this script from cron and let 'er rip. Thanks for the help.

-banjer

ineya 07-01-2009 04:49 AM

I have the same issue, but I can NOT run the command separately.
I need to start up ClearCase view after logging in, so that I can see content of vobs.
Is there any way to do it?

I tried it this way, but I wasn't successful:
Code:

rsync -avz -e 'ssh -o "LocalCommand ./startView"' user@10.77.62.20:/vobs/Build/makefile .

olexiyb 11-04-2009 02:01 PM

--rsync-path option
 
You can use --rsync-path option to run command on remote side before rsync itself.

Quote:

rsync --rsync-path='command_before_rsync && rsync'


All times are GMT -5. The time now is 08:45 PM.