标签:oracle
Oracle Study之案例--数据恢复神器Flashback(1)
Flashback:
Flashback 技术是以Undo segment中的内容为基础的, 因此受限于UNDO_RETENTON参数。要使用flashback 的特性,必须启用自动撤销管理表空间。
在Oracle 11g里又出了一个新特性:Oracle Flashback Data Archive. FDA通过将变化数据另外存储到创建的闪回归档区(Flashback Archive)中,以和undo区别开来,这样就可以为闪回归档区单独设置存储策略,使之可以闪回到指定时间之前的旧数据而不影响undo策略。
在Oracle 10g中, Flash back家族分为以下成员: Flashback Database, Flashback Drop,Flashback Query(分Flashback Query,Flashback Version Query, Flashback Transaction Query 三种) 和Flashback Table。
用户误操作数据恢复:
对于用户误操作产生的数据丢失,传统上都采用备份(逻辑备份)或物理备份(不完全恢复)来恢复数据;如果有逻辑备份,恢复相对较容易,如果没有逻辑备份,通过物理备份进行不完全恢复,需要付出很大的成本才能恢复数据;而从Oracle 9i以来,Oracle推出了Flashback得工具,可以通过此工具快速恢复因用户误操作而丢失的数据。
对于DML操作,Flashback可以通过Undo block来恢复用户产生的误操作。
1、Flashback query 特性flashback可以通过undo block查询过去时间点或scn的数据 2、Flashback version query 特性可以得到特定的表在某一个时间段内的任何修改记录 3、Flashback transaction query 特性可以限制用户在某一个事务级别上检查数据库的修改操作,适用于诊断问题、分析性能、审计事务。 4、Flashback table 特性允许oracle通过flashback table语句,将表回滚到前一个时间点或者scn上。
一、Flashback Query
Flashback 是ORACLE 自9i 就开始提供的一项特性,在9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数据,可用来进行数据比对,或者修正意外提交造成的错误数据,该项特性也被称为Flashback Query。
正如前言中所提,Flashback Query 是利用多版本读一致性的特性从UNDO 表空间读取操作前的记录数据!
什么是多版本读一致性
Oracle 采用了一种非常优秀的设计,通过undo 数据来确保写不堵塞读,简单的讲,不同的事务在写数据时,会将数据的前映像写入undo 表空间,这样如果同时有其它事务查询该表数据,则可以通过undo 表空间中数据的前映像来构造所需的完整记录集,而不需要等待写入的事务提交或回滚。
flashback query 有多种方式构建查询记录集,记录集的选择范围可以基于时间或基于scn,甚至可以同时查询出记录在undo 表空间中不同事务时的前映象。用法与标准查询非常类似,要通过flashback query 查询undo 中的撤销数据,最简单的方式只需要在标准查询语句的表名后面跟上as of timestamp(基于时间)或as of scn(基于scn)即可。as of timestamp|scn 的语法是自9iR2 后才开始提供支持。
案例分析:
1、基于时间点(As of Timestamp)
利用在undo tablespace 里已经被提交的undo block(未被覆盖),可以通过查询的方式将表里面的记录回到过去某个时间点。
1、构建测试环境 07:01:37 SQL> conn scott/tiger Connected. 07:01:41 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 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 14 rows selected. 2、DML误操作 07:01:45 SQL> delete from test ; 14 rows deleted. 07:01:59 SQL> commit; Commit complete. 07:02:03 SQL> select * from test; no rows selected 07:02:05 SQL> insert into test select * from emp where rownum <3; 2 rows created. 07:02:35 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 07:02:37 SQL> commit; Commit complete. 2、利用logminer工具查找误操作的时间点(挖掘current redo或archive log) 07:03:03 SQL> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- 1 1 0 52428800 1 YES UNUSED 0 2 1 1 52428800 1 NO CURRENT 1261015 17-AUG-11 3 1 0 52428800 1 YES UNUSED 0 07:03:20 SQL> col member for a50 07:03:23 SQL> select group#,member from v$logfile GROUP# MEMBER ---------- -------------------------------------------------- 3 /u01/app/oracle/oradata/prod/redo03.log 2 /u01/app/oracle/oradata/prod/redo02.log 1 /u01/app/oracle/oradata/prod/redo01.log 11:19:31 SQL> conn /as sysdba Connected. 11:19:35 SQL> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- 1 1 12 52428800 2 YES INACTIVE 823116 29-SEP-11 2 1 14 52428800 2 NO CURRENT 828692 29-SEP-11 3 2 9 52428800 2 YES INACTIVE 824371 29-SEP-11 4 2 11 52428800 2 NO CURRENT 828868 29-SEP-11 5 1 13 52428800 2 YES INACTIVE 828670 29-SEP-11 6 2 10 52428800 2 YES INACTIVE 828817 29-SEP-11 6 rows selected. 11:19:41 SQL> col member for a50 11:19:57 SQL> select group# ,member from v$logfile; GROUP# MEMBER ---------- -------------------------------------------------- 2 +DG1/prod/onlinelog/group_2.262.762877491 2 +RECOVERY/prod/onlinelog/group_2.258.762877501 1 +DG1/prod/onlinelog/group_1.261.762877473 1 +RECOVERY/prod/onlinelog/group_1.257.762877479 3 +DG1/prod/onlinelog/group_3.266.762877849 3 +RECOVERY/prod/onlinelog/group_3.259.762877855 4 +DG1/prod/onlinelog/group_4.267.762877859 4 +RECOVERY/prod/onlinelog/group_4.260.762877867 6 +DG1/prod/onlinelog/group_6.272.763037401 6 +RECOVERY/prod/onlinelog/group_6.262.763037407 5 +DG1/prod/onlinelog/group_5.271.763037441 GROUP# MEMBER ---------- -------------------------------------------------- 5 +RECOVERY/prod/onlinelog/group_5.261.763037613 12 rows selected. 分析current redolog: 11:20:07 SQL> execute dbms_logmnr.add_logfile(logfilename=>‘+DG1/prod/onlinelog/group_2.262.762877491‘,options=>dbms_logmnr.new); PL/SQL procedure successfully completed. 11:20:57 SQL> alter session set nls_date_format=‘yyyy-mm-dd‘; Session altered. 11:21:32 SQL> execute dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog); PL/SQL procedure successfully completed. 07:05:21 SQL> execute dbms_logmnr.end_logmnr; PL/SQL procedure successfully completed. 查看DML操作的时间点: 11:23:11 SQL> select username,scn,timestamp,sql_redo from v$logmnr_contents where seg_name=‘EMP1‘; USERNAME SCN TIMESTAMP SQL_REDO ------------------------------ ---------- ---------- -------------------------------------------------- 830293 2011-09-29 delete from "SCOTT"."EMP1" where "EMPNO" = ‘7369‘ and "ENAME" = ‘SMITH‘ and "JOB" = ‘CLERK‘ and "MGR " = ‘7902‘ and "HIREDATE" = TO_DATE(‘1980-12-17‘, ‘yyyy-mm-dd‘) and "SAL" = ‘800‘ and "COMM" IS NULL and "DEPTNO" = ‘20‘ and ROWID = ‘AAAM01AAEAAAAGEA AA‘; 3、flashback query基于时间点的查询 07:08:42 SQL> conn scott/tiger Connected. 07:08:48 SQL> select * from test as of timestamp to_timestamp(‘2011-08-17 07:01:59‘,‘yyyy-mm-dd hh24:mi:ss‘); 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 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 14 rows selected. 将查询到的数据写入到表中: 07:08:50 SQL> insert into test (select * from test as of timestamp to_timestamp(‘2011-08-17 07:01:59‘,‘yyyy-mm-dd hh24:mi:ss‘)); 14 rows created. 07:09:10 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. ---至此,数据恢复完成!
2、基于SCN(As of SCN)
---对于事务的scn亦可以通过logminer对日志分析得到!
07:09:13 SQL> conn /as sysdba Connected. 07:10:28 SQL> 07:10:28 SQL> select current_scn from v$database; CURRENT_SCN ----------- 1263945 07:10:39 SQL> conn scott/tiger Connected. 07:13:44 SQL> delete from test; 16 rows deleted. 07:13:51 SQL> commit; Commit complete. 07:13:56 SQL> select * from test as of scn 1263945 ; 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.
Oracle 事务timestamp和scn关系:
每隔5 分钟,系统产生一次系统时间标记与scn 的匹配并存入sys.smon_scn_time 表,该表中记录了最近1440个系统时间标记与scn 的匹配记录,由于该表只维护了最近的1440 条记录,因此如果使用as of timestamp 的方式则只能flashback 最近5 天内的数据(假设系统是在持续不断运行并无中断或关机重启之类操作的话)。
注意理解系统时间标记与scn 的每5 分钟匹配一次这句话,举个例子,比如scn:339988,339989 分别匹配08-05-3013:52:00 和2008-13:57:00,则当你通过as of timestamp 查询08-05-30 13:52:00 或08-05-30 13:56:59 这段时间点
内的时间时,oracle 都会将其匹配为scn:339988 到undo 表空间中查找,也就说在这个时间内,不管你指定的时间点是什么,查询返回的都将是08-05-30 13:52:00 这个时刻的数据。
查看SCN 和 timestamp 之间的对应关系:
select scn,to_char(time_dp,‘yyyy-mm-dd hh24:mi:ss‘)from sys.smon_scn_time;
15:49:45 SYS@ test3 >select scn,to_char(time_dp,‘yyyy-mm-dd hh24:mi:ss‘)from sys.smon_scn_time order by 2 desc; SCN TO_CHAR(TIME_DP,‘YY ---------- ------------------- 623337 2015-01-13 07:45:26 623229 2015-01-13 07:40:13 623128 2015-01-13 07:35:28 623009 2015-01-13 07:30:12 622906 2015-01-13 07:25:24 622799 2015-01-13 07:20:11 622695 2015-01-13 07:15:12 622580 2015-01-13 07:10:24 622474 2015-01-13 07:05:11 622390 2015-01-13 07:01:17 622161 2015-01-13 06:56:04 622063 2015-01-13 06:51:20 621954 2015-01-13 06:46:04 621855 2015-01-13 06:41:19
---未完待续
本文出自 “天涯客的blog” 博客,请务必保留此出处http://tiany.blog.51cto.com/513694/1603630
Oracle Study之案例--数据恢复神器Flashback(1)
标签:oracle
原文地址:http://tiany.blog.51cto.com/513694/1603630