首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> Oracle 資料庫 >> Oracle 資料庫基礎

Oracle資料庫備份恢復實戰(一):exp/imp 完整指南與進階技巧

     編輯:Oracle 資料庫基礎

Oracle 原生邏輯備份工具 `exp`/`imp` (Data Pump 前身) 雖已被 `expdp`/`impdp` 取代,但在舊版本維護、跨版本遷移、小型資料庫仍廣泛使用。本文系統整理三種執行模式、三種匯出模式、進階選項 (分割檔案、增量備份、SYSDBA、傳輸表空間)、效能調優、UNIX 管道壓縮技巧、常見字符集/版本相容問題。

一、基礎命令與執行模式

1. 幫助資訊

exp help=y
imp help=y

2. 三種執行模式

  • 互動式: `exp` 後按提示輸入參數
  • 命令列: `exp user/pwd@db file=/dir/xxx.dmp full=y`
  • 參數檔: `exp parfile=config.par`,參數檔內容:
    userid=user/pass
    buffer=8192000
    compress=n grants=y
    file=/dir/xxx.dmp full=y

二、三種匯出/匯入模式

模式匯出範例匯入範例
表模式`exp user/pwd file=xxx.dmp tables=t1,t2`
`exp user/pwd file=xxx.dmp tables=t1 query="where col1='val'"`
`imp user/pwd file=xxx.dmp tables=t1,t2 fromuser=u1 touser=u2 commit=y ignore=y`
用戶模式`exp user/pwd file=xxx.dmp owner=(u1,u2)`
僅結構:`rows=n`
`imp user/pwd file=xxx.dmp fromuser=u1 touser=u2 commit=y ignore=y`
全庫模式`exp user/pwd file=xxx.dmp full=y``imp user/pwd file=xxx.dmp full=y commit=y ignore=y`

三、進階選項

1. 多分割檔案 (突破檔案系統大小限制)

exp user/pwd file=1.dmp,2.dmp,3.dmp filesize=1000m full=y
imp user/pwd file=1.dmp,2.dmp,3.dmp filesize=1000m ...

2. 增量匯出/匯入 (僅 SYS/SYSTEM 可執行,9i 後 exp 不支援 inctype)

  • 完全增量 (`inctype=complete`):備份整庫
  • 增量型 (`inctype=incremental`):上次備份後變更
  • 累積型 (`inctype=cumulative`):上次完全備份後所有變更

匯入:`imp user/pwd full=y inctype=system/restore/inctype`

3. SYSDBA 權限匯出/匯入 (技支/傳輸表空間專用)

imp 'sys/pwd@inst as sysdba' tablespaces=ts1 transport_tablespace=y file=xxx.dmp datafiles=xxx.dbf

4. 傳輸表空間 (TTS) —— 移動資料如複製檔案般快速

前 10g 限制: 同平台、同字符集、同區塊大小、目標無同名表空間、SYS 物件不可遷、自包含物件集、部分物件類型不支援。
10g+ 跨平台: 位元組序相同 + RMAN 轉換檔案格式。

TTS 標準流程

  1. 來源表空間設唯讀:`ALTER TABLESPACE ts1 READ ONLY;`
  2. 匯出元資料:`exp 'sys/pwd as sysdba' transport_tablespace=y tablespaces=(ts1,ts2)`
  3. 複製 `.dbf` 資料檔 + `.dmp` 至目標 (二進制模式 FTP)
  4. 來源表空間恢復讀寫
  5. 目標端掛載:`imp 'sys/pwd as sysdba' transport_tablespace=y datafiles=('path/ts1.dbf') tablespaces=ts1 tts_owners=usr1`
  6. 目標表空間設讀寫

四、效能調優

目標關鍵參數/作法
加速 EXP`DIRECT=Y`、`BUFFER` 加大、`LARGE_POOL_SIZE` 增加、輸出檔置本地非 NFS 磁碟、UNIX 管道模式
加速 IMP先 `INDEXFILE` 建索引腳本→匯入數據→建索引;`COMMIT=N`、`ANALYZE=N`;大表空間/回滾段;`NOARCHIVELOG`;單用戶模式;UNIX 管道

五、UNIX 管道壓縮備份/還原 (零落地空間)

# 備份: exp 寫入管道 → gzip 壓縮 → 輸出 .dmp.gz
mkfifo /home/exppipe
gzip < /home/exppipe > exp.dmp.gz &
exp user/pwd file=/home/exppipe full=y buffer=20000000
rm /home/exppipe

# 還原: gunzip 解壓 → 管道 → imp 讀取
mkfifo /home/exppipe
gunzip < exp.dmp.gz > /home/exppipe &
imp user/pwd file=/home/exppipe full=y
rm /home/exppipe

六、常見問題對策

1. 字符集轉換技巧 (二進制編輯 DMP 檔頭)

  • 用 UltraEdit 開啟 DMP,讀取第 2-3 位元組 (如 `00 01` → 十進位 1)
  • `SELECT NLS_CHARSET_NAME(1) FROM DUAL;` → 得知來源字符集 (如 US7ASCII)
  • `SELECT NLS_CHARSET_ID('ZHS16GBK') FROM DUAL;` → 取得目標字符集 ID (如 852 → 十六進位 354)
  • 將 DMP 第 2-3 位元組改為 `03 54`,即完成字符集標識轉換

2. 版本相容性鐵律

  • IMP 版本必須匹配目標資料庫版本 (匯入 11g 必用 11g imp)
  • EXP 版本必須匹配來源/目標中較低版本 (10g→11g 用 10g exp)
  • IMP 不能匯入高版本 EXP 產生的 DMP (無向上相容)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved