欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle误删除表数据后的数据恢复详解

程序员文章站 2022-08-04 16:58:52
oracle误删除表数据后的恢复详解   测试环境: system:ibm aix 5l     &nbs...

oracle误删除表数据后的恢复详解
 
测试环境:
system:ibm aix 5l                         oracle version:10gr2
 

1. undo_retention参数的查询与修改

使用show parameter undo命令查看当前的数据库参数undo_retention设置。
显示如下:
sql> show parameter undo

name                                 type        value
------------------------------------ ----------- ------------------------------
undo_management                      string      auto
undo_retention                       integer     900
undo_tablespace                      string      undotbs2
undo_retention(保持力),900单位是秒,即15分钟。
修改默认的undo_retention参数设置:
sql> alter system set undo_retention=10800 scope=both;

system altered.

sql> show parameter undo

name                                 type        value
------------------------------------ ----------- ------------------------------
undo_management                      string      auto
undo_retention                       integer     10800
undo_tablespace                      string      undotbs2
undo_retention 10800,单位秒,即3小时。

2. oracle误删除表数据后的的快速恢复功能方法

2.1 方法一
通过oracle提供的回闪功能

exec dbms_flashback.enable_at_time(to_date('2011-04-15 08:21:00','yyyy-mm-dd hh24:mi:ss'));
set serveroutput on
declare r_temp hr.job_history%rowtype;
cursor c_temp is select * from hr.job_history;
begin
open c_temp;
dbms_flashback.disable;
loop
fetch c_temp into r_temp;
exit when c_temp%notfound;
insert into hr.job_history(employee_id,job_id,start_date,end_date) values (r_temp.employee_id,r_temp.job_id,r_temp.start_date,r_temp.end_date);
commit;
end loop;
close c_temp;
end;

这种办法可以将删除的数据恢复到对应的表中,首先要保证该用户有执行dbms_flashback包的权限。

2.2 方法二
insert into hr.job_history
select * from hr.job_history as of timestamp to_timestamp('2011-04-15 08:20:00', 'yyyy-mm-dd hh24:mi:ss');
这种方法简单,容易掌握,功能和上面的一样,此处的时间为你误操作之前的时间,最好是离误操作比较近的,因为oracle保存在回滚保持段里的数据时间有一定的时间限制,这个限制由undo_retention 这个参数值决定。

查看first_change#,next_change#,first_time
sql> set pagesize 9999
sql> col fscn for 999999999
sql> col nscn for 999999999
sql> select name,first_change# fscn,next_change# nscn,first_time from v$archived_log;

当前的scn为:
sql> select dbms_flashback.get_system_change_number fscn from dual;
      fscn
----------
   3435958

使用应用用户尝试闪回
sql> connect username/password
connected.

现有数据:
sql> select count(*) from hs_passport;
  count(*)
----------
    851998
创建恢复表:
sql> create table hs_passport_recov as select * from hs_passport where 1=0;

table created.

选择scn向前恢复:
sql> select count(*) from hs_passport as of scn 12929970422;
  count(*)
----------
    861686

尝试多个scn,获取最佳值(如果能得知具体时间,那么可以获得准确的数据闪回)

sql> select count(*) from hs_passport as of scn &scn;
enter value for scn: 12929941968
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12929941968
 count(*)
----------
  861684

sql> /
enter value for scn: 12927633776
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12927633776
select count(*) from hs_passport as of scn 12927633776
           *
error at line 1:
ora-01466: unable to read data - table definition has changed

sql> /
enter value for scn: 12929928784
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12929928784

 count(*)
----------
  825110

sql> /
enter value for scn: 12928000000
old  1: select count(*) from hs_passport as of scn &scn
new  1: select count(*) from hs_passport as of scn 12928000000
select count(*) from hs_passport as of scn 12928000000
           *
error at line 1:
ora-01466: unable to read data - table definition has changed

最后选择恢复到scn为12929941968的时间点
sql> insert into hs_passport_recov select * from hs_passport as of scn 12929941968;

861684 rows created.

sql> commit;

commit complete.

数据恢复简单例子
在过去,如果用户误删/更新了数据后,作为用户并没有什么直接的方法来进行恢复,他们必须求助dba来对数据库进行恢复,到了oracle9i,这一难堪的局面有所改善。oracle 9i中提供了一项新的技术手段--闪回查询,用户使用闪回查询可以及时取得误操作前的数据,并可以针对错误进行相应的恢复措施,而这一切都无需dba干预。

3. 下面我们通过一个例子来具体说明闪回查询的用法

示例
3.1 使用闪回查询前必须确定下面两个参数:
undo_management = auto
undo_retention = 10800;
这个时间可以随便设,它表示在系统中保留提交了的undo信息的时间,10800就是保留3小时,即180分钟。
3.2 使用闪回查询

sql> conn /as sysdba
connected.
sql> drop user lsf cascade;

user dropped.

sql> create user lsf identified by lsf;

user created.

sql> grant connect,resource to lsf;

grant succeeded.

sql> grant execute on dbms_flashback to lsf;

grant succeeded.

sql> conn lsf/lsf
connected.
sql> create table t(id int, name varchar2(20));

table created.

sql> insert into t values(1,'lsf');

1 row created.

sql> insert into t values(2,'lsf');

1 row created.

sql> insert into t values(3,'lsf');

1 row created.

sql> commit;

commit complete.

sql> select * from t;

    id name
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf

sql> set time on
10:12:50 sql> delete from t where id=1;

1 row deleted.

10:13:02 sql> commit;

commit complete.

10:13:10 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     2 lsf
     3 lsf

10:13:18 sql> execute dbms_flashback.enable_at_time(to_date('2011-04-15 10:12:50','yyyy-mm-dd hh24:mi:ss'));

pl/sql procedure successfully completed.

10:13:50 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf

10:13:57 sql> execute dbms_flashback.disable;

pl/sql procedure successfully completed.

10:15:48 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     2 lsf
     3 lsf

3.3 使用闪回查询恢复数据

10:16:59 sql> truncate table t;

table truncated.

10:18:15 sql> select * from t;

no rows selected

10:18:22 sql> insert into t values(1,'lsf');

1 row created.

10:19:42 sql> insert into t values(2,'lsf');

1 row created.

10:19:48 sql> insert into t values(3,'lsf');

1 row created.

10:19:55 sql> insert into t values(4,'lsf');

1 row created.

10:20:07 sql> insert into t values(5,'lsf');

1 row created.

10:20:15 sql> insert into t values(6,'lsf');

1 row created.

10:20:21 sql> commit;

commit complete.

10:20:26 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

10:20:56 sql> delete t;

6 rows deleted.

10:21:27 sql> commit;

commit complete.

10:21:40 sql> declare
10:22:29  2 cursor flash_recover is
10:22:43  3 select * from t;
10:22:50  4 t_recode t%rowtype;
10:23:11  5 begin
10:23:14  6 dbms_flashback.enable_at_time(to_date('2011-04-15 10:20:56','yyyy-mm-dd hh24:mi:ss'));
10:24:22  7 open flash_recover;
10:24:39  8 dbms_flashback.disable;
10:24:59  9 loop
10:25:05 10 fetch flash_recover into t_recode;
10:25:24 11 exit when flash_recover%notfound;
10:25:45 12 insert into t values(t_recode.id,t_recode.name);
10:26:35 13 end loop;
10:26:39 14 close flash_recover;
10:26:50 15 commit;
10:26:56 16 end;
10:26:58 17 /

pl/sql procedure successfully completed.

10:27:00 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

我们可以已经恢复了所有的6条纪录,但是由于闪回查询的局限性,有可能不能恢复所有的6条记录,原因就在下面。

4. 局限性
4.1 闪回查询是基于scn的,虽然我们执行的是:
dbms_flashback.enable_at_time(to_date('2011-04-15 10:20:56','yyyy-mm-dd hh24:mi:ss'));
但oracle并不会精确的这个时间点,而是round down到最近的一次scn,然后从这个scn开始进行恢复。而oracle 9i是每五分钟记录一次scn的,并将scn和对应时间的映射做个纪录。
因此如果使用dbms_flashback.enable_at_time来进行恢复,为了避免恢复失败,我们可以先等5分钟,然后再进行恢复。
使用dbms_flashback.enable_at_time进行恢复还有一个缺点,那就是在oracle 9i中scn和对应时间的映射信息只会保留5天,因此我们无法通过dbms_flashback.enable_at_time来恢复5天前的数据。如果你想使用闪回查询来恢复5天前的数据,你必须自己来确定需要恢复的scn,然后使用dbms_flashback.enable_at_system_change_number(scn_number); 来定位你的恢复时间点,下面是使用方法:

10:27:27 sql> variable scn_save number;
10:32:47 sql> execute :scn_save := dbms_flashback.get_system_change_number;

pl/sql procedure successfully completed.

10:33:24 sql> print scn_save;

 scn_save
----------
  3438420

10:33:41 sql> execute dbms_flashback.enable_at_system_change_number(:scn_save);

pl/sql procedure successfully completed.

10:34:31 sql> select * from t;

    id name
---------- ------------------------------------------------------------
     1 lsf
     2 lsf
     3 lsf
     4 lsf
     5 lsf
     6 lsf

6 rows selected.

另外,在使用dbms_flashback.enable_at_time前,你必须设定你的nls_date_format的精确程度,oracle默认的是精确到天,如果你不设定,像上面的例子你不会得到预期结果。
4.2 如果你使用sysdate和dbms_flashback.get_system_change_number来获取时间点或者scn值,你必须注意它们取得都是当前的时间点和scn值。
4.3 你只能在事务开始时进入闪回查询模式,如果之前有dml操作,则必须commit。
4.4 闪回查询无法恢复到表结构改变之前,因为闪回查询使用的当前的数据字典。