初探data pump export (二)

pingley發表於2012-03-19
初探data pump export (二)
data pump export 提供的介面
透過expdp 命令呼叫data pump export 實用工具,具體的操作特權取決於你提供的引數。另外不建議使用sysdba呼叫 export,因為sysdba 是內部的和特殊功能相關的,sysdba 的表現形式和普遍使用者不同,除非oracle 技術支援人員要求你使用sysdba。
一、命令列介面。允許你透過命令列直接指定大部分的export 引數。當你指定export 操作的時候指定的引數很少,另外不考慮引數重用的話建議使用,命令列呼叫方式。
[oracle@zeng ~]$ expdp hr tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes
二、引數檔案介面。允許你透過命令列介面指定一個引數檔案(使用 parfile)。如果需要提供的export 引數較多,或者考慮到操作的重複性引數的重用性,提供給export 的引數中使用了引號,建議使用引數檔案呼叫的方式。這樣如果下次需要執行相同的或者類似的export 操作,改改引數就好了。可以提高效率,減少錯誤。
先編輯一個引數檔案寫入下面的內容。
[oracle@zeng ~]$ vi expfile.par
-- INSERT --                                                            
tables=employees,jobs
dumpfile=exp_hr.dmp
reuse_dumpfiles=yes
logfile=export_hr.log
使用引數檔案呼叫data pump export
[oracle@zeng ~]$ expdp hr parfile=/home/oracle/expfile.par
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:05:47 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "HR"."SYS_EXPORT_TABLE_01":  hr/******** parfile=/home/oracle/expfile.par 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
. . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows
. . exported "HR"."JOBS"                                 6.984 KB      19 rows
Master table "HR"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_TABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/exp_hr.dmp
Job "HR"."SYS_EXPORT_TABLE_01" successfully completed at 16:06:12
三、互動式命令列介面。在作業正在執行中或者出現異常的時候,你需要對作用進行監控和干預,可以在工作執行的時候按 Ctrl+C 進入互動式命令列。或者出現異常的時候使用expdp attach job_name 進入。也可以在另外一個地方執行expdp attach job_name 對作業進行監控和干預。
[oracle@zeng ~]$ expdp hr tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 15:58:07 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "HR"."SYS_EXPORT_TABLE_01":  hr/******** tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
^C
Export> stop_job
Are you sure you wish to stop this job ([yes]/no): yes
data pump export 提供的匯出模式
data pump export 提供了5種不同的模式供匯出不同的資料庫部分。透過在命令列或者引數檔案中
指定匯出的模式。
一、完全匯出模式(full export mode)
透過指定full 引數執行該模式。在完全匯出模式下,整個資料庫都會被匯出。使用該模式需要datapump_exp_full_database 角色的支援。
關於full 引數
預設是不啟用的(full=no)。如果你需要執行整個資料庫的匯出,指定full=yes。在該模式下面整個資料庫中的資料(包括後設資料)都會被匯出。可以透過過濾來限制匯出。在full mode 下不會匯出system 模式,其中存放著oracle 管理的資料,和後設資料.比如不匯出的系統模式包括,sys,ordsys,mdsys.授權給sys 擁有的物件也不會匯出.
二、schema 模式(schema mode)
透過指定schemas 引數來執行該模式。這是預設的匯出模式。如果執行匯出的使用者擁有datapump_exp_full_database 角色。你可以指定一個schemas 列表.如果使用者沒有datapump_exp_full_database只能匯出自己的schema。(sys schema 不能作為匯出源。)交叉參照的模式中的物件不會被匯出,除非把該表空間也列在schemas 中.比如一個表中定義的一個觸發器位於另外一個表空間,則這個觸發器不會被匯出。
schemas 引數的語法格式:schemas=schema_name,schema_name... 如果不指定schemas 預設就是當前執行expdp 的使用者的schemas。
先把兩項和data pump 相關的強大特權都回收回來。
SQL> revoke datapump_imp_full_database from hr;
Revoke succeeded
SQL> revoke datapump_exp_full_database from hr;
Revoke succeeded.
想匯出別人的schemas 不行了吧。
[oracle@zeng ~]$ expdp schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:27:15 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Username: hr
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-31631: privileges are required
ORA-39109: Unprivileged users may not operate upon other users' schemas
只給hr 授予datapump_imp_full_database .
SQL> grant datapump_imp_full_database to hr;
Grant succeeded.
[oracle@zeng ~]$ expdp hr/hr schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes   
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:28:48 2012
.
.
.
Master table "HR"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:
  /opt/oracle11g/admin/oracl/exp_zeng.dump
Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at 16:30:00
只有datapump_imp_full_database 依然可以匯出其他使用者的schema。
只給hr 授予datapump_exp_full_database .
SQL> revoke datapump_imp_full_database from hr;
Revoke succeeded.
SQL> grant datapump_exp_full_database to hr;
Grant succeeded
[oracle@zeng ~]$ expdp hr/hr schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes
.
.
.
Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:
  /opt/oracle11g/admin/oracl/exp_zeng.dump
Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at 16:34:22
只有datapump_exp_full_database 依然可以匯出其他使用者的schema。從上面的實驗可以看出
這兩個角色是很強大的,回頭找些資料來認真學習下這兩個角色。另外不建議hr/hr 這種提供密碼的方式,因為這很容易就洩露了使用者的密碼。
三、表模式(table mode)
透過指定tables 引數來指定該模式。在表模式下,僅僅指定的表,表分割槽,和依賴他們的物件會被匯出。如果tables 引數和transporttable=always 一起指定。那麼僅有後設資料會被匯出。實際要匯出的資料你應該複製相應的資料檔案到目標資料庫。這將會是export 操作非常的快。如果你跨平臺或者發行版本複製資料檔案,可能需要經過RMAN處理.如果擁有datapump_exp_full_database角色,使用者可以匯出其他schema 中的表.同樣的交叉參照的模式中的物件不會被匯出。
在得出下面的輸出的前,我遇到了兩個錯誤:
ORA-29335: tablespace 'USERS' is not read only
使用transportable=always 引數需要把物件所在的表空間alter 為read_only。
SQL>  alter tablespace users read only;
Tablespace altered.
另外一個是:ORA-01647: tablespace 'USERS' is read-only, cannot allocate space in it
因為我是使用hr 來執行操作的,既然hr 所在的表空間已經設定為只讀了,所有分配不到data pump export job 所需要的工作空間。把執行匯出操作的使用者改為system 再測試就ok了。或者改為其他預設表空間不是users 並且有許可權的使用者。
[oracle@zeng ~]$ expdp system tables=hr.employees transportable=always dumpfile=exp_hr.dmp reuse_dumpfiles=yes logfile=exp_hr.log
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 17:18:45 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "SYSTEM"."SYS_EXPORT_TABLE_01":  system/******** tables=hr.employees transportable=always dumpfile=exp_hr.dmp reuse_dumpfiles=yes logfile=exp_hr.log 
Processing object type TABLE_EXPORT/TABLE/PLUGTS_BLK
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/END_PLUGTS_BLK
Master table "SYSTEM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/exp_hr.dmp
******************************************************************************
Datafiles required for transportable tablespace USERS:
  /opt/oracle11g/oradata/oracl/users01.dbf
Job "SYSTEM"."SYS_EXPORT_TABLE_01" successfully completed at 17:19:14
四、表空間模式(tablespace mode )
透過指定tablespaces 引數來執行該模式,在表空間模式下,只有在指定表空間中的表及其依賴物件會被匯出(包括物件的資料與後設資料)。在表空間模式下,有特權的使用者可以匯出所有的表及其依賴物件,沒有特權的使用者只能匯出在自己schemas中的資料。
先把上面alter 為只讀的表空間,alter 為讀寫狀態。
SQL> alter tablespace users read write;
Tablespace altered.
[oracle@zeng ~]$ expdp zeng tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log  job_name=exp_zeng 
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 18:38:34 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "ZENG"."EXP_ZENG":  zeng/******** tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log job_name=exp_zeng 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
Master table "ZENG"."EXP_ZENG" successfully loaded/unloaded
******************************************************************************
Dump file set for ZENG.EXP_ZENG is:
  /opt/oracle11g/admin/oracl/exp_zeng.dmp
Job "ZENG"."EXP_ZENG" successfully completed at 18:39:18
從上面長長的expdp 引數可以想到,使用parfile是更好的選擇,因為這樣可以用漂亮的方式給expdp提供匯出引數。
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
可得zeng 只匯出了屬於他自己的表account_manager,並且表中只有一條記錄。因為zeng 沒有特權。
不信的話可以檢視user_objects 確定一下。
SQL> show user
USER is "ZENG"
SQL> select object_name from user_objects;
OBJECT_NAME
--------------------------------------------------------------------------------
CONSTRAINTS_ID#_PK
ACCOUNT_MANAGER
改由hr 來執行上面的命令。
[oracle@zeng ~]$ expdp hr/hr tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log  job_name=exp_zeng     
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 18:44:36 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "HR"."EXP_ZENG":  hr/******** tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log job_name=exp_zeng 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 512 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "HR"."COUNTRIES"                            6.367 KB      25 rows
. . exported "HR"."DEPARTMENTS"                          7.007 KB      27 rows
. . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows
. . exported "HR"."JOBS"                                 6.984 KB      19 rows
. . exported "HR"."JOB_HISTORY"                          7.054 KB      10 rows
. . exported "HR"."LOCATIONS"                            8.273 KB      23 rows
. . exported "HR"."REGIONS"                              5.476 KB       4 rows
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
Master table "HR"."EXP_ZENG" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.EXP_ZENG is:
  /opt/oracle11g/admin/oracl/exp_zeng.dmp
Job "HR"."EXP_ZENG" successfully completed at 18:45:34
結果就完全不同了hr 把他自己模式中的表全面都匯出了,還把zeng 模式中的表也給匯出了。
因為hr 是有DATAPUMP_EXP_FULL_DATABASE 角色的大佬。
SQL> select * from dba_role_privs
  2  where grantee='HR';
GRANTEE                        GRANTED_ROLE                   ADM DEF
------------------------------ ------------------------------ --- ---
HR                             RESOURCE                       NO  YES
HR                             DATAPUMP_EXP_FULL_DATABASE     NO  YES
五、傳輸表空間模式(transportable tablespace mode)
透過指定transport_tablespaces 引數來執行該模式。在傳輸表空間模式,只有在指定表空間的表及其依賴的物件會被匯出。資料檔案的複製是一項單獨的操作,也就是說dumpfile 中只有相關物件的後設資料。另外一個傳輸表空模式的作用停止以後不能重新開始。加密的列不支援傳輸表空間模式。傳輸表空間模式匯出的dumpfile 不能匯入到比源資料庫版本更低的目標資料庫中。
在執行表空間模式前需要把要執行表空間模式的表空間alter 為只讀。另外不能使用schema 在該表空間的使用者來執行匯出,否則會報ORA-01647 錯誤。
[oracle@zeng ~]$ expdp system transport_tablespaces=users dumpfile=tablespace_exp.dmp logfile=tablespace.log      
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 23:13:14 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01":  system/******** transport_tablespaces=users dumpfile=tablespace_exp.dmp logfile=tablespace.log 
Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
Processing object type TRANSPORTABLE_EXPORT/TABLE
Processing object type TRANSPORTABLE_EXPORT/INDEX
Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT
Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/REF_CONSTRAINT
Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
Master table "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TRANSPORTABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/tablespace_exp.dmp
******************************************************************************
Datafiles required for transportable tablespace USERS:
  /opt/oracle11g/oradata/oracl/users01.dbf
Job "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 23:15:14
上面我執行了一個傳輸表空間模式的匯出,data pump export 還很聰明,會告訴你需要傳輸的表空間檔案:
Dump file set for SYSTEM.SYS_EXPORT_TRANSPORTABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/tablespace_exp.dmp
注:之所有需要把表空間alter 為只讀,是因為要保證匯出資料與現存資料的一致性。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26110315/viewspace-719071/,如需轉載,請註明出處,否則將追究法律責任。

相關文章