Oracle 备份恢复之 Flashback

来源:这里教程网 时间:2026-03-03 11:40:40 作者:
<div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <span style="background-color:inherit;line-height:1.5;"><b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">1.Flashback的目的</span></b></span> </div> <span style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;">在推出Flashback之前,如果你对数据误操作,并已提交,这时想回退该误操作,将会是很件麻烦的事情。有人可能会说可以用备份恢复到误操作之前,但正确的操作数据也一起没了。唯一可能的办法就是日志挖掘(logminer),但日志挖掘非常繁琐,很难定位。</span><br style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;" /> <span style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;">因此,Oracle推出了Flashback技术,主要目的就是为了恢复因误操作而丢失的数据。</span> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <br style="background-color:inherit;" /> </div> <b style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"><span style="background-color:inherit;font-size:medium;">2.Flashback家族介绍</span></b><br style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;" /> <span style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;">数据库级别:Flashback Database</span><br style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;" /> <span style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;">表级别:Flashback Drop和Flashback Table,Flashback Data Archive</span><br style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;" /> <span style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;">记录级别:Flashback Version Query和Flashback Transaction Query</span> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <span style="background-color:inherit;font-size:medium;"><br style="background-color:inherit;" /> </span> </div> <b style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"><span style="background-color:inherit;font-size:medium;">3.Flashback家族 应用原理</span></b> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> 闪回原理基于oracle的 Log、<span style="background-color:inherit;line-height:1.5;">&nbsp;Recycle Bin、</span><span style="background-color:inherit;line-height:1.5;">UNDO回滚段作用。</span> </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <br style="background-color:inherit;" /> </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> UNDO闪回: </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> &nbsp;Flashback Database &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;回滚数据库<br style="background-color:inherit;" /> &nbsp;Flashback Drop &nbsp; &nbsp;&nbsp; &nbsp;恢复误删除的表<br style="background-color:inherit;" /> &nbsp;Flashback Version Query &nbsp; &nbsp;&nbsp; &nbsp; 恢复误操作的数据<br style="background-color:inherit;" /> &nbsp;Flashback Transaction Query &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;恢复误操作的数据<br style="background-color:inherit;" /> &nbsp;Flashback Table&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;恢复误操作的数据 <div style="background-color:inherit;"> <b style="background-color:inherit;"><br style="background-color:inherit;" /> </b> </div> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">4.Flashback 技术发展历程</span></b><br style="background-color:inherit;" /> Flashback Version/Transaction Query 和 Flashback Table、Flashback Query和Flashback Table都是利用undo实现回退功能,当需要闪回到过去某一时刻时,先利用Flashback Query查询,确认闪回的SCN或Timestamp,然后再利用Flashback Table真正实现闪回。 </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <br style="background-color:inherit;" /> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">5. 9i 的 Flashback Query</span></b><br style="background-color:inherit;" /> 9i的Flashback Query可以查询过去某个时间点对象的状态,测试如下:<br style="background-color:inherit;" /> 系统当前时间为:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;alter session set nls_date_format='yyyy-mm-dd:hh24:mi:ss';<br style="background-color:inherit;" /> A105024@O02DMS1&gt;select sysdate from dual;<br style="background-color:inherit;" /> SYSDATE<br style="background-color:inherit;" /> -------------------<br style="background-color:inherit;" /> 2011-12-16:02:51:16<br style="background-color:inherit;" /> test表里有一条数据:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;select * from test;<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; ID<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br style="background-color:inherit;" /> 现把该数据删除:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;delete from test;<br style="background-color:inherit;" /> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 此时test表中无数据:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;select * from test;<br style="background-color:inherit;" /> no rows selected<br style="background-color:inherit;" /> 但是可以通过Flashback Query查询删除之前的数据:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;select * from test as of timestamp to_timestamp('2011-12-16:02:51:16','yyyy-mm-dd:hh24:mi:ss');<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; ID<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br style="background-color:inherit;" /> 必要时还可以恢复数据:<br style="background-color:inherit;" /> A105024@O02DMS1&gt;insert into test select * from test as of timestamp to_timestamp('2011-12-16:02:51:16','yyyy-mm-dd:hh24:mi:ss');<br style="background-color:inherit;" /> A105024@O02DMS1&gt;commit; </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <span style="background-color:inherit;font-size:medium;"><br style="background-color:inherit;" /> <b style="background-color:inherit;">6. Flashback Version Query</b></span><br style="background-color:inherit;" /> 10g新引入的Version Query可以看到过去某个时间段内,数据是如何变化的,也就是数据的演变历史,为此,10g还引入了一个新的伪列ORA_ROWSCN:记录该数据最后一次修改时的SCN。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;create table test (id number, name varchar(10)); <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;insert into test values (1,'first'); <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;update test set name='second' where id=1; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;update test set name='third' where id=1; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 我们创建了一个测试表,插入一条数据,并对该数据进行两次更新,下面看如何用Version Query获得数据的演变历史: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;select versions_xid,versions_startscn,versions_endscn,versions_operation,id,name from test versions between scn minvalue and maxvalue where id=1; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> VERSIONS_XID &nbsp; &nbsp; VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_OPERATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ID NAME<br style="background-color:inherit;" /> ---------------- ----------------- --------------- -------------------- ---------- ----------<br style="background-color:inherit;" /> 12000900D0720000 &nbsp; &nbsp; &nbsp; &nbsp;6629455993 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;U &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 third <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 0C0018003D720000 &nbsp; &nbsp; &nbsp; &nbsp;6629455988 &nbsp; &nbsp; &nbsp;6629455993 U &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 second <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 0D002A00AD6E0000 &nbsp; &nbsp; &nbsp; &nbsp;6629455888 &nbsp; &nbsp; &nbsp;6629455988 I &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 first <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 从下往上看,正好对应着我们之前对该数据的操作历史。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 从上面加黑这行可以看出,在SCN 6629455888和6629455993 这段期间,数据的状态为(1,second),对应的操作为update。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> &nbsp; <div style="background-color:inherit;"> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;"><br style="background-color:inherit;" /> </span></b> </div> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">7. Flashback Transaction Query</span></b> <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> Transaction Query可以查看事务的演变历史,使用这个功能需要访问flashback_transaction_query视图。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;create table test (id number); <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 开始第一个事务: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;insert into test values (1); <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;update test set id=11 where id=1; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 开始第二个事务: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;insert into test values (2); <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;update test set id=22 where id=2; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;commit; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 查看flashback_transaction_query视图获得事务的演变历史: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> A105024@O02DMS1&gt;select XID,OPERATION,COMMIT_SCN,UNDO_SQL from flashback_transaction_query where xid in <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> &nbsp; 2 &nbsp;(select versions_xid from test versions between scn minvalue and maxvalue);<br style="background-color:inherit;" /> XID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OPERATION &nbsp;COMMIT_SCN UNDO_SQL<br style="background-color:inherit;" /> ---------------- ---------- ---------- ------------------------------------------------------------<br style="background-color:inherit;" /> 10000D000C720000 UPDATE &nbsp; &nbsp; 6629456273 update "A105024"."TEST" set "ID" = '2' where ROWID = 'AACsnzAAEAAABSnAAB'; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 10000D000C720000 INSERT &nbsp; &nbsp; 6629456273 delete from "A105024"."TEST" where ROWID = 'AACsnzAAEAAABSnAAB'; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 10000D000C720000 BEGIN &nbsp; &nbsp; &nbsp;6629456273 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 0C0002002A720000 UPDATE &nbsp; &nbsp; 6629456260 update "A105024"."TEST" set "ID" = '1' where ROWID = 'AACsnzAAEAAABSnAAA'; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 0C0002002A720000 INSERT &nbsp; &nbsp; 6629456260 delete from "A105024"."TEST" where ROWID = 'AACsnzAAEAAABSnAAA'; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 0C0002002A720000 BEGIN &nbsp; &nbsp; &nbsp;6629456260 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">8. Flashback Table</span></b> <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 根据Flashback Query的演变历史,就可以确定需要回退的时间点,然后再利用Flashback Table功能真正实现回退,注意:在真正回退之前,必须启用row movement。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">Flashback Drop</b> <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> Flashback drop是从Oracle10g开始才有的功能,原理是每个表空间都会有严格回收站的逻辑区域,当drop时,被删除的表及其关联对象不会被物理删除,只是转移到回收站中,给用户提供一个恢复的可能。 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 使用Flashback drop需要注意以下几点: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 1. 对system表空间无效 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 2. sqlplus的版本不能低于10g,否则很多命令无法使用 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 下面做个测试: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; create table test as select * from dba_objects; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; drop table test; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt;<b style="background-color:inherit;">&nbsp;show recyclebin;</b><br style="background-color:inherit;" /> ORIGINAL NAME &nbsp; &nbsp;RECYCLEBIN NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OBJECT TYPE &nbsp;DROP TIME<br style="background-color:inherit;" /> ---------------- ------------------------------ ------------ -------------------<br style="background-color:inherit;" /> TEST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BIN$S5L+aNpzQmOScn8VfpJBAA==$0 TABLE &nbsp; &nbsp; &nbsp; &nbsp;2011-12-15:22:38:30 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; create table test as select * from dba_objects where 2=1; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; drop table test; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt;<b style="background-color:inherit;">&nbsp;show recyclebin;</b><br style="background-color:inherit;" /> ORIGINAL NAME &nbsp; &nbsp;RECYCLEBIN NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OBJECT TYPE &nbsp;DROP TIME<br style="background-color:inherit;" /> ---------------- ------------------------------ ------------ -------------------<br style="background-color:inherit;" /> TEST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BIN$XHwrz1OaQaSeq/NQIE85hw==$0 TABLE &nbsp; &nbsp; &nbsp; &nbsp;2011-12-15:22:39:34<br style="background-color:inherit;" /> TEST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BIN$S5L+aNpzQmOScn8VfpJBAA==$0 TABLE &nbsp; &nbsp; &nbsp; &nbsp;2011-12-15:22:38:30<br style="background-color:inherit;" /> 此时recyclebin里有两个test表,查看中两个表的数据个数: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; select count(*) from "BIN$XHwrz1OaQaSeq/NQIE85hw==$0";<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; select count(*) from "BIN$S5L+aNpzQmOScn8VfpJBAA==$0";<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp;1000000 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 假设想要恢复后一张表: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; flashback table "BIN$S5L+aNpzQmOScn8VfpJBAA==$0" to before drop;<br style="background-color:inherit;" /> Flashback complete. <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; select count(*) from test;<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp;1000000<br style="background-color:inherit;" /> 假设现在还要恢复前一张表,并重新命名为test2:<br style="background-color:inherit;" /> SQL&gt; flashback table "BIN$XHwrz1OaQaSeq/NQIE85hw==$0" to before drop rename to t<br style="background-color:inherit;" /> est2;<br style="background-color:inherit;" /> Flashback complete. <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; select count(*) from test2;<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">9.Flashback Database</span><br style="background-color:inherit;" /> 1. 配置Flashback Database</b><br style="background-color:inherit;" /> 1)Flashback 功能默认是关闭的:<br style="background-color:inherit;" /> SQL&gt; select name,flashback_on from v$database;<br style="background-color:inherit;" /> NAME &nbsp; &nbsp; &nbsp;FLASHBACK_ON<br style="background-color:inherit;" /> --------- ------------------<br style="background-color:inherit;" /> O01DMS0 &nbsp; NO <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">2)配置Flash recovery area:</b><br style="background-color:inherit;" /> SQL&gt; alter system set db_recovery_file_dest_size=2G scope=both;<br style="background-color:inherit;" /> SQL&gt; alter system set db_recovery_file_dest='H:\flashback' scope=both; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">3)启用Flashback Database 功能:</b><br style="background-color:inherit;" /> SQL&gt; shutdown immediate<br style="background-color:inherit;" /> SQL&gt; startup mount<br style="background-color:inherit;" /> SQL&gt; alter database flashback on;<br style="background-color:inherit;" /> SQL&gt; select name,flashback_on from v$database;<br style="background-color:inherit;" /> NAME &nbsp; &nbsp; &nbsp;FLASHBACK_ON<br style="background-color:inherit;" /> --------- ------------------<br style="background-color:inherit;" /> O01DMS0 &nbsp; YES <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">4)设置db_flashback_retention_target:<br style="background-color:inherit;" /> </b>SQL&gt; alter system set db_flashback_retention_target=1440 scope=both; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">5)打开数据库:<br style="background-color:inherit;" /> </b>SQL&gt; alter database open; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">2. Flashback Database 操作<br style="background-color:inherit;" /> </b>1)模拟数据丢失: <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; create table test as select * from dba_objects;<br style="background-color:inherit;" /> Table created.<br style="background-color:inherit;" /> SQL&gt; select count(*) from test;<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp;10318 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; truncate table test;<br style="background-color:inherit;" /> Table truncated.<br style="background-color:inherit;" /> SQL&gt; select count(*) from test;<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">2)确认能恢复的时间点</b><br style="background-color:inherit;" /> 能回退的最早时间,取决于保留的Flashback database log的多少,可以从v$flashback_database_log查看:<br style="background-color:inherit;" /> SQL&gt; select to_char(OLDEST_FLASHBACK_TIME,'yyyy-mm-dd hh24:mi:ss') from v$flashback_database_log;<br style="background-color:inherit;" /> TO_CHAR(OLDEST_FLAS<br style="background-color:inherit;" /> -------------------<br style="background-color:inherit;" /> 2011-12-15 02:41:48 <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;">3)恢复数据到指定时间点<br style="background-color:inherit;" /> </b>SQL&gt; shutdown immediate;<br style="background-color:inherit;" /> SQL&gt; startup mount;<br style="background-color:inherit;" /> SQL&gt; flashback database to timestamp to_timestamp('2011-12-15 02:43:00','yyyy-mm-dd hh24:mi:ss');<br style="background-color:inherit;" /> Flashback complete. <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> 恢复成功后,最好先以readonly的方式打开数据库,以确认恢复达到预期,如果没有达到预期,还可以再进行恢复:<br style="background-color:inherit;" /> SQL&gt; alter database open read only;<br style="background-color:inherit;" /> Database altered.<br style="background-color:inherit;" /> SQL&gt; select count(*) from test;<br style="background-color:inherit;" /> select count(*) from test<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br style="background-color:inherit;" /> ERROR at line 1:<br style="background-color:inherit;" /> ORA-00942: table or view does not exist <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; shutdown immediate;<br style="background-color:inherit;" /> SQL&gt; startup mount;<br style="background-color:inherit;" /> SQL&gt; flashback database to timestamp to_timestamp('2011-12-15 02:49:00','yyyy-mm-dd hh24:mi:ss');<br style="background-color:inherit;" /> Flashback complete. <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> SQL&gt; alter database open read only;<br style="background-color:inherit;" /> Database altered.<br style="background-color:inherit;" /> SQL&gt; select count(*) from test;<br style="background-color:inherit;" /> &nbsp; COUNT(*)<br style="background-color:inherit;" /> ----------<br style="background-color:inherit;" /> &nbsp; &nbsp; &nbsp;10318 </div> <div style="font-family:微软雅黑;font-size:14px;line-height:21px;white-space:normal;widows:auto;background-color:#FFFFFF;"> <br style="background-color:inherit;" /> <b style="background-color:inherit;">4)打开数据库</b><br style="background-color:inherit;" /> 恢复成功后,以resetlog方式打开数据库:<br style="background-color:inherit;" /> SQL&gt; shutdown immediate;<br style="background-color:inherit;" /> SQL&gt; startup mount<br style="background-color:inherit;" /> SQL&gt; alter database open resetlogs; <div style="background-color:inherit;"> <br style="background-color:inherit;" /> </div> <b style="background-color:inherit;"><span style="background-color:inherit;font-size:medium;">10.Flashback Data Archive</span><br style="background-color:inherit;" /> </b>Oracle11g则为flashback家族又带来一个新的成员:flashback data archive。<br style="background-color:inherit;" /> &nbsp; &nbsp; 初看起来,flashback data archive和flashback query没有太大的不同,都是通过as of能够查询之前的数据,但是他们的实现机制是不一样的。Flashback query是通过直接从undo中读取信息来构造旧数据,这样就有一个限制,就是undo中的信息不能被覆盖。而undo段是循环使用的,只要事务提交,之前的undo信息就可能被覆盖,虽然可以通过undo_retention等参数来延长undo的存活期,但这个参数会影响所有的事务,设置过大,可能导致undo tablespace快速膨胀。<br style="background-color:inherit;" /> &nbsp; &nbsp; Falshback data archive特性则通过将变化数据另外存储到创建的flashback archive中,以和undo区别开来,这样就可以通过为flashback archive单独设置存活策略,使得可以闪回到指定时间之前的旧数据而不影响undo策略。并且可以根据需要指定哪些数据库对象需要保存历史变化数据,而不是将数据库中所有对象的变化数据都保存下来,这样可以极大的减少空间需求。 </div>

相关推荐