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

一、Flashback Table:

对于DML的误操作,可以通过Undo block对表进行回退(两种模式:基于时间和基于SCN)

案例分析:

1、基于SCN(可以通过logminer找到DML操作的时间点和SCN)

模拟测试环境:
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
     1264179
07:16:18 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:16:23 SQL> delete from test;                                                                                                          
16 rows deleted.
07:16:50 SQL> commit;                                                                                                                   
Commit complete.

07:16:52 SQL> select * from test;                                                                                                        
no rows selected
07:16:57 SQL> insert into test select * from emp where rownum=1;                                                                         
1 row created.
07:17:17 SQL> commit;                                                                                                                    
Commit complete.
07:17:19 SQL> select * from test;                                                                                                        
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

通过flashback table回退:      
07:17:21 SQL> flashback table test to scn 1264179;                                                                                       
flashback table test to scn 1264179
                *
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled

回退table必须支持row movement:
07:17:41 SQL> alter table test enable row  movement;                                                                                     
Table altered.

07:18:01 SQL> flashback table test to scn 1264179;                                                                                       
Flashback complete.
07:18:05 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、基于timestamp(可以通过logminer找到DML操作的时间点和SCN)

05:43:31 SQL> delete from scott.emp1;
14 rows deleted.

05:44:25 SQL> flashback table scott.emp1 to timestamp to_timestamp(‘2011-03-18 04:50:00‘,‘yyyy-mm-dd hh24:mi:ss‘);
Flashback complete.

05:44:32 SQL> select * from scott.emp1;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10
14 rows selected.

二、Flashback Drop

在实际开发和维护中,我们有时候会遇到把数据表drop掉的情况。过去这种情况,我们只能通过之前保留的备份,进行不完全的备份。这样的工作量很大也很麻烦。从Oracle10g起,引入了回收站的机制,将drop掉的数据表保存在回收站中。当发现误删除的时候,可以通过回收站回收数据表。

回收站机制类似于我们在Windows上的回收站。在windows中,当我们选择删除一个文件时,本质上并没有将文件从硬盘上删除,只是将文件以一种形式改名,这样就能从回收站中看到。

Oracle的回收站也是采用同样的原理。下面我们做一个简单的实验

首先,确定系统参数。在Oracle10g中,有一个参数recyclebin,控制数据表回收站机制的启动和关闭。

用sys帐号登录,确定recyclebin参数:

SQL> show parameter recyclebin;

NAME                                TYPE       VALUE

------------------------------------ ----------- ------

recyclebin                          string     on                //当取值为on的时候,表示开启回收站功能;

案例分析:

1)06:52:29 SQL> select * from tab;                                                                                                         
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
DEPT                           TABLE
EMP                            TABLE
BONUS                          TABLE
SALGRADE                       TABLE
TEST                           TABLE
T01                            TABLE
T02                            TABLE
7 rows selected.
06:52:31 SQL> drop table t01;                                                                                                            
Table dropped.

查看回收站:
06:52:38 SQL> show recycle;                                                                                                              
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T01              BIN$qrJLbL74ZgvgQKjA8Agb/A==$0 TABLE        2011-08-17:06:52:38

--------除了system 表空间,其余表空间都有一个类似windows 回收站,在drop table,实际上把table 改名后放入recyclebin。

06:52:44 SQL> flashback table t01 to before drop;                                                                                        
Flashback complete.

06:54:05 SQL> show recycle;                                                                                                              
06:54:07 SQL>  select * from tab;                                                                                                        
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
DEPT                           TABLE
EMP                            TABLE
BONUS                          TABLE
SALGRADE                       TABLE
TEST                           TABLE
T01                            TABLE
T02                            TABLE
7 rows selected.

06:54:11 SQL> drop table t02 purge;    //purge  会彻底的删除table                                                                                                    
Table dropped.
06:54:40 SQL> show recycle;  
-----------清空recyclebin

06:54:43 SQL> drop table t01;                                                                                                            
Table dropped.
06:55:49 SQL> show recycle;                                                                                                              
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T01              BIN$qrJLbL75ZgvgQKjA8Agb/A==$0 TABLE        2011-08-17:06:55:49
06:55:51 SQL> purge recyclebin;                                                                                                          
Recyclebin purged.

06:55:57 SQL> show recycle;                                                                                                              
06:55:59 SQL> 
--------------如何恢复同一个schema 下同名的table

06:56:32 SQL> drop table test;                                                                                                           
Table dropped.
06:56:42 SQL> create table test as  select * from emp;                                                                                   
Table created.
06:56:46 SQL> select * from tab;                                                                                                         
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
DEPT                           TABLE
EMP                            TABLE
BONUS                          TABLE
SALGRADE                       TABLE
BIN$qrJLbL76ZgvgQKjA8Agb/A==$0 TABLE
TEST                           TABLE
6 rows selected.
06:56:50 SQL> show recycle;                                                                                                              
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
TEST             BIN$qrJLbL76ZgvgQKjA8Agb/A==$0 TABLE        2011-08-17:06:56:36
06:56:58 SQL> flashback table test to before drop;                                                                                       
flashback table test to before drop
*
ERROR at line 1:
ORA-38312: original name is used by an existing object
06:57:09 SQL> flashback table test to before drop rename to test_old;                                                                    
Flashback complete.
06:57:32 SQL> select * from tab;                                                                                                         
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
DEPT                           TABLE
EMP                            TABLE
BONUS                          TABLE
SALGRADE                       TABLE
TEST_OLD                       TABLE
TEST                           TABLE
6 rows selected.

