--從controlfile讀取scn資訊set linesize 140col dummy for a140set linesize 140 numformat 999999999999999999prompt --系統scnselect checkpoint_change# from v$database;prompt --資料檔案scnselect file#,checkpoint_change# from v$datafile;prompt --終止scnselect file#,last_change# from v$datafile;
--從資料檔案頭擷取資訊--查詢v$datafile_headerset linesize 140 numformat 999999999999999999col error for a20select file#,STATUS online_fromctlf,error ,recover,fuzzy,checkpoint_change# from v$datafile_header;
--查詢v$datafile_header的基表set linesize 140 numformat 9999999999999select hxfil file#,fhsta status,fhscn scn ,fhrba_seq seq#,fhrba_bno seq#_block,fhrba_bof seq#_block_startbyte from x$kcvfh;--看狀態是不是8192與0,還有獲得rba以便知道恢複時從哪個logfile(archivelog 或 redo)開始。select hxfil,fhsta,fhrba_seq from x$kcvfh;
select max(fhafs) from x$kcvfh;--查看datafile是否需要recover,如果是0則不需要,大於0,則代表資料檔案處於不一致的狀態,那麼需要至少需要recover到什麼SCN。--open read write下應該都是0--fhafs absolute fuzzy scn, 即Minimum PITR SCN。--代表了此資料檔案的所有資料區塊中,現在最大的那個scn。我們實施recover時至少得把這個資料檔案的所有資料區塊recover到這個scn,所有資料區塊scn才一致。
--查詢日誌情況--查詢v$logselect GROUP# ,SEQUENCE#,ARCHIVED,STATUS,FIRST_change# from v$log order by sequence# asc;查詢v$log_historyselect sequence#,first_change#,next_change# from v$log_history;查詢v$archived_logset lin 140col name for a100select NAME,sequence#,first_change#,next_change#,decode(STATUS,'A','Available','D','Deleted','U','Unavailable','X','Expired') status from v$archived_log;
--庫open時從current redo log中讀取資訊,獲得當前最新的scn號。select dbms_flashback.get_system_change_number from dual;set linesize 140 numformat 999999999999999999col current_scn for a30select current_scn from v$database;--我們會發現這個scn會不斷重新整理,儘管DB沒有事務。還是有些內部事務我不知道呢?還是每隔一段時間就必須得重新整理呢?--答案是:用current_scn查,就如同sequence,你查一下它就往前一點,不往前怎麼得出current的scn呢?而用dbms_flashback查,則查多次也不會往前遞增(除非有其他事務使scn前進)
recover是系統scn>啟動scn
介質恢複(資料檔案更舊)system SCN=datafile SCN>start SCN,stop SCN NULL/notNULL舊資料檔案系統啟動時,每個資料檔案scn(來自control file)會與每個資料檔案頭的啟動scn做比較,假如有些不一致,甚至全部都不一致,則要進行recover,即media recover。
執行個體恢複然後,會看end scn(來自控制檔案)是否為空白,假如是,證明並沒有一致性關閉資料庫。沒有一致關閉資料庫(即關閉時沒有執行全量checkpoint),可以用shut abort類比,表現為資料檔案在執行checkpoint後,還對資料檔案中的資料區塊有write操作,即fuzzy=yes,此時資料檔案中的資料區塊的scn號,會出現有的比資料檔案頭的scn號還大的情況,我們就要實行intance recovery。所以執行個體恢複,從checkpoint scn開始,往後的日誌要應用到資料檔案中。
控制檔案更舊system SCN=datafile SCN<=start SCN,stop SCN notNULL/NULLrecover database using backup controlfile;這裡可以選擇AUTO。此時會從這箇舊的控制檔案的scn號,去v$archived_log中尋找是在哪個low scn與high scn之間,從而確定是哪個歸檔日誌開始recover。從backup controlfile的scn號,一直recover到當前v$datafile_header的scn,然後會提示找不到最新一號的日誌。因為backup controlfile沒有記載新的歸檔日誌與logfile資訊,只能從歸檔目的地按圖索驥地recover,此時我們再執行一次recover database using backup controlfile until cancel;並指定current online redo log,即可完成apply。儘管如此,控制檔案的scn號與datafile的scn是沒有變化的(真不知道這個recover起什麼作用)此時就可以alter database open resetlogs了。一旦使用了using backup controlfile,oracle就會認為是不完全恢複,所以就必須open resetlogs了。還有另外一種不用Open resetlogs的方法,使用舊的控制檔案backup to trace,reuse database ... noresetlogs來重建控制檔案,這樣就可以recover database自動回復並open database而不用resetlogs了(切記:必須有所有的online redo logs才可以這樣!)。
因為backup controlfile只有備份時刻的archived log資訊,並沒有DB crash時刻重建控制檔案,才能成功open庫。
重建控制檔案,假如指定noresetlog,那麼open時候不指定resetlogs也可以。控制檔案會跟資料檔案的scn號。
ERROR
NULL if the datafile header read and validation were successful. If the read failed then the rest of the columns are NULL. If the validation failed then the rest of columns may display invalid data. If there is an error then usually the datafile must be restored from a backup before it can be recovered or used.
RECOVER File needs media recovery (YES | NO)
查看資料檔案頭dbid:select FHDBI from x$kcvfh
FUZZY
資料檔案裡面的有的資料區塊的scn號,大於資料檔案頭的scn號。
x$kcvfh是v$datafile_header的源,v$datafile_header相信大家在oracle恢複工作時會經常和他碰面,因為他不僅包含了checkpoint_change#,更重要的是它包含了這個checkpoint_change#所在的logfile的sequence#,準確的說rba,有了rba,在恢複時就能準確的知道到底需要哪個logfile(archivelog or redo)。
x$kcvfh有三個欄位非常有意義。
1)FHRBA_SEQ:表示當前聯機日誌對應的記錄序號
2)FHRBA_BNO:表示the log file block number
3)FHRBA_BOF:表示the byte offset
其實fhrba_seq,fhrba_bno,fhrba_bof這3個欄位對應的就是rba,rba的意思是:Recent entries in the redo thread of an Oracle instance are addressed using a 3-part redo byte address, or RBA. An RBA is comprised of : the log file sequence number (4 bytes) the log file block number (4 bytes) the byte offset into the block at which the redo record starts (2 bytes)
在datafile header上記錄rba,在恢複時就能非常準確的知道需要哪個記錄檔(通過the log file sequence number)以及哪個block(通過the log file block number)以及在這個日誌block上從哪個byte開始讀取恢複(通過the byte offset)
select hxfil file_num,fhrba_seq sequence,fhrba_bno sequence_block,fhrba_bof block_offset from x$kcvfh;
col file_name for a30select HXFIL File_num,substr(HXFNM,1,45) File_name, FHSCN SCN,FHRBA_SEQ Sequence from X$KCVFH;
判斷datafile做recover需要的archive log的sequence是多少,也就是說做recover到這個sequence,那麼control file和datafile header中的記錄就一致了。
The value for the column X$KCVFH.FHSTA (file header status) for an open database with an online datafiles in versions prior to version 10 were all 4, 9i,10g open狀態9i全部是4 (fuzzy)10g 8196(system,fuzzy),其他4
一致關閉後,startup mount(fuzz=no)8192(system) 0(其餘)
(10g以後)(scn與time互換)scn-> timeselect to_char(scn_to_timestamp(9065273060811),'yyyy-mm-dd hh24:mi:ss') scn from dual;
time -> scnselect timestamp_to_scn( to_timestamp('2013-09-13 07:15:31','yyyy-mm-dd hh24:mi:ss') ) scn from dual;
雖然參數“_allow_resetlogs_corruption”可以在checkpoint SCN不一致時強制開啟資料庫,但是這樣的資料庫在open後必須馬上作全庫的export,然後重建資料庫並import資料。
以下條件需要使用using backup controlfile 1)、使用備份控制檔案2)、重建resetlogs控制檔案,如果重建立noresetlogs不必要使用using backup controlfile
以下條件需要使用resetlog1)在不完全恢複(介質恢複)2)使用備份控制檔案
1. recover database using backup controlfile如果丟失當前控制檔案,用冷備份的控制檔案恢複的時候,用來告訴oracle,不要以controlfile中的scn作為恢複的終點;
2. recover database until cancel如果丟失current/active redo的時候,手動指定終點。
3. recover database using backup controlfile until cancel;如果 丟失當前controlfile並且current/active redo都丟失,會先去 自動 應用歸檔日誌,可以實現最大的恢複;
4. recover database until cancel using backup controlfile;如果 丟失當前controlfile並且current/active redo都丟失,以舊的redo中的scn為恢複終點。因為沒有應用歸檔日誌,所有會遺失資料。
二、總結1、using backup controlfile用於恢複備份的控制檔案與當前的控制檔案不一致的情形2、一旦使用了using backup controlfile方式,控制檔案的類型將由 current 轉移到 backup 類型,同時open_resetlogs為required3、一旦使用了using backup controlfile方式,後續再次使用recover database將變得無效4、必須要使用 resetlogs 方式開啟資料庫,即使我們做的是完全恢複
recover database using backup controlfile until cancel;然後輸入測試庫的redo,逐個試,直接能起庫為止。/paic/hq/t1inv/data/oradata/t1inv/redo61.log
recover automatdic standby DATABASE until TIME '2013-12-09 06:30:00';