[Flashback]Flashback Database閃回資料庫實驗

梓沐發表於2016-02-15

Flashback Database閃回資料庫功能極大的降低了由於使用者錯誤導致的資料丟失的恢復成本。這是一種以空間換取縮短恢復時間的解決方案,這是值得的。

1.使用Flashback Database的前提條件

1)啟用了flashback database

2)必須開啟flash recovery area,若為racflash recovery area必須位於共享儲存中。

3)必須處於archivelog模式,開啟force logging

2.檢查資料庫是否滿足上述條件

1)驗證是否開啟了flashback功能和force logging

SQL> select flashback_on,force_logging from v$database;

FLASHBACK_ON       FOR

------------------ ---

YES                YES

如果flashback_onno狀態,修改方法:http://blog.csdn.net/u011364306/article/details/49249187

如果force_loggingno狀態,修改方法如下:

--這裡已經開啟過,所以重複開啟會報錯

SQL> alter database force logging;

alter database force logging

*

ERROR at line 1:

ORA-12920: database is already in force logging mode

2)驗證是否開啟flash recovery area

SQL> show parameter db_recovery

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

db_recovery_file_dest                string      /u01/oracle/fast_recovery_area

db_recovery_file_dest_size           big integer 10G

3)驗證是否開啟歸檔模式

SQL> archive log list

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination            USE_DB_RECOVERY_FILE_DEST

Oldest online log sequence     25

Next log sequence to archive   27

Current log sequence           27

3.驗證資料庫可以回滾的最早SCNTIME

檢視v$flasback_database_log,如果資料庫要恢復的資料比這個時間還早,則閃回無能為力

--時間格式化

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

--透過v$flasback_database_log查詢可以閃回的最早的scn號和時間

SQL> select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;

OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI

-------------------- -------------------

             1372033 2015-10-19 13:36:13

4.建立測試資料

1)建立測試表test_1,test_2,test_3

SQL>  create table test_1 as select * from dba_objects;

Table created.

SQL> create table test_2 as select * from test_1;

Table created.

SQL> create table test_3 as select * from test_1;

Table created.

SQL> select count(*) from test_1;

  COUNT(*)

----------

     86610

SQL> select count(*) from test_2;

  COUNT(*)

----------

     86610

SQL> select count(*) from test_3;

  COUNT(*)

----------

     86610

SQL> set time on

2)test_2truncate掉,將test_3drop

14:24:38 SQL> truncate table test_2;

Table truncated.

14:24:57 SQL> drop table test_3;

Table dropped.

3)使用閃回資料庫功能恢復到時間14:24:38

14:25:05 SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

--將資料庫啟動到mount exclusive模式

14:25:42 SQL> startup mount exclusive

ORACLE instance started.

Total System Global Area  784998400 bytes

Fixed Size                  2257352 bytes

Variable Size             478154296 bytes

Database Buffers          301989888 bytes

Redo Buffers                2596864 bytes

Database mounted.

--閃回資料庫語句

14:26:02 SQL> flashback database to timestamp(to_date('2015-10-19 14:24:38','yyyy-mm-dd hh24:mi:ss'));

Flashback complete.

4)閃回資料庫後有2中方法可以修復資料庫:

使用open read only一般推薦使用該方法,在以只讀模式開啟後,可以將需要恢復的表單獨exp出來,再透過recover database恢復資料庫到原來的狀態,再將缺失的資料篩選重新imp到資料庫中。這樣可以保證對資料庫的影響降到最低,保證對資料庫的其他表沒有影響。

read only開啟後檢視三張表的狀態:

14:27:19 SQL> alter database open read only;

Database altered.

14:27:29 SQL> select count(*) from test_1;

  COUNT(*)

----------

     86610

14:27:37 SQL> select count(*) from test_2;

  COUNT(*)

----------

     86610

14:27:39 SQL> select count(*) from test_3;

  COUNT(*)

----------

     86610

取消閃回的結果,恢復資料庫到原來的狀態

14:28:16 SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

14:28:43 SQL> startup mount

ORACLE instance started.

Total System Global Area  784998400 bytes

Fixed Size                  2257352 bytes

Variable Size             478154296 bytes

Database Buffers          301989888 bytes

Redo Buffers                2596864 bytes

Database mounted.

--恢復資料庫語句

14:29:05 SQL> recover database;

Media recovery complete.

14:29:10 SQL> alter database open;

Database altered.

14:29:20 SQL> select count(*) from test_1;

  COUNT(*)

----------

     86610

14:29:30 SQL> select count(*) from test_2;

  COUNT(*)

----------

         0

14:29:31 SQL> select count(*) from test_3;

select count(*) from test_3

                     *

ERROR at line 1:

ORA-00942: table or view does not exist

透過上面結果可以檢視資料庫已經恢復到原來的狀態

5)使用open resetlogs,但是透過open resetlogs方式開啟資料庫後,閃回到時間點之後的資料將全部丟失,很多表都會因此受到影響,因此慎重使用

繼續建立test_4

14:45:40 SQL> create table test_4 as select * from test_1;

Table created.

閃回資料庫

14:46:00 SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

14:46:15 SQL> startup mount exclusive

ORACLE instance started.

Total System Global Area  784998400 bytes

Fixed Size                  2257352 bytes

Variable Size             478154296 bytes

Database Buffers          301989888 bytes

Redo Buffers                2596864 bytes

Database mounted.

14:46:31 SQL> flashback database to timestamp(to_date('2015-10-19 14:24:38','yyyy-mm-dd hh24:mi:ss'));

Flashback complete.

使用open restlogs開啟資料庫

14:47:28 SQL> alter database open resetlogs;

Database altered.

檢視test_1test_4

14:47:51 SQL> select count(*) from test_1;

  COUNT(*)

----------

     86610

14:48:01 SQL> select count(*) from test_2;

  COUNT(*)

----------

     86610

14:48:03 SQL> select count(*) from test_3;

  COUNT(*)

----------

     86610

--發現後來建立的test_4表被丟棄

14:48:05 SQL> select count(*) from test_4;

select count(*) from test_4

                     *

ERROR at line 1:

ORA-00942: table or view does not exist


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

相關文章