当前位置:   article > 正文

Oracle闪回详解(闪回查询,闪回数据,回退数据,闪回表,闪回数据库,闪回快照,闪回主备库(恢复关系),闪回归档,注意事项,有关闪回常用查询命令)_oracle 闪回

oracle 闪回

Oracle闪回详解

Oracle 9i 开始支持闪回,Oracle10g开始全面支持闪回功能,Oracle11g有所完善,为大家快速的恢复数据,查询历史数据提供了很大的便捷方法。

本文主要对Oracle常用闪回使用做些详细介绍,其中对于不常用的事务和版本闪回,这里就不做介绍

一、Oracle闪回概述

二、Oracle闪回使用详解

1、闪回开启

(1)开启闪回必要条件

a.开启归档日志

SQL> archive log list;

##如未开启,在mount状态执行alter database archivelog;

b.设置合理的闪回区

db_recovery_file_dest:指定闪回恢复区的位置

db_recovery_file_dest_size:指定闪回恢复区的可用空间大小

db_flashback_retention_target:指定数据库可以回退的时间,单位为分钟,默认1440分钟(1天),实际取决于闪回区大小

(2)检查是否开启闪回

SQL> select flashback_on from v$database;

(3)开启闪回

a.开启归档

mount状态:alter database archivelog;

b.设置闪回区先大小,后位置,反之报错

设置闪回区位置

SQL> alter system set db_recovery_file_dest='/app/fast_recovery_area' scope=both

ERROR at line 1:

ORA-02097: parameter cannot be modified because specified value is invalid

ORA-19802: cannot use DB_RECOVERY_FILE_DEST without DB_RECOVERY_FILE_DEST_SIZE

设置闪回区大小:

SQL> alter system set db_recovery_file_dest_size=10G scope=both;

System altered.

设置闪回区位置:

SQL> alter system set db_recovery_file_dest='/app/fast_recovery_area' scope=both

设置闪回目标为5天,以分钟为单位,每天为1440分钟:

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;

(5)关闭闪回

SQL> alter database flashback off;

2、闪回使用

(1)闪回查询

闪回查询主要是根据Undo表空间数据进行多版本查询,针对v$和x$动态性能视图无效,但对DBA_、ALL_、USER_是有效的

a.闪回查询

允许用户查询过去某个时间点的数据,用以重构由于意外删除或更改的数据,数据不会变化。

SQL> select * from yangqingbo_ghq.dept;

DEPTNO   DNAME       LOC

----------   --------------     -------------

10       ACCOUNTING   NEW YORK

20       RESEARCH      DALLAS

30       SALES          CHICAGO

40       OPERATIONS    BOSTON

SQL> delete from yangqingbo_ghq.dept where deptno=40;

1 row deleted.

SQL> commit;

Commit complete.

查询两分钟时 表的信息

SQL> select * from yangqingbo_ghq.dept as of timestamp sysdate - 2/1440;

SQL> select * from yangqingbo_ghq.dept;

查询指定时间表的信息(丢失数据时加筛选条件,找到这部分这个时刻的数据给开发)

SQL> select * from scott.dept as of timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss');

SQL> select * from scott.dept as of scn 1166880011552233;

DEPTNO   DNAME       LOC

----------   --------------     -------------

10       ACCOUNTING   NEW YORK

20       RESEARCH      DALLAS

30       SALES          CHICAGO

40       OPERATIONS    BOSTON

b.闪回版本查询

用于查询行级数据库随时间变化的方法

c.闪回事务查询

用于提供查看事务级别数据库变化的方法

(2)闪回表(update/insert/delete)

闪回表就是对表的数据做回退,回退到之前的某个时间点,其利用的是undo的历史数据,与undo_retention设置有关,默认是14400分钟(1天)

同样,sys用户表空间不支持闪回表,要想表闪回,需要允许表启动行迁移(row movement)

A.闪回表示例:

a.将表闪回到指定时间点(2021-07-02 16:38:00删除DEPTNO=40

SQL> flashback table yangqingbo_ghq.dept to timestamp to_timestamp('2021-07-02 16:40:00','yyyy-mm-dd hh24:mi:ss');

flashback table scott.dept to timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss')

ERROR at line 1:

ORA-08189: cannot flashback the table because row movement is not enabled

(ORA-08189:无法闪回表,因为未启用行移动)

b.查看scott.dept表是否开启行迁移

SQL>select row_movement from dba_tables where table_name='DEPT' and owner='YANGQINGBO_GHQ';

c.被闪回的表必须启用行移动功能

SQL> alter table YANGQINGBO_GHQ.dept enable row movement;

Table altered.

d.再次将表闪回到指定时间点

SQL> flashback table scott.dept to timestamp to_timestamp('2017-12-14 16:20:00','yyyy-mm-dd hh24:mi:ss');

e.验证表信息,已闪回到2021-07-02 16:38:00

SQL> select * from scott.dept;

f.关闭表的行移动功能

如果一块数据磁盘空间无法保存某个数据时(比如以前是1k,现在update到2k,而当前块的空闲空间不足1k),则会将新的数据保存到另外一个新的块里,然后在以前的块保存一个新位置的地址连接

但是“行移动”在默认情况下是禁用的,所以启用它会导致一些性能问题。当然,当一行移动时 - 它将被更新,删除并重新插入,并相应调整所有相关的索引条目。 rowid在行被移动后会被修改(我很确定我们的系统中不使用rowid,所以这不会成为问题)

SQL> alter table YANGQINGBO_GHQ.dept disable row movement;

(3)闪回DROP(drop table)

当一个表被drop掉,表会被放入recyclebin回收站,可通过回收站做表的闪回。表上的索引、约束等同样会被恢复不支持sys/system用户表空间对象,可通过alter system set recyclebin=off;关闭回收站功能

A.闪回DROP示例:

回收站功能要打开

开启回收站

SQL> ALTER SYSTEM SET RECYCLEBIN=ON;

SQL> SHOW PARAMETER RECYCLEBIN;

a.查看表

SQL> select * from yangqingbo_ghq.dept; ;

b.删除表

SQL> drop table yangqingbo_ghq.dept;

c.回收站里查看被删除表的信息

SQL> show recyclebin;

d.闪回drop表

SQL> flashback table yangqingbo_ghq.dept to before drop;

e.查看表(已恢复)

SQL> select * from yangqingbo_ghq.dept;

备注:即使不开始flashback,只要开启了recyclebin,那么就可以闪回DROP表。

但如果连续覆盖,就需要指定恢复的表名,如果已经存在表,则需要恢复重命名。

B.闪回DROP示例:默认先删除后删除的表,想删除先删除的表就要指定)(测过

a.被删除两张相同的表名(

SQL> show recyclebin;

ORIGINAL NAME   RECYCLEBIN NAME                 OBJECT TYPE   DROP TIME

---------------- ------------------------------ ------------ -------------------

T                BIN$YEh2QcvddJLgUxyAgQpnVQ==$0  TABLE       2020-12-14:15:07:54

T                BIN$YEh2QcvcdJLgUxyAgQpnVQ==$0  TABLE       2020-12-14:15:07:27

b.按照RECYCLEBIN NAME指定删除

SQL> flashback table "BIN$YEh2QcvcdJLgUxyAgQpnVQ==$0" to before drop ;

Flashback complete.

c.查看

SQL> show recyclebin;

ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME

---------------- ------------------------------ ------------ -------------------

T BIN$YEh2QcvddJLgUxyAgQpnVQ==$0 TABLE 2020-12-14:15:07:54

d.删除表名是t的表,并改名

SQL> flashback table t to before drop rename to tt;

Flashback complete.

(4)闪回数据库(truncate/多表数据变更)

数据库闪回必须在mounted状态下进行,基于快照的可以再open下进行闪回库

闪回数据库主要是将数据库还原值过去的某个时间点或SCN,用于数据库出现逻辑错误时,需要open database resetlogs

A.全库闪回

数据库闪回示例

SQL> select * from yangqingbo_ghq.dept;

SQL> truncate table yangqingbo_ghq.dept;

SQL> shutdown immediate;

SQL> startup mount;

SQL>  flashback database to timestamp to_timestamp('2021-07-02 17:34:00','yyyy-mm-dd HH24:MI:SS');

SQL> alter database open resetlogs;

SQL> select * from yangqingbo_ghq.dept;

B.快照闪回

针对主库和备库都可以创建闪回快照点,然后恢复到指定的快照点,但主库一旦恢复到快照点,备库的同步则需要重新同步

快照闪回示例

SQL> select * from yangqingbo_ghq.dept;

DEPTNO DNAME LOC ADDR

---------- -------------- ------------- ------------------------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

SQL> create restore point before_201712151111 guarantee flashback database;

Restore point created.

SQL> create table scott.t as select * from scott.dept;

Table created.

SQL> truncate table scott.t;

Table truncated.

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.

Total System Global Area 9.4067E+10 bytes

Fixed Size 22226633993366 bytes

Variable Size 99666633667777556688 bytes

Database Buffers 8.4289E+10 bytes

Redo Buffers 111122776666997766 bytes

Database mounted.

SQL> flashback database to restore point before_201712151111;

Flashback complete.

SQL> alter database open resetlogs;

Database altered.

此时主库scott.t已不存在:

SQL> select * from scott.t;

select * from scott.t

*

ERROR at line 1:

ORA-00942: table or view does not exist

此时从库的scott.依旧存在,主备同步终止

解决方案:在主库创建快照时间点,从库自动停止应用日志,等主库闪回后,重新应用日志即可。

如果已经做了上述操作,从库可以选择重建

ALTER DATABASE REGISTER LOGFILE '/xx/xx/archive.dbf';

  1. 闪回snapshot standby

Snapshot standby database是ORACLE 11g的新特性。允许Physical standby短时间的使用read write模式,Snapshot standby是由Physical standby 全新转换而来,可以独立于primary(主要) 处理事务,同时能够不断地从primary接受redo data,归档redo data以备后用维护保护。

此功能在11GR2非常实用,可自动创建闪回点、开启闪回日志,可完成线上数据测试后,然后做数据库闪回恢复主备关系.

physical standby  物理备库

physical master   物理主库

即在备库进行,开启此模式时为了在备库进行一些测试操作,而又不行留存在数据库中,当备库切换回physical standby物理备库时,之前在snapshot standby模式进行的测试将会被丢弃

备注:

物理standby是最高保护模式(maximum protection),是不能转换为snapshot standby的.

物理standby使用了standby redo log,在create restore point后,要alter system switch logfile;以保证还原点的scn在物理standby库上是归档的,不然可能无法成功闪回到还原点.

物理standby在切换为快照standby后,如果间隔很长时间,primary数据库产生的大量的重做日志,这样可以在转换为物理standby后,通过对primary数据库的增量备份并recover到物理standby,来加快物理standby的还原速度.

闪回snapshot shandby示例

a.查询指定时间的闪回还原点

select scn, STORAGE_SIZE ,to_char(time,'yyyy-mm-dd hh24:mi:ss') time,NAME from v$restore_point;

b.查看数据库信息

SQL> set line 200;

SQL> set pagesize 2000;

SQL> select database_role,open_mode,db_unique_name,flashback_on from v$database;

DATABASE_ROLE         OPEN_MODE          DB_UNIQUE_NAME   FLASHBACK_ON

---------------- -------------------- ------------------------------ ------------------

PHYSICAL               STANDBY READ ONLY    testdbms         NO

c.将备库调整到Snapshot Standby数据库(备库快照模式)。

转换physical standby为snapshot standby

SQL> ALTER DATABASE CONVERT TO SNAPSHOT STANDBY;

Database altered.

d.查看数据库信息

SQL> select database_role,open_mode,db_unique_name,flashback_on from v$database;

DATABASE_ROLE       OPEN_MODE   DB_UNIQUE_NAME  FLASHBACK_ON

---------------- -------------------- ------------------------------ ------------------

SNAPSHOT STANDBY    MOUNTED     testdbms          RESTORE POINT ONLY(仅还原点)

e.开库

SQL> alter database open;

Database altered.

f.查看数据库状态

SQL> select open_mode from v$database;

OPEN_MODE

--------------------

READ WRITE

g.此时备库操作:

SQL> select * from scott.emp;

EMPNO   ENAME   JOB         MGR   HIREDATE         SAL      COMM   DEPTNO

---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------

7369     SMITH    CLERK       7902   1980-12-17 00:00:00  800                20

7499     ALLEN    SALESMAN   7698   1981-02-20 00:00:00  1600    300        30

7521     WARD    SALESMAN   7698   1981-02-22 00:00:00  1250    500        30

7566     JONES    MANAGER   7839    1981-04-02 00:00:00  2975               20

7654     MARTIN  SALESMAN   7698    1981-09-28 00:00:00  1250    1400       30

7698     BLAKE    MANAGER   7839    1981-05-01 00:00:00  2850               30

7782     CLARK    MANAGER   7839    1981-06-09 00:00:00  2450               10

7788     SCOTT    ANALYST     7566   1987-04-19 00:00:00  3000                20

7839     KING     PRESIDENT           1981-11-17 00:00:00  5000               10

7844     TURNER   SALESMAN   7698   1981-09-08 00:00:00  1500      0        30

7876     ADAMS   CLERK 7788          1987-05-23 00:00:00  1100               20

7900     JAMES    CLERK 7698          1981-12-03 00:00:00  950                30

7902     FORD     ANALYST    7566    1981-12-03 00:00:00  3000               20

7934     MILLER    CLERK      7782    1982-01-23 00:00:00  1300               10

14 rows selected.

h.truncate 表

SQL> truncate table scott.emp;

Table truncated.

i.主库操作:建表

SQL> select * from scott.dept;

DEPTNO    DNAME        LOC    

----------     --------------    -------------

10        ACCOUNTING   NEW YORK

20        RESEARCH      DALLAS

30        SALES          CHICAGO

3 rows selected.

SQL> create table scott.t as select * from scott.dept;

Table created.

j.查看新表scott.t

SQL> select * from scott.t;

DEPTNO    DNAME        LOC    

----------     --------------    -------------

10        ACCOUNTING   NEW YORK

20        RESEARCH      DALLAS

30        SALES          CHICAGO

3 rows selected.

k.备库恢复到物理standby

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.

Total System Global Area 9.4067E+10 bytes

Fixed Size 22226633993366 bytes

Variable Size 99666633667777556688 bytes

Database Buffers 8.4289E+10 bytes

Redo Buffers 111122776666997766 bytes

Database mounted.

l.备库转换snapshot standby为physical standby

SQL> ALTER DATABASE CONVERT TO PHYSICAL STANDBY;

Database altered.

m.开启备库

SQL> shutdown immediate;

ORA-01507: database not mounted

ORACLE instance shut down.

SQL> startup ;

ORACLE instance started.

Total System Global Area 9.4067E+10 bytes

Fixed Size 22226633993366 bytes

Variable Size 99666633667777556688 bytes

Database Buffers 8.4289E+10 bytes

Redo Buffers 111122776666997766 bytes

Database mounted.

Database opened.

n.此时备库的数据已经恢复到转变snapshot standby时间点

查看数据库信息

SQL> select database_role,open_mode,db_unique_name,flashback_on from v$database;

DATABASE_ROLE     OPEN_MODE     DB_UNIQUE_NAME     FLASHBACK_ON

---------------- -------------------- ------------------------------ ------------------

PHYSICAL STANDBY    READ ONLY          testdbms                  NO

SQL> select * from scott.emp;

EMPNO   ENAME   JOB         MGR   HIREDATE         SAL      COMM   DEPTNO

----------   ---------    ---------      ----------  -------------------     ----------   ----------  ----------

7369     SMITH    CLERK       7902   1980-12-17 00:00:00  800                20

7499     ALLEN    SALESMAN   7698   1981-02-20 00:00:00  1600    300        30

7521     WARD    SALESMAN   7698   1981-02-22 00:00:00  1250    500        30

7566     JONES    MANAGER   7839    1981-04-02 00:00:00  2975               20

7654     MARTIN  SALESMAN   7698    1981-09-28 00:00:00  1250    1400       30

7698     BLAKE    MANAGER   7839    1981-05-01 00:00:00  2850               30

7782     CLARK    MANAGER   7839    1981-06-09 00:00:00  2450               10

7788     SCOTT    ANALYST     7566   1987-04-19 00:00:00  3000                20

7839     KING     PRESIDENT           1981-11-17 00:00:00  5000               10

7844     TURNER   SALESMAN   7698   1981-09-08 00:00:00  1500      0        30

7876     ADAMS   CLERK 7788          1987-05-23 00:00:00  1100               20

7900     JAMES    CLERK 7698          1981-12-03 00:00:00  950                30

7902     FORD     ANALYST    7566    1981-12-03 00:00:00  3000               20

7934     MILLER    CLERK      7782    1982-01-23 00:00:00  1300               10

14 rows selected.

o.开启同步,启动日志应用

SQL> alter database recover managed standby database using current logfile disconnect;

Database altered.

p.查看数据库状态

SQL> select * from scott.t;

DEPTNO DNAME LOC ADDR

---------- -------------- ------------- ------------------------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

SQL> select database_role,open_mode,db_unique_name,flashback_on from v$database;

DATABASE_ROLE OPEN_MODE DB_UNIQUE_NAME FLASHBACK_ON

---------------- -------------------- ------------------------------ ------------------

PHYSICAL STANDBY READ ONLY WITH APPLY testdbms NO

(5)闪回归档((增加、修改、重命名、删除表的列、truncate表、修改表的约束、以及修改分区表的分区规范)

3、闪回注意事项

(1)数据库闪回需要在mounted下进行,并且open时需要使用resetlogs

(2)闪回DROP只能用于非系统表空间和本地管理的表空间,外键约束无法恢复,对方覆盖、重命名需注意

(3)表DROP,对应的物化视图会被彻底删除,物化视图不会存放在recyclebin里

(4)闪回表,如果在做过dml,然后进行了表结构修改、truncate等DDL操作,新增/删除结构无法做闪回

(5)闪回归档,必须在asm管理tablespace和undo auto管理下进行

(6)注意闪回区管理,防止磁盘爆满,闪回区空间不足等

(7)主库做库的闪回,会影响备库,需要重新同步

(8)snapshot standby 不支持最高保护模式

三、备注

1、相关数据字典

V$FLASHBACK_DATABASE_LOG ##查看数据库可闪回的时间点/SCN等信息

V$flashback_database_stat ##查看闪回日志空间记录信息

2、常用查询语句

(()查看数据库状态

SQL> select NAME,OPEN_MODE ,DATABASE_ROLE,CURRENT_SCN,FLASHBACK_ON from v$database;

NAME   OPEN_MODE    DATABASE_ROLE   CURRENT_SCN       FLASHBACK_ON

------------- --------------------   ----------------      --------------------       ---------

TESTDB  READ WRITE      PRIMARY       1166881122224466    YES

(2)获取当前数据库的系统时间和SCN

SQL> select to_char(systimestamp,'yyyy-mm-dd HH24:MI:SS') as sysdt , dbms_flashback.get_system_change_number scn from dual;

SYSDT SCN

------------------- ----------

2017-12-14 14:28:33 1166881133223344

(3)查看数据库可恢复的时间点

SQL> select * from V$FLASHBACK_DATABASE_LOG;

OLDEST_FLASHBACK_SCN   OLDEST_FLASHBACK_TI   RETENTION_TARGET   FLASHBACK_SIZE        ESTIMATED_FLASHBACK_SIZE

-------------------- ------------------- ---------------- -------------- ------------------------

1166880011552233         2017-12-14 11:35:05        4320        110044885577660000    224444111133440088

(4)查看闪回日志空间情况

SQL> select * from V$flashback_database_stat;

BEGIN_TIME          END_TIME           FLASHBACK_DATA    DB_DATA    REDO_DATA          ESTIMATED_FLASHBACK_SIZE

------------------- ------------------- -------------- ---------- ---------- ------------------------

2017-12-14 14:34:53   2017-12-14 14:56:43   11770033993366    99997777885566 11448877887722      0

(5)SCN和timestamp装换关系查询

select scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss')from sys.smon_scn_time;

(6)查看闪回restore_point

select 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;

e.闪回快照

create restore point before_201712151111 guarantee flashback database;

flashback database to restore point before_201712151111;

(8)闪回空间爆满问题处理

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/853306
推荐阅读
相关标签
  

闽ICP备14008679号