分类: Oracle
2012-09-20 09:46:32
oracle Flashback详解
(1)使用flashback必须将初始化参数undo_management设置为auto,并且必须合理设置初始化参数undo_retention;
(2)9i只能实现行级恢复,10g能flashback table、flashback database;
(3)使用flashback查询实现行级恢复:
●select * from aaauser as of timestamp sysdate-1/4
●select * from aaauser as of scn xxxxx;
(4)使用flashback table恢复表到先前状态,必须先激活表行移动特性:
●alter table aaauser enable row movement;
●flashback table aaauser to timestamp sysdate-1/8;
(5)使用flashback table恢复被删除表:
●flashback table aaauser to before drop;
●flashback table aaaaa to before drop rename to aaaaab;(改名)
Flashback Drop does not work for tables that:
– Reside in the SYSTEM tablespace
– Use fine-grained auditing or Virtual Private Database
– Reside in a dictionary-managed tablespace
– Have been purged, either by manual purging or automatic purging under space pressure
* The following dependencies are not protected:
– Bitmap-join indexes
– Materialized view logs
– Referential integrity constraints
– Indexes dropped before tables
(6)使用flashback database恢复数据库到先前状态:
●限制:1、数据库必须处于ARCHIVELOG模式;
2、必须配置初始化参数db_flashback_retention_target控制可以恢复到的最早时间点;
●当激活数据库的flashback特征时,要求数据库必须处于MOUNT状态;
1、startup mount;
2、alter database flashback on;
3、flashback database to scn xxx;
4、alter database open resetlogs;
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT EXCLUSIVE;
SQL> ALTER SYSTEM SET DB_FLASHBACK_RETENTION_TARGET=2880 SCOPE=BOTH;
SQL> ALTER DATABASE FLASHBACK ON;
SQL> ALTER DATABASE OPEN;
RMAN> FLASHBACK DATABASE TO TIME = “TO_DATE(’2004-05-27 16:00:00′, ‘YYYY-MM-DD HH24:MI:SS’)”;
RMAN> FLASHBACK DATABASE TO SCN=23565;
RMAN> FLASHBACK DATABASE TO SEQUENCE=223 THREAD=1;
●You cannot use Flashback Database in the following situations:
* The control file has been restored or re-created.
* A tablespace has been dropped.
* A data file has been shrunk.
●When you enable Flashback Database, the new RVWR background process is started;
● V$FLASHBACK_DATABASE_LOG;
V$FLASHBACK_DATABASE_STAT;
在设置了闪回恢复区后,可以启动闪回数据库功能。
*/
–1.检查是否启动了flash recovery area
show parameter db_recovery_file
–2.检查是否启用了归档
archive log list;
–3.flashback database 默认是关闭的,查看方法
select flashback_on from v$database;
–4.查询当前的scn
SELECT CURRENT_SCN FROM V$DATABASE;
–5.查询当前的时间
select to_char(sysdate,’yy-mm-dd hh24:mi:ss’) time from dual;
–6.查看SCN 和 timestamp 之间的对应关系:
select scn,to_char(time_dp,’yyyy-mm-dd hh24:mi:ss’)from sys.smon_scn_time;
–7.恢复到时间点,或者恢复到SCN
flashback database to timestamp to_timestamp(’09-10-14 14:37:05′,’yy-mm-dd hh24:mi:ss’);
flashback database to scn 947921;
– B. flashback table 恢复误drop表
drop table sphsy.login_table;
select * from flash_table;
–purge table sphsy.login_table;清空回收站
flashback table sphsy.login_table to before drop;
select * from sphsy.login_table;
– C. flashback query 实现行级恢复
/*
flashback查询用于获取先前时间点的表行级数据。当使用flashback查询时,
需要在表名后指定as of timestamp子句或as of SCN子句,其中as of timestamp用于指定早期时间点,
而as of SCN用于指定早期的SCN值,示例如下:
*/
– 1.查原始记录 ,区间内有62 行
select *
from sphsy.login_table a
where a.id > 201204171078
and a.id < 201204171141
order by a.id ;
– 2.晚于区间的有 3016
select program,count(*)
from sphsy.login_table a
where a.id >= 201204171141
group by program ;
–3. 删除
delete from sphsy.login_table a
where a.id > 201204171078
and a.id < 201204171141
–4.利用闪回特性查到区间内,有62行
select * from sphsy.login_table
as of timestamp to_timestamp(’2012-04-17 17:20:30′,’YYYY-MM-DD HH24:MI:SS’)
where id > 201204171078
and id < 201204171141
– 5.不利用闪回特性,直接查询发现没有
select * from sphsy.login_table
where id > 201204171078
and id < 201204171141
– 6.进行数据恢复
– 禁止表上的触发器
alter trigger sphsy.T_INS_LOGIN_TABLE disable ;
– 恢复数据
insert into sphsy.login_table
select * from sphsy.login_table
as of timestamp to_timestamp(’2012-04-17 17:20:30′,’YYYY-MM-DD HH24:MI:SS’)
where id > 201204171078
and id < 201204171141
– 恢复触发器
alter trigger sphsy.T_INS_LOGIN_TABLE enable ;
– 7.晚于区间的数据回来了3130 = 3016 +62 + 后来的数据。实现了区间恢复误删除。
select program,count(*)
from sphsy.login_table a
where a.id >= 201204171078
group by program ;
– D. flashback table 恢复表到先前状态
/*
flashback查询可以用于恢复被误删除的表行数据,但是用户在表上执行了其他的DML语句误操作(insert或update),则不能直接使用flashback查询将表数据恢复到先前时间点,从oracle10g开始,使用flashback table语句可以将表恢复到先前时间点,通过使用该特征,可以避免执行基于时间点的不完全恢复,注意如果要在某个表上使用flashback table特征,则要求必须具有以下条件:
a.用户必须具有flashback any table系统权限或flashback对象权限
b.用户必修在表上具有select insert delete和alter权限
c.必须合理设置初始化参数undo_retention,以确保UNDO信息保留足够时间
d.必须激活行移动特征:alter table table_name enable row movement;
*/
– 1.查原始记录 ,区间内有62 行
select *
from sphsy.login_table a
where a.id > 201204171078
and a.id < 201204171141
order by a.id ;
– 2.晚于区间的有 3074
select count(*)
from sphsy.login_table a
where a.id >= 201204171141;
–3. 删除 ,先记下时间点,2012-04-17 17:43:46
select to_char(sysdate,’YYYY-MM-DD HH24:MI:SS’) from dual ;
delete from sphsy.login_table a
where a.id > 201204171078
and a.id < 201204171141
– 4.删除之后表 sphysy.login_table继续有修改 ,行3082
select count(*)
from sphsy.login_table a
where a.id >= 201204171141;
–5.激活行移动特征
alter table sphsy.login_table enable row movement
–6.利用闪回特性,直接恢复到删除时间点前
flashback table sphsy.login_table to timestamp to_timestamp(’2012-04-17 17:43:46′,’YYYY-MM-DD HH24:MI:SS’);
– 7.晚于区间的数据 回到了3080 ,说明时间点之后的修改丢失。
select count(*)
from sphsy.login_table a
where a.id >= 201204171141
– 8.往前推1分,恢复到删除之前,删除的62条也回来了。
flashback table sphsy.login_table to timestamp to_timestamp(’2012-04-17 17:40:46′,’YYYY-MM-DD HH24:MI:SS’);
– 62 行
select count(*)
from sphsy.login_table a
where a.id > 201204171078
and a.id < 201204171141
– 删除之后的数据为3074,代表还有修改丢失。
select count(*)
from sphsy.login_table a
where a.id >= 201204171141