閃回資料庫

lusklusklusk發表於2017-08-17
閃回主要有閃回表、閃回查詢、閃回資料庫(資料來自閃回日誌)三種,這裡主要了解閃回資料庫

閃回資料庫的最佳適用場景:當發生Drop table tablename purge或Truncate table或UNDO不足導致無法閃回表,需要找回丟失的表,但是丟失表之後的資料也不能丟,做閃回資料庫到丟失表之前的時刻點,做完後開啟到open read only模式下去,去檢視是否達到要求,達到後再exp匯出丟失的表,再shutdown immediate,再startup mount,再SQL>recover database;,再alter database open,再imp這張丟失的表

很重要的一點:如果執行flashback database後還沒有open resetlgos,但是後悔執行falshback database了,可以直接recover database再alter database open,這樣和重啟了一次一樣。

Use the FLASHBACK DATABASE statement to return the database to a past time or system change number (SCN). This statement provides a fast alternative to performing incomplete database recovery.
Following a FLASHBACK DATABASE operation, in order to have write access to the flashed back database, you must reopen it with an ALTER DATABASE OPEN RESETLOGS statement.
You must have the SYSDBA system privilege. A fast recovery area must have been prepared for the database. The database must have been put in FLASHBACK mode with an ALTER DATABASE FLASHBACK ON statement unless you are flashing the database back to a guaranteed restore point. The database must be mounted but not open. In addition:
The database must run in ARCHIVELOG mode.
The database must be mounted, but not open, with a current control file. The control file cannot be a backup or re-created. When the database control file is restored from backup or re-created, all existing flashback log information is discarded.
The database must contain no online tablespaces for which flashback functionality was disabled with the SQL statement ALTER TABLESPACE ... FLASHBACK OFF.

使用FLASHBACK DATABASE語句將資料庫返回到過去的時間或系統更改編號(SCN)。 此語句提供了執行不完整資料庫恢復的快速替代方法。
在進行FLASHBACK DATABASE操作之後,為了對閃回的資料庫進行寫入訪問,必須使用ALTER DATABASE OPEN RESETLOGS語句重新開啟它。
您必須具有SYSDBA系統許可權。 必須為資料庫準備快速恢復區域。 必須使用ALTER DATABASE FLASHBACK ON語句將資料庫置於FLASHBACK模式,除非將資料庫閃回到保證還原點。 做閃回操作時資料庫必須在MOUNT狀態下,不能在OPEN狀態下。此外有如下限制
資料庫必須以ARCHIVELOG模式執行。
必須使用當前的控制檔案把資料庫開啟至MOUNT狀態而非OPNE狀態。 控制檔案不能是備份或重新建立。 當資料庫控制檔案從備份或重新建立恢復時,所有現有的閃回日誌資訊都將被丟棄。
資料庫不包含禁用閃回功能的聯機表空間。
上面的MOUNT狀態指的是做閃回操作時,資料庫必須是MOUNT狀態,而不是指Alter database flashback on必須在MOUNT狀態下執行,Alter database flashback on可以在MOUNT/OPEN兩種狀態下執行

V$database.flashback_on值為YES就代表開啟了閃回資料庫
Select flashback_on from v$database;
Flashback on的前提條件:archivelog模式+db_recovery_file_dest不為空
Archive log list限定的Archive destination可以不是USE_DB_RECOVERY_FILE_DEST

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.


開啟閃回資料庫除了產生歸檔日誌,也產生閃回日誌(不是產生一個歸檔日誌就會同時產生一個閃回日誌)
閃回日誌的兩個檢視V$FLASHBACK_DATABASE_LOG、V$FLASHBACK_DATABASE_LOGFILE
閃回日誌的相關引數db_flashback_retention_target,控制閃回日誌存放時間




閃回資料庫的一次案例
1、建立test1、test2
2、記錄當前時間date
3、drop test1、truncate test2、create test3
4、要求恢復test1、test2的資料,並且不能造成test3丟失

