Oracle 11g 主動選擇的不完全恢復,基於SCN的,DML操作

LuiseDalian發表於2014-05-05

使用者的錯誤操作:delete, update, drop, commit

如果採用完全恢復,這些誤操作還是會被完成,所以如果想撤銷誤操作,就需要把資料庫恢復到誤操作之前。

定位錯誤操作的時間,logmnr

--開啟SQL*Plus時間顯示

scott@TESTDB11>set time on

15:34:04 scott@TESTDB11>select * from emp;

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

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

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      ......

      7900 JAMES      CLERK           7698 03-DEC-81        950                    30

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

 

14 rows selected.

 

--刪除所有資料,被刪除的大概時間是15:34:26

15:34:26 scott@TESTDB11>delete from emp;

 

14 rows deleted.

--提交

15:35:13 scott@TESTDB11>commit;

 

Commit complete.

 

15:35:16 scott@TESTDB11>select * from emp;

 

no rows selected

 

--3次日誌切換,目的就是讓記錄剛才改變的日誌被歸檔出來

sys@TESTDB11>alter system switch logfile;

 

System altered.

 

--檢視歸檔出來的日誌,確定需要挖掘的日誌序號為8

sys@TESTDB11>col name for a40

sys@TESTDB11>alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';

 

Session altered.

sys@TESTDB11>select sequence#, name, first_time, next_time from v$archived_log;

 SEQUENCE# NAME                                     FIRST_TIME          NEXT_TIME

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

 

         1 /archive2/1_1_823247647.dbf              2013-08-12 07:54:07 2013-08-12 08:24:28

         1 /archive1/1_1_823247647.dbf              2013-08-12 07:54:07 2013-08-12 08:24:28      

...

         7 /archive2/1_7_823247647.dbf              2013-08-12 08:27:00 2013-08-12 08:27:01

         8 /archive1/1_8_823247647.dbf              2013-08-12 08:27:01 2013-08-12 15:40:42

         8 /archive2/1_8_823247647.dbf              2013-08-12 08:27:01 2013-08-12 15:40:42

 

130 rows selected.

 

-- 進行挖掘,根據前面的時間確定操作的SCN和時間為: 2703654 2013-08-12 15:35:13

sys@TESTDB11>exec dbms_logmnr.add_logfile(logfilename => '/archive1/1_8_823247647.dbf', options => dbms_logmnr.new);

 

PL/SQL procedure successfully completed.

 

sys@TESTDB11>exec dbms_logmnr.start_logmnr(options => dbms_logmnr.dict_from_online_catalog);

 

PL/SQL procedure successfully completed.

 

sys@TESTDB11>col sql_redo for a50

sys@TESTDB11>select scn, timestamp, sql_redo from v$logmnr_contents where seg_owner='SCOTT' and seg_name = 'EMP';

 

       SCN TIMESTAMP           SQL_REDO

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

   2683065 2013-08-12 08:46:52 insert into "SCOTT"."EMP"("EMPNO","ENAME","JOB","M

                               GR","HIREDATE","SAL","COMM","DEPTNO") values ('793

                               4','MILLER','CLERK','7782',TO_DATE('1982-01-23 00:

                               00:00', 'yyyy-mm-dd hh24:mi:ss'),'1300',NULL,'10')

                               ;

 

   2703654 2013-08-12 15:35:13 delete from "SCOTT"."EMP" where "EMPNO" = '7369' a

 

       SCN TIMESTAMP           SQL_REDO

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

                               nd "ENAME" = 'SMITH' and "JOB" = 'CLERK' and "MGR"

                                = '7902' and "HIREDATE" = TO_DATE('1980-12-17 00:

                               00:00', 'yyyy-mm-dd hh24:mi:ss') and "SAL" = '800'

                                and "COMM" IS NULL and "DEPTNO" = '20' and ROWID

                               = 'AAAU4PAAEAAAACTAAA';

 

   .......省略

 

   2703654 2013-08-12 15:35:13 delete from "SCOTT"."EMP" where "EMPNO" = '7934' a

                               nd "ENAME" = 'MILLER' and "JOB" = 'CLERK' and "MGR

                               " = '7782' and "HIREDATE" = TO_DATE('1982-01-23 00

                               :00:00', 'yyyy-mm-dd hh24:mi:ss') and "SAL" = '130

                               0' and "COMM" IS NULL and "DEPTNO" = '10' and ROWI

                               D = 'AAAU4PAAEAAAACTAAN';

 

 

30 rows selected.

 

-- 檢視當前資料庫檢查點號, 上面確定的那個檢查點2703654,肯定要比這個小。

sys@TESTDB11>select name, checkpoint_change# from v$datafile_header;

 

NAME                                     CHECKPOINT_CHANGE#

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

/oradata/TestDB11/system01.dbf                      2703910

/oradata/TestDB11/sysaux01.dbf                      2703910

/oradata/TestDB11/undotbs01.dbf                     2703910

/oradata/TestDB11/users01.dbf                       2703910

/oradata/TestDB11/example01.dbf                     2703910

/oradata/TestDB11/newundotbs01.dbf                  2703910

 

6 rows selected.

 

--下面要進行不完全恢復

sys@TESTDB11>shutdown mount force;

SP2-0717: illegal SHUTDOWN option

sys@TESTDB11>startup mount force;

ORACLE instance started.

 

Total System Global Area  855982080 bytes

Fixed Size                  2230792 bytes

Variable Size             641730040 bytes

Database Buffers          209715200 bytes

Redo Buffers                2306048 bytes

Database mounted.

 

--還原

sys@TESTDB11>!cp /pooldisk02/backup01/inconsistent/*.dbf /oradata/TestDB11

 

--人工計算一下恢復到檢查點,確定恢復會在哪個日誌停下來(其實不需要人工計算,Oralce會自己計算)

sys@TESTDB11>col name for a40

sys@TESTDB11>select sequence#, name, first_change#, next_change# from v$archived_log;

 

 SEQUENCE# NAME                                     FIRST_CHANGE# NEXT_CHANGE#

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

         1 /archive1/1_1_823247647.dbf                    2679129      2681425

         ....省略

         7 /archive2/1_7_823247647.dbf                    2681524      2681527

         8 /archive1/1_8_823247647.dbf                    2681527      2703910

         8 /archive2/1_8_823247647.dbf                    2681527      2703910

 

 

--恢復

sys@TESTDB11>recover database until change 2703654;

Media recovery complete.

sys@TESTDB11>select sequence#, group#, status from v$log;

 

 SEQUENCE#     GROUP# STATUS

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

         7          1 INACTIVE

         9          3 CURRENT

         8          2 INACTIVE

 

 

--此時檢視資料檔案中頭中的檢查點號,為我們指定恢到的SCN號而且一致,可以開庫

sys@TESTDB11>col name for a50

sys@TESTDB11>select name, checkpoint_change# from v$datafile_header;

 

NAME                                               CHECKPOINT_CHANGE#

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

/oradata/TestDB11/system01.dbf                                2703654

/oradata/TestDB11/sysaux01.dbf                                2703654

/oradata/TestDB11/undotbs01.dbf                               2703654

/oradata/TestDB11/users01.dbf                                 2703654

/oradata/TestDB11/example01.dbf                               2703654

/oradata/TestDB11/newundotbs01.dbf                            2703654

 

6 rows selected.   

 

sys@TESTDB11>alter database open resetlogs;

 

Database altered.

 

--日誌已經重置了

sys@TESTDB11>select sequence#, group#, status from v$log;

 

 SEQUENCE#     GROUP# STATUS

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

         1          1 CURRENT

         0          2 UNUSED

         0          3 UNUSED

 

--檢查資料,都被恢復回來了(即解決了誤操作的問題)

16:20:30 scott@TESTDB11>select * from emp;

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

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

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

.......

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

 

14 rows selected.

 

--刪除無用的歸檔

[oracle@S1011:/archive1]$ rm *

[oracle@S1011:/archive2]$ rm *

 

--resetlogs之後,要做一個整個資料庫備份(呼叫我們之前做好的指令碼backup02.sql)

sys@TESTDB11>@backup_script/backup02.sql

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17013648/viewspace-1153852/,如需轉載,請註明出處,否則將追究法律責任。

相關文章