当前位置: 代码迷 >> 综合 >> Flashback database--闪回数据库
  详细解决方案

Flashback database--闪回数据库

热度:64   发布时间:2024-01-17 09:41:05.0

        Flashback Database提供数据库级别的恢复,可以将整个数据库恢复到指定的时间点。

Flashback Database引入Flashback Logs,日志记录操作执行前要修改的数据,即数据块的前映象。这些信息被写入专用存储区Flash Recovery Area

一、实现Flashback Database的必要条件:

*数据库必须处于归档模式

*数据库必须指定了Flash Recovery Area

Flashback Recovery Area由两个初始化参数控制:

1)DB_RECOVERY_FILE_DEST:指定FRA的存储路径,可以指定一个文件系统下的路径,或者ASM磁盘组,但是不能指向裸设备。

2)DB_RECOVERY_FILE_DEST_SIZE:指定FRA的最大可用空间。

 

SYS@orcl>show parameter db_recovery_file                   ---查看参数

 

NAME                                     TYPE                                                                         VALUE

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

db_recovery_file_dest                    string        /u01/app/oracle/flash_recovery_area

db_recovery_file_dest_size                            biginteger 8G

 

SYS@orcl>alter system setdb_recovery_file_dest_size=3G scope=both;                 ---设置可用空间大小,先设置大小,再设置路径

SYS@orcl>alter system set db_recovery_file_dest ='/u01/app/oracle/flash_recovery_area'     ---设置恢复区路径

 

*数据库必须启动Flashback Database

select flashback_on v$database  ---查看是否启用Flashback Database特性,NO是没有启用,YES是启用。

alter database flashback on  --数据库必须在mount状态才能启用FlashbackDatabase特性,同时数据库已经打开归档模式。

alter database flashback off --关闭flashback database特性,数据库也必须在mount状态下才能关闭。

 

开启数据库闪回功能步骤:

SYS@orcl>shutdown immediate;                   ---关闭数据库

Database closed.

Database dismounted.

ORACLE instance shutdown.


SYS@orcl>startup mount;                                ---启动数据库到mount状态

ORACLE instancestarted.

 

Total System GlobalArea  524288000 bytes

FixedSize                    2022080 bytes

VariableSize                  188745024 bytes

DatabaseBuffers          327155712 bytes

RedoBuffers                    6365184 bytes

Database mounted.

SYS@orcl>alter database archivelog;                   ---开启日志归档模式

 Database altered.


SYS@orcl>alter database flashback on;               ---开启数据库闪回

 Database altered.


 SYS@orcl>alter database open;                             ---设置数据库到open状态

 Database altered.


*DB_FLASHBACK_RETENTION_TARGET参数

该参数用来控制flashbacklogs数据保留的时间,默认是1440分钟,也就是24小时。如果保留时间设置比较长,要注意db_recovery_file_dest_size的空间是否足够。

 

SYS@orcl>show parameter flashback

 NAME                                     TYPE        VALUE

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

db_flashback_retention_target            integer         1440

 

SYS@orcl>alter system setdb_flashback_retention_target=2880;                    ---设置保留时间是48小时

 

*启用Force Logging

Force Loggin用来强制所有操作都写入重做日志,否则Nologging方式下插入的数据,flashback database命令也许能正确执行,但这部分数据可能无法恢复,建议将数据库处于Force Logging模式。

SYS@orcl>select  force_logging from v$database;              --查询是否启用Force Logging

    

SYS@orcl>alter  database force logging;                         --将数据库置于Force Logging模式

 

二、Flashback database相关的视图

V$FLASHBACK_DATABASE_LOG

SYS@orcl>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

 

OLDEST_FLASHBACK_SCN指的闪回数据库到最低的scn号,不能闪回到此scn号之前的时刻。OLDEST_FLASHBACK_TIME则是此scn对应的时间。


SYS@orcl>select  oldest_flashback_scn oldest_scn, to_char(oldest_flashback_time,'yyyy-mm-ddhh24:mi:ss') oldest_time from v$flashback_database_log;

 OLDEST_SCNOLDEST_TIME

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

   1224280 2012-11-04 13:20:08

 

V$FLASHBACK_DATABASE_STAT

SYS@orcl>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

 

V$FLASHBACK_DATABASE_STAT视图显示闪回数据时记录的状态信息。

 

三、制约因素

闪回点之间不允许出现表空间删除,数据文件收缩,控制文件重建。

 

四、闪回数据库例子

例子演示删除scott用户的emp表,之后scott创建一个test表,再往里面插入1条数据,然后把数据库闪回到删除emp表前一时刻,找回被删除的emp表,之后建立的test表和其中的数据仍然保留。

 

[oracle@oraedu~]$ sqlplus / as sysdba

SQL*Plus: Release10.2.0.1.0 - Production on Sun Nov 4 14:16:44 2012 2012

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

 

Connected to:

Oracle Database 10gEnterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

 

SYS@orcl>select flashback_on,log_mode,current_scnfrom v$database;      ---查询数据库是否启用数据库闪回和归档模式,以及系统的scn

 

FLASHBACK_ON          LOG_MODE        CURRENT_SCN                                                   ---结果显示数据库闪回和归档模式都已启用

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

YES                          ARCHIVELOG                             1245412

 

 /*

--或者查询出当时操作的时间,闪回到当时的时刻。

SYS@orcl>select  to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') current_time from dual;

 CURRENT_TIME

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

2012-11-04 14:18:44

 */


 SYS@orcl>select count(*) from scott.emp;          ---查询scott用户的emp表记录数

   COUNT(*)

----------

14

 

  SYS @orcl>drop table scott.emp purge;                       --- 删除 scott 用户的 emp

 Table dropped.


 SYS@orcl>select count(*) from scott.emp;

select count(*) fromscott.emp

                          *

ERROR at line 1:

ORA-00942: table orview does not exist

 

SYS@orcl>create table scott.test as select * fromscott.dept;          ---创建scott用户的test表并插入数据

 Table created.


 SYS@orcl>select  count(*) from scott.test;

   COUNT(*)

----------

 4


 SYS@orcl>shutdown immediate;                   ---关闭数据库,准备进行数据库闪回操作

 Database closed.

Database dismounted.

ORACLE instance shutdown.

 

SYS@orcl>startup mount;                 ---启动数据库到mount状态

ORACLE instancestarted.

 Total System GlobalArea  524288000 bytes

FixedSize                    2022080 bytes

VariableSize                  176162112 bytes

DatabaseBuffers          339738624 bytes

RedoBuffers                    6365184 bytes

Database mounted.

 

SYS@orcl>flashback database to scn 1245412;              ---闪回数据库到之前查询到的scn

 Flashback complete.

 

/*     也可以闪回到之前查询的timestamp

SYS@orcl> flashback database to timestampto_timestamp('2012-11-04 14:18:44','yyyy-mm-dd hh24:mi:ss');

 */

 

SYS@orcl>alter database open read only;                   --打开数据库到只读状态,查询emp是否闪回,同时防止其他用户登陆数据库进行数据操作。

 Database altered.

 

SYS@orcl>select count(*) from scott.emp;                 ---查询结果显示emp表已经闪回

   COUNT(*)

----------

14

 

SYS@orcl>select count(*) from scott.test;       

 select count(*) fromscott.test

                           *

ERROR at line 1:

ORA-00942: table orview does not exist

  ---由于数据库闪回到创建test表之前,所以删除emp后创建的test表不存在,这时我们要利用exp命令导出emp表再运行recoverdatabase命令,重新应用数据库产生的redo,将数据库修复到flashback database操作前的状态,然后再通过导入的方式,将emp表重新导入,这样对现有数据的影响最小,不会有数据丢失。

 

SYS@orcl>exit

Disconnected fromOracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

[oracle@oraedu ~]$ exp scott/oracle tables=emp file=scott.emp.bak             ---导出emp

 

Export: Release10.2.0.1.0 - Production on Sun Nov 4 14:25:30 2012

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

 

Connected to: OracleDatabase 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

Export done inUS7ASCII character set and AL16UTF16 NCHAR character set

