一. 理解什么是數(shù)據(jù)庫恢復(fù)
當(dāng)我們使用一個數(shù)據(jù)庫時,總希望數(shù)據(jù)庫的內(nèi)容是可靠的、正確的,但由于計算機(jī)系統(tǒng)的故障(硬件故障、軟件故障、網(wǎng)絡(luò)故障、進(jìn)程故障和系統(tǒng)故障)影響數(shù)據(jù)庫系統(tǒng)的操作,影響數(shù)據(jù)庫中數(shù)據(jù)的正確性,甚至破壞數(shù)據(jù)庫, 使數(shù)據(jù)庫中全部或部分?jǐn)?shù)據(jù)丟失。因此當(dāng)發(fā)生上述故障后,希望能重構(gòu)這個完整的數(shù)據(jù)庫,該處理稱為數(shù)據(jù)庫恢復(fù)。恢復(fù)過程大致可以分為復(fù)原(Restore)與恢復(fù)(Recover)過程。
數(shù)據(jù)庫恢復(fù)可以分為以下兩類:
1.1實例故障的一致性恢復(fù)
當(dāng)實例意外地(如掉電、后臺進(jìn)程故障等)或預(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ù),進(jìn)行向前滾。該數(shù)據(jù)記錄在在線日志,
包括對回滾段的內(nèi)容恢復(fù)。
(2) 回滾未提交的事務(wù),按步1重新生成回滾段所指定的操作。
(3) 釋放在故障時正在處理事務(wù)所持有的資源。
(4) 解決在故障時正經(jīng)歷一階段提交的任何懸而未決的分布事務(wù)。
1.2介質(zhì)故障或文件錯誤的不一致恢復(fù)
介質(zhì)故障是當(dāng)一個文件、一個文件的部分或磁盤不能讀或不能寫時出現(xiàn)的故障。文件錯誤一般指意外的錯誤導(dǎo)致文件被刪除或意外事故導(dǎo)致文件的不一致。這種狀態(tài)下的數(shù)據(jù)庫都是不一致的,需要DBA手工來進(jìn)行數(shù)據(jù)庫的恢復(fù),這種恢復(fù)有兩種形式,決定于數(shù)據(jù)庫運(yùn)行的歸檔方式和備份方式。
(1) 完全介質(zhì)恢復(fù)可恢復(fù)全部丟失的修改。一般情況下需要有數(shù)據(jù)庫的備份且數(shù)據(jù)庫運(yùn)行在歸檔狀態(tài)下并且有可用歸檔日志時才可能。對于不同類型的錯誤,有不同類型的完全恢復(fù)可使用,其決定于毀壞文件和數(shù)據(jù)庫的可用性。
(2) 不完全介質(zhì)恢復(fù)是在完全介質(zhì)恢復(fù)不可能或不要求時進(jìn)行的介質(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 YSTEM01.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ù)庫備份腳本
冷備份腳本:
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)機(jī)日志,控制文件(在一個目
錄下),假如成功備份,所有文件是一致的;
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 ystem01.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ù)庫歸檔方式下才可以運(yùn)行;
2、以上腳本可以在數(shù)據(jù)庫運(yùn)行狀態(tài)下備份數(shù)據(jù)庫所有的數(shù)據(jù)文件(除了臨時數(shù)據(jù)文件),沒有必要備份聯(lián)機(jī)日志;
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ù)庫必須運(yùn)行在歸檔模式下;
2、 RMAN將自動備份數(shù)據(jù)文件,運(yùn)行可靠;
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ù)信息,常見錯誤信息,部分?jǐn)?shù)據(jù)庫修改記錄等。一般令名規(guī)則為<SID>Alrt.log或Alrt<SID>.log,如我的測試數(shù)據(jù)庫的報警日志文件的名稱為testalrt.log。
報警日志文件的路徑是根據(jù)初始化參數(shù)background_dump_dest來決定的,如在我的機(jī)器上,該參數(shù)值為 D:/Oracle/admin/test/bdump,那么,你就可以在該路徑下找到該文件。
2、 后臺進(jìn)程跟蹤文件
后臺進(jìn)程跟蹤文件的路徑與報警日志文件的路徑一致,在某些情況下,你可以通過后臺跟蹤文件的信息了解更多的需要恢復(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)機(jī)日志或是控制文件,都可以通過這個辦法恢復(fù),因為這個恢復(fù)過程是Restore所有的冷備份文件,而這個備份點上的所有文件是一致的,與最新的數(shù)據(jù)庫沒有關(guān)系,就好比把數(shù)據(jù)庫又放到了一個以前的"點"上;
4、對于非歸檔模式下,最好的辦法就是采用OS的冷備份,建議不要用RMAN來作冷備份,效果不好,因為RMAN不備份聯(lián)機(jī)日志,restore不能根本解決問題;
5、假如沒有備份聯(lián)機(jī)日志,如RMAN的備份,就需要利用不完全恢復(fù)(until cancel)的方法來重新創(chuàng)建聯(lián)機(jī)日志文件。
4.2歸檔模式下丟失或損壞一個數(shù)據(jù)文件
4.2.1 OS備份方案
在歸檔方式下?lián)p壞或丟失一個數(shù)據(jù)文件,假如存在相應(yīng)的備份與該備份以來的歸檔日志,恢復(fù)還是比較簡單的,可以作到盡量少的Down機(jī)時間,并能作到數(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ù)庫錯誤,脫機(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'
還可以查看報警文件(見上一個恢復(fù)案例)或動態(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、 打開數(shù)據(jù)庫,拷貝備份回來(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ù)庫的數(shù)據(jù)(完全恢復(fù))
SQL> select * from test;
A
--------------------------------
1
2
說明:
1、采用熱備份,需要運(yùn)行在歸檔模式下,可以實現(xiàn)數(shù)據(jù)庫的完全恢復(fù),也就是說,從備份后到數(shù)據(jù)庫崩潰時的數(shù)據(jù)都不會丟失;
2、可以采用全備份數(shù)據(jù)庫的方式備份,對于非凡情況,也可以只備份特定的數(shù)據(jù)文件,如只備份用戶表空間(一般情況下對于某些寫非凡頻繁的數(shù)據(jù)文件,可以單獨加大備份頻率);
3、假如在恢復(fù)過程中,發(fā)現(xiàn)損壞的是多個數(shù)據(jù)文件,即可以采用一個一個數(shù)據(jù)文件的恢復(fù)方法(第5步中需要對數(shù)據(jù)文件一一脫機(jī),第6步中需要對數(shù)據(jù)文件分別恢復(fù)),也可以采用整個數(shù)據(jù)庫的恢復(fù)方法;
4、假如是系統(tǒng)表空間的損壞,不能采用此方法。
4.2.2 RMAN備份方案
RMAN也可以進(jìn)行聯(lián)機(jī)備份,而且備份與恢復(fù)方法將比OS備份更簡單可靠。
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ù)庫表空間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ù)在測試表中插入記錄
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ù)庫,模擬丟失數(shù)據(jù)文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down
C:/>del D:/Oracle/ORADATA/TEST/USER01.DBF
5、 啟動數(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/USER01.DBF'
6、 先打開數(shù)據(jù)庫
SQL> alter database datafile 3 offline drop;
Database altered.
SQL> alter database open;
Database altered.
7、 恢復(fù)該表空間
恢復(fù)腳本可以是恢復(fù)單個數(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;
}
過程如下:
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)容冗長,省略--編者
RMAN>
8、 檢查數(shù)據(jù)是否完整
SQL> alter database open;
Database altered.
SQL> select * from test;
A
---------------------------------------
1
2
說明:
1、RMAN也可以實現(xiàn)單個表空間或數(shù)據(jù)文件的恢復(fù),恢復(fù)過程可以在mount下或open方式下,假如在open方式下恢復(fù),可以減少down機(jī)時間;
2、假如損壞的是一個數(shù)據(jù)文件,建議offline并在open方式下恢復(fù);
3、這里可以看到,RMAN進(jìn)行數(shù)據(jù)文件與表空間恢復(fù)的時候,代碼都比較簡單,而且能保證備份與恢復(fù)的可靠性,所以建議采用RMAN的備份與恢復(fù).
4.3丟失多個數(shù)據(jù)文件,實現(xiàn)整個數(shù)據(jù)庫的恢復(fù).
4.3.1 OS備份方案
OS備份歸檔模式下?lián)p壞(丟失)多個數(shù)據(jù)文件,進(jìn)行整個數(shù)據(jù)庫的恢復(fù)
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ù)庫,備份除臨時數(shù)據(jù)文件后的所數(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 YSTEM01.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
模擬媒體毀壞(這里刪除多個數(shù)據(jù)文件)
5、 啟動數(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 1 - see DBWR trace file
ORA-01110: data file 1: 'D:/Oracle/ORADATA/TEST YSTEM01.DBF'
具體信息可以查看報警文件
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 YSTEM01.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)找不到指定的文件。
通過查詢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
有四個數(shù)據(jù)文件需要恢復(fù)
6、 拷貝備份回到原地點(restore),開始恢復(fù)數(shù)據(jù)庫(recover)
restore過程:
C:/>copy D:/DATABAK YSTEM01.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過程:
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、 打開數(shù)據(jù)庫,檢查數(shù)據(jù)庫的數(shù)據(jù)(完全恢復(fù))
SQL> alter database open;
Database altered.
SQL> select * from test;
A
---------------------------------------
1
2
說明:
1、只要有備份與歸檔存在,就可以實現(xiàn)數(shù)據(jù)庫的完全恢復(fù)(不丟失數(shù)據(jù));
2、適合于丟失大量數(shù)據(jù)文件,或包含系統(tǒng)數(shù)據(jù)文件在內(nèi)的數(shù)據(jù)庫的恢復(fù);
3、恢復(fù)過程在mount下進(jìn)行,假如恢復(fù)成功,再打開數(shù)據(jù)庫,down機(jī)時間可能比較長一些。
4.3.2 RMAN備份方案
RMAN備份歸檔模式下?lián)p壞(丟失)多個數(shù)據(jù)文件,進(jìn)行整個數(shù)據(jù)庫的恢復(fù)
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ù)庫
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 YSTEM01.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ù)在測試表中插入記錄
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 YSTEM01.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、啟動數(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 1 - see DBWR trace file
ORA-01110: data file 1: 'D:/Oracle/ORADATA/TEST YSTEM01.DBF'
查詢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
可以知道有四個數(shù)據(jù)文件需要恢復(fù).
6、利用RMAN進(jìn)行恢復(fù)
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 database;
4> recover database;
5> sql 'alter database open';
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=17 devtype=DISK
RMAN-03022: compiling command: restore
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-03022: compiling command: IRESTORE
RMAN-03023: executing command: IRESTORE
RMAN-08016: channel c1: starting datafile backupset restore
RMAN-08502: set_count=4 set_stamp=494074368 creation_time=15-MAY-03
RMAN-08089: channel c1: specifying datafile(s) to restore from backup set
RMAN-08523: restoring datafile 00001 to D:/Oracle/ORADATA/TEST YSTEM01.DBF
RMAN-08523: restoring datafile 00002 to D:/Oracle/ORADATA/TEST/RBS01.DBF
RMAN-08523: restoring datafile 00003 to D:/Oracle/ORADATA/TEST/USER01.DBF
RMAN-08523: restoring datafile 00004 to D:/Oracle/ORADATA/TEST/TEMP01.DBF
RMAN-08523: restoring datafile 00005 to D:/Oracle/ORADATA/TEST/TOOLS01.DBF
RMAN-08523: restoring datafile 00006 to D:/Oracle/ORADATA/TEST/INDX01.DBF
RMAN-08023: channel c1: restored backup piece 1
RMAN-08511: piece handle=D:/BACKUP/FULL04EN5UG0_4_1 tag=DBFULL 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 327 is already on disk as file D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00327.ARC
RMAN-06050: archivelog thread 1 sequence 328 is already on disk as file D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00328.ARC
RMAN-06050: archivelog thread 1 sequence 329 is already on disk as file D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00329.ARC
RMAN-06050: archivelog thread 1 sequence 330 is already on disk as file D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00330.ARC
RMAN-03023: executing command: recover(4)
RMAN-08515: archivelog filename=D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00327.ARC thread=1 sequence=327
RMAN-08515: archivelog filename=D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00328.ARC thread=1 sequence=328
RMAN-08055: media recovery complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter database open
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
RMAN>
7、 檢查數(shù)據(jù)庫的數(shù)據(jù)(完全恢復(fù))
SQL> select * from test;
A
---------------------------------------
1
2
說明:
1、只要有備份與歸檔存在,RMAN也可以實現(xiàn)數(shù)據(jù)庫的完全恢復(fù)(不丟失數(shù)據(jù));
2、同OS備份數(shù)據(jù)庫恢復(fù),適合于丟失大量數(shù)據(jù)文件,或包含系統(tǒng)數(shù)據(jù)文件在內(nèi)的數(shù)據(jù)庫的恢復(fù);
3、目標(biāo)數(shù)據(jù)庫在mount下進(jìn)行,假如恢復(fù)成功,再打開數(shù)據(jù)庫;
4、RMAN的備份與恢復(fù)命令相對比較簡單并可靠,建議有條件的話,都采用RMAN進(jìn)行數(shù)據(jù)庫的備份。
4.4 不完全恢復(fù)案例
4.4.1 OS備份下的基于時間的恢復(fù)
不完全恢復(fù)可以分為基于時間的恢復(fù),基于改變的恢復(fù)與基于撤消的恢復(fù),這里已基于時間的恢復(fù)為例子來說明不完全恢復(fù)過程。
基于時間的恢復(fù)可以不完全恢復(fù)到現(xiàn)在時間之前的某一個時間,對于某些誤操作,如刪除了一個數(shù)據(jù)表,可以在備用恢復(fù)環(huán)境上恢復(fù)到表的刪除時間之前,然后把該表導(dǎo)出到正式環(huán)境,避免一個人為的錯誤。
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ù)庫,這里最好備份所有的數(shù)據(jù)文件,包括臨時數(shù)據(jù)文件:
SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql
或冷備份也可以
3、 刪除測試表,假定刪除前的時間為T1,在刪除之前,便于測試,繼續(xù)插入數(shù)據(jù)并應(yīng)用到歸
檔。
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;
Statement processed.
SQL> alter system switch logfile;
Statement processed.
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
2003-05-21 14:43:01
SQL> drop table test;
Table dropped.
4、 預(yù)備恢復(fù)到時間點T1,找回刪除的表,先關(guān)閉數(shù)據(jù)庫:
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down.
5、 拷貝剛才備份的所有數(shù)據(jù)文件回來
C:/>copy D:/DATABAK/*.DBF D:/Oracle/ORADATA/TEST/
6、 啟動到mount下
SQL> startup mount;
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.
7、 開始不完全恢復(fù)數(shù)據(jù)庫到T1時間
SQL> recover database until time '2003-05-21:14:43:01';
ORA-00279: change 30944 generated at 05/21/2003 14:40:06 needed for thread 1
ORA-00289: suggestion : D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00191.ARC
ORA-00280: change 30944 for thread 1 is in sequence #191
Specify log: {<RET>=suggested filename AUTO CANCEL}
auto
Log applied.
Media recovery complete.
8、 打開數(shù)據(jù)庫,檢查數(shù)據(jù)
SQL> alter database open resetlogs;
Database altered.
SQL> select * from test;
A
---------------------------------------
1
2
說明:
1、不完全恢復(fù)最好備份所有的數(shù)據(jù),冷備份亦可,因為恢復(fù)過程是從備份點往后恢復(fù)的,假如因為其中一個數(shù)據(jù)文件的時間戳(SCN)大于要恢復(fù)的時間點,那么恢復(fù)都是不可能成功的;
2、不完全恢復(fù)有三種方式,過程都一樣,僅僅是recover命令有所不一樣,這里用基于時間的恢復(fù)作為示例;
3、不完全恢復(fù)之后,都必須用resetlogs的方式打開數(shù)據(jù)庫,建議馬上再做一次全備份,因為resetlogs之后再用以前的備份恢復(fù)是很難了;
4、以上是在刪除之前獲得時間,但是實際應(yīng)用中,很難知道刪除之前的實際時間,但可以采用大致時間即可,或可以采用分析日志文件(logmnr),取得精確的需要恢復(fù)的時間;
5、一般都是在測試機(jī)后備用機(jī)器上采用這種不完全恢復(fù),恢復(fù)之后導(dǎo)出/導(dǎo)入被誤刪的表回生產(chǎn)系統(tǒng).
4.4.2 RMAN備份下的基于改變的恢復(fù)
以上用OS備份說明了一個基于時間的恢復(fù),現(xiàn)在用RMAN說明一個基于改變的恢復(fù)
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ù)庫
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=874705288)
RMAN> run{
2> allocate channel c1 type disk;
3> backup full tag 'dbfull' format 'd:/backup/full%u_%s_%p' database
4> include current controlfile;
5> sql 'alter system archive log current';
6> release channel c1;
7> }
//屏幕輸出內(nèi)容冗長,省略--編輯
RMAN>
3、 刪除測試表,在刪除之前,便于測試,繼續(xù)插入數(shù)據(jù)并應(yīng)用到歸檔,并獲取刪除前的scn號。
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;
Statement processed.
SQL> alter system switch logfile;
Statement processed.
SQL> select max(ktuxescnw * power(2, 32) + ktuxescnb) scn from x$ktuxe;
SCN
----------
31014
SQL> drop table test;
Table dropped.
4、 預(yù)備恢復(fù)到SCN 31014,先關(guān)閉數(shù)據(jù)庫,然后啟動到mount下
SQL> shutdown immediate;
Database closed.
Database dismounted.
Oracle instance shut down.
SQL> startup mount;
5、 開始恢復(fù)到改變點SCN 31014
RMAN> run{
2> allocate channel c1 type disk;
3> restore database;
4> recover database until scn 31014;
5> sql 'ALTER DATABASE OPEN RESETLOGS';
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=10 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=1 set_stamp=494613682 creation_time=21-MAY-03
RMAN-08089: channel c1: specifying datafile(s) to restore from backup set
RMAN-08523: restoring datafile 00001 to D:/Oracle/ORADATA/TEST YSTEM01.DBF
RMAN-08523: restoring datafile 00002 to D:/Oracle/ORADATA/TEST/RBS01.DBF
RMAN-08523: restoring datafile 00003 to D:/Oracle/ORADATA/TEST/USERS01.DBF
RMAN-08523: restoring datafile 00004 to D:/Oracle/ORADATA/TEST/TEMP01.DBF
RMAN-08523: restoring datafile 00005 to D:/Oracle/ORADATA/TEST/TOOLS01.DBF
RMAN-08523: restoring datafile 00006 to D:/Oracle/ORADATA/TEST/INDX01.DBF
RMAN-08023: channel c1: restored backup piece 1
RMAN-08511: piece handle=D:/BACKUP/FULL01ENmd5I_1_1 tag=DBFULL 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 191 is already on disk as file D:/ORACL
E/ORADATA/TEST/ARCHIVE/TESTT001S00191.ARC
RMAN-06050: archivelog thread 1 sequence 192 is already on disk as file D:/ORACL
E/ORADATA/TEST/ARCHIVE/TESTT001S00192.ARC
RMAN-03023: executing command: recover(4)
RMAN-08515: archivelog filename=D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00191.AR
C thread=1 sequence=191
RMAN-08515:archivelog filename=D:/Oracle/ORADATA/TEST/ARCHIVE/TESTT001S00192.ARC
Thread=1 sequence=192
RMAN-08055: media recovery complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: ALTER DATABASE OPEN RESETLOGS
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
6、 檢查數(shù)據(jù)
Database altered.
SQL> select * from test;
A
---------------------------------------
1
2
可以看到,表依然存在。
說明:
1、 RMAN也可以實現(xiàn)不完全恢復(fù),方法比OS備份恢復(fù)的方法更簡單可靠;
2、 RMAN可以基于時間,基于改變與基于日志序列的不完全恢復(fù),基于日志序列的恢復(fù)可以指定恢復(fù)到哪個日志序列,如
run {
allocate channel ch1 type disk;
allocate channel ch2 type 'sBT_tape';
set until logseq 1234 thread 1;
restore controlfile to '$Oracle_HOME/dbs/cf1.f' ;
replicate controlfile from '$Oracle_HOME/dbs/cf1.f';
alter database mount;
restore database;
recover database;
sql "ALTER DATABASE OPEN RESETLOGS";
}
3、 與所有的不完全恢復(fù)一樣,必須在mount下,restore所有備份數(shù)據(jù)文件,需要resetlogs;
4、 基于改變的恢復(fù)比基于時間的恢復(fù)更可靠,但是可能也更復(fù)雜,需要知道需要恢復(fù)到哪一個改變號(SCN),在正常生產(chǎn)中,獲取SCN的辦法其實也有很多,如查詢數(shù)據(jù)庫字典表(V$archived_log or v$log_history),或分析歸檔與聯(lián)機(jī)日志(logmnr)等。
第五章 其它恢復(fù)案例
5.1 損壞聯(lián)機(jī)日志的恢復(fù)方法
5.1.1 損壞非當(dāng)前聯(lián)機(jī)日志
大家都清楚,聯(lián)機(jī)日志分為當(dāng)前聯(lián)機(jī)日志和非當(dāng)前聯(lián)機(jī)日志,非當(dāng)前聯(lián)機(jī)日志的損壞是比較簡單的,一般通過clear命令就可以解決問題。
1、啟動數(shù)據(jù)庫,碰到ORA-00312 or ORA-00313錯誤,如
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: 'D:/Oracle/ORADATA/TEST/REDO01.LOG'
從這里我們知道日志組1的數(shù)據(jù)文件損壞了
從報警文件可以看到更具體的信息
2、 查看V$log視圖
SQL> select group#,sequence#,archived,status from v$log;
GROUP# SEQUENCE# ARCHIVED STATUS
---------- ---------- -------- ----------------
1 1 YES INACTIVE
2 2 YES INACTIVE
3 3 NO CURRENT
可以知道,該組是非當(dāng)前狀態(tài),而且已經(jīng)歸檔。
3、 用CLEAR命令重建該日志文件
SQL>alter database clear logfile group 1;
假如是該日志組還沒有歸檔,則需要用
SQL>alter database clear unarchived logfile group 1;
4、 打開數(shù)據(jù)庫,重新備份數(shù)據(jù)庫
SQL>alter database open;
說明:
1、假如損壞的是非當(dāng)前的聯(lián)機(jī)日志文件,一般只需要clear就可以重建該日志文件,但是假如該數(shù)據(jù)庫處于歸檔狀態(tài)但該日志還沒有歸檔,就需要強(qiáng)行clear;
2、建議clear,非凡是強(qiáng)行clear后作一次數(shù)據(jù)庫的全備份;
3、此方法適用于歸檔與非歸檔數(shù)據(jù)庫。
5.1.2 損壞當(dāng)前聯(lián)機(jī)日志
歸檔模式下當(dāng)前日志的損壞有兩種情況,
一、是數(shù)據(jù)庫是正常關(guān)閉,日志文件中沒有未決的事務(wù)需要實例恢復(fù),當(dāng)前日志組的損 壞就可以直接用alter database clear unarchived logfile group n來重建。
二、是日志組中有活動的事務(wù),數(shù)據(jù)庫需要媒體恢復(fù),日志組需要用來同步,有兩種補(bǔ)救辦法:
A. 最好的辦法就是通過不完全恢復(fù),可以保證數(shù)據(jù)庫的一致性,但是這種辦法要求在歸檔方式下,并且有可用的備份
B. 通過強(qiáng)制性恢復(fù),但是可能導(dǎo)致數(shù)據(jù)庫不一致。
下面分別用來說明這兩種恢復(fù)方法:
5.1.2.1 通過備份來恢復(fù)
1、 打開數(shù)據(jù)庫,會碰到一個類似的錯誤
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: 'D:/Oracle/ORADATA/TEST/REDO01.LOG'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件
2、 查看V$log,發(fā)現(xiàn)是當(dāng)前日志
SQL> select group#,sequence#,archived,status from v$log;
GROUP# SEQUENCE# ARCHIVED STATUS
--------- ---------- -------- ----------------
1 1 NO CURRENT
2 2 YES INACTIVE
3 3 YES INACTIVE
3、 發(fā)現(xiàn)clear不成功
SQL> alter database clear unarchived logfile group 1;
alter database clear unarchived logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of thread 1
ORA-00312: online log 1 thread 1: 'D:/Oracle/ORADATA/TEST/REDO01.LOG'
4、 拷貝有效的數(shù)據(jù)庫的全備份,并不完全恢復(fù)數(shù)據(jù)庫:
可以采用獲取最近的SCN的辦法用until scn恢復(fù)或用until cnacel恢復(fù)
recover database until cancel
先選擇auto,盡量恢復(fù)可以利用的歸檔日志,然后重新
recover database until cancel
這次輸入cancel,完成不完全恢復(fù),也就是說恢復(fù)兩次。
如:
SQL> recover database until cancel;
Auto
……
SQL> recover database until cancel;
Cancel;
5、 利用alter database open resetlogs打開數(shù)據(jù)庫.
說明:
1、這種辦法恢復(fù)的數(shù)據(jù)庫是一致的不完全恢復(fù),會丟失當(dāng)前聯(lián)機(jī)日志中的事務(wù)數(shù)據(jù);
2、這種方法適合于歸檔數(shù)據(jù)庫并且有可用的數(shù)據(jù)庫全備份;
3、恢復(fù)成功之后,記得再做一次數(shù)據(jù)庫的全備份;
4、建議聯(lián)機(jī)日志文件一定要實現(xiàn)鏡相在不同的磁盤上,避免這種情況的發(fā)生,因為任何數(shù)據(jù)的丟失對于生產(chǎn)來說都是不容許的。
5.1.2.2 假如沒有備份,進(jìn)行強(qiáng)制性恢復(fù)
1、 打開數(shù)據(jù)庫,會碰到一個類似的錯誤
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: 'D:/Oracle/ORADATA/TEST/REDO01.LOG'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件
2、 查看V$log,發(fā)現(xiàn)是當(dāng)前日志
SQL> select group#,sequence#,archived,status from v$log;
GROUP# SEQUENCE# ARCHIVED STATUS
---------- ---------- -------- ----------------
1 1 NO CURRENT
2 2 YES INACTIVE
3 3 YES INACTIVE
3、 發(fā)現(xiàn)clear不成功
SQL> alter database clear unarchived logfile group 1;
alter database clear unarchived logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of thread 1
ORA-00312: online log 1 thread 1: 'D:/Oracle/ORADATA/TEST/REDO01.LOG'
4、 把數(shù)據(jù)庫down掉
SQL>shutdown immediate
5、 在init<sid>.ora中加入如下參數(shù)
_allow_resetlogs_corruption=TRUE
6、 重新啟動數(shù)據(jù)庫,利用until cancel恢復(fù)
SQL>recover database until cancel;
Cancel
假如出錯,不再理會,發(fā)出
SQL>alter database open resetlogs;
7、 數(shù)據(jù)庫被打開后,馬上執(zhí)行一個full eXPort
8、 shutdown數(shù)據(jù)庫,去掉_all_resetlogs_corrupt參數(shù)
9、 重建庫
10、import并完成恢復(fù)
11、建議執(zhí)行一下ANALYZE TABLE ...VALIDATE STRUCTURE CASCADE;
說明:
1、該恢復(fù)方法是沒有辦法之后的恢復(fù)方法,一般情況下建議不要采用,因為該方法可能導(dǎo)致數(shù)據(jù)庫的不一致;
2、該方法也丟失數(shù)據(jù),但是丟失的數(shù)據(jù)沒有上一種方法的數(shù)據(jù)多,主要是未寫入數(shù)據(jù)文件的已提交或未提交數(shù)據(jù);
3、建議成功后嚴(yán)格執(zhí)行以上的7到11步,完成數(shù)據(jù)庫的檢查與分析;
4、全部完成后做一次數(shù)據(jù)庫的全備份;
5、建議聯(lián)機(jī)日志文件一定要實現(xiàn)鏡相在不同的磁盤上,避免這種情況的發(fā)生,因為任何數(shù)據(jù)的丟失對于生產(chǎn)來說都是不容許的。
5.2 損壞控制文件的恢復(fù)方法
5.2.1 損壞單個控制文件
損壞單個控制文件是比較輕易恢復(fù)的,因為一般的數(shù)據(jù)庫系統(tǒng),控制文件都不是一個,而且所有的控制文件都互為鏡相,只要拷貝一個好的控制文件替換壞的控制文件就可以了。
1、 控制文件損壞,最典型的就是啟動數(shù)據(jù)庫出錯,不能mount數(shù)據(jù)庫
SQL>startup
ORA-00205: error in identifying controlfile, check alert log for more info
查看報警日志文件,有如下信息
alter database mount
Mon May 26 11:59:52 2003
ORA-00202: controlfile: 'D:/Oracle/oradata/chen/control01.ctl'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
2、 停止數(shù)據(jù)庫:
SQL>shutdown immediate
3、 拷貝一個好的控制文件替換壞的控制文件或修改init.ora中的控制文件參數(shù),取消這個壞的控制文件。
4、 重新啟動數(shù)據(jù):
SQL>startup
說明:
1、損失單個控制文件是比較簡單的,因為數(shù)據(jù)庫中所有的控制文件都是鏡相的,只需要簡單的
拷貝一個好的就可以了;
2、建議鏡相控制文件在不同的磁盤上;
3、建議多做控制文件的備份,長期保留一份由alter database backup control file to trace產(chǎn)生的控制文件的文本備份。
5.2.2 損壞全部控制文件
損壞多個控制文件,或者人為的刪除了所有的控制文件,通過控制文件的復(fù)制已經(jīng)不能解決問題,這個時候需要重新建立控制文件。
同時注重,alter database backup control file to trace可以產(chǎn)生一個控制文件的文本備份。
以下是具體重新創(chuàng)建控制文件的步驟:
1、 關(guān)閉數(shù)據(jù)庫
SQL>shutdown immediate;
2、 刪除所有控制文件,模擬控制文件的丟失
3、 啟動數(shù)據(jù)庫,出現(xiàn)錯誤,并不能啟動到mount下
SQL>startup
ORA-00205: error in identifying controlfile, check alert log for more info
查看報警日志文件,有如下信息
alter database mount
Mon May 26 11:53:15 2003
ORA-00202: controlfile: 'D:/Oracle/oradata/chen/control01.ctl'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統(tǒng)找不到指定的文件。
4、 關(guān)閉數(shù)據(jù)庫
SQL>shutdown immediate;
5、 在internal或sys下運(yùn)行如下創(chuàng)建控制文件的腳本,注重完整列出聯(lián)機(jī)日志或數(shù)據(jù)文件的路徑,或修改由alter database backup control file to trace備份控制文件時產(chǎn)生的腳本,去掉多余的注釋即可。
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "TEST" NORESETLOGS NOARCHIVELOG
MAXLOGFILES 32
MAXLOGMEMBERS 2
MAXDATAFILES 254
MAXINSTANCES 1
MAXLOGHISTORY 226
LOGFILE
GROUP 1 'D:/Oracle/ORADATA/TEST/REDO01.LOG' SIZE 1M,
GROUP 2 'D:/Oracle/ORADATA/TEST/REDO02.LOG' SIZE 1M,
GROUP 3 'D:/Oracle/ORADATA/TEST/REDO03.LOG' SIZE 1M
DATAFILE
'D:/Oracle/ORADATA/TEST YSTEM01.DBF',
'D:/Oracle/ORADATA/TEST/RBS01.DBF',
'D:/Oracle/ORADATA/TEST/USERS01.DBF',
'D:/Oracle/ORADATA/TEST/TEMP01.DBF',
'D:/Oracle/ORADATA/TEST/TOOLS01.DBF',
'D:/Oracle/ORADATA/TEST/INDX01.DBF'
CHARACTER SET ZHS16GBK;
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE
--if the last shutdown was not normal or immediate
--noarchive
-- RECOVER DATABASE UNTIL CANCELUSING BACKUP CONTROLFILE
--archive
-- RECOVER DATABASE USING BACKUP CONTROLFILE UNTIL CANCEL
-- Database can now be opened normally.
ALTER DATABASE OPEN;
--if recover database until cancel
--ALTER DATABASE OPEN RESETLOGS;
6、 假如沒有錯誤,數(shù)據(jù)庫將啟動到open狀態(tài)下。
說明:
1、重建控制文件用于恢復(fù)全部數(shù)據(jù)文件的損壞,需要注重其書寫的正確性,保證包含了所有的數(shù)據(jù)文件與聯(lián)機(jī)日志;
2、經(jīng)常有這樣一種情況,因為一個磁盤損壞,我們不能再恢復(fù)(store)數(shù)據(jù)文件到這個磁盤,因此在store到另外一個盤的時候,我們就必須重新創(chuàng)建控制文件,用于識別這個新的數(shù)據(jù)文件,這里也可以用這種方法用于恢復(fù)。
5.3 損壞回滾數(shù)據(jù)文件的恢復(fù)方法
回滾段表空間中的一個數(shù)據(jù)文件丟失或者損壞導(dǎo)致數(shù)據(jù)庫無法識別它,在啟動數(shù)據(jù)庫的時候會出現(xiàn)ORA-1157, ORA-1110的錯誤,或者操作系統(tǒng)級別的錯誤,例如ORA-7360。在關(guān)閉數(shù)據(jù)庫的時候(normal或者immediate)會出現(xiàn)ORA-1116, ORA-1110的錯誤,或者操作系統(tǒng)級別的錯誤,例如ORA-7368。
感謝Coolyl的辛勤工作,關(guān)于回滾段的大部分內(nèi)容都是摘自他在itpub的文章。
5.3.1 損壞數(shù)據(jù)文件,但數(shù)據(jù)庫處于Open狀態(tài)
假如你發(fā)現(xiàn)有回滾段的數(shù)據(jù)文件丟失或者損壞了,而此時的數(shù)據(jù)庫是處于打開的狀態(tài)下并且在運(yùn)行,就千萬不要關(guān)閉數(shù)據(jù)庫了,因為在大多數(shù)的情況下打開的時候比關(guān)閉的時候好解決問題一些。
一般也是存在有兩種情況:
A、是offline丟失或損壞的數(shù)據(jù)文件,然后從一個備份中恢復(fù),執(zhí)行介質(zhì)恢復(fù)以保持一致性。但是這種情況要求數(shù)據(jù)庫是歸檔方式下才可以采用的。
B、是offline那個存在丟失或損壞的數(shù)據(jù)文件所在的整個回滾段表空間,然后刪除整個回滾段表空間并重建,但是你必須要殺掉那些在回滾段中已經(jīng)激活的用戶進(jìn)程才可以offline的。
通常第一種情況就比較簡單實現(xiàn),但是更多的用戶事務(wù)將會出錯并且回滾。
A的具體步驟:
1、 offline丟失或損壞的數(shù)據(jù)文件
ALTER DATABASE DATAFILE '<full_path_file_name>' OFFLINE;
2、 從一個有效的備份中恢復(fù)。
3、 執(zhí)行以下查詢:
SELECT V1.GROUP#, MEMBER, SEQUENCE#
FROM V$LOG V1, V$LOGFILE V2
WHERE V1.GROUP# = V2.GROUP# ;
這個將列出你的所有redolog文件以及它們所代表的sequence numbers。
4、 恢復(fù)數(shù)據(jù)文件。
RECOVER DATAFILE '<full_path_file_name>'
5、 確信你應(yīng)用了所有的redolog文件,直至出現(xiàn)提示信息"Media recovery complete"。
6、 online那個數(shù)據(jù)文件。
ALTER DATABASE DATAFILE '<full_path_file_name>' ONLINE;
B的具體步驟:
1、 offline存在丟失或損壞的數(shù)據(jù)文件的回滾段表空間中的所有回滾段。
ALTER ROLLBACK SEGMENT <rollback_segment> OFFLINE;
2、 檢測當(dāng)然回滾段的狀態(tài)。
SELECT SEGMENT_NAME, STATUS FROM DBA_ROLLBACK_SEGS
WHERE TABLESPACE_NAME = '<TABLESPACE_NAME>';
3、 刪除所有offline的回滾段
DROP ROLLBACK SEGMENT <rollback_segment>;
4、 處理那些online狀態(tài)的回滾段。
重新執(zhí)行第二步的查詢
假如你已經(jīng)執(zhí)行過offline操作的回滾段狀態(tài)仍然是online,則說明這個回滾段內(nèi)有活動的事務(wù)。你要接著查詢
SELECT SEGMENT_NAME, XACTS ACTIVE_TX, V.STATUS
FROM V$ROLLSTAT V, DBA_ROLLBACK_SEGS
WHERE TABLESPACE_NAME = '<TABLESPACE_NAME>' AND SEGMENT_ID = USN;
假如沒有返回結(jié)果,則證實存在丟失或損壞的數(shù)據(jù)文件的回滾段表空間中的所有回滾段都已經(jīng)被offline了,然后重新執(zhí)行第二步,第三步。假如查詢有結(jié)果返回,則狀態(tài)應(yīng)該是"PENDING OFFLINE".接著查看ACTIVE_TX列,假如值為0,則表明此回滾段中已經(jīng)沒有未處理的事務(wù)了,很快就會被offline的,然后等它offline后重新執(zhí)行2,3步后跳至第六步。假如值大于0,則繼續(xù)到第五步。
5、 強(qiáng)制那些包含活動事務(wù)的回滾段offline。
活動的事務(wù)應(yīng)該被提交或者回滾,執(zhí)行下面的查詢看看哪些用戶占用了回滾段:
SELECT S.SID, S.SERIAL#, S.USERNAME, R.NAME "ROLLBACK"
FROM V$session S, V$TRANSACTION T, V$ROLLNAME R
WHERE R.NAME IN ('<PENDING_ROLLBACK_1>', ... ,
'<PENDING_ROLLBACK_N>')
AND S.TADDR = T.ADDR AND T.XIDUSN = R.USN;
最好能直接聯(lián)系到那些user讓他們自己去回滾或者提交事務(wù),假如不能做到的話,那就只能強(qiáng)制性的殺掉進(jìn)程了。
ALTER SYSTEM KILL SESSION '<SID>, <SERIAL#>';
殺掉進(jìn)程后再過一段時間后回滾段會自動清除那些事務(wù),然后就可以回到第二步繼續(xù)查詢了。
6、 刪除回滾段。
DROP TABLESPACE <tablespace_name> INCLUDING CONTENTS;
7、 重建回滾段并online它們。
說明:
1、數(shù)據(jù)庫假如是open狀態(tài),就可以直接在open狀態(tài)下解決問題,沒有必要停下數(shù)據(jù)庫,增加down機(jī)時間;
2、不管上上面那種恢復(fù)方法都是正常性的恢復(fù),不會引起數(shù)據(jù)的不一致或錯誤。
5.3.2數(shù)據(jù)庫關(guān)閉,但是數(shù)據(jù)文件中沒有活動事務(wù)
這種情況下最簡單的方法就是offline drop掉這個壞了的或者丟失的數(shù)據(jù)文件,然后以restricted模式打開數(shù)據(jù)庫然后刪除并且重建包含損壞文件的回滾段表空間。
具體步驟如下:
1、 確定數(shù)據(jù)庫是正常的關(guān)閉的。方法是可以去查看alert文件,到最后看是否有如下信息:
"alter database dismount
Completed: alter database dismount"
假如有的話,就證實數(shù)據(jù)庫是正常關(guān)閉的,否則就不能用這個方法去恢復(fù)。
2、 修改init參數(shù)文件,移去ROLLBACK_SEGMENTS中包含的損壞數(shù)據(jù)文件的回滾段表空間的回滾段,假如你不能確定哪些回滾段是壞的,簡單的方法是你可以注釋掉整個ROLLBACK_SEGMENTS。
3、 以restricted模式去mount數(shù)據(jù)庫。
STARTUP RESTRICT MOUNT
4、 offline drop掉那個壞的數(shù)據(jù)文件
ALTER DATABASE DATAFILE '<full_path_file_name>' OFFLINE DROP;
5、 打開數(shù)據(jù)庫
ALTER DATABASE OPEN
假如你看到如下信息"Statement processed",則跳到第7步,假如你看到ORA-604, ORA-376, and ORA-1110的錯誤信息,繼續(xù)第6步。
6、 正常的關(guān)閉數(shù)據(jù)庫,然后在init文件中注釋掉ROLLBACK_SEGMENTS,并加入隱含參數(shù)
_corrupted_rollback_segments = ( <rollback1>,...., <rollbackN> )
然后以restricted模式打開數(shù)據(jù)庫
STARTUP RESTRICT
7、 刪除掉那個包含損壞文件的回滾段表空間。
DROP TABLESPACE <tablespace_name> INCLUDING CONTENTS;
8、 重建回滾段表空間,記得創(chuàng)建后要把回滾段都o(jì)nline。
9、 重新使數(shù)據(jù)庫對所有用戶可用。
ALTER SYSTEM DISABLE RESTRICTED SESSION;
10、然后正常關(guān)閉數(shù)據(jù)庫,修改init文件,假如開始只是注釋掉了ROLLBACK_SEGMENTS的,就去掉注釋即可,假如加了隱含參數(shù)的,注釋掉它,并在ROLLBACK_SEGMENTS加入所有的回滾段。
11、正常啟動數(shù)據(jù)庫:
Startup
說明:
1、這種方法的前提條件是數(shù)據(jù)庫是正常關(guān)閉(不是abort)可用;
2、這種方法是正常方法,不會引起數(shù)據(jù)錯誤。
5.3.3 數(shù)據(jù)庫關(guān)閉,數(shù)據(jù)文件中有活動事務(wù),沒有可用備份。
一般造成這種原因的情況是采用了shutdown abort或其它原因異常關(guān)機(jī)(如斷電)導(dǎo)致的。
1、開啟一個事務(wù)
SQL> set transaction use rollback segment rbs0;
Transaction set.
SQL> insert into test (a) values (1);
1 row created.
2、異常關(guān)閉
SQL> shutdown abort;
Oracle instance shut down.
3、刪除rbs的一個數(shù)據(jù)文件
C:>del D:/Oracle/oradata/chen/rbs01.
4、修改INIT<sid>.ora :
rollback_segments=(system)
添加_corrupted_rollback_segments=(rbs0,rbs1,rbs2……)
5、SQL>Startup mount
6、SQL>alter database datafile 'd:/Oracle/oradata/t8i/rbs01.dbf' offline drop;
數(shù)據(jù)庫已更改。
7、SQL>recover database ;
完成介質(zhì)恢復(fù)。
8、SQL>alter database open ;
數(shù)據(jù)庫已更改。
9、SQL>select * from v$rollname;
USN NAME
---- -------
0 SYSTEM
10、SQL>select segment_name,tablespace_name,status
FROM dba_rollback_segs;
SEGMENT_NAME TABLESPACE_NAME STATUS
----------- ------ ------------------------------------
SYSTEM SYSTEM ONLINE
RBS0 RBS NEEDS RECOVERY
RBS1 RBS NEEDS RECOVERY
RBS2 RBS NEEDS RECOVERY
11、SQL>drop rollback segment rbs0;
重算段已丟棄。
SQL>drop rollback segment rbs1;
重算段已丟棄。
SQL>drop rollback segment rbs2;
重算段已丟棄。
12、SQL>select segment_name,tablespace_name,status
FROM dba_rollback_segs;
SEGMENT_NAME TABLESPACE_NAME STATUS
-------------------------------------
SYSTEM SYSTEM ONLINE
13、SQL>drop tablespace rbs including contents;
表空間已丟棄。
14、重建新的回滾表空間及回滾段,并聯(lián)機(jī)。
15、SQL>shutdown abort
16、再修改INIT<sid>.ora :
rollback_segments=(rbs0,rbs1,rbs2)
將_corrupted_rollback_segments=(rbs0,rbs1,rbs2)去掉。
17、SQL>startup
說明:
1、這種辦法是萬不得以的時候使用的方法,假如有備份,都建議從備份上進(jìn)行恢復(fù);
2、這種方法恢復(fù)的數(shù)據(jù)庫,可能會引起數(shù)據(jù)庫的數(shù)據(jù)錯誤;
3、恢復(fù)成功以后,建議exp/imp數(shù)據(jù),并重新分析檢查數(shù)據(jù)庫。
5.3.4 數(shù)據(jù)庫關(guān)閉,數(shù)據(jù)文件中有活動事務(wù),從備份恢復(fù)
1、從一個有效的備份中恢復(fù)損壞的數(shù)據(jù)文件。
2、mount數(shù)據(jù)庫。
3、執(zhí)行以下查詢:
SELECT FILE#, NAME, STATUS FROM V$DATAFILE;
假如發(fā)現(xiàn)要恢復(fù)的文件是offline狀態(tài)的話,要先online它:
ALTER DATABASE DATAFILE '<full_path_file_name>' ONLINE;
4、執(zhí)行以下查詢
SELECT V1.GROUP#, MEMBER, SEQUENCE#, FIRST_CHANGE#
FROM V$LOG V1, V$LOGFILE V2
WHERE V1.GROUP# = V2.GROUP# ;
這個將列出redlog文件所代表的sequence和first change numbers。
5、假如數(shù)據(jù)庫是非歸檔情況下,執(zhí)行以下查詢:
SELECT FILE#, CHANGE# FROM V$RECOVER_FILE;
假如CHANGE#大于最小的redolog文件的FIRST_CHANGE#,則數(shù)據(jù)文件可以被恢復(fù),記得在應(yīng)用日志的時候要把所有redolog文件全部應(yīng)用一遍。
假如CHANGE#小于最小的redolog文件的FIRST_CHANGE#,則數(shù)據(jù)文件就不可以被恢復(fù)了,這時候你要從一個有效的全備份中去恢復(fù)數(shù)據(jù)庫了,假如沒有全備份的話,那你就只能把數(shù)據(jù)庫強(qiáng)制打開到一個不一致的狀態(tài)去exp出數(shù)據(jù),然后重新建庫導(dǎo)入數(shù)據(jù),因為這種方式的恢復(fù)Oracle是不推薦用戶自己做的,所以這里我就不具體說明了。
6、恢復(fù)數(shù)據(jù)文件:
RECOVER DATAFILE '<full_path_file_name>'
7、確信你應(yīng)用了所有的redolog文件,直至出現(xiàn)提示信息"Media recovery complete"。
8、打開數(shù)據(jù)庫。
說明:
1、這種方法要求在歸檔有備份的方式下進(jìn)行,而且是建議方式;
2、這種方法不會導(dǎo)致數(shù)據(jù)庫的錯誤。
5.4 損壞臨時數(shù)據(jù)文件的恢復(fù)方法
臨時數(shù)據(jù)文件的恢復(fù)是比較簡單的,因為臨時文件中不涉及到其它的有用的數(shù)據(jù),所以可以刪除后重建。
1、關(guān)閉數(shù)據(jù)庫:
SQL>shutdown immediate
2、刪除臨時數(shù)據(jù)文件,模擬媒體失敗;
3、啟動數(shù)據(jù)庫,檢測到文件錯誤;
4、脫機(jī)該數(shù)據(jù)文件:
SQL>alter database datafile '文件名全名' offline drop;
5、打開數(shù)據(jù)庫
SQL>alter database open
6、刪除該臨時表空間
SQL>drop tablespace temp(或其它臨時表空間名稱);
7、重新創(chuàng)建該表空間,并重新分配給用戶。
說明:
1、臨時數(shù)據(jù)文件是非重要文件,不保存永久數(shù)據(jù),可以隨時刪除重建,不影響數(shù)據(jù)庫的數(shù)據(jù)安全;
2、假如重新建立以后,別忘了重新分配給用戶。
第六章. 常見恢復(fù)誤區(qū)
1、可以不需要備份,只有歸檔就能進(jìn)行數(shù)據(jù)庫的向前的恢復(fù)
答:這個在Oracle 9i以前起碼是不可能的,在別的數(shù)據(jù)庫我也沒有聽說過,不完全恢復(fù)的主要思路是利用不完全點之前的備份,加上歸檔日志,恢復(fù)到不完全恢復(fù)點,9i中出現(xiàn)了一個Flashback的特性,這個特性的使用,也是有很多局限的。
2、進(jìn)行不完全恢復(fù)只需要拷貝一個需要恢復(fù)的備份數(shù)據(jù)文件
答:不完全恢復(fù)需要拷貝所有的數(shù)據(jù)文件,最好包括臨時數(shù)據(jù)文件在內(nèi),否則需要另外的處理,假如有一個數(shù)據(jù)文件的SCN大于不完全恢復(fù)點,那么這個恢復(fù)都將是失敗的。
3、使用RMAN目錄與目標(biāo)數(shù)據(jù)庫在同一數(shù)據(jù)庫能很好進(jìn)行數(shù)據(jù)庫的恢復(fù)
答:使用恢復(fù)目錄與目標(biāo)數(shù)據(jù)庫在同一個數(shù)據(jù)庫中,將存在很大的恢復(fù)局限,如該數(shù)據(jù)庫的系統(tǒng)數(shù)據(jù)文件的損害,數(shù)據(jù)庫根本不能open,那么RMAN也就無法連接恢復(fù)目錄,也就不存在恢復(fù)了。
第七章. 小結(jié)
這里我們反復(fù)演示了多種情況下的恢復(fù)方案,通過這些演示,我們應(yīng)該把握了如下內(nèi)容:
1、利用OS與RMAN進(jìn)行各種常規(guī)備份與恢復(fù)。
2、熟悉沒有備份或簡單的非常規(guī)備份與恢復(fù)的方法。