再續解密Oracle備份工具-expdp/impdp
在這個信息的時代,數據規模和數據量的增長以爆炸式的速度擴張。之前幾百M或數G規模的數據量都堪稱龐大。現如今測試系統所佔空間都是這一數據的幾十倍甚至百倍。原生imp/exp工就這兩匹老馬在處理這麼大的數據量就力不從心了。從10g開始,data pump橫空出世,中文名叫數據泵。
數據泵的優點
a.爲數據及數據對象提供更細微級別的選擇性(使用exclude,include,content參數)
b. 可以設定數據庫版本號(主要是用於兼容老版本的數據庫系統)
c. 並行執行
d.預估導出作業所需要的磁盤空間(使用estimate_only參數)
e.支持分佈式環境中通過數據庫鏈接實現導入導出
f.支持導入時重新映射功能(即將對象導入到新的目標數據文件,架構,表空間等)
g. 支持元數據壓縮及數據採樣
要使用data pump工具,要指定一個directory對象。
什麼是directory對象?
a. 字面意思就是目錄,它不是實體,只是一個指向,指向操作系統中的一個具體路徑。
b. 每個directory對象都有read、write兩個權限,通過grant授權給指定用戶。
c.擁有directory對象的read/write權限的用戶就可以讀/寫該directory對象實際指定的操作系統路徑下的文件。
一、目錄操作
1、在操作系統層面創建目錄
mkdir /home/oracle/expdp
2、在庫中創建目錄、並授權給scott用戶
sqlplus / as sysdba SQL> create directory expdp as '/home/oracle/expdp'; SQL> grant read,write on directory expdp to scott;
3、查看當前庫中所有目錄
SQL> select OWNER','DIRECTORY_NAME','DIRECTORY_PATH from dba_directories;
二、expdp 參數說明:
1、導出某用戶下所有對象
expdp scott/lipengfei directory=expdp dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log
2、導出部分表
(1)expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp tables=\(emp,dept\) logfile=scott_emp.log (2)expdp scott/lipengfei directory=expdp dumpfile=scott_E_D.dmp tables=\(scott.E%,scott.D%\);
3、指定條件導出
expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp logfile=scott_emp.log tables=emp query=\"where sal \>1000\"
4、導出時除某對象【靜態收集信息,序列,視圖,表】
expdp scott/lipengfei exclude=STATISTICS,SEQUENCE,VIEW,TABLE:\" IN \(\'EMP\',\'DEPT\'\)\" directory=expdp dumpfile=scott_2015_06_02.dmp logfile=scott_2015_06_02.log
5、導出同時壓縮dmp文件(compression=ALL,此值在11g中才有)
(1)創建測試表,並insert大量數據 create table li nologging as select * from all_objects; insert into li select * from li; / / / / commit; (2)查看測試表所在物理大小 select segment_name,bytes/1024/1024 from user_segments where segment_name=’LI'; (3)導出測試表,並加上compression=ALL參數 expdp scott/lipengfei directory=EXPDP dumpfile=scott_all_compression.dmp SCHEMAS=SCOTT logfile=scott_all_compression.log compression=ALL (4)導出測試表 expdp scott/lipengfei directory=EXPDP dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log (5)查看2次導出dmp文件的大小 ls -lh *.dmp 【通過查看文件大小,可以看出加上compression=ALL參數後,壓縮比堪比"gzip -9"】 (6)對dmp文件進行第二次壓縮 zip scott_all_compression.zip scott_all_compression.dmp
6、content爲all 時,將導出對象定義及其所有數據.爲data_only時,只導出對象數據,爲metadata_only時,只導出對象定義
expdp scott/lipengfei directory=EXPDP dumpfile=scott_metadata_only.dmp content=metadata_only logfile=scott_metadata_only.log
ls -lh *.dmp【通過查看文件大小,可以只導出對象定義,dmp文件很小】
三、impdp 參數說明:
1、導入某用戶所有對象
(1)創建表空間 SQL> create tablespace lipengfei datafile '/home/oracle/app/oracle/oradata/ecom/lipengfei.dbf' size 100M AUTOEXTEND OFF; (2)創建用戶指定密碼及默認表空間 SQL> create user lipengfei identified by lipengfei default tablespace lipengfei; (3)解鎖新創建的用戶 SQL> alter user lipengfei account unlock; (4)給新建的用戶授權最基本的角色及權限 SQL> grant connect,resource to lipengfei; SQL> grant create table to lipengfei; SQL> grant create view to lipengfei; (5)將數據泵指定的directory讀寫權限授權lipengfei SQL> grant read, write on directory EXPDP to lipengfei ; (6)登錄lipengfei,創建表及數據初始化 sqlplus lipengfei/lipengfei create table hehe(a int,b varchar2(10)); insert into hehe values(2,'d'); insert into hehe values(4,'e'); insert into hehe values(6,'f'); commit; create view nimei as select a from hehe; create table haha(id int); insert into haha values(1); commit; (7)導出lipengfei用戶所有對象 expdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp SCHEMAS=LIPENGFEI logfile=lipengfei_all.log (8)登錄lipengfei模擬數據全部丟失 sqlplus lipengfei/lipengfei drop view nimei; drop table hehe; drop table haha; (9)把上面導出的數據還原回lipengfei用戶 impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp logfile=lipengfei_all.log
2、導入的對象已存在
當使用IMPDP完成數據庫導入時,如遇到表已存在時,Oracle提供給我們如下四種處理方式:
a.忽略(SKIP,默認行爲);
b.在原有數據基礎上繼續增加(APPEND);
c.先DROP表,然後創建表,最後完成數據插入(REPLACE);
d.先TRUNCATE,再完成數據插入(TRUNCATE)。
impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp TABLE_EXISTS_ACTION=TRUNCATE logfile=lipengfei_all.log
3、lipengfei用戶數據 導入 shiqiang用戶
(1)創建表空間 SQL> create tablespace shiqiang datafile '/home/oracle/app/oracle/oradata/ecom/shiqiang.dbf' size 100M AUTOEXTEND OFF; (2)創建用戶指定用戶及默認表空間 SQL> create user shiqiang identified by shiqiang default tablespace shiqiang; (3)將新建用戶解鎖 SQL> alter user shiqiang account unlock; (4)授權新建用戶最基本的角色及權限 SQL> grant connect,resource to shiqiang; SQL> grant create table to shiqiang; SQL> grant create view to shiqiang; (5)將數據泵指定的directory讀寫權限授權shiqiang SQL> grant read, write on directory EXPDP to shiqiang ; (6)將lipengfei用戶的導出的dmp文件,導入shiqiang用戶中 impdp shiqiang/shiqiang directory=expdp remap_schema=lipengfei:shiqiang remap_tablespace=lipengfei:shiqiang dumpfile=lipengfei_all.dmp logfile=lipengfei_shiqiang.log ;
4、只導入部分表
(1)模擬數據丟失 sqlplus lipengfei/lipengfei drop view nimei; drop table hehe; drop table haha; (2)將之前備份的dmp文件還原回lipengfei用戶,並指定只恢復haha表 impdp lipengfei/lipengfei directory=expdp tables=haha dumpfile=lipengfei_all.dmp logfile=lipengfei_only_haha.log (3)數據恢復後查看 sqlplus lipengfei/lipengfei select * from hehe; select * from haha;
5、高版本導入低版本
(1)11g導出,並指定version參數 expdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp SCHEMAS=SHIQIANG logfile=shiqiang_11g_all.log version=10.2.0.1.0 (2)10g導入 impdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp logfile=shiqiang_11g_all.log
雖然oracle對自己產品的宣傳一向有誇大的傳統,不過大家要理解,有了前面原生的exp/imp工具的鋪墊,對比來看,數據泵的導入和導出,有了很大的提升。
如果你之前有使用過exp/imp工具,通過上面expdp/impdp的一些例子,我相信你可以真正感受到,什麼叫高效!