[terry笔记]Flashback

时间:2023-03-08 21:55:27
[terry笔记]Flashback

flashback 闪回,主要功能有两个:闪回查询(flashback query)、闪回恢复(flashback table/database)

开启闪回flashback功能(归档下才可开启数据库闪回)

select name,flashback_on from v$database; /*查看flashback是否开启*/
show parameter db_recovery; /*查看闪回恢复区及大小是否设置*/
alter system set db_recovery_file_dest_size='2G'; /*先设置闪回恢复区的大小*/
alter system set db_recovery_file_dest='c:\u01\fast_recovery_area'; /*再设置闪回恢复区路径*/
alter system set db_flashback_retention_target = 1440; /*设置数据库回退的时间,默认1440分钟为一天时间*/
alter database flashback on; /*开启flashback功能*/

闪回查询需要设置undo_management=auto(默认即可)
闪回表需要recycle bin
闪回数据库需要flashback area

一、flashback query闪回查询
1.as of timestamp

select * from t as of timestamp sysdate-20/1440; /*按时间查询,此查询是查询20分钟前的*/ 

2.as of scn

select * from t as of scn 1234567;

当前scn:

select dbms_flashback.get_system_change_number from dual;
select current_scn from v$database;

其实在oracle内部as of timestamp/scn 最后都是转换为scn进行闪回查询。
可以通过timestamp_to_scn与scn_to_timestamp进行转换

SQL> select timestamp_to_scn(sysdate) from dual;
SQL> select to_char(scn_to_timestamp(1582900),'yyyy-mm-dd hh24:mi:ss') from dual;

闪回查询需要保证表的结构没有做过ddl

二、flashback table闪回表
如果删除的表含有索引、触发器、约束、物化试图等,需要在恢复表后恢复其完整性。

flashback table xx.xxx to before drop rename to xxx; /*按照删除闪回不用开启行迁移*/

alter table xxx enable row movement; /*因为闪回表需要改变rowid,所以需要行迁移*/
select row_movement from user_tables where table_name = 'XXX';
/*根据scn/timestamp闪回,需要注意其触发器会失效,可以加上选项enable triggers*/
flashback table xxx to scn 1234567;
flashback table xxx to timestamp sysdate-x/1440;

此功能需要开启回收站:

show parameter recyclebin

select object_name,original_name,type from recyclebin; /*查询回收站中都有哪些对象*/
SQL> drop table t;
表已删除。 SQL> select object_name,original_name,type from recyclebin; OBJECT_NAME ORIGINAL_NAME TYPE
------------------------------ -------------------------------- -------------------------
BIN$OiJJIiwuTaCJqY+UV8eTWQ==$0 T TABLE SQL> flashback table t to before drop;
flashback table "BIN$OiJJIiwuTaCJqY+UV8eTWQ==$0" to before drop;
闪回完成。

三、flashback database闪回数据库
闪回数据库无法恢复被删除的数据文件,控制文件不可以被重建,不支持shrink后的恢复。
需要设置db_recovery_file_dest(_size),并且会在这里面产生flashback logs,需要开启force logging

select flashback_on,force_logging from v$database;
alter database flashback on;
alter database force logging;
show parameter db_flashback_retention_target /*希望能恢复到多长时间以内,默认1440分钟*/
SQL> select current_scn from v$database;

CURRENT_SCN
-----------
1592391 SQL> drop table t purge;
表已删除。 SQL> shutdown immediate
ORA-01031: 权限不足
SQL> conn / as sysdba
已连接。
SQL> shutdown immediate
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。 SQL> startup mount;
ORACLE 例程已经启动。
Total System Global Area 535662592 bytes
Fixed Size 1385840 bytes
Variable Size 352324240 bytes
Database Buffers 176160768 bytes
Redo Buffers 5791744 bytes
数据库装载完毕。 SQL> flashback database to scn 1592391;
闪回完成。 SQL> alter database open resetlogs;
数据库已更改。
/*open resetlogs会丢失flashback的时间点到目前的数据,可以用read only的方式打开数据库,用逻辑导出误操作的表,再执行recover database重新应用redo,将数据库恢复到flashback database之前状态,然后逻辑导入。*/
alter database open read only;