oracle闪回

in oracle-flashback •  7 years ago 

闪回数据库database
3.1.3 flashback database的配置
flashback database要求数据库必须处于归档模式,且闪回之后必须使用resetlogs打开数据库。
a.查看数据库的归档模式及闪回是否启用
SQL> select log_mode,open_mode,flashback_on from v$database;

    LOG_MODE     OPEN_MODE  FLASHBACK_ON
    ------------ ---------- ------------------
    ARCHIVELOG   READ WRITE   NO             

--FLASHBACK_ON为NO,则表示闪回特性尚未启用

b.查看及设置闪回目录、闪回目录空间大小等
    --下面查看恢复目录及恢复目路分配的大小
    --可以使用alter system set db_recovery_file_dest 来设置新路径
    --可以使用alter system set db_recovery_file_dest_size来设定新的大小
    SQL> show parameter db_recovery  

    NAME                   TYPE        VALUE
    ------------------------------------ ----------- ------------------------------
    db_recovery_file_dest       string      /u01/app/oracle/flash_recovery
                                                     _area
    db_recovery_file_dest_size           big integer 2G     
    
c.设置闪回保留目标生存期
    SQL> show parameter db_flashback      --缺省为分钟,即小时

    NAME                    TYPE      VALUE
    ------------------------------------  -----------  ------------------------------
    db_flashback_retention_target  integer     1440
                
    SQL> alter system set db_flashback_retention_target=30;                                 --设定保留时间为半小时
    
d.在mount状态下来启用flashback,如在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.
    
    --一致性关闭数据库后,在mount状态下设置flashback
    SQL>startup mount exclusive;
    
    SQL> select status from v$instance;

    STATUS
    ------------
    MOUNTED         

    SQL> alter database flashback on;     

--开启闪回数据库功能

    SQL> ho ps -ef | grep rvw             

--可以看到新增了后台进程rvwr
oracle 3563 1 0 12:12 ? 00:00:00 ora_rvwr_orcl

    --下面查看闪回区分配的大小为大约M,闪回分钟以内的数据则需要M左右的空间
    --注意列oldest_flashback_time说明了允许返回的最早的时间点
    SQL>select oldest_flashback_scn,old_flhbck_scn,oldest_flashback_time old_flhbck_tim,
      2  retention_target rete_trgt,flashback_size/1024/1024 flhbck_siz,
      3  estimated_flashback_size/1024/1024 est_flhbck_size
      4  from v$flashback_database_log;

    OLD_FLHBCK_SCN OLD_FLHBC  RETE_TRGT FLHBCK_SIZ EST_FLHBCK_SIZE
    -------------- --------- ---------- ---------- ---------------
            915137 24-OCT-10         30     7.8125      11.2519531

    SQL> select * from v$flashback_database_stat;   --查看闪回

    BEGIN_TIM END_TIME  FLASHBACK_DATA    DB_DATA  REDO_DATA ESTIMATED_FLASHBACK_SIZE
    --------- --------- -------------- ---------- ---------- ------------------------
    24-OCT-10 24-OCT-10        7905280   86802432   96329728                        0

    SQL> select * from v$sgastat where name like 'flashback%';       --查看sga中分配的闪回空间大小

    POOL         NAME                            BYTES
    ------------ -------------------------- ----------
    shared pool  flashback generation buff     3981204
    shared pool  flashback_marker_cache_si        9196
                    
    SQL> ho ls -hlt $ORACLE_BASE/flash_recovery_area/ORCL/flashback  --查看生成的闪回日志
    total 7.9M
    -rw-r----- 1 oracle oinstall 7.9M Oct 24 12:37 o1_mf_6d7dkogw_.flb。

