ORACLE FLASHBACK DATABASE 總結,oracleflashback
閃回資料庫(FLASHBACK DATABASE)是oracle資料庫非常重要的一項功能,與通過undo 擷取表資料的曆史版本進而執行邏輯錯誤恢複的功能相比,flashback database 的功能完全不同:undo資料是針對整個資料庫的;undo資料的儲存時間是有限的;undo 的本質是為了保持資料的一致性。而flashback database可以只針對某資料表空間儲存曆史資料,並且只要儲存空間允許,flashback database可以儲存任意長的曆史時間。flashback database 日誌中儲存的是資料庫資料的前鏡像。
閃回資料庫整個架構包括一個進程(rvmr),一段儲存空間(flash recovery area),一種日誌(flashback database log)。資料區塊的”前鏡像“儲存在閃回日誌中(flashback database log)。
啟用閃回資料庫
要想使用Flashback Database, 必須使用Flash Recovery Area。 要配置的2個參數如下,一個是大小,一個是位置。如果資料庫是RAC,flash recovery area 必須位於共用儲存中。資料庫必須處於archivelog 模式.參數db_flashback_retention_target控制閃回日誌的保留時間,預設是1440,即24小時。
SQL> show parameter db_recoveryNAME TYPE VALUE------------------------------------ ----------- ------------------------------db_recovery_file_dest string /u01/app/oraele/fast_recovery_ areadb_recovery_file_dest_size big integer 4122MSQL> show parameter flashbackNAME TYPE VALUE------------------------------------ ----------- ------------------------------db_flashback_retention_target integer 1440
SQL> archive log listDatabase log mode Archive ModeAutomatic archival EnabledArchive destination USE_DB_RECOVERY_FILE_DESTOldest online log sequence 5Next log sequence to archive 7Current log sequence 7
要開啟閃回資料庫功能,可以使用alter database flashback on;
在11g下,可以在mount和open模式來執行該命令:
SQL> select * from v$version;BANNER--------------------------------------------------------------------------------Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit ProductionPL/SQL Release 11.2.0.3.0 - ProductionCORE 11.2.0.3.0 ProductionTNS for Linux: Version 11.2.0.3.0 - ProductionNLSRTL Version 11.2.0.3.0 - ProductionSQL> alter database flashback on;Database altered.SQL> select status from v$instance;STATUS------------OPEN
在10g下,需要在mount狀態下,執行該命令
SQL> select * from v$version;BANNER----------------------------------------------------------------Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64biPL/SQL Release 10.2.0.5.0 - ProductionCORE 10.2.0.5.0 ProductionTNS for Linux: Version 10.2.0.5.0 - ProductionNLSRTL Version 10.2.0.5.0 - ProductionSQL> select flashback_on from v$database;FLASHBACK_ON------------------NOSQL> select status from v$instance;STATUS------------OPENSQL> alter database flashback on;alter database flashback on*ERROR at line 1:ORA-38759: Database must be mounted by only one instance and not open.
建立實驗表
SQL> truncate table test;Table truncated.SQL> insert into test values((select count(*) from test),(select current_scn from v$database));1 row created.SQL> commit;Commit complete.SQL> insert into test values((select count(*) from test),(select current_scn from v$database));1 row created.SQL> commit;Commit complete.SQL> insert into test values((select count(*) from test),(select current_scn from v$database));1 row created.SQL> commit;Commit complete.SQL> select * from test; ID SCN---------- -------------------- 0 1121575 1 1131647 2 1142043
在執行完flashback database 命令之後,可以使用多種方式修複資料庫:
1). 直接alter database open resetlogs 開啟資料庫,指定scn 或者timestamp 時間點之後產生的資料統統丟失。
2). 先執行alter database open read only 命令以read-only 模式開啟資料庫,查看恢複後的資料是否滿足要求,如果滿足要求,則通過resetlogs開啟資料庫,否則,重新執行flashback 操作。
3). 先執行alter database open read only 命令以read-only 模式開啟資料庫,然後立刻通過邏輯匯出的方式將誤操作涉及表的資料匯出,再執行recover database 命令以重新應用程式資料庫產生的redo,將資料庫修複到flashback database 操作前的狀態,然後再通過邏輯匯入的方式,將之前誤操作的表重新匯入,這樣的話對現有資料的影響最小,不會有資料丟失。
下面示範第二種方式:
SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> flashback database to scn 1121575;Flashback complete.SQL> alter database open read only;Database altered.SQL> select * from test;no rows selectedSQL> startup mount forceORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL>SQL>SQL>SQL>SQL>SQL> shutdown immediateORA-01109: database not openDatabase dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> flashback database to scn 1121600;Flashback complete.SQL> select * from test;select * from test *ERROR at line 1:ORA-01219: database not open: queries allowed on fixed tables/views onlySQL> alter database open read only;Database altered.SQL> select * from test; ID SCN---------- -------------------- 0 1121575SQL> startup mount forceORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> alter database open resetlog;alter database open resetlog *ERROR at line 1:ORA-02288: invalid OPEN modeSQL> alter database open resetlogs;Database altered.SQL> select * from test; ID SCN---------- -------------------- 0 1121575
與flashback database 相關的視圖
V$database
通過這個視圖可以查看是否啟用了Flashback database功能
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
2. V$flashback_database_log
Flashback Database 所能回退到的最早時間,取決與保留的Flashback Database Log 的多少, 該視圖就可以查看許多有用的資訊。
Oldest_flashback_scn / Oldest_flashback_time : 這兩列用來記錄可以恢複到最早的時點
Flashback_size: 記錄了當前使用的Flash Recovery Area 空間的大小
Retention_target: 可以恢複的時間長度
Estimated_flashback_size: 根據恢復長度對需要的空間大小的估計值
3. V$flashback_database_stat
這個視圖用來對Flashback log 空間情況進行更細粒度的記錄和估計。 這個視圖以小時為單位記錄單位時間內資料庫的活動量,Flashback_Data 代表Flashback log產生數量,DB_Date 代表資料改變數量,Redo_Date代表日誌數量,通過這3個數量可以反映出資料的活動特點,更準確的預計Flash Recovery Area的空間需求
SQL> desc v$flashback_database_stat Name Null? Type ----------------------------------------- -------- ---------------------------- BEGIN_TIME DATE END_TIME DATE FLASHBACK_DATA NUMBER DB_DATA NUMBER REDO_DATA NUMBER ESTIMATED_FLASHBACK_SIZE NUMBER
閃回資料庫的工作原理
為了實現閃回資料庫,Oracle需要記錄資料區塊的前景象before image到一種日誌中,這種日誌被命名為flashback database logs閃回日誌。當一個資料區塊首次被修改時,前台進程會將該資料區塊的before image拷貝到位於shared pool中的flashback log buffer中,RVWR進程定期地將flashback log buffer中的記錄重新整理到磁碟上。 在DBWR進程可以寫出相關髒塊到磁碟之前,DBWR必須保證該buffer header相關FBA(Flashback Byte Address)的flashback log buffer已經寫出到閃回日誌。 這被稱作 先寫閃回日誌 機制。在常規的閃回日誌維護操作中 , RVWR進程定期地插入閃回標記(flashback markers)到flashback database logs中。 閃回標記(flashback markers)的作用是在閃回資料庫是告知Oracle如何flashback 到之前的某個時間點。 在閃回操作執行過程中, Oracle 會用閃回標記(flashback markers)中的資訊來決定多大範圍的flashback database log需要用來還原資料區塊景象block image; 之後Oracle 會利用前向恢複(forward recovery)的方式把資料庫穿越到使用者指定閃回的SCN或者時間點。需要注意的是不是資料庫中的每一次block change 都會觸發before image被記錄到閃回日誌flashback log中。 如果每一次block change都記錄flashback log record 那麼閃回日誌會要比 redo log大的多!對於hot block熱塊,Oracle僅在一段時間內記錄一次block image到閃回日誌; Oracle 內部通過閃回分界線(flashback barriers)實現這一點。在常規資料庫狀態下,flashback barriers被周期性的觸發(一般為15分鐘),對應每一個閃回分界線(flashback barriers)會有一個(flashback markers)被寫出到閃回日誌。常規情況下,對於每一個被修改的資料區塊在一個閃回地區(被分界線barriers分割的地區)內僅記錄一個block image ,無論這個資料區塊在這段地區內被修改了多少次、被寫出過多少次到磁碟上。
當進行閃回時,先恢複到對應target time,即我們的之前的某個鏡像上,然後根據redo log,重新應用redo log,將資料恢複到我們指定的閃回的時間點上。從這裡可以看出,閃回資料庫是需要redo log和歸檔日誌支援的。
閃回資料庫的限制
1:閃回資料庫不能用於介質恢複
2:閃回資料庫不能用於恢複使用了shrink技術的資料檔案,不能用於恢複已經刪除的資料檔案
3:不能從重建控制檔案或者恢複的控制檔案,進行閃回資料庫,因為控制檔案中的閃回日誌資訊被到期丟棄了
4:我們不能使用閃回資料庫將資料庫閃回到執行nologging操作的時間點,因為有可能會造成資料快損壞
類比一下控制檔案恢複的情景:
[oraele@vm1 ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 27 09:13:12 2015Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> recover database using backup controlfile;ORA-00279: change 1138457 generated at 01/26/2015 16:10:27 needed for thread 1ORA-00289: suggestion :/u01/app/oraele/fast_recovery_area/ORCL/archivelog/2015_01_27/o1_mf_1_1_%u_.arcORA-00280: change 1138457 for thread 1 is in sequence #1Specify log: {<RET>=suggested | filename | AUTO | CANCEL}/u01/app/oraele/oradata/orcl/redo01.logLog applied.Media recovery complete.SQL> alter database open resetlogs;Database altered.SQL> select * from test; ID SCN---------- -------------------- 0 1121575 1 1126681 2 1138419 3 1150711SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> flashback database to scn 1138419;flashback database to scn 1138419*ERROR at line 1:ORA-38729: Not enough flashback database log data to do FLASHBACK.SQL> desc v$flashback_database_log Name Null? Type ----------------------------------------- -------- ---------------------------- OLDEST_FLASHBACK_SCN NUMBER OLDEST_FLASHBACK_TIME DATE RETENTION_TARGET NUMBER FLASHBACK_SIZE NUMBER ESTIMATED_FLASHBACK_SIZE NUMBERSQL> select olddest_flashback_scn from v$flashback_database_log;select olddest_flashback_scn from v$flashback_database_log *ERROR at line 1:ORA-00904: "OLDDEST_FLASHBACK_SCN": invalid identifierSQL> c/oldd/old 1* select oldest_flashback_scn from v$flashback_database_logSQL> /OLDEST_FLASHBACK_SCN-------------------- 1150901SQL>
SQL> select begin_time from v$flashback_database_stat;BEGIN_TIM---------27-JAN-15
在使用閃回資料庫技術或者其他閃回技術時,我們需要指定需要閃回到的scn或者時間點,為了簡化工作,可以 建立restroe point來配合資料庫閃回。
restore point分為normal restorepoint 和 guaranteed restore point兩種。normal restore point 僅僅作為scn和時間點的別名使用,無其他用途,佔用的控制檔案空間也非常小,通常無需特別關注。控制檔案會自動清除normal restore point 的資訊,清除時遵循如下規則:
(1) The most recent 2048 restorepoints are always kept in the control file, regardless of their age.
(2) Any restore point more recentthan the value of CONTROL_FILE_RECORD_KEEP_TIME is retained,regardless of how many restore points are defined.
guaranteed restore point 同樣作為scn和時間點的別名使用,但是控制檔案不會自動清除guaranteed restore point ,必須通過手工才可以刪除。另外,在不開啟flashback database的情況下,guaranteed restore point仍然可以使我們使用flashback database技術,只是此時我們只可以閃回到guaranteed restore point指定的時間點。此時,仍然會生出閃回日誌,只是閃回日誌中僅僅儲存guaranteed restore point時間點之後資料區塊第一次發生改變時的“before image”。
我們知道,flashback database 與nologging的支援不太好,可能會造成壞塊(間歇性的保持資料區塊前鏡像,需要使用redo恢複資料)。但是在沒有啟用flashback database情況下的guaranteed restore point可以很好的支援nologging,因為此時不需要使用redo日誌資料。在啟用flashback database log的情況下,如果建立guaranteed restore point,可以保證在guaranteed restore point到目前時間的任意時間點資料庫都是可以閃回的。If you enableFlashback Database and define one or more guaranteed restore points, then thedatabase performs normal flashback logging.
flashback database 可以針對某資料表空間關閉,如:
SQL> alter database flashback on;Database altered.SQL> alter tablespace users flashback off;Tablespace altered.SQL> create table test1 tablespace users as select * from test;Table created.SQL> insert into test values(1,1);1 row created.SQL> commit;Commit complete.SQL> insert into test1 select * from test;1 row created.SQL> commit;Commit complete.SQL> select current_scn from v$database;CURRENT_SCN----------- 1188094SQL> insert into test select * from test;1 row created.SQL> commit;Commit complete.SQL> insert into test1 select * from test1;1 row created.SQL> commit;Commit complete.SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> flashback database to scn 2 1188094;flashback database to scn*ERROR at line 1:ORA-38753: Cannot flashback data file 4; no flashback log data.ORA-01110: data file 4: '/u01/app/oraele/oradata/orcl/users01.dbf'SQL> alter tablespace users offline;alter tablespace users offline*ERROR at line 1:ORA-01109: database not openSQL> alter tablespace users read only;alter tablespace users read only*ERROR at line 1:ORA-01109: database not openSQL> alter database open;Database altered.SQL> alter tablespace users offline;Tablespace altered.SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 267227136 bytesFixed Size 2227504 bytesVariable Size 192938704 bytesDatabase Buffers 67108864 bytesRedo Buffers 4952064 bytesDatabase mounted.SQL> flashback database to scn 1188094;Flashback complete.SQL> alter database open resetlogs;Database altered.SQL> alter tablespace users online;alter tablespace users online*ERROR at line 1:ORA-01190: control file or data file 4 is from before the last RESETLOGSORA-01110: data file 4: '/u01/app/oraele/oradata/orcl/users01.dbf'
flashback off的資料表空間在閃回資料庫之前要offline(不參與閃回),所有資料檔案scn不一致前不能開啟資料庫。
聯機前要對offline的資料檔案做部分還原和不完全恢複,但相對也能節省時間