oracle 闪回
原文地址:https://www.cnblogs.com/rangle/p/8039282.html
Oracle 9i 开始支持闪回,Oracle10g开始全面支持闪回功能,Oracle11g有所完善,为大家快速的恢复数据,查询历史数据提供了很大的便捷方法。
本文主要对Oracle常用闪回使用做些详细介绍,其中对于不常用的事务和版本闪回,这里就不做介绍
一、Oracle闪回概述
闪回级别 | 闪回场景 | 闪回技术 | 对象依赖 | 影响数据 |
数据库 | 表截断、逻辑错误、其他多表意外事件 | 闪回DATABASE | 闪回日志、undo | 是 |
DROP | 删除表 | 闪回DROP | 回收站(recyclebin) | 是 |
表 | 更新、删除、插入记录 | 闪回TABLE | 还原数据,undo | 是 |
查询 | 当前数据和历史数据对比 | 闪回QUERY | 还原数据,undo | 否 |
版本查询 | 比较行版本 | 闪回Version Query | 还原数据,undo | 否 |
事务查询 | 比较 | 闪回Transaction Query | 还原数据,undo | 否 |
归档 | DDL、DML | 闪回Archive | 归档日志 | 是 |
二、Oracle闪回使用详解
1、闪回开启
(1)开启闪回必要条件
a.开启归档日志
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/U01/app/oracle/oradata/testdb/arch
Oldest online log sequence 844
Next log sequence to archive 846
Current log sequence 846
##如未开启,在mount状态执行alter database archivelog;
b.设置合理的闪回区
db_recovery_file_dest:指定闪回恢复区的位置
db_recovery_file_dest_size:指定闪回恢复区的可用空间大小
db_flashback_retention_target:指定数据库可以回退的时间,单位为分钟,默认1440分钟(1天),实际取决于闪回区大小
(2)检查是否开启闪回
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
NO
(3)开启闪回
a.开启归档
mount状态:alter database archivelog;
b.设置闪回区
SQL> alter system set db_recovery_file_dest='/home/U01/app/oracle/fast_recovery_area' scope=both;
System altered.
SQL> alter system set db_recovery_file_dest_size=60G scope=both;
System altered.
SQL> alter system set db_flashback_retention_target=4320 scope=both;
System altered.
c.开启flashback (10g在mount开启)
SQL> alter database flashback on;
Database altered.
(4)确定闪回开启
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
(5)关闭闪回
SQL> alter database flashback off;
Database altered.
2、闪回使用
(1)闪回查询
闪回查询主要是根据Undo表空间数据进行多版本查询,针对v$和x$动态性能视图无效,但对DBA_、ALL_、USER_是有效的
a.闪回查询
允许用户查询过去某个时间点的数据,用以重构由于意外删除或更改的数据,数据不会变化。
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> delete from scott.dept where deptno=40;
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from scott.dept as of timestamp sysdate-10/1440;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from scott.dept as of timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss');
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * from scott.dept as of scn 16801523;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
b.闪回版本查询
用于查询行级数据库随时间变化的方法
c.闪回事务查询
用于提供查看事务级别数据库变化的方法
(2)闪回表(update/insert/delete)
闪回表就是对表的数据做回退,回退到之前的某个时间点,其利用的是undo的历史数据,与undo_retention设置有关,默认是14400分钟(1天)
同样,sys用户表空间不支持闪回表,要想表闪回,需要允许表启动行迁移(row movement)
闪回表示例:
SQL> flashback table scott.dept to timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss');
flashback table scott.dept to timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss')
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled
SQL> select row_movement from dba_tables where table_name='DEPT' and owner='SCOTT';
ROW_MOVE
--------
DISABLED
SQL> alter table scott.dept enable row movement;
Table altered.
SQL> flashback table scott.dept to timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss');
Flashback complete.
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> alter table scott.dept disable row movement;
Table altered.
(3)闪回DROP(drop table)
当一个表被drop掉,表会被放入recyclebin回收站,可通过回收站做表的闪回。表上的索引、约束等同样会被恢复
不支持sys/system用户表空间对象,可通过alter system set recyclebin=off;关闭回收站功能
闪回DROP示例:
SQL> select * from t ;
ID NAME
---------- ---------------------------------------
1
2
3
4
30
SQL> drop table t;
Table dropped.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T BIN$YEh2QcvZdJLgUxyAgQpnVQ==$0 TABLE 2017-12-14:15:02:06
SQL> flashback table t to before drop;
Flashback complete.
SQL> select * from t;
ID NAME
---------- -------------------------------------
1
2
3
4
30
备注:即使不开始flashback,只要开启了recyclebin,那么就可以闪回DROP表。
但如果连续覆盖,就需要指定恢复的表名,如果已经存在表,则需要恢复重命名。
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T BIN$YEh2QcvddJLgUxyAgQpnVQ==$0 TABLE 2017-12-14:15:07:54
T BIN$YEh2QcvcdJLgUxyAgQpnVQ==$0 TABLE 2017-12-14:15:07:27
SQL> flashback table "BIN$YEh2QcvcdJLgUxyAgQpnVQ==$0" to before drop ;
Flashback complete.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T BIN$YEh2QcvddJLgUxyAgQpnVQ==$0 TABLE 2017-12-14:15:07:54
SQL> flashback table t to before drop rename to tt;
Flashback complete.
(4)闪回数据库(truncate/多表数据变更)
数据库闪回必须在mounted状态下进行,基于快照的可以再open下进行闪回库
闪回数据库主要是将数据库还原值过去的某个时间点或SCN,用于数据库出现逻辑错误时,需要open database resetlogs
a.全库闪回
数据库闪回示例
SQL> select * from scott.EMP;
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.
SQL> truncate table scott.EMP;
Table truncated.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 9.4067E+10 bytes
Fixed Size 2263936 bytes
Variable Size 9395242112 bytes
Database Buffers 8.4557E+10 bytes
Redo Buffers 112766976 bytes
Database mounted.
SQL> flashback database to timestamp to_timestamp('2017-12-14 14:12:46','yyyy-mm-dd HH24:MI:SS');
Flashback complete.
SQL> alter database open resetlogs;
Database altered.
SQL> select * from scott.emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17