ORACLE備份&恢復案例

shwenwen發表於2007-10-08
ORACLE備份&恢復案例[@more@]

ORACLE備份&恢復案例

ORACLE BACKUP&RESTORE SCHEME

第一章. 理解什麼是資料庫恢復
當我們使用一個資料庫時,總希望資料庫的內容是可靠的、正確的,但由於計算機系統的故障(硬體故障、軟體故障、網路故障、程式故障和系統故障)影響資料庫系統的操作,影響資料庫中資料的正確性,甚至破壞資料庫,使資料庫中全部或部分資料丟失。因此當發生上述故障後,希望能重構這個完整的資料庫,該處理稱為資料庫恢復。恢復過程大致可以分為復原(Restore)與恢復(Recover)過程。

資料庫恢復可以分為以下兩類:

1.1例項故障的一致性恢復
當例項意外地(如掉電、後臺程式故障等)或預料地(發出SHUTDOUM ABORT語句)中止時出現例項故障,此時需要例項恢復。例項恢復將資料庫恢復到故障之前的事務一致狀態。如果在線上後備發現例項故障,則需介質恢復。在其它情況ORACLE在下次資料庫起動時(對新例項裝配和開啟),自動地執行例項恢復。如果需要,從裝配狀態變為開啟狀態,自動地激發例項恢復,由下列處理:

??(1) 為了解恢復資料檔案中沒有記錄的資料,進行向前滾。該資料記錄在線上日誌,包括對回滾段的內容恢復。

??(2) 回滾未提交的事務,按步1重新生成回滾段所指定的操作。

??(3) 釋放在故障時正在處理事務所持有的資源。

??(4) 解決在故障時正經歷一階段提交的任何懸而未決的分佈事務。

1.2介質故障或檔案錯誤的不一致恢復
介質故障是當一個檔案、一個檔案的部分或磁碟不能讀或不能寫時出現的故障。

檔案錯誤一般指意外的錯誤導致檔案被刪除或意外事故導致檔案的不一致。

這種狀態下的資料庫都是不一致的,需要DBA手工來進行資料庫的恢復,這種恢復有兩種形式,決定於資料庫執行的歸檔方式和備份方式。

(1) 完全介質恢復可恢復全部丟失的修改。一般情況下需要有資料庫的備份且資料庫執行在歸檔狀態下並且有可用歸檔日誌時才可能。對於不同型別的錯誤,有不同型別的完全恢復可使用,其決定於毀壞檔案和資料庫的可用性。

(2) 不完全介質恢復是在完全介質恢復不可能或不要求時進行的介質恢復。重構受損的資料庫,使其恢復介質故障前或使用者出錯之前的一個事務一致性狀態。不完全介質恢復有不同型別的使用,決定於需要不完全介質恢復的情況,有下列型別:基於撤消、基於時間和基於修改的不完全恢復。

??基於撤消(CANCEL)恢復:在某種情況,不完全介質恢復必須被控制,DBA可撤消在指定點的操作。基於撤消的恢復地在一個或多個日誌組(線上的或歸檔的)已被介質故障所破壞,不能用於恢復過程時使用,所以介質恢復必須控制,以致在使用最近的、未損的日誌組於資料檔案後中止恢復操作。

??基於時間(TIME)和基於修改(SCN)的恢復:如果DBA希望恢復到過去的某個指定點,是一種理想的不完全介質恢復,一般發生在恢復到某個特定操作之前,恢復到如意外刪除某個資料表之前。


第二章. 資料庫恢復案例測試環境
2.1 資料庫環境
以下的所有案例都是透過測試經過,環境為:

OS:Windows 2000 Server

DB:Oracle 816

DBNAME:TEST

資料檔案:

SQL> select file#,status,enabled,name from v$datafile;


FILE# STATUS ENABLED NAME

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

1 SYSTEM READ WRITE D:ORACLEORADATATESTSYSTEM01.DBF

2 ONLINE READ WRITE D:ORACLEORADATATESTRBS01.DBF

3 ONLINE READ WRITE D:ORACLEORADATATESTUSERS01.DBF

4 ONLINE READ WRITE D:ORACLEORADATATESTTEMP01.DBF

5 ONLINE READ WRITE D:ORACLEORADATATESTTOOLS01.DBF

6 ONLINE READ WRITE D:ORACLEORADATATESTINDX01.DBF

控制檔案:

SQL> select * from v$controlfile;


STATUS NAME

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

D:ORACLEORADATATESTCONTROL01.CTL

D:ORACLEORADATATESTCONTROL02.CTL

D:ORACLEORADATATESTCONTROL03.CTL

聯機日誌:

SQL> select * from v$logfile;


GROUP# STATUS MEMBER

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

1 STALE D:ORACLEORADATATESTREDO01.LOG

2 D:ORACLEORADATATESTREDO02.LOG

3 STALE D:ORACLEORADATATESTREDO03.LOG

2.2 資料庫備份指令碼
冷備份指令碼

rem script:coldbak.sql

rem creater:chenjiping

rem date:5.8.2003

rem desc:offline full backup database


--connect database

connect internal/password;

--shutdown database

shutdown immediate;

--Copy Data file

!xcopy d:oracleoradatatest*.dbf d:database/H/R;

--Copy Control file

!xcopy d:oracleoradatatest*.ctl d:database/H/R;

--Copy Log file

!xcopy d:oracleoradatatest*.log d:database/H/R;

--startup database

startup;


說明:

1、以上指令碼在資料庫關閉狀態下備份資料庫所有的資料檔案,聯機日誌,控制檔案(在一個目錄下),如果成功備份,所有檔案是一致的。

2、沒有備份引數檔案,引數檔案可以另外備份,沒有必要每次都備份,只需要在改變設定後備份一次。

3、如果以上命令沒有成功依次執行,那麼備份將是無效的,如連線資料庫不成功,那麼肯定關閉資料庫也不成功,那麼備份則無效

4、冷備份建議下人工干預下執行。


資料庫OS熱全備份指令碼

rem script:hotbak.sql

rem creater:chenjiping

rem date:5.8.2003

rem desc:backup all database datafile in archive


--connect database

connect internal/password;


--archive

alter system archive log current;

--start


alter tablespace system begin backup;

!xcopy d:oracleoradatatestsystem01.dbf d:databak/H/R;

alter tablespace system end backup;


alter tablespace rbs begin backup;

!xcopy d:oracleoradatatestrbs01.dbf d:databak/H/R;

alter tablespace rbs end backup;


alter tablespace users begin backup;

!xcopy d:oracleoradatatestusers01.dbf d:databak/H/R;

alter tablespace users end backup;


alter tablespace tools begin backup;

!xcopy d:oracleoradatatesttools01.dbf d:databak/H/R;

alter tablespace tools end backup;


alter tablespace indx begin backup;

!xcopy d:oracleoradatatestindx01.dbf d:databak/H/R;

alter tablespace indx end backup;

--end


--bak control file

--binary

alter database backup controlfile to 'd:databakcontrolbinbak.000';

--ascii

alter database backup controlfile to trace;


alter system archive log current;

說明:

1、熱備份必須在資料庫歸檔方式下才可以執行

2、以上指令碼可以在資料庫執行狀態下備份資料庫所有的資料檔案(除了臨時資料檔案),沒有必要備份聯機日誌。

3、歸檔日誌至少需要一次完整備份之後的所有日誌。

4、如果以上命令沒有成功依次執行,那麼備份也是無效的,如連線資料庫不成功,那麼備份則無效


RMAN備份只講敘有恢復目錄的情況,如果沒有恢復目錄,情形大致相似。以下是RMAN的熱備份全備份的指令碼:

# script:bakup.rcv

# creater:chenjiping

# date:5.8.2003

# desc:backup all database datafile in archive with rman


# connect database

connect rcvcat ;

connect target internal/virpure;


# start backup database

run{

allocate channel c1 type disk;

backup full tag 'dbfull' format 'd:backupfull%u_%s_%p' database

include current controlfile;

sql 'alter system archive log current';

release channel c1;

}

# end


說明:

1、 資料庫必須執行在歸檔模式下

2、 RMAN將自動備份資料檔案,執行可靠

3、 歸檔日誌另外備份處理,但至少需要儲存一次備份來的日誌

4、 沒有必要用RMAN做冷備份,效果不好


以上舉例說明了資料庫的恢復案例的測試環境與部分備份測試指令碼,其它的備份指令碼可以根據以上指令碼演變而來或在案例中加以說明。

資料庫的自動例項將不加以說明,這裡只舉例說明媒體錯誤或人為錯誤造成的恢復可能。

以上包括以下案例都是在WINDOWS+ORACLE816上測試驗證的,在不同的作業系統與不同的資料庫版本中略有差別。


第三章. 瞭解與恢復相關的資訊
1、理解報警日誌檔案

報警日誌檔案一般記載了資料庫的啟動/關閉資訊,歸檔資訊,備份資訊,恢復資訊,常見錯誤資訊,部分資料庫修改記錄等。一般令名規則為Alrt.log或Alrt.log,如我的測試資料庫的報警日誌檔案的名稱為testalrt.log。

報警日誌檔案的路徑是根據初始化引數background_dump_dest來決定的,如在我的機器上,該引數值為 D:Oracleadmintestbdump,那麼,你就可以在該路徑下找到該檔案

2、後臺程式跟蹤檔案

後臺程式跟蹤檔案的路徑與報警日誌檔案的路徑一致,在某些情況下,你可以透過後臺跟蹤檔案的資訊瞭解更多的需要恢復的資訊。如在資料庫需要恢復的時候,報警日誌檔案中常有這樣的語句:

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 1 - see DBWR trace file

透過提示的DBWR跟蹤檔案,可以查詢到更詳細的資訊。

3、v$recover_file與v$recovery_log

這是兩個動態效能檢視,可以在mount下檢視,透過這兩個檢視,你可以瞭解詳細的需要恢復的資料檔案與需要使用到的歸檔日誌。

第四章. 資料庫恢復案例
4.1非歸檔模式下的備份與恢復
備份方案:採用OS冷備份

1.連線資料庫並建立測試表

SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003

(c) Copyright 1999 Oracle Corporation. All rights reserved.

SQL> connect internal/password as sysdba;

Connected.

SQL> create table test(a int);

Table created

SQL> insert into test values(1);

1 row inserted

SQL> commit;

Commit complete


2.備份資料庫

SQL> @coldbak.sql 或在DOS下 svrmgrl @coldbak.sql


3.再插入記錄

SQL> insert into test values(2);

1 row inserted

SQL> commit;

Commit complete

SQL> select * from test;

A

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

1

2

4.關閉資料庫

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.


5.毀壞一個或多個資料檔案,如刪除user01.dbf

C:>del D:ORACLEORADATATESTUSERS01.DBF

模擬媒體毀壞


6.重新啟動資料庫,會發現如下錯誤

SQL> startup

ORACLE instance started.


Total System Global Area 102020364 bytes

Fixed Size 70924 bytes

Variable Size 85487616 bytes

Database Buffers 16384000 bytes

Redo Buffers 77824 bytes

Database mounted.

ORA-01157: cannot identify/lock data file 3 - see DBWR trace file

ORA-01110: data file 3: 'D:ORACLEORADATATESTUSERS01.DBF'


在報警檔案中,會有更詳細的資訊

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 3 - see DBWR trace file

ORA-01110: data file 3: 'D:ORACLEORADATATESTUSERS01.DBF'

ORA-27041: unable to open file

OSD-04002: unable to open file

O/S-Error: (OS 2) 系統找不到指定的檔案。


7.複製備份復原到原來位置(restore過程)

C:>xcopy d:database*.* d:oracleoradatatest/H/R/S


8.開啟資料庫,檢查資料

SQL> alter database open;

Database altered.

SQL> select * from test;

A

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

1


這裡可以發現,資料庫恢復成功,但在備份之後與崩潰之前的資料丟失了。

說明:

1、非歸檔模式下的恢復方案可選性很小,一般情況下只能有一種恢復方式,就是資料庫的冷備份的完全恢復,僅僅需要複製原來的備份就可以(restore),不需要recover。

2、這種情況下的恢復,可以完全恢復到備份的點上,但是可能是丟失資料的,在備份之後與崩潰之前的資料將全部丟失。

3、不管毀壞了多少資料檔案或是聯機日誌或是控制檔案,都可以透過這個辦法恢復,因為這個恢復過程是Restore所有的冷備份檔案,而這個備份點上的所有檔案是一致的,與最新的資料庫沒有關係,就好比把資料庫又放到了一個以前的“點”上。