server uses AL32UTF8character set (possible charset conversion)

 

About to exportspecified tables via Conventional Path ...

. . exportingtable                            EMP         14 rows exported

EXP-00091: Exportingquestionable statistics.

EXP-00091: Exportingquestionable statistics.

Export terminatedsuccessfully with warnings.

 

SYS@orcl>shutdown immediate;                           ---再次关闭数据库,并startupmount状态,进行数据库恢复

Database closed.

Database dismounted.

ORACLE instance shutdown.


SYS@orcl>startupmount;

ORACLE instancestarted.

 

Total System GlobalArea  524288000 bytes

FixedSize                    2022080 bytes

VariableSize                  180356416 bytes

DatabaseBuffers          335544320 bytes

RedoBuffers                    6365184 bytes

Database mounted.

SYS@orcl>recover database;                              ---应用redo log进行数据库恢复

Media recoverycomplete.


SYS@orcl>alter database open read only;                         ---打开数据库到只读状态,查询test表是否应用redo log进行创建

 Database altered.

 

SYS@orcl>select count(*) from scott.test;                     ---test表已经创建并插入数据

   COUNT(*)

----------

 4

 

SYS@orcl>select count(*) from scott.emp;                

select count(*) fromscott.emp

                           *

ERROR at line 1:

ORA-00942: table orview does not exist

 

 ---执行recover database后由于应用了scn=1245240之后的redo log,所以emp表又被删除,这时要把刚才导出的emp表再导入数据库。

 

SYS@orcl>shutdown immediate;                    ---数据库处于read only模式不允许插入数据,需要再次关闭数据库,并正常打开,准备导入emp表。

Database closed.

Database dismounted.

ORACLE instance shutdown.


SYS@orcl>startup;

ORACLE instancestarted.

 

Total System GlobalArea  524288000 bytes

FixedSize                    2022080 bytes

VariableSize                  192939328 bytes

DatabaseBuffers          322961408 bytes

RedoBuffers                    6365184 bytes

Database mounted.

Database opened.

 

SYS@orcl>exit

Disconnected fromOracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

[oracle@oraedu ~]$ imp scott/oracle tables=emp file=scott.emp.bak      ---导入之前备份的emp

 

Import: Release10.2.0.1.0 - Production on Sun Nov 4 14:31:45 2012

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

 

Connected to: OracleDatabase 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

 

Export file createdby EXPORT:V10.02.01 via conventional path

import done inUS7ASCII character set and AL16UTF16 NCHAR character set

import server usesAL32UTF8 character set (possible charset conversion)

. importing SCOTT'sobjects into SCOTT

. importing SCOTT'sobjects into SCOTT

. . importingtable                         "EMP"         14 rowsimported

IMP-00089: Finegrain policy "MYPOLICY1" not recreated on table/view "EMP"

About to enableconstraints...

Import terminatedsuccessfully with warnings.

 

[oracle@oraedu~]$ sqlplus / as sysdba

 SQL*Plus: Release10.2.0.1.0 - Production on Sun Nov 4 14:32:31 2012

 

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

 

Connected to:

Oracle Database 10gEnterprise Edition Release 10.2.0.1.0 - 64bit Production

With thePartitioning, OLAP and Data Mining options

 

SYS@orcl>select count(*) from scott.emp;                          ---emp表数据已找回

   COUNT(*)

----------

14

 

SYS@orcl>select count(*) from scott.test;                          ---同时test表数据也存在

   COUNT(*)

----------

 4

 

    执行完flashback database命令后有两种方式修复数据库

1)直接alter database open resetlogs打开数据库,当然,指定scn之后产生的数据都会丢失。

2)另一种方式先执行alter database open read only命令以read only模式打开数据,然后通过逻辑导出的方式将误操作涉及表的数据导出,再执行recover database命令以重新应用数据库产生的redo,将数据库修复到flashbackdatabase操作前的状态,然后再通过逻辑导入的方式,将之前误操作的表重新导入,这样对现有数据的影响最小,不会有数据丢失。

    本例中使用的是第二种方式修复数据库。

  相关解决方案