oracle數據庫備份與恢復

全量備份 full=y

exp username/password@sid file=d:\exp.dmp full=y

數據恢復

imp username/password@sid file=d:\exp.dmp full=y ignore=y


Oracle 的導出導入是一個很常用的遷移工具。 在Oracle 10g中,Oracle 推出了數據泵(expdp/impdp). 它可以通過使用並行,從而在效率上要比exp/imp 要高。

       在Oracle 10g和11g的官方文檔裏沒有搜到有關exp/imp 的說明, 在9i裏找到了相關的使用說明。 連接如下:

 

Export

http://download.oracle.com/docs/cd/B10501_01/server.920/a96652/ch01.htm

 

Import

http://download.oracle.com/docs/cd/B10501_01/server.920/a96652/ch02.htm#SUTIL002

 

       執行備份和恢復的時候,不要在客戶端的shell 窗口執行, 要將備份的的進程添加到服務器的後臺執行。

 

參考:

       Linux 前臺後臺進程說明

       http://blog.csdn.net/tianlesoftware/archive/2011/01/27/6165753.aspx

 

. 命令幫助如下:

 

1.1 export

[oracle@rac1 ~]$ exp help=y

 

Export: Release 10.2.0.1.0 - Production on Tue May 10 10:35:26 2011

 

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

 

 

 

You can let Export prompt you for parameters by entering the EXP

command followed by your username/password:

 

     Example: EXP SCOTT/TIGER

 

Or, you can control how Export runs by entering the EXP command followed

by various arguments. To specify parameters, you use keywords:

 

     Format:  EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)

     Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)

               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table

 

USERID must be the first parameter on the command line.

 

Keyword    Description (Default)      Keyword      Description (Default)

--------------------------------------------------------------------------

USERID     username/password          FULL         export entire file (N)

BUFFER     size of data buffer        OWNER        list of owner usernames

FILE       output files (EXPDAT.DMP)  TABLES       list of table names

COMPRESS   import into one extent (Y) RECORDLENGTH length of IO record

GRANTS     export grants (Y)          INCTYPE      incremental export type

INDEXES    export indexes (Y)         RECORD       track incr. export (Y)

DIRECT     direct path (N)            TRIGGERS     export triggers (Y)

LOG        log file of screen output  STATISTICS   analyze objects (ESTIMATE)

ROWS       export data rows (Y)       PARFILE      parameter filename

CONSISTENT cross-table consistency(N) CONSTRAINTS  export constraints (Y)

 

OBJECT_CONSISTENT    transaction set to read only during object export (N)

FEEDBACK             display progress every x rows (0)

FILESIZE             maximum size of each dump file

FLASHBACK_SCN        SCN used to set session snapshot back to

FLASHBACK_TIME       time used to get the SCN closest to the specified time

QUERY                select clause used to export a subset of a table

RESUMABLE            suspend when a space related error is encountered(N)

RESUMABLE_NAME       text string used to identify resumable statement

RESUMABLE_TIMEOUT    wait time for RESUMABLE

TTS_FULL_CHECK       perform full or partial dependency check for TTS

VOLSIZE              number of bytes to write to each tape volume

TABLESPACES          list of tablespaces to export

TRANSPORT_TABLESPACE export transportable tablespace metadata (N)

TEMPLATE             template name which invokes iAS mode export

 

Export terminated successfully without warnings.

 

1.2 import

[oracle@rac1 ~]$ imp help=y

 

Import: Release 10.2.0.1.0 - Production on Tue May 10 10:35:49 2011

 

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

 

 

 

You can let Import prompt you for parameters by entering the IMP

command followed by your username/password:

 

     Example: IMP SCOTT/TIGER

 

Or, you can control how Import runs by entering the IMP command followed

by various arguments. To specify parameters, you use keywords:

 

     Format:  IMP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)

     Example: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N

               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table

 

USERID must be the first parameter on the command line.

 

Keyword  Description (Default)       Keyword      Description (Default)

--------------------------------------------------------------------------

USERID   username/password           FULL         import entire file (N)

BUFFER   size of data buffer         FROMUSER     list of owner usernames

FILE     input files (EXPDAT.DMP)    TOUSER       list of usernames

SHOW     just list file contents (N) TABLES       list of table names

IGNORE   ignore create errors (N)    RECORDLENGTH length of IO record

GRANTS   import grants (Y)           INCTYPE      incremental import type

INDEXES  import indexes (Y)          COMMIT       commit array insert (N)

ROWS     import data rows (Y)        PARFILE      parameter filename

LOG      log file of screen output   CONSTRAINTS  import constraints (Y)

DESTROY                overwrite tablespace data file (N)

INDEXFILE              write table/index info to specified file

SKIP_UNUSABLE_INDEXES  skip maintenance of unusable indexes (N)

FEEDBACK               display progress every x rows(0)

TOID_NOVALIDATE        skip validation of specified type ids

FILESIZE               maximum size of each dump file

STATISTICS             import precomputed statistics (always)

RESUMABLE              suspend when a space related error is encountered(N)

