Oracle 從 9i 開始提供了 Flashback Query 功能,可用於恢復錯誤的 DML 操作。在 Oracle 10g 中對 Flashback Query 做了較大改進,不再局限於 Flashback Query ,還可用於恢復錯誤的 DDL(Drop) 操作。

  Flashback Query  是透過一個新的 Package:DBMS_FLASH 來實現。DBA 可使用 Flashback Query 可以及時取得錯誤操作 DML(Delete、Update、Insert) 在當前某一時間點資料庫的映射視圖,DBA 可以利用系統時間或系統改變號(SCN:System Change Number)來指定這個唯讀視圖,並可以針對錯誤進行相應的恢復措施。

  以下來實作一下整個過程:

1. 可以先用下列 SQL 語法來查詢 recyclebin 是否有被 drop 的 table,此時應該是沒有的。

SQL>select object_name,original_name,operation,type,droptime from recyclebin;

 2. 建立測試的 table : demo_users

create table demo_users (id char(2), name varchar2(10));
insert into demo_users values ('01','David');
insert into demo_users values ('02','Scott');
select * from demo_users;

 可以查詢到有兩筆資料在 demo_users 的資料表( Table )裡。

recyclebin_table_select_ok.png  

3. 刪除資料表 demo_users 並在查詢資源回收筒中是否有被刪除的資料表。

SQL>drop table demo_users;
SQL>SELECT object_name,original_name,operation,type,droptime 
FROM recyclebin;

 此時發現有一筆資料

recyclebin_table.png  

4. 回復此資料表 demo_users

SQL>flashback table demo_users to before drop ;

 這樣子就可以將資料表 Table:demo_users 回復了。

      Oracle Flashback Database 的特性允許透過 SQL 語法 Flashback Database 語句,讓資料庫回到當前的某一個時間點或者SCN,而不需要做時間點的恢復。Flashback Database 可以迅速將資料庫回到錯誤操作或人為錯誤的前一個時間點,如 Ctrl+Z 的 "復原" 操作,可以不利用備份資料 (RM) 就可以快速的實現以時間點為基礎來作恢復。Oracle 通過新建的 Flashback Logs,記錄資料庫的 Flashback 操作。如果希望能 Flashback Database,需要設置如下參數:DB_RECOVER_FILE_DEST日誌的存放位置, DB_RECOVER_FILE_DEST_SIZE恢復區的大小。在建立資料庫的時候,Oracle將自動建立恢復區,但預設是關閉的,需要執行 alter database flashback on 的指令來啟動。

 

  • 若要回復整個資料庫到當前的某一個時間點,SQL 指令為:
SQL>flashback database to time to_timestamp(xxx);
  •  若要回復某一個資料表 Table 到當前的某一個時間點,SQL 指令為:
SQL> flashback table table_name to timestamp 
to_timestamp('2006-05-30 14:21:43','yyyy-mm-dd hh24:mi:ss');

  • 有關scn值就是記錄在這個table裡:flashback_transaction_query


  table_flashback_transaction_query.png  

參考文獻: http://blog.roodo.com/mywork/archives/1684525.html

 

Configuring Your Database for Oracle Flashback Transaction Query

To configure your database for the Oracle Flashback Transaction Query feature, you or your database administrator must do the following:

  • Ensure that Oracle Database is running with version 10.0 compatibility.

  • Enable supplemental logging:

    ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;

 

Configuring Your Database for Flashback Transaction

To configure your database for the Flashback Transaction feature, you or your database administrator must do the following:

  • With the database mounted but not open, enable ARCHIVELOG:

    ALTER DATABASE ARCHIVELOG;
    
  • Open at least one archive log:

    ALTER SYSTEM ARCHIVE LOG CURRENT;
    
  • If not done already, enable supplemental logging:

    ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
    

 

arrow
arrow

    MIS 發表在 痞客邦 留言(0) 人氣()