温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle Study之案例--数据恢复神器Flashback(3)

发布时间:2020-07-10 08:21:05 来源:网络 阅读:560 作者:客居天涯 栏目:关系型数据库

Oracle Study之案例--数据恢复神器Flashback(3)

Flashback Database:

Oracle Study之案例--数据恢复神器Flashback(3)


案例分析:

      flashback database:利用flashback log 对整个database 做回退到过去的某个时间点(用于DDL 的误操作如drop 和 truncate),类似于RMAN的不完全恢复(media recovery incompelete)

因为flashback database是不完全恢复,在恢复到过去的时间点前应该对数据库备份!
1、查看flashback database是否开启
07:21:27 SQL> select flashback_on from v$database;                                                                                       
FLASHBACK_ON
------------------
NO

2、创建recovery area
07:21:33 SQL> show parameter recover                                                                                                     
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /u01/app/oracle/flash_recovery
                                                 _area
db_recovery_file_dest_size           big integer 2G
recovery_parallelism                 integer     2                                                                                                  ---recovery area可以存储flashback log、archive log、rman backup等                      
[oracle@work ~]$ mkdir -p /disk1/recovery/prod

配置recovery area:
07:22:49 SQL> alter system set db_recovery_file_dest='/disk1/recovery/prod' scope=spfile;                                                
System altered.

在归档模式下启用flashback database:

07:25:06 SQL> alter database flashback on;                                                                                               
Database altered.

07:25:29 SQL> select flashback_on from v$database;                                                                                       
FLASHBACK_ON
------------------
YES

07:25:38 SQL> alter database open;                                                                                                       
Database altered.

案例1:flashback database恢复DDL误操作(基于SCN)

flashback database可以用于基于时间点或SCN的数据恢复(可以通过logminer来查看DDL操作的时间点)

1)模拟环境
15:18:17 SYS@ test1 >select current_scn from v$database;

CURRENT_SCN
-----------
   1264788
   
07:26:30 SQL> select * from test;                                                                                                        
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
16 rows selected.
07:26:36 SQL> drop table test purge;                                                                                                     
Table dropped.

07:27:20 SQL> create table test as select * from emp where rownum=1;                                                                     
Table created.
07:27:25 SQL> select * from test;                                                                                                        
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      
查看flashback 日志:
[oracle@work ~]$ ls /disk1/recovery/prod/PROD/flashback/
o1_mf_74q999lb_.flb

关闭数据库后,在mount状态下恢复:
07:29:22 SQL> shutdown immediate                                                                                                      
Database closed.
Database dismounted.
ORACLE instance shut down.

07:29:53 SQL> startup mount                                                                                                              
ORACLE instance started.
Total System Global Area  314572800 bytes
Fixed Size                  1219184 bytes
Variable Size              71304592 bytes
Database Buffers          239075328 bytes
Redo Buffers                2973696 bytes
Database mounted.

通过flashback database将库恢复到过去的scn:
07:30:02 SQL> flashback database to scn 1264788;                                                                                         
Flashback complete.

将database以read only方式打开,先验证下恢复是否成功,如果不成功,再从新进入mount下恢复
07:31:29 SQL> alter database open read only;                                                                                             
Database altered.

07:31:34 SQL> select * from scott.test;                                                                                                  
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
16 rows selected.

--恢复成功,重新以resetlogs的方式open database
07:31:40 SQL> shutdown immedaite                                                                                                         
SP2-0717: illegal SHUTDOWN option
07:31:45 SQL> shutdown immediate                                                                                                         
Database closed.
Database dismounted.
ORACLE instance shut down.
07:32:02 SQL> startup mount                                                                                                              
ORACLE instance started.
Total System Global Area  314572800 bytes
Fixed Size                  1219184 bytes
Variable Size              71304592 bytes
Database Buffers          239075328 bytes
Redo Buffers                2973696 bytes
Database mounted.
07:32:10 SQL> alter database open resetlogs;                                                                                             
Database altered.

验证:
07:32:25 SQL> select * from scott.test;                                                                                                  
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
16 rows selected.

因为flashback database是不完全恢复,在恢复到过去的时间点前应该对数据库备份!

案例2:基于timestamp的flashback database

04:04:28 SQL> show user;
USER is "SYS"
04:08:04 SQL> conn scott/tiger
Connected.
04:08:13 SQL> select to_char(sysdate,'yyyy-mm-dd hh34:mi:ss') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
2011-03-18 04:08:42

