邏輯備份(Logical Backup)使用 exp 將物件結構與資料導出為 .dmp 檔案,恢復時用 imp 導入。適用於資料庫開啟狀態、跨版本/平台遷移、單一物件備份等場景。本文系統整理 exp/imp 三種模式(表、方案、完整資料庫)的常用參數與實戰指令。
| 參數 | 說明 |
|---|---|
| userid | 執行導出的帳號/密碼[@連線字串] |
| tables | 指定導出的表(支援多表、跨方案) |
| owner | 指定導出的方案(Schema) |
| full=y | 導出整個資料庫(需 DBA/EXP_FULL_DATABASE 權限) |
| inctype | 增量備份類型:COMPLETE / INCREMENTAL / CUMULATIVE |
| rows=y|n | 是否導出資料列(n 僅導結構) |
| file | 導出檔案路徑與檔名 |
| direct=y | 直接路徑導出(Direct Path),大量資料時較快,但要求字符集一致 |
-- 導出自己的表
exp userid=scott/tiger@orcl tables=(emp,dept) file=d:\e1.dmp
-- 導出其他方案的表(需 DBA 權限)
exp userid=system/manager@orcl tables=(scott.emp) file=d:\e2.dmp
-- 僅導出表結構
exp userid=scott/tiger@orcl tables=(emp) file=d:\e3.dmp rows=n
-- 直接路徑導出(大量資料加速)
exp userid=scott/tiger@orcl tables=(emp) file=d:\e4.dmp direct=y
-- 導出自己的方案
exp userid=scott/tiger@orcl owner=scott file=d:\scott.dmp
-- 導出多個方案
exp userid=system/manager@orcl owner=(system,scott) file=d:\system.dmp
-- 完全增量導出(基準備份)
exp userid=system/manager@orcl full=y inctype=complete file=d:\all.dmp
-- 增量導出(僅備份上次備份後變更)
exp userid=system/manager@orcl full=y inctype=incremental file=d:\inc.dmp
-- 累積導出(備份上次完全導出後所有變更)
exp userid=system/manager@orcl full=y inctype=cumulative file=d:\cum.dmp
| 參數 | 說明 |
|---|---|
| userid | 執行導入的帳號/密碼[@連線字串] |
| tables | 指定導入的表 |
| fromuser | 來源方案(導出時的擁有者) |
| touser | 目標方案(導入後的擁有者) |
| file | 導入檔案路徑 |
| full=y | 導入整個導出檔案內容 |
| inctype | 增量導入類型(RESTORE) |
| rows=y|n | 是否導入資料列 |
| ignore=y | 物件已存在時忽略錯誤,僅導入資料 |
-- 導入自己的表
imp userid=scott/tiger@orcl tables=(emp) file=d:\xx.dmp
-- 導入表到其他使用者(需 DBA/IMP_FULL_DATABASE)
imp userid=system/pwd@orcl tables=(emp) file=d:\xx.dmp touser=scott
-- 僅導入結構
imp userid=scott/tiger@orcl tables=(emp) file=d:\xx.dmp rows=n
-- 物件存在時僅導入資料
imp userid=scott/tiger@orcl tables=(emp) file=d:\xx.dmp ignore=y
-- 導入自己的方案
imp userid=scott/tiger file=d:\xxx.dmp
-- 導入其他方案(跨 Schema)
imp userid=system/pwd file=d:\xxx.dmp fromuser=導出用戶 touser=導入用戶 ignore=y
注意:若 fromuser 指定的用戶在目標資料庫不存在,需先建立該用戶與表空間,否則導入會失敗。
imp userid=system/pwd full=y file=d:\xxx.dmp
假設備份排程:週一完全(A)、週二~四增量(B/C/D)、週五累積(E)、週六~日增量(F/G)。週日災難發生時恢復步驟:
CREATE DATABASE 重建資料庫結構imp system/pwd inctype=RESTORE full=y file=Aimp system/pwd inctype=RESTORE full=y file=Eimp system/pwd inctype=RESTORE full=y file=Fexp/imp 時需在 $ORACLE_HOME/bin 目錄下,或已將該路徑加入 PATH。direct=y 會報錯;一般模式會自動轉換但可能影響效能。PARFILE 參數檔管理冗長參數,避免命令列過長。