LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags

Reply
 
Thread Tools
Old 01-03-2009, 07:21 PM   #1
banjer
LQ Newbie
 
Registered: Jan 2009
Location: Baltimore, MD
Posts: 2
Thanked: 0
Question rsync ssh - how to execute shell commands on server


[Log in to get rid of this advertisement]
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!
banjer is offline     Reply With Quote
Old 01-03-2009, 09:48 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :}
Posts: 18,838
Blog Entries: 1
Thanked: 160
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
Tinkster is offline     Reply With Quote
Thanked by:
Old 01-04-2009, 09:42 AM   #3
banjer
LQ Newbie
 
Registered: Jan 2009
Location: Baltimore, MD
Posts: 2
Thanked: 0

Original Poster
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
banjer is offline  
Tag This Post
Reply With Quote
Old 07-01-2009, 05:49 AM   #4
ineya
LQ Newbie
 
Registered: Jul 2007
Posts: 27
Thanked: 0
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 .
ineya is offline     Reply With Quote
Old 11-04-2009, 03:01 PM   #5
olexiyb
LQ Newbie
 
Registered: Dec 2005
Posts: 1
Thanked: 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'
windows_xp_2003 olexiyb is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


All times are GMT -5. The time now is 08:54 AM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration