Oracle備份與恢復(fù)案例
Oracle 備份與恢復(fù)案例
By Piner
一. 理解什么是數(shù)據(jù)庫恢復(fù)
??
當我們使用一個數(shù)據(jù)庫時,總希望數(shù)據(jù)庫的內(nèi)容是可靠的、正確的,但由于計算機系統(tǒng)的故障(硬件故障、軟件故障、網(wǎng)絡(luò)故障、進程故障和系統(tǒng)故障)影響數(shù)據(jù)庫系統(tǒng)的操作,影響數(shù)據(jù)庫中數(shù)據(jù)的正確性,甚至破壞數(shù)據(jù)庫,使數(shù)據(jù)庫中全部或部分數(shù)據(jù)丟失。因此當發(fā)生上述故障后,希望能重構(gòu)這個完整的數(shù)據(jù)庫,該處理稱為數(shù)據(jù)庫恢復(fù)?;謴?fù)過程大致可以分為復(fù)原
(Restore)
與恢復(fù)
(Recover)
過程。
數(shù)據(jù)庫恢復(fù)可以分為以下兩類:
1.1
實例故障的一致性恢復(fù)
當實例意外地(如掉電、后臺進程故障等)或預(yù)料地(發(fā)出
SHUTDOUM ABORT
語句)中止時出現(xiàn)實例故障,此時需要實例恢復(fù)。實例恢復(fù)將數(shù)據(jù)庫恢復(fù)到故障之前的事務(wù)一致狀態(tài)。如果在在線后備發(fā)現(xiàn)實例故障,則需介質(zhì)恢復(fù)。在其它情況
Oracle
在下次數(shù)據(jù)庫起動時(對新實例裝配和打開),自動地執(zhí)行實例恢復(fù)。如果需要,從裝配狀態(tài)變?yōu)榇蜷_狀態(tài),自動地激發(fā)實例恢復(fù),由下列處理:
( 1 ) ? 為了解恢復(fù)數(shù)據(jù)文件中沒有記錄的數(shù)據(jù),進行向前滾。該數(shù)據(jù)記錄在在線日志,
包括對回滾段的內(nèi)容恢復(fù)。
( 2 ) ? 回滾未提交的事務(wù),按步 1 重新生成回滾段所指定的操作。
( 3 ) ? 釋放在故障時正在處理事務(wù)所持有的資源。
( 4 ) ? 解決在故障時正經(jīng)歷一階段提交的任何懸而未決的分布事務(wù)。
1.2
介質(zhì)故障或文件錯誤的不一致恢復(fù)
介質(zhì)故障是當一個文件、一個文件的部分或磁盤不能讀或不能寫時出現(xiàn)的故障。文件錯誤一般指意外的錯誤導(dǎo)致文件被刪除或意外事故導(dǎo)致文件的不一致。這種狀態(tài)下的數(shù)據(jù)庫都是不一致的,需要 DBA 手工來進行數(shù)據(jù)庫的恢復(fù),這種恢復(fù)有兩種形式,決定于數(shù)據(jù)庫運行的歸檔方式和備份方式。
( 1 ) ? 完全介質(zhì)恢復(fù)可恢復(fù)全部丟失的修改。一般情況下需要有數(shù)據(jù)庫的備份且數(shù)據(jù)庫運行在歸檔狀態(tài)下并且有可用歸檔日志時才可能。對于不同類型的錯誤,有不同類型的完全恢復(fù)可使用,其決定于毀壞文件和數(shù)據(jù)庫的可用性。
(
2
)
?
不完全介質(zhì)恢復(fù)是在完全介質(zhì)恢復(fù)不可能或不要求時進行的介質(zhì)恢復(fù)。重構(gòu)受損的數(shù)據(jù)庫,使其恢復(fù)介質(zhì)故障前或用戶出錯之前的一個事務(wù)一致性狀態(tài)。不完全介質(zhì)恢復(fù)有不同類型的使用,決定于需要不完全介質(zhì)恢復(fù)的情況,有下列類型:基于撤消、基于時間和基于修改的不完全恢復(fù)。
基于撤消 (CANCEL) 恢復(fù):在某種情況,不完全介質(zhì)恢復(fù)必須被控制, DBA 可撤消在指定點的操作?;诔废幕謴?fù)地在一個或多個日志組(在線的或歸檔的)已被介質(zhì)故障所破壞,不能用于恢復(fù)過程時使用,所以介質(zhì)恢復(fù)必須控制,以致在使用最近的、未損的日志組于數(shù)據(jù)文件后中止恢復(fù)操作。
基于時間 (TIME) 和基于修改 (SCN) 的恢復(fù):如果 DBA 希望恢復(fù)到過去的某個指定點,是一種理想的不完全介質(zhì)恢復(fù),一般發(fā)生在恢復(fù)到某個特定操作之前,恢復(fù)到如意外刪除某個數(shù)據(jù)表之前。
第二章. 數(shù)據(jù)庫恢復(fù)案例測試環(huán)境
2.1
數(shù)據(jù)庫環(huán)境
以下的所有案例都是通過測試經(jīng)過,環(huán)境為:
? OS : Windows 2000 Server
? DB : Oracle 816
? DBNAME
:
TEST
數(shù)據(jù)文件:
SQL> select file#,status,enabled,name from v$datafile;
??? FILE# STATUS? ENABLED??? ? NAME
----------------------------------------------------------------
???????? 1 SYSTEM? READ WRITE D:\Oracle\ORADATA\TEST\SYSTEM01.DBF
???????? 2 ONLINE? READ WRITE D:\Oracle\ORADATA\TEST\RBS01.DBF
???????? 3 ONLINE? READ WRITE D:\Oracle\ORADATA\TEST\USERS01.DBF
???????? 4 ONLINE? READ WRITE D:\Oracle\ORADATA\TEST\TEMP01.DBF
???????? 5 ONLINE? READ WRITE D:\Oracle\ORADATA\TEST\TOOLS01.DBF
???????? 6 ONLINE? READ WRITE D:\Oracle\ORADATA\TEST\INDX01.DBF
控制文件:
SQL> select * from v$controlfile;
STATUS? NAME
---------------------------------------------------------------------
???? ?? D:\Oracle\ORADATA\TEST\CONTROL01.CTL
???? ?? D:\Oracle\ORADATA\TEST\CONTROL02.CTL
???? ?? D:\Oracle\ORADATA\TEST\CONTROL03.CTL
聯(lián)機日志:
SQL> select * from v$logfile;
??? GROUP# STATUS? ??? MEMBER
---------------------------------------------------------------------
????? 1 ??? STALE?? ?? D:\Oracle\ORADATA\TEST\REDO01.LOG
????? 2????????? ???? D:\Oracle\ORADATA\TEST\REDO02.LOG
????? 3 ??? STALE?? ?? D:\Oracle\ORADATA\TEST\REDO03.LOG
2.2 數(shù)據(jù)庫備份腳本
冷備份腳本 :
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:\Oracle\oradata\test\*.dbf d:\database/H/R;
--Copy Control file
!xcopy d:\Oracle\oradata\test\*.ctl d:\database/H/R;
--Copy Log file
!xcopy d:\Oracle\oradata\test\*.log d:\database/H/R;
--startup database
startup;
說明:
1 、以上腳本在數(shù)據(jù)庫關(guān)閉狀態(tài)下備份數(shù)據(jù)庫所有的數(shù)據(jù)文件,聯(lián)機日志,控制文件(在一個目
錄下),如果成功備份,所有文件是一致的 ;
2 、沒有備份參數(shù)文件,參數(shù)文件可以另外備份,沒有必要每次都備份,只需要在改變設(shè)置后備份一次 ;
3 、如果以上命令沒有成功依次執(zhí)行,那么備份將是無效的,如連接數(shù)據(jù)庫不成功,那么肯定關(guān)閉數(shù)據(jù)庫也不成功,那么備份則無效 ;
4 、冷備份建議下人工干預(yù)下執(zhí)行。
數(shù)據(jù)庫 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:\Oracle\oradata\test\system01.dbf d:\databak/H/R;
alter tablespace system end backup;
alter tablespace rbs begin backup;
!xcopy d:\Oracle\oradata\test\rbs01.dbf d:\databak/H/R;
alter tablespace rbs end backup;
alter tablespace users begin backup;
!xcopy d:\Oracle\oradata\test\users01.dbf d:\databak/H/R;
alter tablespace users end backup;
alter tablespace tools begin backup;
!xcopy d:\Oracle\oradata\test\tools01.dbf d:\databak/H/R;
alter tablespace tools end backup;
alter tablespace indx begin backup;
!xcopy d:\Oracle\oradata\test\indx01.dbf d:\databak/H/R;
alter tablespace indx end backup;
--end
--bak control file
--binary
alter database backup controlfile to 'd:\databak\controlbinbak.000';
--ascii
alter database backup controlfile to trace;
alter system archive log current;
說明:
1 、熱備份必須在數(shù)據(jù)庫歸檔方式下才可以運行;
2 、以上腳本可以在數(shù)據(jù)庫運行狀態(tài)下備份數(shù)據(jù)庫所有的數(shù)據(jù)文件(除了臨時數(shù)據(jù)文件),沒有必要備份聯(lián)機日志;
3 、歸檔日志至少需要一次完整備份之后的所有日志;
4 、如果以上命令沒有成功依次執(zhí)行,那么備份也是無效的,如連接數(shù)據(jù)庫不成功,那么備份則無效。
RMAN 備份只講敘有恢復(fù)目錄的情況,如果沒有恢復(fù)目錄,情形大致相似。以下是 RMAN 的熱備份全備份的腳本:
#?? script:bakup.rcv
#?? creater:chenjiping
#?? date:5.8.2003
#?? desc:backup all database datafile in archive with rman
# connect database
connect rcvcat rman/rman@back;
connect target internal/virpure;
# start backup database
run{
allocate channel c1 type disk;
backup full tag 'dbfull' format 'd:\backup\full%u_%s_%p' database
include current controlfile;
sql 'alter system archive log current';
release channel c1;
}
# end
說明:
1 、 ? 數(shù)據(jù)庫必須運行在歸檔模式下 ;
2 、 ?RMAN 將自動備份數(shù)據(jù)文件,運行可靠 ;
3 、 ? 歸檔日志另外備份處理,但至少需要保存一次備份來的日志 ;
4 、 ? 沒有必要用 RMAN 做冷備份,效果不好。
以上舉例說明了數(shù)據(jù)庫的恢復(fù)案例的測試環(huán)境與部分備份測試腳本,其它的備份腳本可以根據(jù)以上腳本演變而來或在案例中加以說明。
數(shù)據(jù)庫的自動實例將不加以說明,這里只舉例說明媒體錯誤或人為錯誤造成的恢復(fù)可能。
以上包括以下案例都是在 WINDOWS+Oracle816 上測試驗證的,在不同的操作系統(tǒng)與不同的數(shù)據(jù)庫版本中略有差別。
第三章. 了解與恢復(fù)相關(guān)的信息
1 、 ? 理解報警日志文件
報警日志文件一般記載了數(shù)據(jù)庫的啟動 / 關(guān)閉信息,歸檔信息,備份信息,恢復(fù)信息,常見錯誤信息,部分數(shù)據(jù)庫修改記錄等。一般令名規(guī)則為 <SID>Alrt.log 或 Alrt<SID>.log ,如我的測試數(shù)據(jù)庫的報警日志文件的名稱為 testalrt.log 。
報警日志文件的路徑是根據(jù)初始化參數(shù)
background_dump_dest
來決定的,如在我的機器上,該參數(shù)值為
D:\Oracle\admin\test\bdump
,那么,你就可以在該路徑下找到該文件。
2 、 ? 后臺進程跟蹤文件
后臺進程跟蹤文件的路徑與報警日志文件的路徑一致,在某些情況下,你可以通過后臺跟蹤文件的信息了解更多的需要恢復(fù)的信息。如在數(shù)據(jù)庫需要恢復(fù)的時候,報警日志文件中常有這樣的語句:
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
通過提示的 DBWR 跟蹤文件,可以查詢到更詳細的信息。
3
、
v$recover_file
與
v$recovery_log
這是兩個動態(tài)性能視圖,可以在
mount
下查看,通過這兩個視圖,你可以了解詳細的需要恢復(fù)的數(shù)據(jù)文件與需要使用到的歸檔日志。
第四章. 數(shù)據(jù)庫恢復(fù)案例
4.1
非歸檔模式下的備份與恢復(fù)
備份方案:采用 OS 冷備份
1.? 連接數(shù)據(jù)庫并創(chuàng)建測試表
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.? 備份數(shù)據(jù)庫
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.? 關(guān)閉數(shù)據(jù)庫
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down.
5.? 毀壞一個或多個數(shù)據(jù)文件,如刪除 user01.dbf
C:\>del D:\Oracle\ORADATA\TEST\USERS01.DBF
模擬媒體毀壞。
6.? 重新啟動數(shù)據(jù)庫,會發(fā)現(xiàn)如下錯誤
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:\Oracle\ORADATA\TEST\USERS01.DBF'
在報警文件中,會有更詳細的信息
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:\Oracle\ORADATA\TEST\USERS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
7.? 拷貝備份復(fù)原到原來位置 (restore 過程 )
C:\>xcopy d:\database\*.* d:\Oracle\oradata\test/H/R/S
8.? 打開數(shù)據(jù)庫,檢查數(shù)據(jù)
SQL> alter database open;
Database altered.
SQL> select * from test;
???????????????????????? A
---------------------------------------
???????????????????????? 1
這里可以發(fā)現(xiàn),數(shù)據(jù)庫恢復(fù)成功,但在備份之后與崩潰之前的數(shù)據(jù)丟失了。
說明:
1
、非歸檔模式下的恢復(fù)方案可選性很小,一般情況下只能有一種恢復(fù)方式,就是數(shù)據(jù)庫的冷備
份的完全恢復(fù),僅僅需要拷貝原來的備份就可以
(restore)
,不需要
recover
;
2 、這種情況下的恢復(fù),可以完全恢復(fù)到備份的點上,但是可能是丟失數(shù)據(jù)的,在備份之后與崩潰之前的數(shù)據(jù)將全部丟失;
3 、不管毀壞了多少數(shù)據(jù)文件或是聯(lián)機日志或是控制文件,都可以通過這個辦法恢復(fù),因為這個恢復(fù)過程是 Restore 所有的冷備份文件,而這個備份點上的所有文件是一致的,與最新的數(shù)據(jù)庫沒有關(guān)系,就好比把數(shù)據(jù)庫又放到了一個以前的 " 點 " 上;
4 、對于非歸檔模式下,最好的辦法就是采用 OS 的冷備份,建議不要用 RMAN 來作冷備份,效果不好,因為 RMAN 不備份聯(lián)機日志, restore 不能根本解決問題;
5
、如果沒有備份聯(lián)機日志,如
RMAN
的備份,就需要利用不完全恢復(fù)
(until cancel)
的方法來重新創(chuàng)建聯(lián)機日志文件。
4.2
歸檔模式下丟失或損壞一個數(shù)據(jù)文件
4.2.1 OS 備份方案
在歸檔方式下?lián)p壞或丟失一個數(shù)據(jù)文件,如果存在相應(yīng)的備份與該備份以來的歸檔日志,恢復(fù)還是比較簡單的,可以作到盡量少的
Down
機時間,并能作到數(shù)據(jù)庫的完全恢復(fù)。
1 、 ? 連接數(shù)據(jù)庫,創(chuàng)建測試表并插入記錄
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 、 ? 備份數(shù)據(jù)庫
SQL> @hotbak.sql 或在 DOS 下 svrmgrl @hotbak.sql
3 、 ? 繼續(xù)在測試表中插入記錄
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 、 ? 關(guān)閉數(shù)據(jù)庫,模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
C:\>del D:\Oracle\ORADATA\TEST\USERS01.DBF
模擬媒體毀壞。
5 、 ? 啟動數(shù)據(jù)庫錯誤,脫機該數(shù)據(jù)文件:
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
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:\Oracle\ORADA
posted on 2006-05-10 00:14 Shooper.Java 閱讀(151) 評論(0) 編輯 收藏 所屬分類: Oracle