ITPub博客

首页 > 数据库 > Oracle > 基于时间戳闪回数据库

基于时间戳闪回数据库

原创 Oracle 作者:skyin_1603 时间:2016-10-03 23:19:23 0 删除 编辑
我的上一篇博客讲述基于系统改变号SCN闪回数据库,在这里再讲述一下基于时间戳的闪回数据库技术。更多的情况下,
基于时间戳的闪回数据库更多使用,我在前面的一篇博文中也有所提到,基于时间戳(timestamp)与基于SCN两种的闪回技术,都可以相互
转换,转换函数为:timestamp_to_scn() 与 scn_to_timestamp(),只要便于知道哪一个为先,就用哪一种。
以下让我们一起进入Scott用户创建一个实验表来验证基于时间戳的闪回数据库技术:

1、登录Scott用户:
conn scott/tiger0520
2、创建实验表,插入数据并查询:

SQL> create table fbdb_time as select * from fbdb_scn where 1=2;

Table created.
第一条数据

SQL> insert  into  fbdb_time  select  1  as

  2  id,dbms_flashback.get_system_change_number as scn,

  3  sysdate as dd from dual;     

第二条数据 

SQL> insert  into  fbdb_time  select  2  as

  2  id,dbms_flashback.get_system_change_number as scn,sysdate as dd from dual;

1 row created.

SQL> commit;

Commit complete.

 第三条数据

SQL> insert  into  fbdb_time  select  3  as

  2  id,dbms_flashback.get_system_change_number as scn,sysdate as dd from dual;

1 row created.

SQL> commit;

Commit complete.

SQL> select * from fbdb_time;

        ID        SCN DD

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

         1     781596 2016-10-02 10:24:18

         2     781618 2016-10-02 10:25:24

         3     781629 2016-10-02 10:25:45
3、同样的,删除用户Scott模拟损坏数据库:

SQL> drop user scott cascade;

User dropped.

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL>

4、打开数据库到mount状态,并逐步验证数据库的恢复状况:

SQL> startup mount;

ORACLE instance started.


Total System Global Area  835104768 bytes

Fixed Size                  2257840 bytes

Variable Size             587205712 bytes

Database Buffers          243269632 bytes

Redo Buffers                2371584 bytes

Database mounted.

SQL>flashback database to timestamp sysdate-15/1440 ;

Flashback complete.

SQL> alter database open read only;

此时,查看数据是否闪回到预期的状态

SQL> select * from fbdb_time;

        ID        SCN DD

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

         1     781596 2016-10-02 10:24:18

此时发现,还没有完全恢复
我们继续关闭数据库,再次进行恢复

SQL> shutdown abort;

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.


Total System Global Area  835104768 bytes

Fixed Size                  2257840 bytes

Variable Size             587205712 bytes

Database Buffers          243269632 bytes

Redo Buffers                2371584 bytes

Database mounted.
闪回数据库到时间点为2016-10-02 10:25:30,开启到只读状态,并查询数据:

SQL> flashback database to timestamp   

  2  to_date('2016-10-02 10:25:30','yyyy-mm-dd hh24:mi:ss');

Flashback complete.

SQL> alter database open read only;

SQL> select * from fbdb_time;

        ID        SCN DD

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

         1     781596 2016-10-02 10:24:18

         2     781618 2016-10-02 10:25:24

这时候,比上一次的闪回,查询到多了一条数据,这样算尽量地恢复完整,降低数据的损失率
这下,我们就放心再次关闭数据库,闪回到时间点为2016-10-02 10:25:30时的状态,并以

alter database open RESETLOGS方式打开数据库

SQL> shutdown abort;

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.


Total System Global Area  835104768 bytes

Fixed Size                  2257840 bytes

Variable Size             587205712 bytes

Database Buffers          243269632 bytes

Redo Buffers                2371584 bytes

Database mounted.


SQL> alter database open RESETLOGS;

Database altered.

SQL> select * from scott.fbdb_time;

       ID        SCN DD

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

         1     781596 2016-10-02 10:24:18

         2     781618 2016-10-02 10:25:24


这样,多次验证在没有的数据丢失的情况下,我们可以以RESETLOGS方式打开数据库。

注意:
基于事件的闪回一般都没有准确的闪回时间点,那么就需要我们做到尽量精确,把数据的损失降低到
以 最小,那么就需要我们多次的重复以 read only  方式打开数据库。就像一个形容包子馅儿小的笑话说的
那样:“吃包子,第一口没吃到馅儿,第二口,过去了”。我们基于时间的闪回数据库也是一样,不要冲动
接 的大约一个时间闪回,闪回完成后就直接 resetlogs  打开,只要以 resetlogs  打开后,就不可逆了,
的 作为合格的 DBA ,一定要把损失控制到最小,所以,要多次以 read only  方式 打开验证才是上策。




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2125842/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-09-06

  • 博文量
    231
  • 访问量
    215103