|
|
Oracle Shell Scripting
Oracle UNIX/Linux script tips |
Transferring Files Between
Systems
Transferring files between machines is often
necessary for backups, duplicating databases, moving files from
development to production systems and a host of other daily tasks.
There are several tools which can be used for moving files around,
each with their advantages and disadvantages. Here we?ll focus on
the most flexible and secure tools available.
There are two tools which are typically
available for securely transferring files between systems; sftp
and scp. Both offer ssh protocol security but they
work somewhat differently.
sftp - Secure FTP
While sftp is typically used
interactively in the same way the ftp application would be
used, it can also read commands from a file in batch mode. Here's
an example of sftp being used interactively:
$
cd $ORACLE_HOME/network/admin
$ sftp sprog
Connecting to sprog...
Password: password
sftp> cd
/u01/app/oracle/product/10.2.0/db_1/network/admin
sftp> put tnsnames.ora
Uploading tnsnames.ora to
/u01/app/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora
tnsnames.ora 100% 536
0.5KB/s 00:00
sftp> quit
These steps could be combined either into a
separate batch file (using the -b option then giving the file name)
or redirected into sftp on the command line in the same fashion we
have been using for SQL*Plus.
#!/bin/bash
#
cd $ORACLE_HOME/network/admin
sftp sprog << EOF
cd /u01/app/oracle/product/10.2.0/db_1/network/admin
put tnsnames.ora
exit
EOF
This short shell script will now automatically
perform all the steps (except entering the password) that we
accomplished interactively before. The main commands within sftp
are listed in the table below.
Command |
Action |
quit
bye |
Close the connection and exit sftp
(either command will work) |
get |
Retrieve a file from the remote system |
put |
Upload a file to the remote system |
ls |
List the contents of a directory on the
local system. Many options which work with the ls
command in the shell (like -l, -a and -t) also work with the
ls command within sftp. |
pwd |
Show the current working directory on the
remote system |
cd |
Change directory on the remote system |
lpwd |
Show the current working directory on the
local system |
lcd |
Change directory on the local system |
chmod |
Change permissions of a given file |
chown |
Change ownership of a given file |
chgrp |
Change the group of a given file |
rm |
Remove a file from the remote system |
rmdir |
Remove a directory from the remote system |
help |
Display the online help for sftp |
Many of these commands should be familiar and
behave similarly to how they work in the shell. The ability to
change permissions and ownership and to remove remote files and
directories can be very useful in our shell scripts.