文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

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


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