3.2 使用flashback database闪回数据库
步骤(前提归档日志可用)
关闭数据库
启动数据库到mount状态(exclusive模式)
闪回至某个时间点,SCN或log sequence number
使用resetlogs打开数据库
1.使用sqlplus实现闪回
可以接受一个时间标记或一个系统改变号实参
sqlplus几种常用的闪回数据库方法
FLASHBACK [STANDBY] DATABASE [<database_name>] TO [BEFORE] SCN <system_change_number> --基于SCN闪回
FLASHBACK [STANDBY] DATABASE [<database_name>] TO [BEFORE] TIMESTMP <system_timestamp_value>--基于时间戳闪回
FLASHBACK [STANDBY] DATABASE [<database_name>] TO [BEFORE] RESTORE POINT <restore_point_name>--基于时点闪回
如下面的示例:
SQL> flashback database to timestamp('2018-04-10 13:04:30','yyyy-mm-dd hh24:mi:ss');
SQL> flashback database to scn 918987;
SQL> flashback database ro restore point b1_load;

    a.基于时间戳闪回
        SQL> select count(1) from usr1.tb1;   --查询用户usr1下表tb1中的记录数

          COUNT(1)
        ----------
            404944
        
        SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') tm from dual;  --获得系统当前的时间

        TM
        -------------------
        2010-10-24 13:04:30

        SQL> drop user usr1 cascade;     --删除帐户usr1,同时帐户usr1下的所有对象将被删除

        SQL> conn scott/tiger;           --使用scott 帐户登陆

        SQL> create table tb_emp as select * from emp;  --新创建表tb_emp

        SQL> shutdown immediate;        --关系系统

        SQL> startup mount ;             --重新到mount状态

        SQL> flashback database to timestamp  --实施闪回
          2  to_timestamp('2010-10-24 13:04:30','yyyy-mm-dd hh24:mi:ss') ;          
        
        SQL> alter database open resetlogs; 
        
        SQL> select count(1) from usr1.tb1;    --帐户usr1及其对象tb1被成功闪回

          COUNT(1)
        ----------
            404944

        SQL> select count(1) from scott.tb_emp;   --闪回成功后,在闪回点之后修改的数据全部丢失
        select count(1) from scott.tb_emp
                                   *
        ERROR at line 1:
        ORA-00942: table or view does not exist         
        
    b.基于SCN号闪回          
        SQL> select current_scn from v$database;   --获得当前的SCN号

        CURRENT_SCN
        -----------
            918987

        SQL> drop table usr1.tb1;   --删除用户usr1下的表tb1
    
        SQL> alter system checkpoint;    --手动执行检查点

        SQL>  select file#,checkpoint_change# from v$datafile;

             FILE# CHECKPOINT_CHANGE#
        ---------- ------------------
                 5             921478

        SQL> shutdown abort;    
        
        SQL> startup mount;

        SQL> flashback database to scn 918987;

        SQL> select count(1) from usr1.tb1;

          COUNT(1)
        ----------
            404944

    c.基于时点闪回
        SQL> create table t(id int,col varchar2(20));   --创建表t

        SQL> insert into t values(1,'ABC');

        SQL> insert into t values(2,'DEF');

        SQL> commit;

        SQL> create restore point bef_damage;    --创建闪回点

        SQL> insert into t values(3,'GHI');

        SQL> select ora_rowscn,id,col from t;   --查看表t的记录

        ORA_ROWSCN         ID COL
        ---------- ---------- --------------------
           1874406          1 ABC
           1874406          2 DEF
           1874406          3 GHI           
    
        SQL> shutdown immediate;
        
        SQL> startup mount exclusive;
        
        SQL> flashback database to restore point bef_damage;  --实施时点闪回
        
        SQL> alter database open resetlogs;
        
        SQL> select * from t;   --闪回成功后,闪回点之后的数据丢失

                ID COL
        ---------- --------------------
                 1 ABC
                 2 DEF
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @wjwyjy! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

3 years on Steem - The distribution of commemorative badges has begun!
Happy Birthday! The Steem blockchain is running for 3 years.
Vote for @Steemitboard as a witness to get one more award and increased upvotes!