4、對於非歸檔模式下,最好的辦法就是採用OS的冷備份,建議不要用RMAN來作冷備份,效果不好,因為RMAN不備份聯機日誌,restore不能根本解決問題。

5、如果沒有備份聯機日誌,如RMAN的備份,就需要利用不完全恢復(until cancel)的方法來重新建立聯機日誌檔案

4.2歸檔模式下丟失或損壞一個資料檔案
4.2.1 OS備份方案
在歸檔方式下損壞或丟失一個資料檔案,如果存在相應的備份與該備份以來的歸檔日誌,恢復還是比較簡單的,可以作到儘量少的Down機時間,並能作到資料庫的完全恢復。

1、連線資料庫,建立測試表並插入記錄

SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003

(c) Copyright 1999 Oracle Corporation. All rights reserved.

SQL> connect internal/password as sysdba;

Connected.

SQL> create table test(a int) tablespace users;

Table created

SQL> insert into test values(1);

1 row inserted

SQL> commit;

Commit complete


2、備份資料庫

SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql


3、繼續在測試表中插入記錄

SQL> insert into test values(2);

1 row inserted

SQL> commit;

Commit complete

SQL> select * from test;

A

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

1

2

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.


4、關閉資料庫,模擬丟失資料檔案

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down

C:>del D:ORACLEORADATATESTUSERS01.DBF

模擬媒體毀壞


5、啟動資料庫錯誤,離線該資料檔案

SQL> startup

ORACLE instance started.


Total System Global Area 102020364 bytes

Fixed Size 70924 bytes

Variable Size 85487616 bytes

Database Buffers 16384000 bytes

Redo Buffers 77824 bytes

Database mounted.

ORA-01157: cannot identify/lock data file 3 - see DBWR trace file

ORA-01110: data file 3: 'D:ORACLEORADATATESTUSERS01.DBF'

還可以檢視報警檔案(見上一個恢復案例)或動態檢視v$recover_file

如SQL> select * from v$recover_file;


FILE# ONLINE ERROR CHANGE# TIME

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

3 ONLINE 1013500 2003-05-07


離線資料檔案

SQL> alter database datafile 3 offline drop;

Database altered.


6、開啟資料庫,複製備份回來(restore),恢復(recover)該資料檔案,並聯機

SQL> alter database open;

Database altered.

複製備份從備份處

copy d:databak users01.dbf d:oracleoradatatest;

恢復該資料檔案

SQL> recover datafile 3;

ORA-00279: change 1053698 generated at 05/07/2003 17:51:26 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00304.ARC

ORA-00280: change 1053698 for thread 1 is in sequence #304


Specify log: {=suggested | filename | AUTO | CANCEL}

AUTO

ORA-00279: change 1053701 generated at 05/07/2003 17:51:39 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00305.ARC

ORA-00280: change 1053701 for thread 1 is in sequence #305

ORA-00278: log file 'D:ORACLEORADATATESTARCHIVETESTT001S00304.ARC' no

longer needed for this recovery


Log applied.

Media recovery complete.

恢復成功,聯機該資料檔案

SQL> alter database datafile 3 online;

Database altered.


7、檢查資料庫的資料(完全恢復)

SQL> select * from test;

A

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

1

2

說明:

1、採用熱備份,需要執行在歸檔模式下,可以實現資料庫的完全恢復,也就是說,從備份後到資料庫崩潰時的資料都不會丟失。

2、可以採用全備份資料庫的方式備份,對於特殊情況,也可以只備份特定的資料檔案,如只備份使用者表空間(一般情況下對於某些寫特別頻繁的資料檔案,可以單獨加大備份頻率)

3、如果在恢復過程中,發現損壞的是多個資料檔案,即可以採用一個一個資料檔案的恢復方法(第5步中需要對資料檔案一一離線,第6步中需要對資料檔案分別恢復),也可以採用整個資料庫的恢復方法。

4、如果是系統表空間的損壞,不能採用此方法

4.2.2 RMAN備份方案
RMAN也可以進行聯機備份,而且備份與恢復方法將比OS備份更簡單可靠。

1、連線資料庫,建立測試表並插入記錄

SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003

