基於Cancel的不完全恢復適用場景:Recover時,所需的某個歸檔日誌損壞,或主機斷電,current狀態的聯機日誌損壞。
一、環境準備
以SCOTT用戶登錄,新建表EMP1,切換日誌,再新建表EMP2,主機斷電,刪除當前日誌文件,模擬文件損壞。具體命令如下:
SQL> conn scott/oracle
Connected.
SQL> create table emp1 as select * from emp;
Table created.
SQL> conn / as sysdba
Connected.
SQL> alter system switch logfile;
System altered.
SQL> conn scott/oracle
Connected.
SQL> create table emp2 as select * from emp;
Table created.
SQL> conn / as sysdba
Connected.
確認日誌的位置。
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ---------
1 1 5 52428800 1 NO CURRENT 916653 14-OCT-11
2 1 4 52428800 1 YES ACTIVE 915569 14-OCT-11
3 1 3 52428800 1 YES INACTIVE 915566 14-OCT-11
SQL> select * from v$logfile;
GROUP# STATUS TYPE MEMBER IS_
---------- ------- ------- ------------------------------------------------------------ ---
3 ONLINE /home/oracle/oracle/product/oradata/orcl/redo03.log NO
2 ONLINE /home/oracle/oracle/product/oradata/orcl/redo02.log NO
1 ONLINE /home/oracle/oracle/product/oradata/orcl/redo01.log NO
SQL> shutdown abort
ORACLE instance shut down.
SQL> !rm -f /home/oracle/oracle/product/oradata/orcl/redo02.log
二、恢復步驟
1、嘗試打開數據庫
SQL> startup
ORACLE instance started.
Total System Global Area 130023424 bytes
Fixed Size 1218100 bytes
Variable Size 67111372 bytes
Database Buffers 58720256 bytes
Redo Buffers 2973696 bytes
Database mounted.
ORA-00313: open failed for members of log group 2 of thread 1
ORA-00312: online log 2 thread 1: '/home/oracle/oracle/product/oradata/orcl/redo02.log'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
2、嘗試Clear
SQL> alter database clear logfile group 2;
alter database clear logfile group 2
*
ERROR at line 1:
ORA-01624: log 2 needed for crash recovery of instance orcl (thread 1)
ORA-00312: online log 2 thread 1: '/home/oracle/oracle/product/oradata/orcl/redo02.log'
結果提示日誌有用,不能Clear
3、在當前庫做基於Cancel的不完全恢復(這個想法是錯誤的)
SQL> recover database until cancel;
ORA-00279: change 916503 generated at 10/14/2011 23:26:00 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/archive/1_4_764549776.dbf
ORA-00280: change 916503 for thread 1 is in sequence #4
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 916653 generated at 10/14/2011 23:26:58 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/archive/1_5_764549776.dbf
ORA-00280: change 916653 for thread 1 is in sequence #5
ORA-00278: log file '/home/oracle/oracle/product/archive/1_4_764549776.dbf' no longer needed for this recovery
ORA-00308: cannot open archived log '/home/oracle/oracle/product/archive/1_5_764549776.dbf'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/home/oracle/oracle/product/oradata/orcl/system01.dbf'
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SQL> alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/home/oracle/oracle/product/oradata/orcl/system01.dbf'
恢復之後,使用Resetlogs選項,仍無法打開數據庫,提示數據文件不一致,System表空間需要進一步的恢復。
當前日誌損壞時,不能基於當前的數據庫做不完全恢復。只能用以前的備份,做一個基於Cancel的不完全恢復。
4、用以前的備份,做基於Cancel的不完全恢復(其實就是恢復到不能恢復爲止)
SQL> shutdown abort
ORACLE instance shut down.
SQL> !cp /disk2/oracle/coldbak/*.dbf $ORACLE_BASE/oradata/orcl/
SQL> startup mount
ORACLE instance started.
Total System Global Area 130023424 bytes
Fixed Size 1218100 bytes
Variable Size 67111372 bytes
Database Buffers 58720256 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> recover database until cancel;
ORA-00279: change 916502 generated at 10/14/2011 23:13:45 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/archive/1_4_764549776.dbf
ORA-00280: change 916502 for thread 1 is in sequence #4
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 916653 generated at 10/14/2011 23:26:58 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/archive/1_5_764549776.dbf
ORA-00280: change 916653 for thread 1 is in sequence #5
ORA-00278: log file '/home/oracle/oracle/product/archive/1_4_764549776.dbf' no longer needed for this recovery
ORA-00308: cannot open archived log '/home/oracle/oracle/product/archive/1_5_764549776.dbf'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
SQL> alter database open resetlogs;
Database altered.
可以看到,最後數據可以打開了。
5、檢查數據
SQL> conn scott/oracle
Connected.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
RECOVER_TEST TABLE
RECOVER_TEST2 TABLE
EMP_BAK TABLE
EMP1 TABLE
8 rows selected.
可以看到表EMP1存在,表EMP2不存在。因爲EMP2的創建,是保存在當前REDO日誌中的,而當前REDO日誌損壞,所有當前日誌中保存的操作全部丟失了。
歸檔日誌、或者REDO日誌損失,數據庫就只能恢復到丟失的日誌之前了。
數據庫打開的前提之一:數據文件保持一致。當前數據庫不一致無法打開數據庫,只能根據以前一致的備份,恢復到最近的一致的時間點。