1. 程式人生 > 其它 >邏輯備份與恢復(資料泵)

邏輯備份與恢復(資料泵)

exp/imp的缺點是速度太慢, 在大型生產庫中尤其明顯。從10g開始, oracle設計了資料泵, 這是一個伺服器端的工具(exp/imp生成的檔案存放在客戶端,而資料泵生成的檔案存放於服務端), 它為Oracle資料提供高速並行及大資料的遷移。

imp/exp可以在客戶端呼叫, 但是expdp/impdp只能在服務端, 因為在使用expdp/impdp之前需要在資料庫中建立一個Directory(供轉儲檔案和日誌檔案使用的目錄物件)。

在expdp進行匯出時,先建立了MT表, 並把物件的資訊插入到MT表,之後進行匯出動作;匯出完成後,MT表也匯出到轉儲檔案中;匯出任務完成後、或者刪除了匯出任務後,MT表自動刪除;如果匯出任務異常終止,MT表仍然保留。

expdp/impdp也具有四種模式:

  1. 資料庫模式:直接匯出整個庫中的所有物件

  2. 表空間模式:匯出一個或多個表空間中的所有物件

  3. 使用者模式:匯出一個使用者模式中的所有物件

  4. 表模式:匯出一個或多個指定的表或表分割槽

一、expdp的重要引數

directory 供轉儲檔案和日誌檔案使用的目錄物件。
job_name 指定的任務的名稱
content

指定要匯出的資料, 其中有效關鍵字值為:

(ALL) 匯出物件定義及其所有資料
DATA_ONLY 只匯出物件資料
METADATA_ONLY匯出物件定義

reuse_dumpfiles=[y/n] 如果匯出檔案已經存在,是否覆蓋。
compression 壓縮匯出檔案
estimate 指定估算被匯出表所佔用磁碟空間分方法,預設值是BLOCKS
estimate only 是否只估算匯出佔用的磁碟空間,而不進行真正的匯出,預設是N。
exclude 用於指定執行操作時要排除物件型別或相關物件
include 用於指定執行操作時要包含的物件型別或相關物件
query 匯出符合條件的行
attch 連線到現有的作業, 可以用在中斷匯出任務後重新啟動匯出任務

二、expdp實踐

1. 建立目錄物件,並賦予使用者許可權

create directory MY_DIR as '/u01/app/oracle/backupfile
'; grant read,write on directory MY_DIR to scott;

2. 匯出scott的student和address表

expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)";
impdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp    -- 恢復測試

3. 匯出scott的student和address的表結構,不匯出資料

expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)" content=metadata_only reuse_dumpfiles=y;

# reuse_dumpfiles : 如果匯出檔案已經存在,是否覆蓋

4. 匯出資料,但不匯出表結構

expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)" content=data_only reuse_dumpfiles=y;

5. 匯出scott和loto使用者的所有內容

expdp system/123456@orcl directory=MY_DIR dumpfile=expdp_system.dmp schemas="(scott,loto)";

6. 匯出stu表,以及表中的約束,但不匯出索引

 expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables=stu exclude=index;
 注: exclude用於排除指定的型別,如不指明exclude,則會匯出stu中的所有物件。

7. scott下,匯出其他所有表,但不匯出stu和address表

expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp exclude=table:"in('STU')" exclude=table:"in('ADDRESS')";

8. 匯出student表的sno>1的記錄,和address表的sno>2的記錄

1. 編輯引數檔案expdp1.txt
directory=MY_DIR dumpfile=expdp_scott
tables=stu,address
query=stu:"where sno>1",address:"where sno=2";

2. 執行expdp命令
expdp scott/tiger@orcl parfile=expdp1.txt 

三、 impdp的重要引數

  1. content:指定要載入的資料, 其中有效關鍵字值為:(ALL) ,DATA ONLY和METADATA ONLY

  2. estimate:估算所佔用磁碟空間分方法.預設值是BLOCKS

  3. remap_schema用於將物件從一個使用者下匯入到另一個使用者下。

  4. remap_tablespace用於將物件從一個表空間下匯入到另一個表空間下。

  5. remap datafile:用於在不同檔案系統的平臺間, 切換資料檔案路徑。

四、impdp實踐

1. 匯出的scott的使用者檔案,匯入給loto;

  注:需要提前給loto使用者賦予MY_DIR的讀寫許可權

impdp loto/123456@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables=student remap_schema=scott:loto;

2. scott的匯出檔案預設是屬於USERS表空間的,當匯入到loto使用者下也預設是USERS表空間,可以使用remap_tablespaces引數將其匯入到指定表空間下。

expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp schemas=scott;
impdp loto/123456@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp remap_schema=scott:loto remap_tablespace=users:tb1;