oracle數據庫閃回

oracle閃回技術包括閃回刪除和閃回數據庫。閃回刪除主要是關注用戶誤刪除表、索引的數據庫對象;閃回數據庫是一種快速的數據庫恢復方案,這種恢復是基於用戶的邏輯錯誤,比如對錶中的數據做了錯誤的修改、插入了大量的錯誤數據。

[oracle@localhost ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 9 16:50:00 2014

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

查看數據庫能閃回到的最早的scn,如果數據庫沒有開閃回功能是沒有記錄返回的。
SQL> select oldest_flashback_scn from v$flashback_database_log;

no rows selected

查看數據庫是否開閃回
SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
NO

數據庫open狀態嘗試開啓數據庫的閃回功能,返回報錯信息說明需要在mount狀態下來操作。
SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38759: Database must be mounted by only one instance and not open.

關閉數據庫
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

打開數據庫到mount狀態
SQL> startup mount
ORACLE instance started.

Total System Global Area  184549376 bytes
Fixed Size                  1218412 bytes
Variable Size              83888276 bytes
Database Buffers           96468992 bytes
Redo Buffers                2973696 bytes
Database mounted.

數據庫在mount狀態下再次嘗試開啓數據庫的閃回功能,又返回錯誤信息說明數據庫在非歸檔狀態下不支持數據庫的閃回。
SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38707: Media recovery is not enabled.

開歸檔
SQL> alter database archivelog;

Database altered.

開閃回成功
SQL> alter database flashback on;

Database altered.

打開數據庫
SQL> alter database open;

Database altered.

開啓歸檔,默認的歸檔目錄:USE_DB_RECOVERY_FILE_DEST ,與9i不同10g開啓歸檔很要簡單一些。以後可以自行修改歸檔目錄的位置,在此暫時保留默認。
SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     59
Next log sequence to archive   61
Current log sequence           61

檢驗數據庫閃回是否開啓
SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
YES

查看閃回區信息,默認安裝大小爲2G,根據數據庫的具體情況作修改。
SQL> show parameter recover_file_dest

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /u01/app/oracle/flash_recovery_area
db_recovery_file_dest_size           big integer 20G

數據庫可以恢復到多少分鐘以前,默認1440分鐘(一天)
SQL> show parameter flash

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     1440


查看數據庫能恢復到最早的scn和最早的時間
SQL> select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;

OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TIME
-------------------- ---------------------
             1021372     20101130 23:07:18

 

閃回刪除測試:

[oracle@localhost ~]$ sqlplus test/test      

SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 9 16:50:00 2014

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> drop table student;

Table dropped.

SQL> show recyclebin;
ORIGINAL NAME  RECYCLEBIN NAME OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
STUDENT   BIN$+2ROU1DktpLgQKjAkwBc6A==$0 TABLE      2014-06-09:16:50:41
 

SQL> select object_name,original_name,operation from recyclebin;

OBJECT_NAME
--------------------------------------------------------------------------------
ORIGINAL_NAME
--------------------------------------------------------------------------------
OPERATION
---------------------------
BIN$+2ROU1DltpLgQKjAkwBc6A==$0
STUDENT
DROP

SQL> select * from tab;

TNAME
--------------------------------------------------------------------------------
TABTYPE         CLUSTERID
--------------------- ----------
BIN$+2ROU1DltpLgQKjAkwBc6A==$0
TABLE

SQL> flashback table student to before drop;

Flashback complete.

SQL> select * from tab;

TNAME
--------------------------------------------------------------------------------
TABTYPE         CLUSTERID
--------------------- ----------
STUDENT
TABLE

SQL> select * from student;

       SNO SNAME
---------- ------------------------------
  1 xxx
 11 aa

 也可以執行下面語句進行閃回:

flashback table "BIN$+2ROU1DktpLgQKjAkwBc6A==$0" to before drop rename to student;

閃回數據庫測試

[oracle@localhost ~]$ sqlplus test/test

SQL*Plus: Release 10.2.0.4.0 - Production on Tue Jun 10 09:32:52 2014

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from student;

       SNO SNAME
---------- ------------------------------
  1 xxx
 11 aa

[oracle@localhost ~]$ sqlplus sys/oracle as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on Tue Jun 10 09:34:32 2014

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select current_scn from v$database;

CURRENT_SCN
-----------
    6312445

在test用戶下插入數據

SQL> insert into student values(22,'nn');

1 row created.

SQL> commit;

Commit complete.

然後在sys用戶下再查詢current_scn

SQL> /

CURRENT_SCN
-----------
    6312458

通過v$flashback_database_log和v$flashback_database_stat視圖查詢相關閃回數據庫信息

SQL> select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;

OLDEST_FLASHBACK_SCN OLDEST_FLASHBAC
-------------------- ---------------
      6259583 09-JUN-14

SQL> select * from v$flashback_database_stat;

BEGIN_TIME END_TIME FLASHBACK_DATA   DB_DATA  REDO_DATA
--------------- --------------- -------------- ---------- ----------
ESTIMATED_FLASHBACK_SIZE
------------------------
10-JUN-14 10-JUN-14        5529600   7790592    2463744

關閉數據庫並啓動到mount狀態,執行閃回,並以resetlogs打開數據庫

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1224736768 bytes
Fixed Size      2083560 bytes
Variable Size    335545624 bytes
Database Buffers   872415232 bytes
Redo Buffers     14692352 bytes
Database mounted.
SQL> flashback database to scn 6312445;

Flashback complete.

SQL> alter database open resetlogs;

Database altered.

 

備註:

1、使用sysdba身份登陸,可以查詢回收站裏所有的對象,也可以指定條件查詢

 SQL> select object_name,original_name,operation from dba_recyclebin where original_name='STUDENT';

OBJECT_NAME
--------------------------------------------------------------------------------
ORIGINAL_NAME
--------------------------------------------------------------------------------
OPERATION
---------------------------
BIN$+2ROU1DltpLgQKjAkwBc6A==$0
STUDENT
DROP

2、無論是OS認證還是密碼認證,以sysdba登陸默認用戶是sys,sys用戶的默認表空間是system,system與sysaux表空間無法做回收站的操作

3、執行表閃回後,與表相關聯的對象也會被閃回,但是對象名已經發生變化,需要重命名或者重建對象

4、在閃回數據庫時,往往要用到具體的時間或者scn,我們可以定義容易記憶的復原點(scn的別名)來執行閃回,步驟如下

     4.1、創建一個復原點

 SQL> create restore point rp0;

Restore point created.

     4.2、創建有保證的復原點

SQL> create restore point rp1 guarantee flashback database;

Restore point created.

     4.3、查詢復原點信息

SQL> select name,scn,storage_size,guarantee_flashback_database from v$restore_point;

NAME
--------------------------------------------------------------------------------
       SCN STORAGE_SIZE GUARANTEE
---------- ------------ ---------
RP1
   6316026     31883264 YES

RP0
   6322551       0 NO

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章