04:09:02 SQL> conn /as sysdba
Connected.
04:09:07 SQL> select current_scn from v$database;
CURRENT_SCN
-----------
    1437597
04:09:10 SQL> conn scott/tiger
Connected.
04:09:29 SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
EMP                            TABLE
DEPT                           TABLE
BONUS                          TABLE
SALGRADE                       TABLE
QUEST_SL_TEMP_EXPLAIN1         TABLE
EMP1                           TABLE
ERRLOG                         TABLE
PART_SALES                     TABLE
T01                            TABLE
DEPT1                          TABLE
10 rows selected.
select * from t01;
        ID NA
---------- --
         1 TM
04:10:07 SQL> insert into t01 values(2,'aa');
1 row created.
04:10:17 SQL> insert into t01 values(3,'bb');
1 row created.
04:10:26 SQL> commit;
Commit complete.
04:10:30 SQL> drop table t01;
Table dropped.

04:10:59 SQL> conn /as sysdba
Connected.
04:11:03 SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
04:11:28 SQL> startup mount
ORACLE instance started.
Total System Global Area  314572800 bytes
Fixed Size                  1219184 bytes
Variable Size             184550800 bytes
Database Buffers          125829120 bytes
Redo Buffers                2973696 bytes
Database mounted.

将database恢复到过去的时间点:
04:12:07 SQL> flashback database to timestamp to_timestamp('2011-03-18 04:10:26','yyyy-mm-dd hh34:mi:ss');
Flashback complete.
04:13:34 SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
04:14:10 SQL> alter database open noresetlogs;
alter database open noresetlogs
*
ERROR at line 1:
ORA-01610: recovery using the BACKUP CONTROLFILE option must be done

04:14:18 SQL> alter database open resetlogs;
Database altered.

04:15:35 SQL> select * from v$log;
    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- -------------------
         1          1          0   52428800          2 YES UNUSED                       0
         2          1          0   52428800          2 YES UNUSED                       0
         3          1          0   52428800          2 YES UNUSED                       0
         4          1          0   52428800          2 YES UNUSED                       0
         5          1          0   52428800          2 YES UNUSED                       0
         6          1          1   52428800          2 NO  CURRENT                1437637 2011-03-18 04:14:27
6 rows selected.
04:15:59 SQL> conn scott/tiger
Connected.
04:16:18 SQL> select * from t01;
        ID NA
---------- --
         1 TM
04:34:35 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
04:34:37 SQL> select * from v$flashback_database_log;
OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI RETENTION_TARGET FLASHBACK_SIZE TIMATED_FLASHBACK_SIZE
-------------------- ------------------- ---------------- -------------- ----------------------             
1436931 2011-03-18 03:51:43             1440        8192000           115703808


[oracle@rh7 ~]$ sqlplus '/as sysdba'
SQL*Plus: Release 11.2.0.1.0 Production on Thu Jan 15 11:38:34 2015
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to an idle instance.
11:41:52 SYS@ test1 >select name,FLASHBACK_ON from v$database;
NAME      FLASHBACK_ON
--------- ------------------
TEST1     YES
Elapsed: 00:00:00.00
11:38:34 SYS@ test1 >startup
ORACLE instance started.
Total System Global Area  523108352 bytes
Fixed Size                  1337632 bytes
Variable Size             188745440 bytes
Database Buffers          327155712 bytes
Redo Buffers                5869568 bytes
Database mounted.
Database opened.

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

OLD_FLHBCK_SCN OLD_FLHBCK_T  RETE_TRGT FLHBCK_SIZ EST_FLHBCK_SIZE
-------------- ------------ ---------- ---------- ---------------
      11898580 10-JAN-15          1440 305.429688      266.601563
      
--查看闪回
11:40:15 SYS@ test1 >select * from v$flashback_database_stat;
BEGIN_TIME   END_TIME     FLASHBACK_DATA    DB_DATA  REDO_DATA ESTIMATED_FLASHBACK_SIZE
------------ ------------ -------------- ---------- ---------- ------------------------
15-JAN-15    15-JAN-15            573440      24576     109056                        0
--查看sga中分配的闪回空间大小
11:40:45 SYS@ test1 >select * from v$sgastat where name like 'flashback%';
POOL         NAME                            BYTES
------------ -------------------------- ----------
shared pool  flashback generation buff     4149140
shared pool  flashback_marker_cache_si        9196


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI