Flashback_oracle閃回功能的使用

andyann發表於2011-06-03
1、概念:

閃回(flashback):當出現邏輯錯誤時(比如使用者誤刪除了表、或者系統管理員誤刪除了使用者等),能夠非常快速的完成對業務資料的恢復。

2、應用範圍:

(1)系統管理員誤刪除了使用者
(2)使用者失誤截斷了表或者更改了表的資料(truncate,delete,update,insert)。

不適應情況:
(1)對於資料塊損壞或者聯機日誌檔案損壞,必須採用介質恢復。
(2)使用者錯誤的執行了某個批處理任務,或者該批處理任務的指令碼編寫有問題,使得多個表的邏輯出現問題,無法採用閃回表的方式進行恢復。

3、檢查閃迴環境。

(1)歸檔必須開啟。

[oracle@anpc ~]$ sqlplus / as sysdba

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /Oracle/arch
Oldest online log sequence     681
Next log sequence to archive   683
Current log sequence           683

(2)閃回區設定:

SQL> show parameter db_recovery_file

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /Oracle/app/flash_recovery_are
                                                 a
db_recovery_file_dest_size           big integer 4G


SQL> show parameter db_flashback_retention_target;

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

預設:24小時,1440分鐘。
此引數是設定閃回資料的最長儲存時間

(3)閃回開啟:

SQL> select  flashback_on  from v$database;      

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

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

Total System Global Area 1820540928 bytes
Fixed Size                  2145344 bytes
Variable Size            1258292160 bytes
Database Buffers          553648128 bytes
Redo Buffers                6455296 bytes
Database mounted.
SQL> alter database flashback on;

Database altered.

SQL> alter database open;

Database altered.


4、閃回實驗:

(1)閃回資料庫。

SQL> create user ftest identified by ftest default tablespace users;

User created.

SQL> grant connect,resource to ftest;

Grant succeeded.

SQL> connect ftest/ftest
Connected.
SQL> create table testf as select * from user_objects;

Table created.

SQL> select count(*) from testf;

  COUNT(*)
----------
         1

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as "the time drop user" from dual;

the time drop user
-------------------
2011-06-02 17:28:52

SQL> drop user ftest cascade;

User dropped.

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

Total System Global Area 1820540928 bytes
Fixed Size                  2145344 bytes
Variable Size            1258292160 bytes
Database Buffers          553648128 bytes
Redo Buffers                6455296 bytes
Database mounted.
SQL> flashback database to timestamp to_date('2011-06-02 17:28:52','yyyy-mm-dd hh24:mi:ss');

Flashback complete.

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open

SQL> alter database open read only
  2  ;

Database altered.

SQL> connect ftest/ftest
Connected.
SQL> select * from testf;

OBJECT_NAME
--------------------------------------------------------------------------------
SUBOBJECT_NAME                  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE
------------------------------ ---------- -------------- -------------------
CREATED      LAST_DDL_TIM TIMESTAMP           STATUS  T G S  NAMESPACE
------------ ------------ ------------------- ------- - - - ----------
EDITION_NAME
------------------------------
TESTF
                                    79652          79652 TABLE
02-JUN-11    02-JUN-11    2011-06-02:17:13:50 VALID   N N N          1

SQL> connect sys as sysdba
Enter password:
Connected.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1820540928 bytes
Fixed Size                  2145344 bytes
Variable Size            1258292160 bytes
Database Buffers          553648128 bytes
Redo Buffers                6455296 bytes
Database mounted.
SQL> alter database open resetlogs;

Database altered.


(2)閃回表:

所謂閃回表,就是將表裡的資料會退到歷史上的某個時間點。
原理:利用undo表空間裡記錄的資料被改變前的值,閃回。

與閃回密切關係的數值:
A、undo_retention  undo保留時間預設900秒。

B、retention guarantee 模式修改,當undo表空間不夠時避免commit的事務被覆蓋,而不能保留預設900S。
   alter tablespace undotbs1 retention guarantee;
   alter tablespace undotbs1 retention noguarantee;

C、資料行的移動特性。

實驗測試:

SQL> show parameter undo_retention;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_retention                       integer     900

SQL> ALTER SYSTEM SET undo_retention=10800 SCOPE=BOTH;

System altered.

SQL> delete from test;

9 rows deleted.

SQL> commit;

Commit complete.

SQL> flashback table test to timestamp to_date('2011-06-03 11:13:54','yyyy-mm-dd hh24:mi:ss');
flashback table test to timestamp to_date('2011-06-03 11:13:54','yyyy-mm-dd hh24:mi:ss')
                *
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled


SQL> alter table test enable row movement;

Table altered.

SQL> flashback table test to timestamp to_date('2011-06-03 11:13:54','yyyy-mm-dd hh24:mi:ss');

Flashback complete.

SQL> select count(*) from test;

  COUNT(*)
----------
         9

注意:
如果閃回的期間發生過DDL操作,則閃回表操作失敗。


(3)Oracle回收站的使用。


SQL> drop table testf;

Table dropped.

SQL> show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
TESTF            BIN$pMZ2DnFf/HrgQKjADP1k6Q==$0 TABLE        2011-06-03:10:25:02

恢復表並改表名:

SQL> flashback table testf to before drop rename to testfnew;

Flashback complete.

SQL> drop table testfnew;

Table dropped.

SQL> show recyclebin
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
TESTFNEW         BIN$pMZ2DnFg/HrgQKjADP1k6Q==$0 TABLE        2011-06-03:10:52:01

恢復表不更改表名
SQL> flashback table "BIN$pMZ2DnFg/HrgQKjADP1k6Q==$0" to before drop;

Flashback complete.

SQL> select count(*) from testfnew;

  COUNT(*)
----------
         1

注意:表恢復過來後表上的物件需要重建,如索引重建。

備註--與回收站有關的命令:
SQL> purge index "索引命名";清除回收站的索引。
SQL> purge tablespace users;清除回收站裡面屬於user表空間的所有物件佔用的空間。
SQL> purge user_recyclebin; 清除回收站裡面屬於當前使用者的所有物件佔用的空間。
SQL> purge dba_recyclebin;  清楚回收站裡所有物件佔用的空間。
SQL> DROP TABLE TABLENAME PURGE; 徹底刪除表,不放到回收站。

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

相關文章