1. 程式人生 > >讓天下沒有難用的資料庫 » 使用logmnr找回資料

讓天下沒有難用的資料庫 » 使用logmnr找回資料

應用程式的誤刪除了線上的一條資料,需要恢復

刪除時間:2010-11-08 00:11:21

1.首先考慮使用oracle的flashback query

select *
from users

as of scn
timestamp_to_scn(to_timestamp(‘22010-11-08 00:11:21’,
‘yyyy-mm-dd hh24:mi:ss’)
where tadget_id = 91742
and user_id = 224529394;

發現該資料提示已經找不到了(undo_retention: 1800)

2.使用logmnr,分析歸檔日誌,來找出資料:

根據刪除的大致時間,從V$archived_log中找出那個時間段的歸檔日誌:
17:25:35

[email protected] misc> select * from (select name, FIRST_TIME
17:29:06 2 from V$archived_log
17:29:06 3 where FIRST_TIME >
17:29:06 4 to_timestamp(‘2010-11-08 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’)
17:29:06 5 order by FIRST_TIME) where rownum<10;

NAME FIRST_TIME
——————– ——————-
misc_stb_cm3 2010-11-08 00:03:35


misc_stb_second 2010-11-08 00:03:35
/arc/archive/misc/1_ 2010-11-08 00:03:35
149148_663475111.arc

misc_stb_cm3 2010-11-08 00:13:36
misc_stb_second 2010-11-08 00:13:36
/arc/archive/misc/1_ 2010-11-08 00:13:36
149149_663475111.arc

misc_stb_cm3 2010-11-08 00:23:36
misc_stb_second 2010-11-08 00:23:36
/arc/archive/misc/1_ 2010-11-08 00:23
/arc/archive/misc/1_149148_663475111.arc

/arc/archive/misc/1_149149_663475111.arc

3.使用logmnr分析歸檔日誌:

col scn for a20
col timestamp for a20
col owner for a20
col oper for a20
col redo for a20
col undo for a20

新增日誌:exec sys.dbms_logmnr.add_logfile(LogFileName => ‘/arc/archive/misc/1_149149_663475111.arc’,Options => dbms_logmnr.new);
分析日誌:exec sys.dbms_logmnr.start_logmnr(Options => sys.dbms_logmnr.DICT_FROM_ONLINE_CATALOG);

分析結果中資訊不準確的話,新增其他日誌進行分析,繼續新增:exec sys.dbms_logmnr.add_logfile(LogFileName => ‘/arc/archive/misc/1_149148_663475111.arc’);
查詢分析結果:
select t.scn scn, t.timestamp timestamp, t.seg_owner owner, t.operation oper, t.sql_redo redo, t.sql_undo undo
from V$logmnr_Contents t
where t.seg_name = upper(‘users’)
and sql_redo like ‘%91742%’;

9411510185912 2010-11-08 00:11:21 TOP
DELETE
delete from “USERS” where “ID” = ‘106889195’ and “TADGET_ID” =
‘91742’ and “USER_ID” = ‘224529394’ and “STATUS” = ‘1’ and “EXPIRY_DATE” = TO_DA
TE(‘2010-11-08 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’) and “GMT_CREATE” = TO_DATE(‘2
010-10-08 16:54:39’, ‘yyyy-mm-dd hh24:mi:ss’) and “GMT_MODIFIED” = TO_DATE(‘2010
-10-08 16:54:39’, ‘yyyy-mm-dd hh24:mi:ss’) and “INSTANCE_ID” = ‘21937’ and “USER
_NICK” = ‘營店’ and ROWID = ‘AAAdGiAAKAAACyzABr’;
insert into  “USERS”(“ID”,”TADGET_ID”,”USER_ID”,”STATUS”,”EXPIRY
_DATE”,”GMT_CREATE”,”GMT_MODIFIED”,”INSTANCE_ID”,”USER_NICK”) values (‘106889195
‘,’91742′,’224529394′,’1’,TO_DATE(‘2010-11-08 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’
),TO_DATE(‘2010-10-08 16:54:39’, ‘yyyy-mm-dd hh24:mi:ss’),TO_DATE(‘2010-10-08 16
:54:39’, ‘yyyy-mm-dd hh24:mi:ss’),’21937′,’營店’);

9411510185916 2010-11-08 00:11:21 TOP
INSERT
insert into  “USERS”(“ID”,”TADGET_ID”,”USER_ID”,”STATUS”,”EXPIRY
_DATE”,”GMT_CREATE”,”GMT_MODIFIED”,”INSTANCE_ID”,”USER_NICK”) values (‘109752386
‘,’91742′,’224529394′,’1’,TO_DATE(‘2010-12-08 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’
),TO_DATE(‘2010-11-08 00:11:21’, ‘yyyy-mm-dd hh24:mi:ss’),TO_DATE(‘2010-11-08 00
:11:21’, ‘yyyy-mm-dd hh24:mi:ss’),’21937′,’一道關懷保健專營店’);
delete from  “USERS” where “ID” = ‘109752386’ and “TADGET_ID” =
‘91742’ and “USER_ID” = ‘224529394’ and “STATUS” = ‘1’ and “EXPIRY_DATE” = TO_DA
TE(‘2010-12-08 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’) and “GMT_CREATE” = TO_DATE(‘2
010-11-08 00:11:21’, ‘yyyy-mm-dd hh24:mi:ss’) and “GMT_MODIFIED” = TO_DATE(‘2010
-11-08 00:11:21’, ‘yyyy-mm-dd hh24:mi:ss’) and “INSTANCE_ID” = ‘21937’ and “USER
_NICK” = ‘營店’ and ROWID = ‘AAAdGiAATAAACIjAA1’;

關閉分析的日誌:exec sys.dbms_logmnr.end_logmnr;

從分析出的的日中,可以看到相應sql的undo和redo sql;

執行相應的sql,既可以恢復資料。