闪回数据库概念:
10G新增功能,在启用flashback database功能后,数据库会定期将发生变化的数据块的 前镜像写入闪回日志的日志文件中,
在进行数据库闪回时,这些数据块可以直接复制回来以满足数据库的恢复需要。
REDO LOG可以用来辅助数据恢复到更精确时间点,可以缩短恢复时间。
闪回数据库的日志文件由recovery writer RVWR进程写入
闪回日志文件由RVWR进程在闪回恢复区中自动创建和维护。
闪回数据库特性默认是关闭,需要在MOUNT状态打开。
闪回时可将无关的表空间暂时OFFLINE以加快恢复速度,只将SYSTEM UNDO 及所要闪回的数据对应的表空间在线即可。
不能使用闪回数据库的场景:
数据文件物理损坏--只用进行物理恢复了
数据文件shrink
使用备份的控制文件--或者重建了控制文件,因为闪回日志信息记录在控制文件中
表空间删除--
想闪回到比闪回日志中最小SCN更早的时间点--
如删除用户,可以先闪回到删除前,READ ONLY模式打开,然后EXPDP导出被删除用户。
完全恢复数据库,READ WRITE打开,再将用户导入。
总结一下开启闪回数据库功能简要步骤:
1.重启数据库到MOUNT状态-startup mount;
2.打开归档模式-alter database archivelog; 非归档时执行打开闪回数据库将报错:ORA-38707: Media recovery is not enabled.
3.设置闪回区域--需要提前在OS中创建好目录并指定大小- 未指定或指定空间不足报:ORA-38709: Recovery Area is not enabled.
ORA-38708: not enough space for first flashback database log file
alter system set db_recovery_file_dest_size=2g;
alter system set db_recovery_file_dest='/backup/flashback_area';
4.打开数据库,查询归档模式-archive log list; 并查询V$DATABASE.FLASHBACK_ON字段的状态,
下面实验打开闪回数据库功能,并实际进行一次闪回数据库操作来恢复数据。
1.查看数据库归档状态并打开闪回数据库功能
22:08:49 SQL> conn / as sysdba
Connected.
22:08:54 SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/archbys001/
Oldest online log sequence 46
Next log sequence to archive 48
Current log sequence 48
如未打开归档,则报以下错误:
SYS@ bys3>alter database noarchivelog;
Database altered.
SYS@ bys3>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.
查看闪回状态,默认是NO,没打开。
22:08:56 SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
NO
22:09:18 SQL> host
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 9.2G 8.9G 51% /
/dev/sda1 99M 21M 74M 22% /boot
tmpfs 3.0G 484M 2.6G 16% /dev/shm
/dev/sda5 4.6G 1.4G 3.1G 31% /backup
[oracle@oel-01 ~]$ cd /backup
[oracle@oel-01 backup]$mkdir flashback_area
[oracle@oel-01 backup]$ ls
flashback_area
full
lost+found
[oracle@oel-01 backup]$ exit
exit
手动指定一下闪回区的大小和目录所在,可以使用默认值。
22:10:53 SQL> alter system set db_recovery_file_dest_size=1g;
System altered.
22:11:47 SQL> alter system set db_recovery_file_dest='/backup/flashback_area';
System altered.
如未指定闪回区,报错如下:
SYS@ bys3>alter database archivelog;
Database altered.
SYS@ bys3>alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38709: Recovery Area is not enabled.
定义一个时间上限,设置数据库能够闪回的最大时间上限,单位分钟。2880是两天。
可以使用默认值。
22:11:58 SQL> alter system set db_flashback_retention_target=2880;
System altered.
22:14:12 SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
22:14:36 SQL> startup mount;
ORACLE instance started.
Total System Global Area 845348864 bytes
Fixed Size 1339796 bytes
Variable Size 583011948 bytes
Database Buffers 255852544 bytes
Redo Buffers 5144576 bytes
Database mounted.
22:20:43 SQL> show parameter db_recovery
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /backup/flashback_area
db_recovery_file_dest_size big integer 1G
打开闪回数据库特性
22:21:18 SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38708: not enough space for first flashback database log file
22:21:32 SQL>
alter system set db_recovery_file_dest_size=2g;
System altered.
22:26:22 SQL>
alter database flashback on;
Database altered.
22:26:27 SQL> alter database open;
Database altered.
22:26:57 SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
2.在数据库中做DDL及DML操作,并删除所建表
22:27:35 SQL> conn bys/bys
Connected.
22:34:41 SQL> create table test7 as select * from test6;
Table created.
22:37:43 SQL> select * from test7;
ABC
---------
1
2
22:37:53 SQL> select current_scn from v$database;
CURRENT_SCN
-----------
1373570
22:38:02 SQL> drop table test7;
Table dropped.
22:38:19 SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
TEST7 BIN$39Q/PNkxLungQFXeqUARIQ==$0 TABLE 2013-06-23:22:38:18
22:38:24 SQL> purge recyclebin;
Recyclebin purged.
22:39:52 SQL> show recyclebin;
22:39:54 SQL> select oldest_flashback_scn from v$flashback_database_log;
OLDEST_FLASHBACK_SCN
--------------------
1373161
22:40:28 SQL> ALTER SESSION SET nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
Session altered.
查询最早的SCN对应的时间---我这里是上面打开闪回数据库特性的时间。
也可以从OLDEST_FLASHBACK_SCN列查最早SCN
22:40:35 SQL> SELECT oldest_flashback_time FROM v$flashback_database_log;
OLDEST_FLASHBACK_TIM
--------------------
23-JUN-2013 22:26:27
3.重启数据库到MOUNT状态进行闪回数据库操作
22:42:19 SQL> shutdown immediate;
ORA-01031: insufficient privileges
22:43:00 SQL> conn / as sysdba
Connected.
22:43:05 SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
22:43:16 SQL> startup mount;
ORACLE instance started.
Total System Global Area 845348864 bytes
Fixed Size 1339796 bytes
Variable Size 583011948 bytes
Database Buffers 255852544 bytes
Redo Buffers 5144576 bytes
Database mounted.
22:49:14 SQL> ALTER SESSION SET nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
Session altered.
22:49:20 SQL> select sysdate from dual;
SYSDATE
--------------------
23-JUN-2013 22:49:25
22:49:25 SQL> SELECT oldest_flashback_time FROM v$flashback_database_log;
OLDEST_FLASHBACK_TIM
--------------------
23-JUN-2013 22:26:27
22:50:38 SQL> select oldest_flashback_scn from v$flashback_database_log;
OLDEST_FLASHBACK_SCN
--------------------
1373161
22:50:48 SQL>
flashback database to scn 1373570;
Flashback complete.
下面语句是测试过可用的使用时间戳闪回,我未使用。
SQL> flashback database to timestamp to_timestamp('2013-06-26 14:05:10','yyyy-MM-dd hh24:mi:ss');
resetlogs打开之后,不能再闪回到 resetlogs之前的时间点。
22:51:53 SQL> alter database open resetlogs;
Database altered.
22:53:18 SQL> show user
USER is "SYS"
4.使用BYS用户登陆验证第2步删除表操作之前的数据是否正常
22:54:12 SQL> conn bys/bys
Connected.
22:55:13 SQL> select * from test7;
ABC
---------
1
2
补充:不确定闪回的时间或SCN是否足够使数据恢复,可以以READ ONLY方式打开数据库
即flashback database to scn 1373570;或flashback database to timestamp to_timestamp('2013-06-26 14:05:10','yyyy-MM-dd hh24:mi:ss');
然后 alter database open read only;然后查询数据看是否需要的数据已经恢复。
如果未恢复,可以继续使用flashback database to SCN/TIMESTAMP这样语句来恢复,直到恢复出需要的数据。
确定恢复出全部需用的数据后,就可以使用alter database open resetlogs;语句打开数据库了。
resetlogs打开数据库后,就不能再使用flashback回到resetlogs之前的时间点了。
增加一个强制检查点的恢复实验--可用于打PSU时刷新数据字典时
SQL> startup mount;
ORACLE instance started.
Total System Global Area 536870912 bytes
Fixed Size 1220432 bytes
Variable Size 125829296 bytes
Database Buffers 406847488 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> create restore point p1 guarantee flashback database;
Restore point created.
SQL> alter database open;
Database altered.
SQL> create user bys identified by bys;
User created.
SQL> grant dba to bys;
Grant succeeded.
SQL> Flashback database to restore point p1;
Flashback database to restore point p1
*
ERROR at line 1:
ORA-38757: Database must be mounted and not open to FLASHBACK.
SQL> conn bys/bys
Connected.
SQL> conn / as sysdba
Connected.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 536870912 bytes
Fixed Size 1220432 bytes
Variable Size 125829296 bytes
Database Buffers 406847488 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> Flashback database to restore point p1;
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> select username from dba_users;
USERNAME
------------------------------
OLTP_USER
OE
HR
SST
SH
TSMSYS
DIP
DBSNMP
OUTLN
SYS
SYSTEM
11 rows selected.
SQL>