Solved

Unix Shell Scripting Copy from one unix server to another

Posted on 2011-03-02
19
1,659 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
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 76

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
 
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 76

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 76

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 76

Expert Comment

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

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

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 76

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 76

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 76

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now