专注做网管的电脑公司
  • 微信公众号
  • 联系我们
  • 客服热线:13726442969
  • ORACLE【闪回】基于时间闪回数据库

    2020-12-31 08:06:29 226
    一、在scott用户下创建一张表、并记录系统时间(如果记录了系统时间,那么可以准确的闪回到指定时间点)
    SCOTT@ORA11GR2>create table fbdb_time as select sysdate as dd from dual;
    Table created.
    SCOTT@ORA11GR2>select sysdate from dual;
    SYSDATE
    -------------------
    2012-11-17 21:52:26
    SCOTT@ORA11GR2>
    二、删除scott用户,并将数据库启动到mount状态下
    SCOTT@ORA11GR2>
    conn / as sysdba
    Connected.
    SYS@ORA11GR2>drop user scott cascade;
    User dropped.
    SYS@ORA11GR2>startup mount force;
    ORACLE instance started.
    Total System Global Area  849530880 bytes
    Fixed Size                  1339824 bytes
    Variable Size             641732176 bytes
    Database Buffers          201326592 bytes
    Redo Buffers                5132288 bytes
    Database mounted.
    SYS@ORA11GR2>
    三、第一次闪回,我们不用刚刚记录的时间,我们闪回到三分钟之前,看看是什么情况
    SYS@ORA11GR2>
    flashback database to timestamp  sysdate - 3/1440;
    Flashback complete.
    SYS@ORA11GR2>
    四、此时,数据库可以启动到read only状态,查看是否闪回到我想要的时间点(即:scott用户已创建fbdb_time表以后)
    SYS@ORA11GR2>
    alter database open read only;
    Database altered.
    SYS@ORA11GR2>
    五、经验证,
    scott.fbdb_time表不存在,说明,我们闪回闪过了。
    SYS@ORA11GR2>
    select * from scott.fbdb_time;
    select * from scott.fbdb_time
                  *
    ERROR at line 1:
    ORA-00942: table or view does not exist
    SYS@ORA11GR2>
    六、重新到mount状态下,再次闪回(如果我们不知道具体的时间点的话,我们只能一次一次以read only打开数据库试验,直到我们满意为止)
    SYS@ORA11GR2>
    startup mount force;
    ORACLE instance started.
    Total System Global Area  849530880 bytes
    Fixed Size                  1339824 bytes
    Variable Size             641732176 bytes
    Database Buffers          201326592 bytes
    Redo Buffers                5132288 bytes
    Database mounted.
    SYS@ORA11GR2>
    七、由于我们记录了当时的时间,所以,我们直接闪回到那个时间点即可
    SYS@ORA11GR2>
    flashback database to timestamp to_date('2012-11-17 21:52:26','yyyy-mm-dd hh24:mi:ss');
    Flashback complete.
    SYS@ORA11GR2>
    八、再次以read only打开数据库,验证,正如我们所想的一样,回到了删除scott用户之前的状态
    SYS@ORA11GR2>
    alter database open read only;
    Database altered.
    SYS@ORA11GR2>conn scott/tiger
    Connected.
    SCOTT@ORA11GR2>conn / as sysdba
    Connected.
    SYS@ORA11GR2>select * from scott.fbdb_time;
    DD
    -------------------
    2012-11-17 21:52:09
    SYS@ORA11GR2>
    九、重启数据库,以
    resetlogs方式打开数据库,闪回数据库完成
    SYS@ORA11GR2>
    shutdown abort;
    ORACLE instance shut down.
    SYS@ORA11GR2>
    SYS@ORA11GR2>startup mount;
    ORACLE instance started.
    Total System Global Area  849530880 bytes
    Fixed Size                  1339824 bytes
    Variable Size             641732176 bytes
    Database Buffers          201326592 bytes
    Redo Buffers                5132288 bytes
    Database mounted.
    SYS@ORA11GR2>alter database open resetlogs;
    Database altered.
    SYS@ORA11GR2>
    十、小结
    如果使用了闪回数据库,往往都是在不知道具体时间的情况下,为了损失更小,我们只能一次又一次的read only打开数据库来验证结果;
    一般情况下,开启闪回数据库的生产库很少,毕竟还是比较耗资源的(特别是DML操作比较频繁的数据库);
    还有就是,如果我们使用了闪回功能,那么归档日志都重新开始了,我们之前的备份就失效了,在使用了闪回数据库后,记得做一次数据库的全备。