(c) Copyright 1999 Oracle Corporation. All rights reserved.

SQL> connect internal/password as sysdba;

Connected.


SQL> create table test(a int) tablespace users;

Table created

SQL> insert into test values(1);

1 row inserted

SQL> commit;

Commit complete


2、備份資料庫表空間users

C:>rman

Recovery Manager: Release 8.1.6.0.0 - Production

RMAN> connect rcvcat

RMAN-06008: connected to recovery catalog database

RMAN> connect target internal/virpure

RMAN-06005: connected to target database: TEST (DBID=1788174720)


RMAN> run{

2> allocate channel c1 type disk;

3> backup tag 'tsuser' format 'd:backuptsuser_%u_%s_%p'

4> tablespace users;

5> release channel c1;

6> }


RMAN-03022: compiling command: allocate

RMAN-03023: executing command: allocate

RMAN-08030: allocated channel: c1

RMAN-08500: channel c1: sid=16 devtype=DISK


RMAN-03022: compiling command: backup

RMAN-03025: performing implicit partial resync of recovery catalog

RMAN-03023: executing command: partial resync

RMAN-08003: starting partial resync of recovery catalog

RMAN-08005: partial resync complete

RMAN-03023: executing command: backup

RMAN-08008: channel c1: starting full datafile backupset

RMAN-08502: set_count=5 set_stamp=494177612 creation_time=16-MAY-03

RMAN-08010: channel c1: specifying datafile(s) in backupset

RMAN-08522: input datafile fno=00003 name=D:ORACLEORADATATESTUSER01.DBF

RMAN-08013: channel c1: piece 1 created

RMAN-08503: piece handle=D:BACKUPTSUSER_05EN93AC_5_1 comment=NONE

RMAN-08525: backup set complete, elapsed time: 00:00:01

RMAN-03023: executing command: partial resync

RMAN-08003: starting partial resync of recovery catalog

RMAN-08005: partial resync complete

RMAN-03022: compiling command: release

RMAN-03023: executing command: release

RMAN-08031: released channel: c1

RMAN>


3、繼續在測試表中插入記錄

SQL> insert into test values(2);

1 row inserted

SQL> commit;

Commit complete

SQL> select * from test;

A

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

1

2

SQL> alter system switch logfile;

System altered.

SQL>r

1* alter system switch logfile;

System altered.


4、關閉資料庫,模擬丟失資料檔案

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down


C:>del D:ORACLEORADATATESTUSER01.DBF


5、啟動資料庫,檢查錯誤

SQL> startup

ORACLE instance started.

Total System Global Area 102020364 bytes

Fixed Size 70924 bytes

Variable Size 85487616 bytes

Database Buffers 16384000 bytes

Redo Buffers 77824 bytes

Database mounted.

ORA-01157: cannot identify/lock data file 3 - see DBWR trace file

ORA-01110: data file 3: 'D:ORACLEORADATATESTUSER01.DBF'


6、先開啟資料庫

SQL> alter database datafile 3 offline drop;

Database altered.

SQL> alter database open;

Database altered.


7、恢復該表空間

恢復指令碼可以是恢復單個資料檔案

run{

allocate channel c1 type disk;

restore datafile 3;

recover datafile 3;

sql 'alter database datafile 3 online';

release channel c1;

}

也可以是,恢復表空間

run{

allocate channel c1 type disk;

restore tablespace users;

recover tablespace users;

sql 'alter database datafile 3 online';

release channel c1;

}

過程如下:

C:>rman

Recovery Manager: Release 8.1.6.0.0 - Production

RMAN> connect rcvcat

RMAN-06008: connected to recovery catalog database

RMAN> connect target internal/virpure

RMAN-06005: connected to target database: TEST (DBID=1788174720)


RMAN> run{

2> allocate channel c1 type disk;

3> restore datafile 3;

4> recover datafile 3;

5> sql 'alter database datafile 3 online';

6> release channel c1;

7> }


RMAN-03022: compiling command: allocate

RMAN-03023: executing command: allocate

RMAN-08030: allocated channel: c1

RMAN-08500: channel c1: sid=13 devtype=DISK

RMAN-03022: compiling command: restore

RMAN-03022: compiling command: IRESTORE

RMAN-03023: executing command: IRESTORE