flashback Drop不支持sys用户:
----system 表空间不存在recyclebin ,表直接被删除
06:57:36 SQL> conn /as sysdba                                                                                                            
Connected.
06:58:33 SQL> 
06:58:33 SQL> create table test as select * from user_tables;                                                                            
Table created.
06:58:42 SQL> drop table test;                                                                                                          
Table dropped.
06:58:46 SQL> show recycle; 

闪回表回收站——3个视图
使用方面,闪回特性还要关注两个回收站视图。user_recyclebin、all_recyclebin、dba_recyclebin。

所谓的闪回drop,就是一种对象假删除技术。当系统参数recyclebin被设置为on的时候,Oracle是开启闪回drop功能的。当对数据表使用drop的时候,Oracle并不是将对象直接删除,而是采用了对象改名。将删除的数据表进行改名(逻辑上),改为BIN$开头的一个编码。这个编码占据了原有数据表的所有资源(包括对象信息和存储信息),但是不能算成为原对象的等价体。也就是说,如果我们直接操作这个修改名,系统会报错,因为Oracle不认为这个对象是一个数据表。数据表T的数据字典信息被删除,而改名的对象信息没有加入其中。

时间: 2025-01-12 07:30:53

Oracle Study之案例--数据恢复神器Flashback(2)的相关文章

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

Oracle Study之案例--数据恢复神器Flashback(1) Flashback: Flashback 技术是以Undo segment中的内容为基础的, 因此受限于UNDO_RETENTON参数.要使用flashback 的特性,必须启用自动撤销管理表空间. 在Oracle 11g里又出了一个新特性:Oracle Flashback Data Archive. FDA通过将变化数据另外存储到创建的闪回归档区(Flashback Archive)中,以和undo区别开来,这样就可以为闪

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

Flashback Database: 案例分析: flashback database:利用flashback log 对整个database 做回退到过去的某个时间点(用于DDL 的误操作如drop 和 truncate),类似于RMAN的不完全恢复(media recovery incompelete) 因为flashback database是不完全恢复,在恢复到过去的时间点前应该对数据库备份! 1.查看flashback database是否开启 07:21:27 SQL> selec

Oracle Study之案例--Oracle 11g DataGuard Snapshot Standby

Oracle Study之案例--Oracle 11g  DataGuard Snapshot Standby Oracle 11g的Data Guard不仅仅带给我们的是Active Data Guard实时查询特性,同时还带来了另外一个新特性,这便是Snapshot Standby数据库功能,此项功能可将备库置身于"可读写状态"用于不方便在生产环境主库中测试的内容,比如模拟上线测试等任务.当备库读写状态下任务完成后,可以非常轻松的完成Snapshot Standby数据库角色切换回

Oracle Study之案例--异构平台传输表空间(Linux至AIX)

Oracle Study之案例--异构平台传输表空间(Linux至AIX) 系统架构: 可                   源    库               目标库 操作系统 Linux RH6    AIX 5.3-09 主机名 rh6(192.168.8.245) aix211(192.168.8.211) 数据版本 Oracle 11gR2 Oracle 11gR2 数据库名 prod orcl 表空间 test1 test1    可传输表空间概述 Oracle 的可传输表空

Oracle Study之案例--RMAN ORA-19921错误

Oracle Study之案例--RMAN ORA-19921错误 系统环境: 操作系统: AIX5.3 Oracle:   Oracle 10gR2 错误现象:         数据库在通过rman连接时出现以下错误: [11:01:51 [email protected]: ~]$rman target / Recovery Manager: Release 10.2.0.1.0 - Production on Mon Jan 19 11:01:53 2015 Copyright (c) 1

Oracle Study之案例--Oracle ASSM管理方式下的BITMAP

Oracle Study之案例--Oracle ASSM管理方式下的Bitmap      在基于此在LMT(Extent Local Management)下Oracle建议我们使用ASSM(Automatic Segment-Space Management),看看 Oracle doc是如何来解释ASSM的: This keyword tells Oracle that you want to use bitmaps to manage the free space with in seg

Oracle Study之案例--重建数据库控制文件

Oracle Study之案例--重建数据库控制文件 系统环境: 操作系统: Linux RH6 数据库:   Oracle 11gR2    案例分析:           数据库中所有的控制文件被意外破坏,非归档的库,在有trace备份的情况下,重建控制文件. 1.控制文件trace脚本 [[email protected] ~]$ cat crctr.sql  CREATE CONTROLFILE REUSE DATABASE "TEST3" NORESETLOGS  NOARC

Oracle Study之案例--AIX安装RAC错误之(rootpre)

Oracle Study之案例--AIX安装RAC错误之(rootpre) Oracle 10g for AIX 5309 rootpre执行时报以下错误: # ./rootpre.sh Required file ./pw-syscall32 is missing. 解决办法: 1.下载补丁号2896876,上传至服务端重新执行即可 2.以下方法没有尝试(仅作参考) Copy the "rootpre" sub-directory to a temporary location an

Oracle Study之案例--RMAN备份配置参数

Oracle Study之案例--RMAN备份配置参数 1.PARALLELISM  我们还可以通过parallelism参数来指定同时"自动"创建多少个通道:RMAN > configure device type disk parallelism 3 ; 表示启动三个通道,可以加快备份恢复的速度. 案例分析: RMAN> show all; using target database control file instead of recovery catalog CO