RESUMABLE_NAME         text string used to identify resumable statement

RESUMABLE_TIMEOUT      wait time for RESUMABLE

COMPILE                compile procedures, packages, and functions (Y)

STREAMS_CONFIGURATION  import streams general metadata (Y)

STREAMS_INSTANTIATION  import streams instantiation metadata (N)

VOLSIZE                number of bytes in file on each volume of a file on tape

 

The following keywords only apply to transportable tablespaces

TRANSPORT_TABLESPACE import transportable tablespace metadata (N)

TABLESPACES tablespaces to be transported into database

DATAFILES datafiles to be transported into database

TTS_OWNERS users that own data in the transportable tablespace set

 

Import terminated successfully without warnings.

[oracle@rac1 ~]$

 

.  Export

1. 表模式

       備份某個用戶模式下指定的對象(表)。業務數據庫通常採用這種備份方式。若備份到本地文件,使用如下命令:

exp icdmain/icd rows=y indexes=n compress=n buffer=65536 feedback=100000 volsize=0 file=exp.dmp log=exp.log tables=tab1,tab2,tab3

 

若直接備份到磁帶設備,使用如下命令:

exp icdmain/icd rows=y indexes=n compress=n buffer=65536 feedback=100000 volsize=0 file=/dev/rmt0 log=exp.log tables=tab1,tab2,tab3

 

       注:在磁盤空間允許的情況下,應先備份到本地服務器,然後再拷貝到磁帶。出於速度方面的考慮,儘量不要直接備份到磁帶設備。

 

2. 用戶模式

       備份某個用戶模式下的所有對象。業務數據庫通常採用這種備份方式。 若備份到本地文件,使用如下命令:

       exp icdmain/icd owner=icdmain rows=y indexes=n compress=n buffer=65536 feedback=100000 file=exp.dmp log=exp.log

 

若直接備份到磁帶設備,使用如下命令:

       exp icdmain/icd owner=icdmain rows=y indexes=n compress=n buffer=65536 feedback=100000 volsize=0 file=/dev/rmt0 log=exp.log

 

       注:如果磁盤有空間,建議備份到磁盤,然後再拷貝到磁帶。如果數據庫數據量較小,可採用這種辦法備份。

 

3. 完全模式

       備份完整的數據庫。業務數據庫不採用這種備份方式。備份命令爲:

      exp icdmain/icd rows=y indexes=n compress=n buffer=65536 feedback=100000 full=y  file=exp_.dmp log=exp.log

 

 

三.IMPORT

       import 要與export 對應。 就是採用什麼方式export,就需要採用什麼方式import,因此import 也有三種模式:表恢復、用戶恢復、完全恢復。

 

1. 表模式

此方式將根據按照表模式備份的數據進行恢復。  

 

1.1 恢復備份數據的全部內容

       imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n file=exp.dmp log=imp.log

 

若從磁帶設備恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n volsize=0 file=/dev/rmt0 log=imp.log

 

1.2 恢復備份數據中的指定表:

若從本地文件恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n file=exp.dmp log=imp.log tables=t1,t2,t3

 

若從磁帶設備恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n volsize=0 file=/dev/rmt0  

log=imp.log tables=t1,t2,t3

 

2. 用戶模式

此方式將根據按照用戶模式備份的數據進行恢復。

 

2.1. 恢復備份數據的全部內容

若從本地文件恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n file=exp.dmp log=imp.log

 

若從磁帶設備恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n volsize=0 file=/dev/rmt0 log=imp.log

 

2.2. 恢復備份數據中的指定表

若從本地文件恢復,使用如下命令:

imp icdmain/icd fromuser=icdmain touser=icdmain rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=n volsize=0 file=exp.dmp log=imp.log tables=t1,t2,t3;

 

 

3. 完全模式

       如果備份方式爲完全模式,採用下列恢復方法:

 

imp system/manager rows=y indexes=n commit=y buffer=65536 feedback=100000 ignore=y volsize=0 full=y file=exp.dmp log=imp.log

 

 

4. 參數說明

4.1. ignore參數

       Oracle在恢復數據的過程中,當恢復某個表時,該表已經存在,就要根據ignore參數的設置來決定如何操作。

       ignore=y,Oracle不執行CREATE TABLE語句,直接將數據插入到表中,如果插入的記錄違背了約束條件,比如主鍵約束,則出錯的記錄不會插入,但合法的記錄會添加到表中。

      ignore=n,Oracle不執行CREATE TABLE語句,同時也不會將數據插入到表中,而是忽略該表的錯誤,繼續恢復下一個表。 ­

 

4.2. indexes參數

       在恢復數據的過程中,若indexes=n,則表上的索引不會被恢復,但是主鍵對應的唯一索引將無條件恢復,這是爲了保證數據的完整性。

 

