6.10 在两个数据库之间复制文件
可以通过OracleNet在数据库之间直接复制文件,不用使用OS命令或实用程序复制文件。可以使用DBMS_FILE_TRANSFER程序包在同一服务器内或者在服务器之间复制二进制文件。COPY_FILE过程在本地系统上复制文件,GET_FILE过程从远程服务器复制文件到本地服务器,而PUT_FILE过程读取并复制一个本地文件到某个远程文件系统。
6.10.1 COPY_FILE
copy_file过程允许在相同或不同的服务器上将二进制文件从一个位置复制到另一个位置。在复制这些文件之前,必须首先创建源目录对象和目标目录对象,如下所示:
create or replace directory source_dir as '/u01/app/oracle/source';
create or replace directory dest_dir as '/uol/app/oracle/dest';
创建了源冃录和目标目录之后,就可以使用COPY_FILE过程拷贝文件,如下所示:
BEGIN
DBMS_FILE_TRANSFER.COPY_FILE(
source_directory_object => 'source_dir',
source_file_name => 'test01.dbf',
destination_directory_object >= 'dest_dir',
destination_file_name => 'test01_copy.dbf');
END;
/
要通过检査目标目录来确保正确地复制副本。
6.10.2 GET_FILE
使用get_file过程将二进制文件从远程服务器复制到本地服务器。首先,登录到远程服务器并创建源目录对象,如下所示:
connect system/system_passwd@remote_db
create or replace directory source_dir as '/u01/app/oracle/source';
接着,登录到本地服务器并创建目标目录对象,如下所示:
connect system/system_passwd@local_db
create or replace directory dest_dir as '/u01/app/oracle/dest';
创建了源目录和目标目录之后,要确保两个数据库之间存在数据库链接,若不存在,就要创建链接:
create database link prod1 connect to system identified by systempasswd using 'prod1';
在能创建数据库链接前,必须保证已经使用(比方说)tnsnames.ora文件建立了与prodl数据库的连接。
现在,执行GETJILE过程,将文件从远程服务器传送到本地服务器,如下所示:
BEGIN
DBMS_FILE_TRANSFER.GET_FILE(
source_directory_object => 'source_dir',
source_file_name => 'test01.dbf',
source_database => 'remote_db',
destination_directory_object >= 'dest_dir',
destination_file_name => 'test01_copy.dbf');
END;
/
请注意,对于SOURCE_DATABASE属性,提供了到远程数据库的数据库链接名。
6.10.3 PUT_FILE
使用putjile过程将一个二进制文件从本地服务器复制到远程服务器。与上述两个过程一样,必须首先创建就和目标目录对象,如下所示(另外,还必须确保从本地到远程数据库的链接存在):
connect system/system_passwd@remote_db
create or replace directory source_dir as '/u01/app/oracle/source';
connect system/system_passwd@local_db
create or replace directory dest_dir as '/u01/app/oracle/dest';
现在,可以使用PUT FILE过程将本地文件放入远程服务器,如下所示:
BEGIN
DBMS_FILE_TRANSFER.PUT_FILE(
source_directory_object => 'source_dir',
source_file_name => 'test01.dbf',
destination_directory_object >= 'dest_dir',
destination_file_name => 'test01_copy.dbf',
destination_database => 'remote_db');
END;
/