1、
SQL> conn t1/123456
Connected.
SQL> create table test1 as select * from dba_users;
Table created.
SQL> create table test2 as select * from dba_data_files;
Table created.

2、
[oracle@localhost flashback]$ date
2017年 08月 17日 星期四 15:00:09 CST

3、
[oracle@localhost flashback]$ sqlplus t1/123456
SQL> drop table test1;
Table dropped.
SQL> truncate table test2;
Table truncated.
SQL> create table test3 as select * from dba_tablespaces;
Table created.
SQL> select count(*) from test1;
select count(*) from test1
                     *
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select count(*) from test2;
  COUNT(*)
----------
         0
SQL> select count(*) from test3;
  COUNT(*)
----------
         6

4、
[oracle@localhost flashback]$ sqlplus  / as sysdba
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area  321548288 bytes
Fixed Size                  2252824 bytes
Variable Size             264245224 bytes
Database Buffers           50331648 bytes
Redo Buffers                4718592 bytes
Database mounted.
SQL>flashback database to timestamp to_timestamp('2017-08-17 15:00:09','yyyy-mm-dd hh24:mi:ss');

--expdp會建立job,所以read only狀態下是無法expdp的,但是可以使用exp
--閃回後,15:00:09之前的test1、test2回來了,但是之後的test3沒有了

SQL> alter database open read only;
Database altered.
SQL> conn t1/123456
Connected.
SQL> select count(*) from test1;
  COUNT(*)
----------
        37
SQL> select count(*) from test2;
  COUNT(*)
----------
         5
SQL> select count(*) from test3;
select count(*) from test3
                     *
ERROR at line 1:
ORA-00942: table or view does not exist

--exp匯出test1、test2表資料
[oracle@localhost flashback]$exp t1/123456 file=/home/oracle/test1_2.dmp tables=test1,test2

--關閉資料庫啟動到mount再recover在open,相當於重啟了一次資料庫,flashback沒有起任何作用
[oracle@localhost ~]$ sqlplus / as sysdba
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area  321548288 bytes
Fixed Size                  2252824 bytes
Variable Size             264245224 bytes
Database Buffers           50331648 bytes
Redo Buffers                4718592 bytes
Database mounted.
SQL> recover database;
Media recovery complete.
SQL> alter database open;
Database altered.
SQL> conn t1/123456
Connected.
SQL> select count(*) from test1;
select count(*) from test1
                     *
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select count(*) from test2;
  COUNT(*)
----------
         0
SQL> select count(*) from test3;
  COUNT(*)
----------
         6

--imp匯入test1、test2,這樣恢復了test1、test2的資料,test3也沒丟失
[oracle@localhost flashback]$imp t1/123456 file=/home/oracle/test1_2.dmp full=y ignore=y
SQL> conn t1/123456
SQL> select count(*) from test1;
  COUNT(*)
----------
        37
SQL> select count(*) from test2;
  COUNT(*)
----------
         5
SQL> select count(*) from test3;
  COUNT(*)
----------
         6





繼續上面的步驟,再次閃回到上面第二步即未drop test1之前的時刻點,不用resetlogs看可以嗎。答案是不可以這樣的。
[oracle@localhost ~]$ sqlplus / as sysdba
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area  321548288 bytes
Fixed Size                  2252824 bytes
Variable Size             264245224 bytes
Database Buffers           50331648 bytes
Redo Buffers                4718592 bytes
Database mounted.
SQL> flashback database to timestamp to_timestamp('2017-08-17 15:00:09','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 resetlogs;
Database altered.
SQL> conn t1/123456
Connected.
SQL> select count(*) from test1;
  COUNT(*)
----------
        37
SQL> select count(*) from test2;
  COUNT(*)
----------
         5
SQL> select count(*) from test3;
select count(*) from test3
                     *
ERROR at line 1:
ORA-00942: table or view does not exist

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

相關文章