RMAN-08016: channel c1: starting datafile backupset restore

RMAN-08502: set_count=5 set_stamp=494177612 creation_time=16-MAY-03

RMAN-08089: channel c1: specifying datafile(s) to restore from backup set

RMAN-08523: restoring datafile 00003 to D:ORACLEORADATATESTUSER01.DBF

RMAN-08023: channel c1: restored backup piece 1

RMAN-08511: piece handle=D:BACKUPTSUSER_05EN93AC_5_1 tag=TSUSER params=NULL

RMAN-08024: channel c1: restore complete

RMAN-03023: executing command: partial resync

RMAN-08003: starting partial resync of recovery catalog

RMAN-08005: partial resync complete

RMAN-03022: compiling command: recover

RMAN-03022: compiling command: recover(1)

RMAN-03022: compiling command: recover(2)

RMAN-03022: compiling command: recover(3)

RMAN-03023: executing command: recover(3)

RMAN-08054: starting media recovery

RMAN-03022: compiling command: recover(4)

RMAN-06050: archivelog thread 1 sequence 332 is already on disk as file D:ORACLEORADATATESTARCHIVETESTT001S00332.ARC

RMAN-06050: archivelog thread 1 sequence 333 is already on disk as file D:ORACLEORADATATESTARCHIVETESTT001S00333.ARC

RMAN-06050: archivelog thread 1 sequence 334 is already on disk as file D:ORACLEORADATATESTARCHIVETESTT001S00334.ARC

RMAN-03023: executing command: recover(4)

RMAN-08515: archivelog filename=D:ORACLEORADATATESTARCHIVETESTT001S00332.ARC thread=1 sequence=332

RMAN-08055: media recovery complete

RMAN-03022: compiling command: sql

RMAN-06162: sql statement: alter database datafile 3 online

RMAN-03023: executing command: sql

RMAN-03022: compiling command: release

RMAN-03023: executing command: release

RMAN-08031: released channel: c1

RMAN>


8、檢查資料是否完整

SQL> alter database open;

Database altered.


SQL> select * from test;

A

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

1

2

說明:

1、RMAN也可以實現單個表空間或資料檔案的恢復,恢復過程可以在mount下或open方式下,如果在open方式下恢復,可以減少down機時間

2、如果損壞的是一個資料檔案,建議offline並在open方式下恢復

3、這裡可以看到,RMAN進行資料檔案與表空間恢復的時候,程式碼都比較簡單,而且能保證備份與恢復的可靠性,所以建議採用RMAN的備份與恢復

4.3丟失多個資料檔案,實現整個資料庫的恢復
4.3.1 OS備份方案
OS備份歸檔模式下損壞(丟失)多個資料檔案,進行整個資料庫的恢復

1、連線資料庫,建立測試表並插入記錄

SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003

(c) Copyright 1999 Oracle Corporation. All rights reserved.

SQL> connect internal/password as sysdba;

Connected.


SQL> create table test(a int);

Table created

SQL> insert into test values(1);

1 row inserted

SQL> commit;

Commit complete


2、備份資料庫,備份除臨時資料檔案後的所資料檔案

SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql


3、繼續在測試表中插入記錄

SQL> insert into test values(2);

1 row inserted

SQL> commit;

Commit complete

SQL> select * from test;

A

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

1

2

SQL> alter system switch logfile;

System altered.

SQL> alter system switch logfile;

System altered.


4、關閉資料庫,模擬丟失資料檔案

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down


C:>del D:ORACLEORADATATESTSYSTEM01.DBF

C:>del D:ORACLEORADATATESTINDX01.DBF

C:>del D:ORACLEORADATATESTTOOLS01.DBF

C:>del D:ORACLEORADATATESTRBS01.DBF

模擬媒體毀壞(這裡刪除多個資料檔案)


5、啟動資料庫,檢查錯誤

SQL> STARTUP

ORACLE instance started.

Total System Global Area 102020364 bytes

Fixed Size 70924 bytes

Variable Size 85487616 bytes

Database Buffers 16384000 bytes

Redo Buffers 77824 bytes

Database mounted.

ORA-01157: cannot identify/lock data file 1 - see DBWR trace file

ORA-01110: data file 1: 'D:ORACLEORADATATESTSYSTEM01.DBF'


詳細資訊可以檢視報警檔案

ORA-1157 signalled during: ALTER DATABASE OPEN...

Thu May 08 09:39:36 2003

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 1 - see DBWR trace file

ORA-01110: data file 1: 'D:ORACLEORADATATESTSYSTEM01.DBF'

ORA-27041: unable to open file

OSD-04002: unable to open file

O/S-Error: (OS 2) 系統找不到指定的檔案。


Thu May 08 09:39:36 2003

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 2 - see DBWR trace file

ORA-01110: data file 2: 'D:ORACLEORADATATESTRBS01.DBF'

ORA-27041: unable to open file

OSD-04002: unable to open file

O/S-Error: (OS 2) 系統找不到指定的檔案。


Thu May 08 09:39:36 2003

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 5 - see DBWR trace file

ORA-01110: data file 5: 'D:ORACLEORADATATESTTOOLS01.DBF'

ORA-27041: unable to open file

OSD-04002: unable to open file

O/S-Error: (OS 2) 系統找不到指定的檔案。


Thu May 08 09:39:36 2003

Errors in file D:OracleadmintestbdumptestDBW0.TRC:

ORA-01157: cannot identify/lock data file 6 - see DBWR trace file

ORA-01110: data file 6: 'D:ORACLEORADATATESTINDX01.DBF'

ORA-27041: unable to open file

OSD-04002: unable to open file

O/S-Error: (OS 2) 系統找不到指定的檔案。


透過查詢v$recover_file可以看到

SQL> select * from v$recover_file;


FILE# ONLINE ERROR CHANGE# TIME

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

1 ONLINE FILE NOT FOUND 0

2 ONLINE FILE NOT FOUND 0

5 ONLINE FILE NOT FOUND 0

6 ONLINE FILE NOT FOUND 0

有四個資料檔案需要恢復


6、複製備份回到原地點(restore),開始恢復資料庫(recover)

restore過程:

C:>copy D:DATABAKSYSTEM01.DBF D:ORACLEORADATATEST

C:>copy D:DATABAKTESTINDX01.DBF D:ORACLEORADATATEST

C:>copy D:DATABAKTESTTOOLS01.DBF D:ORACLEORADATATEST

C:>copy D:DATABAKTESTRBS01.DBF.DBF D:ORACLEORADATATEST


Recover過程:

SQL> recover database;

ORA-00279: change 1073849 generated at 05/08/2003 08:58:35 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00311.ARC

ORA-00280: change 1073849 for thread 1 is in sequence #311


Specify log: {=suggested | filename | AUTO | CANCEL}

auto

ORA-00279: change 1073856 generated at 05/08/2003 09:03:27 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00312.ARC

ORA-00280: change 1073856 for thread 1 is in sequence #312

ORA-00278: log file 'D:ORACLEORADATATESTARCHIVETESTT001S00311.ARC' no

longer needed for this recovery


ORA-00279: change 1073858 generated at 05/08/2003 09:11:43 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00313.ARC

ORA-00280: change 1073858 for thread 1 is in sequence #313

ORA-00278: log file 'D:ORACLEORADATATESTARCHIVETESTT001S00312.ARC' no

longer needed for this recovery


ORA-00279: change 1073870 generated at 05/08/2003 09:11:46 needed for thread 1

ORA-00289: suggestion : D:ORACLEORADATATESTARCHIVETESTT001S00314.ARC

ORA-00280: change 1073870 for thread 1 is in sequence #314

ORA-00278: log file 'D:ORACLEORADATATESTARCHIVETESTT001S00313.ARC' no

longer needed for this recovery


Log applied.

Media recovery complete.


7、開啟資料庫,檢查資料庫的資料(完全恢復)

SQL> alter database open;

Database altered.

SQL> select * from test;

A

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

1

2


說明:

1、只要有備份與歸檔存在,就可以實現資料庫的完全恢復(不丟失資料)

2、適合於丟失大量資料檔案,或包含系統資料檔案在內的資料庫的恢復

3、恢復過程在mount下進行,如果恢復成功,再開啟資料庫,down機時間可能比較長一些。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/143526/viewspace-975109/,如需轉載,請註明出處,否則將追究法律責任。

相關文章