Oracle 基於備份控制檔案的恢複

來源:互聯網
上載者:User

通常在當前控制檔案丟失,或者當前的控制檔案與需要恢複的控制檔案不一致的情況下,我們需要重新建立一個控制檔案或者使用 unsing

backup controlfile方式來恢複控制檔案。說簡單點,只要是備份的控制檔案與當前的控制檔案不一致進行恢複資料庫,就需要使用到 unsing

backup controlfile方式,而一旦使用了該方式,則需使用resetlgos選項來開啟資料庫。


一、基於備份控制檔案的恢複注意事項(無論是否使用恢複目錄catalog)

1、即使沒有資料檔案需要還原,當使用unsing backup controlfile 方式時必須結合 recover 命令

2、不論使用備份的控制檔案進行時間點復原或完全恢複,必須使用 open resetlogs 方式開啟資料庫

3、如果聯機日誌不可訪問,必須使用不完全恢複到聯機記錄檔中最早的一個SCN之前。這是因為RMAN並不備份聯機記錄檔

4、在恢複期間,RMAN自動搜尋聯機日誌和沒有記錄在RMAN儲存倉庫中的歸檔日誌以完成恢複

5、RMAN會根據初始化參數檔案中歸檔位置以及控制檔案聯機日誌資訊自動尋找有效歸檔日誌和聯機日誌。使用unsing backup controlfile方

式時,在恢複期間,一旦歸檔目的地以及歸檔格式發生變化,或添加新的聯機日誌成員將收到RMAN-06054錯誤資訊。

本文主要使用熱備方式來完成示範


二、示範unsing backup controlfile的使用

1、控制檔案全部丟失的情形(控制檔案備份後發生變化)

-->首先使用熱備指令碼進行備份

sys@SYBO2SZ> get db_hot_bak.sql

1 set feedback off heading off verify off

2 set pagesize 0 linesize 200

3 define dir='/u02/database/SYBO2SZ/backup/hotbak'

4 define script='/tmp/tmphotbak.sql'

5 spool &script

6 select 'ho cp '||name||' &dir' from v$datafile;

7 spool off

8 alter database begin backup;

9 start &script

10 alter database end backup;

11 alter database backup controlfile to '&dir/contlbak.ctl' reuse;

12 create pfile='&dir/initSYBO2SZ.ora' from spfile;

13* set feedback on heading on verify on pagesize 100


sys@SYBO2SZ> @db_hot_bak


sys@SYBO2SZ> show parameter control_files


NAME TYPE VALUE

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

control_files string /u02/database/SYBO2SZ/controlf

/cntl1SYBO2SZ.ctl, /u02/databa

se/SYBO2SZ/controlf/cntl2SYBO2

SZ.ctl, /u02/database/SYBO2SZ/

controlf/cntl3SYBO2SZ.ctl


-->為資料庫添加新的資料表空間,此時控制檔案將不同於先前備份的控制檔案

sys@SYBO2SZ> create tablespace tbs datafile '/u02/database/SYBO2SZ/oradata/tbs_tmp.dbf' size 10m autoextend on;


Tablespace created.


-->為資料庫添加對象

sys@SYBO2SZ> create table tb_emp tablespace tbs as select * from scott.emp ;


Table created.


sys@SYBO2SZ> select count(*) from tb_emp;


COUNT(*)

----------

14


sys@SYBO2SZ> select * from v$log;


GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM

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

3 1 116 20971520 2 NO CURRENT 1078066 08-SEP-12

4 1 115 20971520 2 YES INACTIVE 1063428 08-SEP-12


sys@SYBO2SZ> select to_char(sysdate,'yyyymmdd hh24:mi:ss') from dual;


