oracle數(shù)據(jù)庫(kù)的還原和備份
?? 當(dāng)我們使用一個(gè)數(shù)據(jù)庫(kù)時(shí),總希望數(shù)據(jù)庫(kù)的內(nèi)容是可靠的、正確的,但由于
?
數(shù)據(jù)庫(kù)恢復(fù)可以分為以下兩類(lèi):
?
1.1 實(shí)例故障的一致性恢復(fù)
?
當(dāng)實(shí)例意外地(如掉電、后臺(tái)進(jìn)程故障等)或預(yù)料地(發(fā)出 SHUTDOUM ABORT 語(yǔ)句)中止時(shí)出現(xiàn)實(shí)例故障,此時(shí)需要實(shí)例恢復(fù)。實(shí)例恢復(fù)將數(shù)據(jù)庫(kù)恢復(fù)到故障之前的事務(wù)一致?tīng)顟B(tài)。如果在在線后備發(fā)現(xiàn)實(shí)例故障,則需介質(zhì)恢復(fù)。在其它情況 Oracle 在下次數(shù)據(jù)庫(kù)起動(dòng)時(shí)(對(duì)新實(shí)例裝配和打開(kāi)),自動(dòng)地執(zhí)行實(shí)例恢復(fù)。如果需要,從裝配狀態(tài)變?yōu)榇蜷_(kāi)狀態(tài),自動(dòng)地激發(fā)實(shí)例恢復(fù),由下列處理:
?
( 1 ) 為了解恢復(fù)數(shù)據(jù)文件中沒(méi)有記錄的數(shù)據(jù),進(jìn)行向前滾。該數(shù)據(jù)記錄在在線日志,
包括對(duì)回滾段的內(nèi)容恢復(fù)。
( 2 ) 回滾未提交的事務(wù),按步 1 重新生成回滾段所指定的操作。
( 3 ) 釋放在故障時(shí)正在處理事務(wù)所持有的資源。
( 4 ) 解決在故障時(shí)正經(jīng)歷一階段提交的任何懸而未決的分布事務(wù)。
1.2
介質(zhì)故障或文件錯(cuò)誤的不一致恢復(fù)
?
介質(zhì)故障是當(dāng)一個(gè)文件、一個(gè)文件的部分或磁盤(pán)不能讀或不能寫(xiě)時(shí)出現(xiàn)的故障。文件錯(cuò)誤一般指意外的錯(cuò)誤導(dǎo)致文件被刪除或意外事故導(dǎo)致文件的不一致。這種狀態(tài)下的數(shù)據(jù)庫(kù)都是不一致的,需要 DBA 手工來(lái)進(jìn)行數(shù)據(jù)庫(kù)的恢復(fù),這種恢復(fù)有兩種形式,決定于數(shù)據(jù)庫(kù)運(yùn)行的歸檔方式和備份方式。
?
( 1 ) 完全介質(zhì)恢復(fù)可恢復(fù)全部丟失的修改。一般情況下需要有數(shù)據(jù)庫(kù)的備份且數(shù)據(jù)庫(kù)運(yùn)行在歸檔狀態(tài)下并且有可用歸檔日志時(shí)才可能。對(duì)于不同類(lèi)型的錯(cuò)誤,有不同類(lèi)型的完全恢復(fù)可使用,其決定于毀壞文件和數(shù)據(jù)庫(kù)的可用性。
?
(
2
)
不完全介質(zhì)恢復(fù)是在完全介質(zhì)恢復(fù)不可能或不要求時(shí)進(jìn)行的介質(zhì)恢復(fù)。重構(gòu)受損的數(shù)據(jù)庫(kù),使其恢復(fù)介質(zhì)故障前或用戶(hù)出錯(cuò)之前的一個(gè)事務(wù)
?
基于撤消 (CANCEL) 恢復(fù):在某種情況,不完全介質(zhì)恢復(fù)必須被控制, DBA 可撤消在指定點(diǎn)的操作。基于撤消的恢復(fù)地在一個(gè)或多個(gè)日志組(在線的或歸檔的)已被介質(zhì)故障所破壞,不能用于恢復(fù)過(guò)程時(shí)使用,所以介質(zhì)恢復(fù)必須控制,以致在使用最近的、未損的日志組于數(shù)據(jù)文件后中止恢復(fù)操作。
?
基于時(shí)間
(TIME)
和基于修改
(SCN)
的恢復(fù):如果
DBA
希望恢復(fù)到過(guò)去的某個(gè)指定點(diǎn),是一種理想的不完全
?
第二章 . 數(shù)據(jù)庫(kù)恢復(fù)案例測(cè)試環(huán)境
2.1
數(shù)據(jù)庫(kù)環(huán)境
以下的所有案例都是通過(guò)測(cè)試經(jīng)過(guò),環(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)機(jī)日志:
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ù)庫(kù)備份腳本
冷備份腳本
:
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;
?
說(shuō)明:
1 、以上腳本在數(shù)據(jù)庫(kù)關(guān)閉狀態(tài)下備份數(shù)據(jù)庫(kù)所有的數(shù)據(jù)文件,聯(lián)機(jī)日志,控制文件(在一個(gè)目
錄下),如果成功備份,所有文件是一致的 ;
2 、沒(méi)有備份參數(shù)文件,參數(shù)文件可以另外備份,沒(méi)有必要每次都備份,只需要在改變?cè)O(shè)置后備份一次 ;
3 、如果以上命令沒(méi)有成功依次執(zhí)行,那么備份將是無(wú)效的,如連接數(shù)據(jù)庫(kù)不成功,那么肯定關(guān)閉數(shù)據(jù)庫(kù)也不成功,那么備份則無(wú)效 ;
4 、冷備份建議下人工干預(yù)下執(zhí)行。
?
數(shù)據(jù)庫(kù) 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;
?
說(shuō)明:
1 、熱備份必須在數(shù)據(jù)庫(kù)歸檔方式下才可以運(yùn)行;
2 、以上腳本可以在數(shù)據(jù)庫(kù)運(yùn)行狀態(tài)下備份數(shù)據(jù)庫(kù)所有的數(shù)據(jù)文件(除了臨時(shí)數(shù)據(jù)文件),沒(méi)有必要備份聯(lián)機(jī)日志;
3 、歸檔日志至少需要一次完整備份之后的所有日志;
4 、如果以上命令沒(méi)有成功依次執(zhí)行,那么備份也是無(wú)效的,如連接數(shù)據(jù)庫(kù)不成功,那么備份則無(wú)效。
?
RMAN 備份只講敘有恢復(fù)目錄的情況,如果沒(méi)有恢復(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
?
說(shuō)明:
1 、 數(shù)據(jù)庫(kù)必須運(yùn)行在歸檔模式下 ;
2 、 RMAN 將自動(dòng)備份數(shù)據(jù)文件,運(yùn)行可靠 ;
3 、 歸檔日志另外備份處理,但至少需要保存一次備份來(lái)的日志 ;
4 、 沒(méi)有必要用 RMAN 做冷備份,效果不好。
?
以上舉例說(shuō)明了數(shù)據(jù)庫(kù)的恢復(fù)案例的測(cè)試環(huán)境與部分備份測(cè)試腳本,其它的備份腳本可以根據(jù)以上腳本演變而來(lái)或在案例中加以說(shuō)明。
數(shù)據(jù)庫(kù)的自動(dòng)實(shí)例將不加以說(shuō)明,這里只舉例說(shuō)明媒體錯(cuò)誤或人為錯(cuò)誤造成的恢復(fù)可能。
以上包括以下案例都是在 WINDOWS+Oracle816 上測(cè)試驗(yàn)證的,在不同的操作系統(tǒng)與不同的數(shù)據(jù)庫(kù)版本中略有差別。
?
第三章 . 了解與恢復(fù)相關(guān)的信息
?
1 、 理解報(bào)警日志文件
報(bào)警日志文件一般記載了數(shù)據(jù)庫(kù)的啟動(dòng) / 關(guān)閉信息,歸檔信息,備份信息,恢復(fù)信息,常見(jiàn)錯(cuò)誤信息,部分?jǐn)?shù)據(jù)庫(kù)修改記錄等。一般令名規(guī)則為 <SID>Alrt.log 或 Alrt<SID>.log ,如我的測(cè)試數(shù)據(jù)庫(kù)的報(bào)警日志文件的名稱(chēng)為 testalrt.log 。
?
報(bào)警日志文件的路徑是根據(jù)初始化參數(shù) background_dump_dest 來(lái)決定的,如在我的機(jī)器上,該參數(shù)值為 D:\Oracle\admin\test\bdump ,那么,你就可以在該路徑下找到該文件。
?
2 、 后臺(tái)進(jìn)程跟蹤文件
?
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
通過(guò)提示的 DBWR 跟蹤文件,可以查詢(xún)到更詳細(xì)的信息。
3
、
v$recover_file
與
v$recovery_log
這是兩個(gè)動(dòng)態(tài)性能視圖,可以在 mount 下查看,通過(guò)這兩個(gè)視圖,你可以了解詳細(xì)的需要恢復(fù)的數(shù)據(jù)文件與需要使用到的歸檔日志。
?
第四章 . 數(shù)據(jù)庫(kù)恢復(fù)案例
4.1
非歸檔模式下的備份與恢復(fù)
?
備份方案:采用 OS 冷備份
?
1.? 連接數(shù)據(jù)庫(kù)并創(chuàng)建測(cè)試表
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ù)庫(kù)
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ù)庫(kù)
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down.
?
5.? 毀壞一個(gè)或多個(gè)數(shù)據(jù)文件,如刪除 user01.dbf
C:\>del D:\Oracle\ORADATA\TEST\USERS01.DBF
模擬媒體毀壞。
?
6.? 重新啟動(dòng)數(shù)據(jù)庫(kù),會(huì)發(fā)現(xiàn)如下錯(cuò)誤
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'
?
在報(bào)警文件中,會(huì)有更詳細(xì)的信息
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ù)原到原來(lái)位置 (restore 過(guò)程 )
C:\>xcopy d:\database\*.* d:\Oracle\oradata\test/H/R/S
?
8.? 打開(kāi)數(shù)據(jù)庫(kù),檢查數(shù)據(jù)
SQL> alter database open;
Database altered.
SQL> select * from test;
???????????????????????? A
---------------------------------------
???????????????????????? 1
?
這里可以發(fā)現(xiàn),數(shù)據(jù)庫(kù)恢復(fù)成功,但在備份之后與崩潰之前的數(shù)據(jù)丟失了。
?
說(shuō)明:
1
、非歸檔模式下的恢復(fù)方案可選性很小,一般情況下只能有一種恢復(fù)方式,就是數(shù)據(jù)庫(kù)的冷備
份的完全恢復(fù),僅僅需要拷貝原來(lái)的備份就可以
(restore)
,不需要
recover
;
2 、這種情況下的恢復(fù),可以完全恢復(fù)到備份的點(diǎn)上,但是可能是丟失數(shù)據(jù)的,在備份之后與崩潰之前的數(shù)據(jù)將全部丟失;
3 、不管毀壞了多少數(shù)據(jù)文件或是聯(lián)機(jī)日志或是控制文件,都可以通過(guò)這個(gè)辦法恢復(fù),因?yàn)檫@個(gè)恢復(fù)過(guò)程是 Restore 所有的冷備份文件,而這個(gè)備份點(diǎn)上的所有文件是一致的,與最新的數(shù)據(jù)庫(kù)沒(méi)有關(guān)系,就好比把數(shù)據(jù)庫(kù)又放到了一個(gè)以前的 " 點(diǎn) " 上;
4 、對(duì)于非歸檔模式下,最好的辦法就是采用 OS 的冷備份,建議不要用 RMAN 來(lái)作冷備份,效果不好,因?yàn)?/span> RMAN 不備份聯(lián)機(jī)日志, restore 不能根本解決問(wèn)題;
5 、如果沒(méi)有備份聯(lián)機(jī)日志,如 RMAN 的備份,就需要利用不完全恢復(fù) (until cancel) 的方法來(lái)重新創(chuàng)建聯(lián)機(jī)日志文件。
?
4.2 歸檔模式下丟失或損壞一個(gè)數(shù)據(jù)文件
?
4.2.1 OS 備份方案
在歸檔方式下?lián)p壞或丟失一個(gè)數(shù)據(jù)文件,如果存在相應(yīng)的備份與該備份以來(lái)的歸檔日志,恢復(fù)還是比較簡(jiǎn)單的,可以作到盡量少的 Down 機(jī)時(shí)間,并能作到數(shù)據(jù)庫(kù)的完全恢復(fù)。
?
1 、 連接數(shù)據(jù)庫(kù),創(chuàng)建測(cè)試表并插入記錄
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ù)庫(kù)
SQL> @hotbak.sql 或在 DOS 下 svrmgrl @hotbak.sql
?
3 、 繼續(xù)在測(cè)試表中插入記錄
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ù)庫(kù),模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
C:\>del D:\Oracle\ORADATA\TEST\USERS01.DBF
模擬媒體毀壞。
?
5 、 啟動(dòng)數(shù)據(jù)庫(kù)錯(cuò)誤,脫機(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
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'
?
還可以查看報(bào)警文件(見(jiàn)上一個(gè)恢復(fù)案例)或動(dòng)態(tài)視圖 v$recover_file
如 SQL> select * from v$recover_file;
?
???? FILE# ONLINE? ERROR??????????????????? CHANGE#???? TIME
---------- ------- ------------------ ---------- -----------
???????? 3?? ONLINE??????????????????????? 1013500??? 2003-05-07
?
脫機(jī)數(shù)據(jù)文件
SQL> alter database datafile 3 offline drop;
Database altered.
?
6 、 打開(kāi)數(shù)據(jù)庫(kù),拷貝備份回來(lái) (restore) ,恢復(fù) (recover) 該數(shù)據(jù)文件,并聯(lián)機(jī) :
?
SQL> alter database open;
Database altered.
拷貝備份從備份處
copy d:\databak\ users01.dbf d:\Oracle\oradata\test;
恢復(fù)該數(shù)據(jù)文件
SQL> recover datafile 3;
?
ORA-00279: change 1053698 generated at 05/07/2003 17:51:26 needed for
thread 1
ORA-00289: suggestion :
D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00304.ARC
ORA-00280: change 1053698 for thread 1 is in sequence #304
?
Specify log: {<RET>=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:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00305.ARC
ORA-00280: change 1053701 for thread 1 is in sequence #305
ORA-00278: log file 'D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00304.ARC' no longer needed for this recovery Log applied.
Media recovery complete.
?
恢復(fù)成功,聯(lián)機(jī)該數(shù)據(jù)文件
SQL> alter database datafile 3 online;
Database altered.
?
7 、 檢查數(shù)據(jù)庫(kù)的數(shù)據(jù)(完全恢復(fù))
SQL> select * from test;
???????????????????????? A
--------------------------------
???????????????????????? 1
???????????????????????? 2
說(shuō)明:
1 、采用熱備份,需要運(yùn)行在歸檔模式下,可以實(shí)現(xiàn)數(shù)據(jù)庫(kù)的完全恢復(fù),也就是說(shuō),從備份后到數(shù)據(jù)庫(kù)崩潰時(shí)的數(shù)據(jù)都不會(huì)丟失 ;
2
、可以采用全
3 、如果在恢復(fù)過(guò)程中,發(fā)現(xiàn)損壞的是多個(gè)數(shù)據(jù)文件,即可以采用一個(gè)一個(gè)數(shù)據(jù)文件的恢復(fù)方法(第 5 步中需要對(duì)數(shù)據(jù)文件一一脫機(jī),第 6 步中需要對(duì)數(shù)據(jù)文件分別恢復(fù)),也可以采用整個(gè)數(shù)據(jù)庫(kù)的恢復(fù)方法;
4
、如果是系統(tǒng)表空間的損壞,不能采用此方法。
4.2.2 RMAN 備份方案
?
RMAN 也可以進(jìn)行聯(lián)機(jī)備份,而且備份與恢復(fù)方法將比 OS 備份更簡(jiǎn)單可靠。
1 、連接數(shù)據(jù)庫(kù),創(chuàng)建測(cè)試表并插入記錄
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ù)庫(kù)表空間 users
C:\>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
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:\backup\tsuser_%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:\Oracle\ORADATA\TEST\USER01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:\BACKUP\TSUSER_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 、 繼續(xù)在測(cè)試表中插入記錄
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 、 關(guān)閉數(shù)據(jù)庫(kù),模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
?
C:\>del D:\Oracle\ORADATA\TEST\USER01.DBF
?
5 、 啟動(dòng)數(shù)據(jù)庫(kù),檢查錯(cuò)誤
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\USER01.DBF'
?
6 、 先打開(kāi)數(shù)據(jù)庫(kù)
SQL> alter database datafile 3 offline drop;
Database altered.
SQL> alter database open;
Database altered.
?
7 、 恢復(fù)該表空間
恢復(fù)腳本可以是恢復(fù)單個(gè)數(shù)據(jù)文件
run{
allocate channel c1 type disk;
restore datafile 3;
recover datafile 3;
sql 'alter database datafile 3 online';
release channel c1;
}
也可以是 , 恢復(fù)表空間
run{
allocate channel c1 type disk;
restore tablespace users;
recover tablespace users;
sql 'alter database datafile 3 online';
release channel c1;
}
過(guò)程如下:
C:\>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
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> }
?
// 輸出內(nèi)容冗長(zhǎng),省略 -- 編者
RMAN>
?
8 、 檢查數(shù)據(jù)是否完整
SQL> alter database open;
Database altered.
?
SQL> select * from test;
???????????????????????? A
---------------------------------------
???????????????????????? 1
???????????????????????? 2
說(shuō)明:
1 、 RMAN 也可以實(shí)現(xiàn)單個(gè)表空間或數(shù)據(jù)文件的恢復(fù),恢復(fù)過(guò)程可以在 mount 下或 open 方式下,如果在 open 方式下恢復(fù),可以減少 down 機(jī)時(shí)間 ;
2 、如果損壞的是一個(gè)數(shù)據(jù)文件,建議 offline 并在 open 方式下恢復(fù) ;
3 、這里可以看到, RMAN 進(jìn)行數(shù)據(jù)文件與表空間恢復(fù)的時(shí)候,代碼都比較簡(jiǎn)單,而且能保證備份與恢復(fù)的可靠性,所以建議采用 RMAN 的備份與恢復(fù) .
?
4.3 丟失多個(gè)數(shù)據(jù)文件,實(shí)現(xiàn)整個(gè)數(shù)據(jù)庫(kù)的恢復(fù) .
?
4.3.1 OS 備份方案
OS 備份歸檔模式下?lián)p壞(丟失)多個(gè)數(shù)據(jù)文件,進(jìn)行整個(gè)數(shù)據(jù)庫(kù)的恢復(fù)
1 、 連接數(shù)據(jù)庫(kù),創(chuàng)建測(cè)試表并插入記錄
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ù)庫(kù),備份除臨時(shí)數(shù)據(jù)文件后的所數(shù)據(jù)文件
SQL> @hotbak.sql 或在 DOS 下 svrmgrl @hotbak.sql
?
3 、 繼續(xù)在測(cè)試表中插入記錄
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ù)庫(kù),模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
?
C:\>del D:\Oracle\ORADATA\TEST\SYSTEM01.DBF
C:\>del D:\Oracle\ORADATA\TEST\INDX01.DBF
C:\>del D:\Oracle\ORADATA\TEST\TOOLS01.DBF
C:\>del D:\Oracle\ORADATA\TEST\RBS01.DBF
模擬媒體毀壞(這里刪除多個(gè)數(shù)據(jù)文件)
?
5 、 啟動(dòng)數(shù)據(jù)庫(kù),檢查錯(cuò)誤
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:\Oracle\ORADATA\TEST\SYSTEM01.DBF'
?
詳細(xì)信息可以查看報(bào)警文件
ORA-1157 signalled during: ALTER DATABASE OPEN...
Thu May 08 09:39:36 2003
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: 'D:\Oracle\ORADATA\TEST\SYSTEM01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
?
Thu May 08 09:39:36 2003
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file
ORA-01110: data file 2: 'D:\Oracle\ORADATA\TEST\RBS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
?
Thu May 08 09:39:36 2003
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: 'D:\Oracle\ORADATA\TEST\TOOLS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
?
Thu May 08 09:39:36 2003
Errors in file D:\Oracle\admin\test\bdump\testDBW0.TRC:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\Oracle\ORADATA\TEST\INDX01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
?
通過(guò)查詢(xún) 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
有四個(gè)數(shù)據(jù)文件需要恢復(fù)
?
6 、 拷貝備份回到原地點(diǎn) (restore) ,開(kāi)始恢復(fù)數(shù)據(jù)庫(kù) (recover)
restore 過(guò)程 :
C:\>copy D:\DATABAK\SYSTEM01.DBF D:\Oracle\ORADATA\TEST\
C:\>copy D:\DATABAK\TEST\INDX01.DBF D:\Oracle\ORADATA\TEST\
C:\>copy D:\DATABAK\TEST\TOOLS01.DBF D:\Oracle\ORADATA\TEST\
C:\>copy D:\DATABAK\TEST\RBS01.DBF.DBF D:\Oracle\ORADATA\TEST\
?
Recover 過(guò)程 :
SQL> recover database;
?
ORA-00279: change 1073849 generated at 05/08/2003 08:58:35 needed for thread 1
ORA-00289: suggestion : D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00311.ARC
ORA-00280: change 1073849 for thread 1 is in sequence #311
?
Specify log: {<RET>=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:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00312.ARC
ORA-00280: change 1073856 for thread 1 is in sequence #312
ORA-00278: log file 'D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00311.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:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00313.ARC
ORA-00280: change 1073858 for thread 1 is in sequence #313
ORA-00278: log file 'D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00312.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:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00314.ARC
ORA-00280: change 1073870 for thread 1 is in sequence #314
ORA-00278: log file 'D:\Oracle\ORADATA\TEST\ARCHIVE\TESTT001S00313.ARC' no
longer needed for this recovery
?
Log applied.
Media recovery complete.
?
7 、 打開(kāi)數(shù)據(jù)庫(kù),檢查數(shù)據(jù)庫(kù)的數(shù)據(jù)(完全恢復(fù))
SQL> alter database open;
Database altered.
SQL> select * from test;
???????????????????????? A
---------------------------------------
???????????????????????? 1
???????????????????????? 2
?
說(shuō)明:
1 、只要有備份與歸檔存在,就可以實(shí)現(xiàn)數(shù)據(jù)庫(kù)的完全恢復(fù)(不丟失數(shù)據(jù)) ;
2
、適合于丟失大量數(shù)據(jù)文件,或包含系統(tǒng)
3
、恢復(fù)過(guò)程在
mount
下進(jìn)行,如果恢復(fù)成功,再打開(kāi)數(shù)據(jù)庫(kù),
down
?
4.3.2 RMAN 備份方案
RMAN
1 、連接數(shù)據(jù)庫(kù),創(chuàng)建測(cè)試表并插入記錄
?
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ù)庫(kù)
DOS 下 C:>\ rman cmdfile=bakup.rcv msglog=backup.log;
?
以下是 backup.log 內(nèi)容。
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> #???? script:bakup.rcv
2> #???? creater:chenjiping
3> #???? date:5.8.2003
4> #???? desc:backup all database datafile in archive with rman
5>
6> #connect database
7> connect rcvcat rman/rman@back ;
8> connect target internal/virpure;
9>
10> #start backup database
11> run{
12> allocate channel c1 type disk;
13> backup full tag 'dbfull' format 'd:\backup\full%u_%s_%p' database
14> include current controlfile;
15> sql 'alter system archive log current';
16> release channel c1;
17> }
18> #end
19>
?
RMAN-06008: connected to recovery catalog database
RMAN-06005: connected to target database: TEST (DBID=1788174720)
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=15 devtype=DISK
RMAN-03022: compiling command: backup
RMAN-03023: executing command: backup
RMAN-08008: channel c1: starting full datafile backupset
RMAN-08502: set_count=4 set_stamp=494074368 creation_time=15-MAY-03
RMAN-08010: channel c1: specifying datafile(s) in backupset
RMAN-08522: input datafile fno=00002 name=D:\Oracle\ORADATA\TEST\RBS01.DBF
RMAN-08522: input datafile fno=00001 name=D:\Oracle\ORADATA\TEST\SYSTEM01.DBF
RMAN-08011: including current controlfile in backupset
RMAN-08522: input datafile fno=00005 name=D:\Oracle\ORADATA\TEST\TOOLS01.DBF
RMAN-08522: input datafile fno=00004 name=D:\Oracle\ORADATA\TEST\TEMP01.DBF
RMAN-08522: input datafile fno=00006 name=D:\Oracle\ORADATA\TEST\INDX01.DBF
RMAN-08522: input datafile fno=00003 name=D:\Oracle\ORADATA\TEST\USER01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:\BACKUP\FULL04EN5UG0_4_1 comment=NONE
RMAN-08525: backup set complete, elapsed time: 00:01:16
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter system archive log current
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
Recovery Manager complete.
到這里表示備份成功。
?
3 、 繼續(xù)在測(cè)試表中插入記錄
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ù)庫(kù),模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
C:\>del D:\Oracle\ORADATA\TEST\SYSTEM01.DBF
C:\>del D:\Oracle\ORADATA\TEST\INDX01.DBF
C:\>del D:\Oracle\ORADATA\TEST\TOOLS01.DBF
C:\>del D:\Oracle\ORADATA\TEST\RBS01.DBF
?
5
、啟動(dòng)
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:\Oracle\ORADATA\TEST\SYSTEM01.DBF'
?
查詢(xún) 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
?
可以知道有四
posted on 2007-01-08 16:27 ASONG 閱讀(518) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): database