Solved

Unix Shell Scripting Copy from one unix server to another

Posted on 2011-03-02
19
1,690 Views
Last Modified: 2012-05-11
I need a shell script which can copy the files from one unix server to another after a exp command is executed successfully and remove the file from the source server.
(exp is a utility in oracle which exports table as dump files, I am sure most of you guys know, but just in case)
for ex: (psuedo code)

exp USERID=usr/pwd TABLES= table_a:sub_part_2002_A FILE=/source/path/dump_area/sub_part_2002_A .dmp

copy "sub_part_2002_A .dmp" to the "/remote/path/dump_area"

After succesful copy it should delete the file from the source path (/source/path/dump_area/sub_part_2002_A .dmp)

----
Also one more thing to this is:
I have a file which has following content:
---
sub_part_2002_A .dmp
sub_part_2002_J .dmp
sub_part_2002_U .dmp
sub_part_2002_R .dmp
sub_part_2002_D .dmp
sub_part_2002_E .dmp
sub_part_2002_M .dmp
sub_part_2002_S .dmp
..........
sub_part_2002_A in the exp command should be taken from this file. After this is done then it should goto the next line of the file and replace it with sub_part_2002_J .dmp in the exp command. You see where I am going?

Thanks@
0
Comment
Question by:D-pk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35023235
There are a few options but they depend on how your unix shop is set up.

Can you run: rcp, ftp, remsh, ???
0
 

Author Comment

by:D-pk
ID: 35023246
I can ftp
0
 
LVL 3

Expert Comment

by:saranyannarayanan
ID: 35023250
rsync also is a good solutiion
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 19

Expert Comment

by:jools
ID: 35023252
scp/sftp would be an ideal candidate for this... but as slightwv pointed out, it depends on your setup.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35023261
If you can ftp, then what do you need help with?

Seems like a pretty simple shell script.
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 35023266
There are a ton of links on how to FTP from a shell script.

Here's the first:
http://www.stratigery.com/scripting.ftp.html
0
 

Author Comment

by:D-pk
ID: 35023267
I need a sample script to work with.. I am very new to shell scripting...
0
 

Author Comment

by:D-pk
ID: 35023512
In the above link's example, where should I give the source file name and its path.

Ex:


#!/bin/sh
HOST='198.xx.xxx.xx'
USER='remoteuserid'
PASSWD='remotepasswd'
FILE='/remote/server/path/file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35023518
The variable:
FILE='/remote/server/path/file.txt'
0
 

Author Comment

by:D-pk
ID: 35023532
if that is the source, where should I give the destination path (Remote server path)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35023533
The other thing you need to make sure of is putting ftp in 'binary' mode.

I have no way to test it but right above the 'put' line add: bin

...
Bin
Put $FILE
...

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35023549
My ftp is rusty but that is the file you are 'putting' on the remote server.

If you need a specific remote dir, try a cd command above the 'put'

As I mentioned, there's a lot of info out there on basic unix commands.  I just posted the first link I came across.

I would suggest you familiarize yourself with any commands you receive here to ensure they don't lead you down the wrong path.
0
 
LVL 3

Expert Comment

by:saranyannarayanan
ID: 35023809
rsync should be good too
http://ss64.com/bash/rsync.html


0
 

Author Comment

by:D-pk
ID: 35024387
I tried this script and it gave me this error:
FTP Connection refused.. I am trying to fix this error to see if ftp is installed or not..
But, I want some suggestions with this script,
I want to make sure the file is copied to the destination server and only after that it should be deleted from my local server...
Please advise...
Thanks@

#!/bin/ksh
file="/local/server/dump_fldr/test.txt"
# while loop
while read line
do
exp USERID=usr/pwd TABLES= table_a:$line FILE=/local/server/dump_fldr/$line.dmp
tmp_file=/local/server/dump_fldr/test2.txt
#file_nm=$line.dmp
ftp -nv <<EOF > ${tmp_file}
    open 154.xx.xx.xx
    user remoteuser remotepwd
    binary
    lcd /local/server/dump_fldr/
    cd /remote/server/dump_fldr/    
    put $line.dmp  
    rm $line.dmp 
EOF

done <"$file"

Open in new window

0
 

Author Comment

by:D-pk
ID: 35024403
my test.txt file contains the list of file names i need to copy...
for ex:
sub_par_2007_a
sub_par_2007_g
sub_par_2007_s
sub_par_2007_y
sub_par_2007_l
.....
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35026707
Did you manually try those ftp parameters on a single file from the command prompt to make sure the ip address, user and password are correct?

I don't have access to UNIX right now so I cannot help debug the script you posted but everything looks like it should work.

Hopefully one of the other Experts can see if there's a flaw in the script that I'm missing.
0
 

Author Comment

by:D-pk
ID: 35047606
One final question...
will the if condition is the correct one to check whether the prev command was successful?

Thanks@
#!/bin/ksh
file="/local/server/dump_fldr/test.txt"
# while loop
while read line
do
exp USERID=usr/pwd TABLES= table_a:$line 

if [ $? -eq 0 ];then
 
ftp -nv <<EOF 
    open 154.xx.xx.xx
    user remoteuser remotepwd
    binary
    lcd /local/server/dump_fldr/
    cd /remote/server/dump_fldr/    
    put $line.dmp  
    rm $line.dmp 
else 
exit;
fi
EOF

done <"$file"

Open in new window

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35048322
I'm not sure what return values exp returns.  I suggest you refer to the online docs for the valid return codes.

As far as the syntax, it looks good if exp follows typical unix coding standards.
0
 

Author Closing Comment

by:D-pk
ID: 35048351
Thanks everyone for their inputs..
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question