TO_CHAR(SYSDATE,

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

20120908 16:30:41


-->切換日誌

sys@SYBO2SZ> alter system archive log current;


System altered.


-->刪除部分記錄用於恢複後驗證

sys@SYBO2SZ> delete from tb_emp where deptno=10;


3 rows deleted.


sys@SYBO2SZ> commit;


Commit complete.


sys@SYBO2SZ> alter system archive log current;


System altered.


sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive

total 143M

-rw-r----- 1 oracle oinstall 15M 2012-09-08 16:20 arch_792094299_1_115.arc

-rw-r----- 1 oracle oinstall 236K 2012-09-08 16:30 arch_792094299_1_116.arc

-rw-r----- 1 oracle oinstall 9.0K 2012-09-08 16:32 arch_792094299_1_117.arc


-->異常關機

sys@SYBO2SZ> shutdown abort;

ORACLE instance shut down.


-->類比所有控制檔案丟失

sys@SYBO2SZ> ho rm -rf /u02/database/SYBO2SZ/controlf/*


sys@SYBO2SZ> ho ls /u02/database/SYBO2SZ/controlf/


-->啟動後收到ORA-00205錯誤

sys@SYBO2SZ> startup

ORACLE instance started.


Total System Global Area 599785472 bytes

Fixed Size 2074568 bytes

Variable Size 276826168 bytes

Database Buffers 314572800 bytes

Redo Buffers 6311936 bytes

ORA-00205: error in identifying control file, check alert log for more info


sys@SYBO2SZ> select instance_name,status from v$instance;


INSTANCE_NAME STATUS

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

SYBO2SZ STARTED


sys@SYBO2SZ> select name,open_mode from v$database;

select name,open_mode from v$database

*

ERROR at line 1:

ORA-01507: database not mounted


-->還原控制檔案

sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl1SYBO2SZ.ctl


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl2SYBO2SZ.ctl


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl3SYBO2SZ.ctl


-->mount資料庫

sys@SYBO2SZ> alter database mount;


Database altered.


-->由於僅僅是丟失了控制檔案,因此我們只還原控制檔案

-->恢複資料庫,提示需要使用BACKUP CONTROLFILE選項,因為控制檔案在備份後發生了變化

sys@SYBO2SZ> recover database;

ORA-00283: recovery session canceled due to errors

ORA-01610: recovery using the BACKUP CONTROLFILE option must be done


-->提示需要實用到116歸檔日誌

sys@SYBO2SZ> recover database using backup controlfile;

ORA-00279: change 1078785 generated at 09/08/2012 16:20:48 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792094299_1_116.arc

ORA-00280: change 1078785 for thread 1 is in sequence #116


-->下面提示在介質恢複期間有未知的檔案添加到控制檔案,且檔案id為9

-->由此可以推斷檔案9是記錄在尾數為116的歸檔日誌中,正好與前面查看的歸檔日誌時間相符

Specify log: {<RET>=suggested | filename | AUTO | CANCEL}

auto

ORA-00283: recovery session canceled due to errors

ORA-01244: unnamed datafile(s) added to control file by media recovery

ORA-01110: data file 9: '/u02/database/SYBO2SZ/oradata/tbs_tmp.dbf'


ORA-01112: media recovery not started


-->使用alter database create datafile重建資料檔案

-->此處故意使用了不同於建立之前的檔案名稱tbs.dbf,此處並沒有任何影響,相對於對資料檔案進行了重新命名

sys@SYBO2SZ> alter database create datafile 9 as '/u02/database/SYBO2SZ/oradata/tbs.dbf';


Database altered.


-->嘗試再次恢複,需要使用為數位116的歸檔日誌,輸入auto後,尾數為116,117的不在需要

sys@SYBO2SZ> recover database using backup controlfile;

ORA-00279: change 1078817 generated at 09/08/2012 16:29:19 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792094299_1_116.arc

ORA-00280: change 1078817 for thread 1 is in sequence #116



Specify log: {<RET>=suggested | filename | AUTO | CANCEL}

auto

ORA-00279: change 1078886 generated at 09/08/2012 16:30:52 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792094299_1_117.arc

ORA-00280: change 1078886 for thread 1 is in sequence #117

ORA-00278: log file '/u02/database/SYBO2SZ/archive/arch_792094299_1_116.arc' no longer needed for this recovery



ORA-00279: change 1078922 generated at 09/08/2012 16:32:22 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792094299_1_118.arc

ORA-00280: change 1078922 for thread 1 is in sequence #118

ORA-00278: log file '/u02/database/SYBO2SZ/archive/arch_792094299_1_117.arc' no longer needed for this recovery


-->提示未找尾數為118的歸檔日誌,118本身還沒有歸檔,因此來說此時是聯機日誌

ORA-00308: cannot open archived log '/u02/database/SYBO2SZ/archive/arch_792094299_1_118.arc'

ORA-27037: unable to obtain file status

Linux-x86_64 Error: 2: No such file or directory

Additional information: 3


-->再次恢複

sys@SYBO2SZ> recover database using backup controlfile;

ORA-00279: change 1078922 generated at 09/08/2012 16:32:22 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_792094299_1_118.arc

ORA-00280: change 1078922 for thread 1 is in sequence #118


-->直接指定redo日誌,介質恢複成功

Specify log: {<RET>=suggested | filename | AUTO | CANCEL}

/u02/database/SYBO2SZ/redolog/log3aSYBO2SZ.log

Log applied.

Media recovery complete.


-->下面需要使用RESETLOGS選項開啟資料庫

sys@SYBO2SZ> alter database open;

alter database open

*

ERROR at line 1:

ORA-01589: must use RESETLOGS or NORESETLOGS option for database open


sys@SYBO2SZ> alter database open resetlogs;


Database altered.


-->驗證建立對象的總記錄數,正好等於刪除後的記錄數11條

sys@SYBO2SZ> select count(*) from tb_emp;


COUNT(*)

----------

11


2、刪除資料表空間類比控制檔案變化的情形

下面的這個例子的處理方式並非最佳,此處僅僅為示範unsing backup controlfile的用法且使用了不完全恢複方式,對於單個資料檔案和

sys@SYBO2SZ> archive log list; -->看看歸檔情況,log sequence從1開始

Database log mode Archive Mode

Automatic archival Enabled

Archive destination /u02/database/SYBO2SZ/archive/

Oldest online log sequence 1

Next log sequence to archive 1

Current log sequence 1

sys@SYBO2SZ> alter system switch logfile;


-->首先熱備資料庫

sys@SYBO2SZ> @db_hot_bak


-->新的incarnation 793471702已經產生,可以看到同時也產生了一個尾數為118的上一個incarnation的歸檔日誌

sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive

total 54M

-rw-r----- 1 oracle oinstall 1.0K 2012-09-08 16:48 arch_792094299_1_118.arc

-rw-r----- 1 oracle oinstall 9.0K 2012-09-08 16:48 arch_792094299_1_117.arc

-rw-r----- 1 oracle oinstall 43K 2012-09-08 16:51 arch_793471702_1_1.arc


-->刪除資料表空間以更新控制檔案

sys@SYBO2SZ> drop tablespace tbs including contents and datafiles;


Tablespace dropped.


sys@SYBO2SZ> alter system switch logfile;


System altered.


-->切換日誌後,新增了尾數為2的歸檔日誌

sys@SYBO2SZ> ho ls -hltr /u02/database/SYBO2SZ/archive

total 54M

-rw-r----- 1 oracle oinstall 1.0K 2012-09-08 16:48 arch_792094299_1_118.arc

-rw-r----- 1 oracle oinstall 9.0K 2012-09-08 16:48 arch_792094299_1_117.arc

-rw-r----- 1 oracle oinstall 43K 2012-09-08 16:51 arch_793471702_1_1.arc

-rw-r----- 1 oracle oinstall 50K 2012-09-08 16:58 arch_793471702_1_2.arc


sys@SYBO2SZ> show parameter background_dump_dest


NAME TYPE VALUE

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

background_dump_dest string /u02/database/SYBO2SZ/bdump


-->從alert log file中查詢資料表空間被刪除的準確時間

sys@SYBO2SZ> ho cat -n /u02/database/SYBO2SZ/bdump/alert_SYBO2SZ.log | grep tbs.dbf

6959 '/u02/database/SYBO2SZ/oradata/imp_tbs.dbf'

6962 '/u02/database/SYBO2SZ/oradata/imp_tbs.dbf'...

11014 alter database create datafile 9 as '/u02/database/SYBO2SZ/oradata/tbs.dbf'

11016 Completed: alter database create datafile 9 as '/u02/database/SYBO2SZ/oradata/tbs.dbf'

11273 Deleted file /u02/database/SYBO2SZ/oradata/tbs.dbf


-->下面可以看到資料表空間及資料檔案被刪除的時間

-->同時也看到了控制檔案進行了自動備份,因為RMAN配置中控制檔案自動備份被置為ON

-->此處使用之前備份的控制檔案來恢複測試,因此此處不考慮使用自動備份的控制檔案

sys@SYBO2SZ> ho more +11270 /u02/database/SYBO2SZ/bdump/alert_SYBO2SZ.log

Sat Sep 8 16:57:56 2012

drop tablespace tbs including contents and datafiles

Sat Sep 8 16:57:58 2012

Deleted file /u02/database/SYBO2SZ/oradata/tbs.dbf

Starting control autobackup

Control autobackup written to DISK device

handle '/u02/database/SYBO2SZ/backup/rman/20120907/SYBO2SZ_lev1_201209071410_c-209726751-20120908-05'

Completed: drop tablespace tbs including contents and datafiles


sys@SYBO2SZ> shutdown immediate;


-->還原控制檔案及所有的資料檔案

sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl1SYBO2SZ.ctl


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl2SYBO2SZ.ctl


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/contlbak.ctl /u02/database/SYBO2SZ/controlf/cntl3SYBO2SZ.ctl


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/*.dbf /u02/database/SYBO2SZ/oradata/.


sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/backup/hotbak/undotbs* /u02/database/SYBO2SZ/undo/.


sys@SYBO2SZ> startup mount;

ORACLE instance started.


-->使用using backup controlfile選項以及設定時間點來恢複資料庫

sys@SYBO2SZ> recover database until time '2012-09-08:16:57:56' using backup controlfile;

ORA-00279: change 1079138 generated at 09/08/2012 16:55:09 needed for thread 1

ORA-00289: suggestion : /u02/database/SYBO2SZ/archive/arch_793471702_1_2.arc

ORA-00280: change 1079138 for thread 1 is in sequence #2



Specify log: {<RET>=suggested | filename | AUTO | CANCEL}

auto

Log applied.

Media recovery complete.


sys@SYBO2SZ> alter database open resetlogs;


Database altered.


-->驗證恢複的對象中的記錄數

sys@SYBO2SZ> select count(*) from tb_emp;


COUNT(*)

----------

11


三、總結:

1、當控制檔案全部丟失或損壞,且沒有及時備份控制檔案的情況下,需要使用unsing backup controlfile方式來恢複

2、使用unsing backup controlfile方式來恢複,需要使用resetlogs方式來開啟資料庫

3、儘可能配置RMAN備份使其自動備份控制檔案


oracle視頻教程請關注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.