Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-03-2009, 06:21 PM   #1
LQ Newbie
Registered: Jan 2009
Location: Baltimore, MD
Posts: 3

Rep: Reputation: 0
Question rsync ssh - how to execute shell commands on server

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:


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'" \
When I run the above script on machine A, I get the error:

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
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912Reputation: 912
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.

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

Old 01-04-2009, 08:42 AM   #3
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:

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.

Old 07-01-2009, 04:49 AM   #4
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:
rsync -avz -e 'ssh -o "LocalCommand ./startView"' user@ .
Old 11-04-2009, 02:01 PM   #5
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.

rsync --rsync-path='command_before_rsync && 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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration