暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle备份恢复之闪回技术

原创 IT邦德 2021-09-16
1035
作者:IT邦德

中国DBA联盟(ACDU)成员,目前从事DBA及程序编程(Web\java\Python)工作,主要服务于生产制造
现拥有 Oracle 11g  OCP/OCM、Mysql、Oceanbase(OBCA)认证
分布式TBase\TDSQL数据库、国产达梦数据库以及红帽子认证
从业8年DBA工作,在数据库领域有丰富的经验
B站主播Oracle、Mysql、PG实战课程,请搜索:jeames007

微信:jem_db
QQ交流群:168797397
更多内容请关注B站(搜索jeames007)

1.闪回查询

原理:用户恢复删除的数据,利用的是undo表空间的数据,闪回查询过去的某一个时间点或SCN的数据信息。

SYS@PROD> conn scott/tiger
SCOTT@PROD> create table t_fb_query1 as select * from dept;
SCOTT@PROD> select * from t_fb_query1;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
SCOTT@PROD> set time on
14:30:49 SCOTT@PROD> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
##也可以更改当前会话的时间格式:
alter session set nls_date_format='yyyy-mm-dd hh24:miss';
select sysdate from dual;
2021-09-11 10:02:15
##查询当前SCN
14:31:58 SYS@PROD> select current_scn from v$database;

CURRENT_SCN
-----------
    1070729
    
模拟删除10号部门的数据
14:33:25 SCOTT@PROD> delete t_fb_query1 where deptno=10;
14:33:53 SCOTT@PROD> select * from t_fb_query1;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON


语法:select * from 表名 as of  timestamp/SCN;

select * from t_fb_query1 
as of timestamp to_timestamp('2021-09-11 10:02:15','yyyy-mm-dd hh24:mi:ss');
   DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
   
 ##利用SCN查询   
14:35:31 SCOTT@PROD> select * from t_fb_query1 as of SCN  1070729;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        
##插入闪回的数据  
insert into scott.t_fb_query1
select * from scott.t_fb_query1 
as of timestamp to_timestamp('2021-09-11 10:02:15','yyyy-mm-dd hh24:mi:ss')
minus
select * from scott.t_fb_query1;

2.闪回表

原理:利用的是Undo表空间的undo数据,闪回表到某个时间点或某个SCN
##连接SCOTT用户创建业务表:
conn scott/tiger
create table fb_1 (id number);
insert into fb_1 values(10);
commit;


alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
select sysdate from dual;


##查看此时的时间或者SCN:
时间:
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
2021-09-11 10:17:27
SCN:conn / as sysdba 
select current_scn from v$database;
CURRENT_SCN
-----------
     1071438

##执行数据删除操作:
delete from scott.fb_1;
commit;

##闪回表到数据删除之前

开启被闪回表的行迁移:
14:42:45 SYS@PROD> alter table scott.fb_1 enable row movement;
 
 闪回查询确认数据:
select  * from scott.fb_1 as of scn 1071438;

执行闪回,可以执行多次闪回操作:
flashback table scott.fb_1 to scn  1071438;

或者
flashback table scott.fb_1 
to timestamp to_timestamp('2021-09-11 10:17:27','yyyy-mm-dd hh24:mi:ss');

验证数据:
SYS@PROD> select * from scott.fb_1;
        ID
----------
        10
##关闭行迁移
SYS@PROD> alter table scott.fb_1 disable row movement;

默认文件1630210657122.png

本文如有错误或不完善的地方请大家多多指正,请关注公众号:IT邦德
留言微信:jem_db 或 QQ:2243967774 皆可,您的批评指正是我写作的最大动力!
大家多多点赞,转发,谢谢

最后修改时间:2021-09-17 00:01:35
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论