Flashback Database闪回数据库功能极大的降低了由于用户错误导致的数据丢失的恢复成本。这是一种以空间换取缩短恢复时间的解决方案,这是值得的。
这里给出闪回数据库的使用方法,体验一下这种恢复操作的便利性。
1.使用Flashback Database的前提条件
1)启用了flashback database
2)必须打开flash recovery area,若为RAC,flash recovery area必须位于共享存储中。
3)必须处于archivelog模式,开启FORCE LOGGIN
2.一一确认上面的前提条件是否满足
1)验证是否启用了flashback database并确认FORCE LOGGIN是否开启
SYS@ora11g> select flashback_on,force_logging from v$database;
FLASHBACK_ON FOR
------------------ ---
YES NO
若flashback_on为“NO”,修改方法见《【Flashback】启用Flashback Database闪回数据库功能》
若force_logging为“NO”,请使如下SQL语句开启。
SYS@ora11g> alter database force logging;
Database altered.
SYS@ora11g> select flashback_on,force_logging from v$database;
FLASHBACK_ON FOR
------------------ ---
YES YES
2)验证是否开启flash recovery area
此步骤在启用闪回数据库功能时已经确认过。
SYS@ora11g> show parameter db_recovery_file
NAME TYPE VALUE
--------------------------- ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery
_area
db_recovery_file_dest_size big integer 3852M
3)数据库是否处于archivelog模式
SYS@ora11g> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 11
Next log sequence to archive 13
Current log sequence 13
3.确认数据库可以前滚到的SCN和Time的方法
如果需要恢复的数据点比这个时间还要早的话,很不幸,闪回数据库功能将无能为力。
SYS@ora11g> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SYS@ora11g> select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;
OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI
-------------------- -------------------
1033529 2012-04-02 03:36:40
4.闪回数据库功能闪亮登场
1)创建测试表fd_1、fd_2和fd_3
SYS@ora11g> create table fd_1 as select * from dba_objects;
Table created.
SYS@ora11g> create table fd_2 as select * from fd_1;
Table created.
SYS@ora11g> create table fd_3 as select * from fd_1;
Table created.
SYS@ora11g> select count(*) from fd_1;
COUNT(*)
----------
72465
SYS@ora11g> select count(*) from fd_2;
COUNT(*)
----------
72465
SYS@ora11g> select count(*) from fd_3;
COUNT(*)
----------
72465
SYS@ora11g> set time on 21:59:40 SYS@ora11g> select sysdate from dual;
SYSDATE
------------------- 2012-04-07 21:59:44
2)truncate表fd_2、drop掉表fd_3 21:59:44 SYS@ora11g> truncate table fd_2;
Table truncated.
22:00:06 SYS@ora11g> drop table fd_3;
Table dropped.
3)使用Flashback Database功能进行恢复到删除前的时间点2012-04-07 21:59:44
22:00:17 SYS@ora11g> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
22:02:04 SYS@ora11g> startup mount exclusive;
ORACLE instance started.
Total System Global Area 313860096 bytes
Fixed Size 1336232 bytes
Variable Size 247467096 bytes
Database Buffers 58720256 bytes
Redo Buffers 6336512 bytes
Database mounted.
22:02:52 SYS@ora11g> Flashback Database to timestamp(to_date('2012-04-07 21:59:44','yyyy-mm-dd hh24:mi:ss'));
Flashback complete.
4)闪回后修复数据库两种方式之一:open read only
推荐使用这样的方法进行恢复,因为在read only方式打开之后,将需要恢复的表EXP导出,然后通过recover database将数据库恢复到原状态,再将缺失的数据IMP到数据库中。这样操作对数据库的影响可以降低到最小,可以保证其他表没有数据的丢失。
read only打开后查看三张表的状态:
22:03:57 SYS@ora11g> alter database open read only;
Database altered.
22:04:33 SYS@ora11g> select count(*) from fd_1;
COUNT(*)
----------
72465
22:04:37 SYS@ora11g> select count(*) from fd_2;
COUNT(*)
----------
72465
22:04:40 SYS@ora11g> select count(*) from fd_3;
COUNT(*)
----------
72465
取消闪回结果,恢复到闪回前状态的方法:
22:04:43 SYS@ora11g> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
22:05:09 SYS@ora11g> startup mount;
ORACLE instance started.
Total System Global Area 313860096 bytes
Fixed Size 1336232 bytes
Variable Size 247467096 bytes
Database Buffers 58720256 bytes
Redo Buffers 6336512 bytes
Database mounted.
22:06:08 SYS@ora11g> recover database;
Media recovery complete.
22:06:18 SYS@ora11g> alter database open;
Database altered.
22:06:41 SYS@ora11g> select count(*) from fd_1;
COUNT(*)
----------
72465
22:06:55 SYS@ora11g> select count(*) from fd_2;
COUNT(*)
----------
0
22:06:59 SYS@ora11g> select count(*) from fd_3;
select count(*) from fd_3
*
ERROR at line 1:
ORA-00942: table or view does not exist
可见,通过上面的recover后,数据库恢复到了闪回前的状态。
5)闪回后修复数据库两种方式之二:open resetlogs
通过open resetlogs方式打开数据库后,很显然,闪回到时间点之后的数据将全部丢失,慎用!
SYS@ora11g> alter database open resetlogs;
SYS@ora11g> select count(*) from fd_1;
COUNT(*)
----------
72465
SYS@ora11g> select count(*) from fd_2;
COUNT(*)
----------
72465
SYS@ora11g> select count(*) from fd_3;
COUNT(*)
----------
72465
5.小结
这里对Flashback Database闪回数据库的语法进行总结。闪回数据库可以在SQL*Plus环境和RMAN环境下使用。
基于时间戳进行闪回数据库操作方法:
Flashback Database to timestamp(to_date(‘2012-04-07 21:59:44’,’yyyy-mm-dd hh24:mi:ss’));
Flashback Database to timestamp(sysdate-1/24);
基于SCN进行闪回数据库操作方法:
Flashback Database to 1321427;
– The End –