Oracle 備份恢復之 Flashback

Davis_itpub發表於2018-06-27
<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>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31520497/viewspace-2156736/,如需轉載,請註明出處,否則將追究法律責任。

相關文章