4.3 字符集轉換

       對於單字節字符集(例如US7ASCII,恢復時,數據庫自動轉換爲該會話的字符集(NLS_LANG參數);

       對於多字節字符集(例如ZHS16CGB231280),恢復時,應儘量使字符集相同(避免轉換),如果要轉換,目標數據庫的字符集應是輸出數據庫字符集的超集。

 

5.  IMP 常見問題及解決方法

5.1  數據庫對象已經存在

      一般情況, 導入數據前應該徹底刪除目標數據下的表, 序列, 函數/過程,觸發器等;  數據庫對象已經存在, 按缺省的imp參數, 則會導入失敗如果用了參數ignore=y, 會把exp文件內的數據內容導入如果表有唯一關鍵字的約束條件, 不合條件將不被導入如果表沒有唯一關鍵字的約束條件, 將引起記錄重複

 

5.2  數據庫對象有主外鍵約束

      不符合主外鍵約束時, 數據會導入失敗,

      解決辦法:

         先導入主表, 再導入依存表

     disable目標導入對象的主外鍵約束, 導入數據後, 再enable它們

 

5.3  權限不夠

       如果要把A用戶的數據導入B用戶下, A用戶需要有imp_full_database權限

 

5.4  導入大表( 大於80M ) , 存儲分配失敗

      默認的EXP時, compress = Y, 也就是把所有的數據壓縮在一個數據塊上.

      導入時, 如果不存在連續一個大數據塊, 則會導入失敗. 導出80M以上的大表時, 記得compress= N, 則不會引起這種錯誤.

 

5.5  impexp使用的字符集不同

      如果字符集不同, 導入會失敗, 可以改變unix環境變量或者NT註冊表裏NLS_LANG相關信息.  導入完成後再改回來.

 

5.6  impexp版本不能往上兼容

       可以從低版本導入高版本,但不能從高版本導入到低版本。

       如果遇到遷移因版本不同的問題,可以用低版本的export 導出,到導入到低版本。

 

 

. 示例

 

4.1 oracle創建表空間,創建用戶

 

//創建臨時表空間

create temporary tablespace test_temp

tempfile ‘/u01/app/oracle/oradata/orcl/test_temp01.Dbf'

size 32m autoextend on next 32m maxsize 2048m

extent management local;

 

//創建數據表空間

create tablespace test_data logging

datafile '/u01/app/oracle/oradata/orcl/test_data01.dbf' ­

size 32m autoextend on

next 32m maxsize 2048m

extent management local;

 

//創建用戶並指定表空間

create user username identified by password

default tablespace test_data

temporary tablespace test_temp;

 

//給用戶授予權限 ­

grant connect,resource to username;

 

先創建一個用戶和表空間,用戶名david,密碼david.在這個表空間下創建一個表:tianle。隨便插入些數據。代碼如下:

 

SQL> create tablespace test_data

  3  datafile '/u01/app/oracle/oradata/orcl/test_data01.dbf'

  4  size 5m;

Tablespace created.

 

SQL> create user david identified by david default tablespace test_data;

SQL> grant connect,resource to david;

SQL> conn david/david

SQL> create table tianle(id number, content varchar2(100));

 

SQL> set wrap off

SQL> column id format a20;

SQL> column content format a50;

 

 

4.2 表模式備份恢復

備份:

[oracle@roy orcl]$ exp david/david rows=y indexes=n compress=n buffer=65536 file=exp_tianle_090101.dmp log=exp_tianle_090101.log tables=(tianle);

恢復:

[oracle@roy orcl]$ imp david/david fromuser=david touser=david rows=y indexes=n commit=y   buffer=65536 file=exp_tianle_090101.dmp log=imp_tianle_090101.log tables=(tianle); ­

 

4.3 用戶模式備份與恢復

備份:

[oracle@roy orcl]$ exp david/david owner=david rows=y indexes=n compress=n buffer=65536 file=exp_david__090101.dmp log=exp_david_090101.log; ­

 

恢復:

[oracle@roy orcl]$ imp david/david fromuser=david touser=david rows=y indexes=n commit=y buffer=65536 file=exp_tianle_090101.dmp log=exp_tianle_090101.log; ­

 

4.4 完全模式備份與恢復

備份:

[oracle@roy orcl]$ exp david/david rows=y indexes=n compress=n buffer=65536 full=y file=exp_fulldatabase_090101.dmp log=exp_fulldatabase_090101.log; ­

 

恢復

[oracle@roy orcl]$ imp david/david rows=y indexes=n commit=y  full=y ignore=y buffer=65536 file=/tmp/exp_fulldatabase_090101.dmp log=/tmp/imp.log;

 

 

 

 

 

-------------------------------------------------------------------------------------------------------

Blog: http://blog.csdn.net/tianlesoftware

Email: [email protected]

DBA1 羣:62697716(滿);   DBA2 羣:62697977(滿)   DBA3 羣:62697850(滿)  

DBA 超級羣:63306533(滿);  DBA4 羣: 83829929  DBA5羣: 142216823   

聊天 羣:40132017   聊天2羣:69087192

--加羣需要在備註說明Oracle表空間和數據文件的關係,否則拒絕申請


發佈了35 篇原創文章 · 獲贊 43 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章