LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-23-2004, 05:27 PM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Bash script - reading from text file


Hi There,

Got a question for you bash experts out there . I have a file (client_ip):

192.168.3.100 twantrd
192.168.3.101 johntrd
192.168.3.102 ricktrd

As you can see, it contains the ip address and hostname of the machine associated with it. I pretty much need a bash script that will ssh into each ip and grab files to be backed and store it back on the server under the appropriate hostname. So it's like:

Server A ---(ssh)---> 192.168.3.100:/home/RAID/
192.168.3.100 ---(rsync)---> Server A:/home/backup/twantrd/

Server A ---(ssh)---> 192.168.3.101:/home/RAID/
192.168.3.101 ---(rsync)---> Server A:/home/backup/johntrd/

etc...

I have no problem creating a for loop to iterate through the file and grab the IP's and ssh'ing into them. My big problem is how to rsync the data to the appropriate hostname folder on ServerA. My partial script is below. Please help if someone can!! I truly appreciate it!! Thanks!

#!/bin/sh

myserver="blah.twantrd.com"
dir="/home/backup/"
backupdir="/home/RAID"
clientip=`cat client_ip | egrep -v '^#' | awk '{print $1}'`
clienthost=`cat client_ip | egrep -v '^#' | awk '{print $2}'`

#
#This will rsync the client's data to the server
#
for ip in $clientip; do
$ssh $ip "(cd $backupdir; rsync -avz * $myserver:$dir)"
done

P.S Forgot, this is all done on the server side (Server A).


-twantrd

Last edited by twantrd; 11-23-2004 at 05:29 PM.
 
Old 11-23-2004, 08:54 PM   #2
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
I think you could cycle through each line of the hosts file like this:
IFS="
"
for line in `cat $hosts | egrep -v '^#'` ; do
client_ip=${line/ */}
client_host=${line/* /}
#Put your ssh command here
done

Note: your variable "client_ip" refers to two different things, thats not good: first it stores the name of the file, then it stores the ip of each client. So I changed the name of the variable storing the filename to "hosts".

Last edited by J_Szucs; 11-23-2004 at 09:02 PM.
 
Old 11-23-2004, 10:59 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Original Poster
Rep: Reputation: 52
Thanks for the input but that still didn't work. "client_ip" and "client_host" both have the ip address and the hostname. Hmmm....

-twantrd
 
Old 11-23-2004, 11:20 PM   #4
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Hi,

Are you having an issue with the with scripting or rsync not working between server A and the clients
 
Old 11-24-2004, 12:38 AM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Original Poster
Rep: Reputation: 52
I *was* having an issue with just iterating the ip address with the associated hostname in the for loop section. Pretty much I wanted to ssh into each of those ip's and rsync the data back to Server A under the path of the hostname that was associated with just that IP. Hmmmm, that sounded kind of confusing .

Anyhow, I just wanted to say thanks to those who tried to help. I got the script working. I was working on this and all of a sudden a lightbulb came on in my head and I found out how. If u guys are interested...I'll post the code. Thanks again!!!

-twantrd
 
  


Reply


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
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM


All times are GMT -5. The time now is 11:29 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration