您好,登錄后才能下訂單哦!
再續(xù)解密Oracle備份工具-expdp/impdp
在這個(gè)信息的時(shí)代,數(shù)據(jù)規(guī)模和數(shù)據(jù)量的增長(zhǎng)以爆炸式的速度擴(kuò)張。之前幾百M(fèi)或數(shù)G規(guī)模的數(shù)據(jù)量都堪稱龐大?,F(xiàn)如今測(cè)試系統(tǒng)所占空間都是這一數(shù)據(jù)的幾十倍甚至百倍。原生imp/exp工就這兩匹老馬在處理這么大的數(shù)據(jù)量就力不從心了。從10g開始,data pump橫空出世,中文名叫數(shù)據(jù)泵。
數(shù)據(jù)泵的優(yōu)點(diǎn)
a.為數(shù)據(jù)及數(shù)據(jù)對(duì)象提供更細(xì)微級(jí)別的選擇性(使用exclude,include,content參數(shù))
b. 可以設(shè)定數(shù)據(jù)庫(kù)版本號(hào)(主要是用于兼容老版本的數(shù)據(jù)庫(kù)系統(tǒng))
c. 并行執(zhí)行
d.預(yù)估導(dǎo)出作業(yè)所需要的磁盤空間(使用estimate_only參數(shù))
e.支持分布式環(huán)境中通過數(shù)據(jù)庫(kù)鏈接實(shí)現(xiàn)導(dǎo)入導(dǎo)出
f.支持導(dǎo)入時(shí)重新映射功能(即將對(duì)象導(dǎo)入到新的目標(biāo)數(shù)據(jù)文件,架構(gòu),表空間等)
g. 支持元數(shù)據(jù)壓縮及數(shù)據(jù)采樣
要使用data pump工具,要指定一個(gè)directory對(duì)象。
什么是directory對(duì)象?
a. 字面意思就是目錄,它不是實(shí)體,只是一個(gè)指向,指向操作系統(tǒng)中的一個(gè)具體路徑。
b. 每個(gè)directory對(duì)象都有read、write兩個(gè)權(quán)限,通過grant授權(quán)給指定用戶。
c.擁有directory對(duì)象的read/write權(quán)限的用戶就可以讀/寫該directory對(duì)象實(shí)際指定的操作系統(tǒng)路徑下的文件。
一、目錄操作
1、在操作系統(tǒng)層面創(chuàng)建目錄
mkdir /home/oracle/expdp
2、在庫(kù)中創(chuàng)建目錄、并授權(quán)給scott用戶
sqlplus / as sysdba SQL> create directory expdp as '/home/oracle/expdp'; SQL> grant read,write on directory expdp to scott;
3、查看當(dāng)前庫(kù)中所有目錄
SQL> select OWNER','DIRECTORY_NAME','DIRECTORY_PATH from dba_directories;
二、expdp 參數(shù)說明:
1、導(dǎo)出某用戶下所有對(duì)象
expdp scott/lipengfei directory=expdp dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log
2、導(dǎo)出部分表
(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、指定條件導(dǎo)出
expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp logfile=scott_emp.log tables=emp query=\"where sal \>1000\"
4、導(dǎo)出時(shí)除某對(duì)象【靜態(tài)收集信息,序列,視圖,表】
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、導(dǎo)出同時(shí)壓縮dmp文件(compression=ALL,此值在11g中才有)
(1)創(chuàng)建測(cè)試表,并insert大量數(shù)據(jù) create table li nologging as select * from all_objects; insert into li select * from li; / / / / commit; (2)查看測(cè)試表所在物理大小 select segment_name,bytes/1024/1024 from user_segments where segment_name=’LI'; (3)導(dǎo)出測(cè)試表,并加上compression=ALL參數(shù) expdp scott/lipengfei directory=EXPDP dumpfile=scott_all_compression.dmp SCHEMAS=SCOTT logfile=scott_all_compression.log compression=ALL (4)導(dǎo)出測(cè)試表 expdp scott/lipengfei directory=EXPDP dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log (5)查看2次導(dǎo)出dmp文件的大小 ls -lh *.dmp 【通過查看文件大小,可以看出加上compression=ALL參數(shù)后,壓縮比堪比"gzip -9"】 (6)對(duì)dmp文件進(jìn)行第二次壓縮 zip scott_all_compression.zip scott_all_compression.dmp
6、content為all 時(shí),將導(dǎo)出對(duì)象定義及其所有數(shù)據(jù).為data_only時(shí),只導(dǎo)出對(duì)象數(shù)據(jù),為metadata_only時(shí),只導(dǎo)出對(duì)象定義
expdp scott/lipengfei directory=EXPDP dumpfile=scott_metadata_only.dmp content=metadata_only logfile=scott_metadata_only.log
ls -lh *.dmp【通過查看文件大小,可以只導(dǎo)出對(duì)象定義,dmp文件很小】
三、impdp 參數(shù)說明:
1、導(dǎo)入某用戶所有對(duì)象
(1)創(chuàng)建表空間 SQL> create tablespace lipengfei datafile '/home/oracle/app/oracle/oradata/ecom/lipengfei.dbf' size 100M AUTOEXTEND OFF; (2)創(chuàng)建用戶指定密碼及默認(rèn)表空間 SQL> create user lipengfei identified by lipengfei default tablespace lipengfei; (3)解鎖新創(chuàng)建的用戶 SQL> alter user lipengfei account unlock; (4)給新建的用戶授權(quán)最基本的角色及權(quán)限 SQL> grant connect,resource to lipengfei; SQL> grant create table to lipengfei; SQL> grant create view to lipengfei; (5)將數(shù)據(jù)泵指定的directory讀寫權(quán)限授權(quán)l(xiāng)ipengfei SQL> grant read, write on directory EXPDP to lipengfei ; (6)登錄lipengfei,創(chuàng)建表及數(shù)據(jù)初始化 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)導(dǎo)出lipengfei用戶所有對(duì)象 expdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp SCHEMAS=LIPENGFEI logfile=lipengfei_all.log (8)登錄lipengfei模擬數(shù)據(jù)全部丟失 sqlplus lipengfei/lipengfei drop view nimei; drop table hehe; drop table haha; (9)把上面導(dǎo)出的數(shù)據(jù)還原回lipengfei用戶 impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp logfile=lipengfei_all.log
2、導(dǎo)入的對(duì)象已存在
當(dāng)使用IMPDP完成數(shù)據(jù)庫(kù)導(dǎo)入時(shí),如遇到表已存在時(shí),Oracle提供給我們?nèi)缦滤姆N處理方式:
a.忽略(SKIP,默認(rèn)行為);
b.在原有數(shù)據(jù)基礎(chǔ)上繼續(xù)增加(APPEND);
c.先DROP表,然后創(chuàng)建表,最后完成數(shù)據(jù)插入(REPLACE);
d.先TRUNCATE,再完成數(shù)據(jù)插入(TRUNCATE)。
impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp TABLE_EXISTS_ACTION=TRUNCATE logfile=lipengfei_all.log
3、lipengfei用戶數(shù)據(jù) 導(dǎo)入 shiqiang用戶
(1)創(chuàng)建表空間 SQL> create tablespace shiqiang datafile '/home/oracle/app/oracle/oradata/ecom/shiqiang.dbf' size 100M AUTOEXTEND OFF; (2)創(chuàng)建用戶指定用戶及默認(rèn)表空間 SQL> create user shiqiang identified by shiqiang default tablespace shiqiang; (3)將新建用戶解鎖 SQL> alter user shiqiang account unlock; (4)授權(quán)新建用戶最基本的角色及權(quán)限 SQL> grant connect,resource to shiqiang; SQL> grant create table to shiqiang; SQL> grant create view to shiqiang; (5)將數(shù)據(jù)泵指定的directory讀寫權(quán)限授權(quán)shiqiang SQL> grant read, write on directory EXPDP to shiqiang ; (6)將lipengfei用戶的導(dǎo)出的dmp文件,導(dǎo)入shiqiang用戶中 impdp shiqiang/shiqiang directory=expdp remap_schema=lipengfei:shiqiang remap_tablespace=lipengfei:shiqiang dumpfile=lipengfei_all.dmp logfile=lipengfei_shiqiang.log ;
4、只導(dǎo)入部分表
(1)模擬數(shù)據(jù)丟失 sqlplus lipengfei/lipengfei drop view nimei; drop table hehe; drop table haha; (2)將之前備份的dmp文件還原回lipengfei用戶,并指定只恢復(fù)haha表 impdp lipengfei/lipengfei directory=expdp tables=haha dumpfile=lipengfei_all.dmp logfile=lipengfei_only_haha.log (3)數(shù)據(jù)恢復(fù)后查看 sqlplus lipengfei/lipengfei select * from hehe; select * from haha;
5、高版本導(dǎo)入低版本
(1)11g導(dǎo)出,并指定version參數(shù) 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導(dǎo)入 impdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp logfile=shiqiang_11g_all.log
雖然oracle對(duì)自己產(chǎn)品的宣傳一向有夸大的傳統(tǒng),不過大家要理解,有了前面原生的exp/imp工具的鋪墊,對(duì)比來看,數(shù)據(jù)泵的導(dǎo)入和導(dǎo)出,有了很大的提升。
如果你之前有使用過exp/imp工具,通過上面expdp/impdp的一些例子,我相信你可以真正感受到,什么叫高效!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。