LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-03-2009, 06:21 PM   #1
banjer
LQ Newbie
 
Registered: Jan 2009
Location: Baltimore, MD
Posts: 3

Rep: Reputation: 0
Question 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!
 
Old 01-03-2009, 08:48 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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
 
Old 01-04-2009, 08:42 AM   #3
banjer
LQ Newbie
 
Registered: Jan 2009
Location: Baltimore, MD
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 07-01-2009, 04:49 AM   #4
ineya
Member
 
Registered: Jul 2007
Posts: 39

Rep: Reputation: 16
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 .
 
Old 11-04-2009, 02:01 PM   #5
olexiyb
LQ Newbie
 
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
--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'
 
  


Reply

Tags
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
wesite which i can open and execute shell commands online sharma.kashyap Linux - General 1 02-12-2007 10:48 PM
can ssh execute multiple commands at the same time? Singist Linux - Networking 2 04-04-2006 09:14 AM
Shell won't execute commands.... frank320 Red Hat 6 03-17-2006 11:12 AM
make c-program that accepts shell commands and execute them boeroe Programming 4 02-18-2005 08:41 AM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM


All times are GMT -5. The time now is 07:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration