程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> ORACLEFLASHBACKDATABASE總結

ORACLEFLASHBACKDATABASE總結

編輯:Oracle教程

ORACLEFLASHBACKDATABASE總結


閃回數據庫(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_recovery

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /u01/app/oraele/fast_recovery_
                                                 area
db_recovery_file_dest_size           big integer 4122M
SQL> show parameter flashback

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     1440

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     5
Next log sequence to archive   7
Current 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 Production
PL/SQL Release 11.2.0.3.0 - Production
CORE    11.2.0.3.0      Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

SQL> 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 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
NO

SQL> select status from v$instance;

STATUS
------------
OPEN

SQL> 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 immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database mounted.
SQL> flashback database to scn  1121575;

Flashback complete.

SQL> alter database open read only;

Database altered.

SQL> select * from test;

no rows selected

SQL> startup mount force
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database mounted.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> shutdown immediate
ORA-01109: database not open


Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database 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 only


SQL> alter database open read only;

Database altered.

SQL> select * from test;

        ID SCN
---------- --------------------
         0 1121575

SQL> startup mount force
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database mounted.
SQL> alter database open resetlog;
alter database open resetlog
                    *
ERROR at line 1:
ORA-02288: invalid OPEN mode


SQL> 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 sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 27 09:13:12 2015

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> recover database using backup controlfile;
ORA-00279: change 1138457 generated at 01/26/2015 16:10:27 needed for thread 1
ORA-00289: suggestion :
/u01/app/oraele/fast_recovery_area/ORCL/archivelog/2015_01_27/o1_mf_1_1_%u_.arc
ORA-00280: change 1138457 for thread 1 is in sequence #1


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/u01/app/oraele/oradata/orcl/redo01.log
Log applied.
Media recovery complete.
SQL> alter database open resetlogs;

Database altered.

SQL> select * from test;

        ID SCN
---------- --------------------
         0 1121575
         1 1126681
         2 1138419
         3 1150711

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database 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                           NUMBER

SQL> 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 identifier


SQL> c/oldd/old
  1* select oldest_flashback_scn from v$flashback_database_log
SQL> /

OLDEST_FLASHBACK_SCN
--------------------
             1150901

SQL>
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
-----------
    1188094

SQL> 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 immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database 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 open


SQL> alter tablespace users read only;
alter tablespace users read only
*
ERROR at line 1:
ORA-01109: database not open


SQL> alter database open;

Database altered.

SQL> alter tablespace users offline;

Tablespace altered.

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  267227136 bytes
Fixed Size                  2227504 bytes
Variable Size             192938704 bytes
Database Buffers           67108864 bytes
Redo Buffers                4952064 bytes
Database 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 RESETLOGS
ORA-01110: data file 4: '/u01/app/oraele/oradata/orcl/users01.dbf'

flashback off的表空間在閃回數據庫之前要offline(不參與閃回),所有數據文件scn不一致前不能打開數據庫。
聯機前要對offline的數據文件做部分還原和不完全恢復,但相對也能節省時間



						

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved