数据库闪回
1. 闪回开启
1、闪回开启a.开启归档alter database archivelog;b.设置闪回区db_recovery_file_dest:指定闪回恢复区的位置db_recovery_file_dest_size:指定闪回恢复区的可用空间大小db_flashback_retention_target:指定数据库可以回退的时间,单位为分钟,默认1440分钟(1天),实际取决于闪回区大小SQL> alter system set db_recovery_file_dest_size=60G scope=both;SQL> alter system set db_recovery_file_dest='/oracle/data/fast_recovery_area' scope=both;SQL> alter system set db_flashback_retention_target=4320 scope=both;c.开启flashback (10g在mount开启)SQL> alter database flashback on;(4)确定闪回开启SQL> select flashback_on from v$database;FLASHBACK_ONYES(5)关闭闪回SQL> alter database flashback off;
2. 闪回drop
ALTER SESSION SET recyclebin = ON; ALTER SYSTEM SET recyclebin = ON SCOPE = SPFILE; SELECT * FROM RECYCLEBIN WHERE TYPE='TABLE'; 1.对system表空间存储的数据无效2.删除时使用purge 则无效3.索引(drop index)无效 (1)、查询这个“回收站”或者查询user_table视图来查找已被删除的表:show recyclebin;select table_name,dropped from user_tables;select object_name,original_name,type,droptime from user_recyclebin;以上表名都是被重命名过的,字段table_name或者object_name就是删除后在回收站中的存放表名。flashback table 原表名 to before drop;flashback table "Bin$DSbdfd4rdfdfdfegdfsf==$0" to before drop rename to 新表名;使用数据库闪回功能,可以使数据库回到过去某一状态, 语法如下:alter database flashback on;flashback database to scn tmp_db1;flashback database to timestamp to_timestamp('2017-6-28 11:30','yyyy-mm-dd hh24:mi:ss');SQL> create table student("id" char(6),"name" varchar2(10),"sex" char(1),"age" integer,"phone" varchar2(12)) tablespace test_db;SQL> drop table student;SQL> drop table student1 purge;SQL> purge table TR02;create table test tablespace users as select * from "BIN$59YTYxa8dv3gU45YHazhyQ==$0";SQL> flashback table student to before drop;SQL> flashback table "BIN$59YTYxbCdv3gU45YHazhyQ==$0" to before drop;
3. 闪回delete
(1) 确定删除数据的时间(在删除数据之前的时间就行,最好是删除数据的时间点)select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;select * from test as of timestamp to_timestamp('2017-06-28 11:04','yyyy-mm-dd hh24:mi:ss');insert into test ( select * from test as of timestamp to_timestamp('2017-06-28 11:04','yyyy-mm-dd hh24:mi:ss'));commit;(2) 如果表结构没有发生改变,还可以直接使用闪回整个表的方式来恢复数据,表闪回要求用户必须要有flash any table权限。alter table test enable row movement;flashback table test to timestamp to_timestamp('2022-09-23 17:17:28','YYYY-MM-DD HH24:MI:SS');alter table test disable row movement;
4. 闪回truncate
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;SQL> truncate table scott.EMP;SQL> shutdown immediate;SQL> startup mount;SQL> flashback database to timestamp to_timestamp('2017-12-14 14:12:46','yyyy-mm-dd HH24:MI:SS');SQL> alter database open resetlogs;SQL> select * from scott.emp;
5. 常用查询语句
1、相关数据字典V$FLASHBACK_DATABASE_LOG V$flashback_database_stat 2、常用查询语句(1)查看数据库状态SQL> select NAME,OPEN_MODE ,DATABASE_ROLE,CURRENT_SCN,FLASHBACK_ON from v$database;NAME OPEN_MODE DATABASE_ROLE CURRENT_SCN FLASHBACK_ONTESTDB READ WRITE PRIMARY 16812246 YES(2)获取当前数据库的系统时间和SCNSQL> select to_char(systimestamp,'yyyy-mm-dd HH24:MI:SS') as sysdt, dbms_flashback.get_system_change_number scn from dual;(3)查看数据库可恢复的时间点SQL> select * from V$FLASHBACK_DATABASE_LOG;OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI RETENTION_TARGET FLASHBACK_SIZE ESTIMATED_FLASHBACK_SIZE16801523 2017-12-14 11:35:05 4320 104857600 244113408(4)查看闪回日志空间情况SQL> select * from V$flashback_database_stat;BEGIN_TIME END_TIME FLASHBACK_DATA DB_DATA REDO_DATA ESTIMATED_FLASHBACK_SIZE2017-12-14 14:34:53 2017-12-14 14:56:43 1703936 9977856 1487872 0(5)SCN和timestamp装换关系查询select scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss')from sys.smon_scn_time;(6)查看闪回restore_pointselect scn, STORAGE_SIZE ,to_char(time,'yyyy-mm-dd hh24:mi:ss') time,NAME from v$restore_point;(7)闪回语句a.闪回数据库 FLASHBACK DATABASE TO TIMESTAMP to_timestamp('2017-12-14 14:28:33','yyyy-mm-dd HH24:MI:SS');; flashback database to scn 16813234;b.闪回DROP其中table_name可以是删除表名称,也可以是别名 flashback table table_name to before drop; flashback table table_name to before drop rename to table_name_new;c.闪回表 flashback table table_name to scn scn_number; flashback table table_name to timestamp to_timestamp('2017-12-14 14:28:33','yyyy-mm-dd hh24:mi:ss');d.闪回查询 select * from table_name as of timestamp to_timestamp('2017-12-14 14:28:33','yyyy-mm-dd hh24:mi:ss'); select * from scott.dept as of scn 16801523;