實驗環境:
伺服器A:172.21.1.30
伺服器B:172.21.1.40
兩台伺服器上分別運行兩套Oracle資料庫,本實驗以scott使用者為例進行測試。
實驗目的:
通過DB_LINK的複製功能,利用expdp,impdp進行資料的遠程備份及恢複。進行測試前,將伺服器B上scott使用者的dept,emp進行刪除,本測試只是使用impdp將伺服器A資料庫下scott使用者的dept,emp表匯入到伺服器B資料庫scott使用者下。
操作步驟:
1.在伺服器A,B上分別以sys身份登入資料庫,並建立directory,db_link,命令如下:
session A: > create directory dp01 as '/bk/dp01';
Directory created.
session A: >create public database link ln_db40 connect to scott identified by tiger using 'ip40';
Database link created.
session A: >grant connect,resource,exp_full_database,imp_full_database to scott identified by tiger;
Grant succeeded.
session B: > create directory dp02 as '/bk/dp02';
Directory created.
session B: >create public database link ln_db30 connect to scott identified by tiger using 'ip30';
Database link created.
session B: >grant connect,resource,exp_full_database,imp_full_database to scott identified by tiger;
Grant succeeded.
2.在確認以上步驟中的db_link沒問題後,做以下操作:
a. 伺服器A:(將本機資料庫中scott使用者下的dept,emp兩張表匯出備份檔案為exp01.dmp)
[oracle@session A: dp01]$ expdp scott/tiger directory=dp01 dumpfile=exp01.dmp network_link=ln_db30 tables=emp,dept
Export: Release 10.2.0.1.0 - Production on Wednesday, 03 August, 2011 14:39:05
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01": scott/********@ip30 directory=dp01 dumpfile=exp01.dmp network_link=ln_db30 tables=emp,dept
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 320 KB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
. . exported "SCOTT"."SYS_EXPORT_SCHEMA_01" 172.8 KB 1073 rows
. . exported "SCOTT"."DEPT" 5.656 KB 4 rows
. . exported "SCOTT"."EMP" 7.820 KB 14 rows
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
/bk/dp01/exp01.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at 14:39:37
[oracle@session A: dp01]$ ls
exp01.dmp export.log import.log
b. 將備份檔案exp01.dmp從伺服器A拷貝到伺服器B:
[oracle@session A: dp01]$ scp –r exp01.dmp 172.21.1.40:/bk/dp02/
c.在伺服器A上,執行以下命令,將dept,emp表匯入到伺服器B的scott使用者下:
[oracle@session A: dp01]$ impdp scott/tiger@ip40 directory=dp02 dumpfile=exp01.dmp tables=emp,dept
Import: Release 10.2.0.1.0 - Production on Wednesday, 03 August, 2011 14:34:57
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Master table "SCOTT"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_TABLE_01": scott/********@ip40 directory=dp02 dumpfile=exp01.dmp tables=emp,dept
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."DEPT" 5.656 KB 4 rows
. . imported "SCOTT"."EMP" 7.820 KB 14 rows
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Job "SCOTT"."SYS_IMPORT_TABLE_01" successfully completed at 14:34:55
3.使用sys使用者登入伺服器A的資料庫,進行測試,查看B的scott使用者下的dept,emp表是否存在:
$ sqlplus / as sysdba
session A: > conn scott/tiger
Connected.
session A: > select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
session A: > select * from tab@ln_db40;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
由以上實驗不難看出,表dept,emp已成功匯入B的scott使用者。
EXPDP 可以匯出所有資料庫的資料,只要它能串連到這個資料庫,或者它登入的資料庫
上能讀取到被匯出的庫的資料,因為此時可以通過資料庫鏈來讀取被匯出的庫的資料。
如下測試:
可以分為三個伺服器:A,B,C,以下命令在A在執行:
[oracle@session A: dp01]$ expdp scott/tiger@ip40 directory=dp02 dumpfile=exp01.dmp network_link=ln_db50 tables=emp,dept
Export: Release 10.2.0.1.0 - Production on Wednesday, 03 August, 2011 14:39:05
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01": scott/********@ip40 directory=dp02 dumpfile=exp01.dmp network_link=ln_db50 tables=emp,dept
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 320 KB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
. . exported "SCOTT"."SYS_EXPORT_SCHEMA_01" 172.8 KB 1073 rows
. . exported "SCOTT"."DEPT" 5.656 KB 4 rows
. . exported "SCOTT"."EMP" 7.820 KB 14 rows
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
/bk/dp01/exp01.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at 14:39:37
說明:
expdp 在伺服器A上;
scott/tiger@ip40 串連到伺服器B的資料庫;
network_link=ln_db30 是伺服器B的資料庫中scott使用者的資料庫鏈,其指向伺服器C的使用者scott,
SCOTT10使用者中只有兩個表dept,emp.
從上面的操作可以看出:expdp 可以是用戶端的程式,但它匯出的資料(DMP)必須存放在它登入
的資料庫的目錄下(本例中,存放在機器B的資料庫directory=dp02下),而被匯出的資料是通過
資料庫鏈(network_link)到機器C的資料庫裡讀取的。
另外注意:在這過程中,A資料庫使用者scott和C資料庫使用者scott(或者資料庫鏈中的使用者)都必須被
授予EXP_FULL_DATABASE 許可權。
nelwork_link意思是從本機資料庫匯入datafile直接到remote database,中間省略了exp的操作,
nelwork_link=source_database_link,確認nelwork_link參數是一個存在的目標資料庫名稱,同時
存在database link.
補充一下兩個限制條件:
nelwork_link parameter Restrictions
Network imports do not support the use of evolved types.
a: When the NETWORK_LINK parameter is used in conjunction with the TABLES parameter,
only whole tables can be imported (not partitions of tables).
所以,當使用impdp匯入時多張表時,如果添加network_link參數,就會報錯如下:
UDI-00011: parameter dumpfile is incompatible with parameter network_link
b: If the USERID that is executing the import job has the IMP_FULL_DATABASE role on the
target database, then that user must also have the EXP_FULL_DATABASE role on the source
database.The only types of database links supported by Data Pump Import are: public,
fixed-user, and connected-user. Current-user database links are not supported.