[20161111]資料庫檔案頭的修復.txt

lfree發表於2016-11-11

[20161111]資料庫檔案頭的修復.txt

--這裡指檔案頭實際上資料檔案第1塊(從0算起)。

--找到一個連結,http://www.cnblogs.com/hrhguanli/p/4708273.html
--要修改的資訊相對較多。

1 .改動資料的DBA,rdba_kcbh
2 .改動檔案的大小,kccfhfsz
3 .改動檔案號,kccfhfno
4 .改動檔案建立時SCN,kcvfhcrs
5 .改動檔案建立時間,kcvfhcrt
6 .改動表空間號,kcvfhtsn
7 .改動相對檔案號,kcvfhrfn
8 .改動表空間的名稱, kcvfhtnm
9 .改動表空間的長度,kcvfhtln
10.改動檢查點的SCN,kcvfhckp
11.改動檢查點的時間,kcvcptim
12.改動檢查點的計數器,kcvfhcpc
13.改動檢查點的控制檔案備份的計數器, kcvfhccc
14.假設你改動是1號檔案的1號塊他的root rdba針指向bootstrap$

--實際上修改沒有這麼多,不過還是按照這個過程操作修改內容。

1.環境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

CREATE TABLESPACE SUGAR DATAFILE
  '/mnt/ramdisk/book/sugar01.dbf' SIZE 40M AUTOEXTEND ON NEXT 16M MAXSIZE UNLIMITED
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

create table t1 tablespace sugar as select rownum id ,lpad('A',32,'A') name from dual connect by level<=1e5;
--建立大小5M的表。

alter system checkpoint;
alter system checkpoint;
...
alter system checkpoint;

CREATE TABLESPACE TEA DATAFILE
  '/mnt/ramdisk/book/tea01.dbf' SIZE 40M AUTOEXTEND ON NEXT 16M MAXSIZE UNLIMITED
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

alter system checkpoint;

--建立的資料檔案大小一致,可以減少一些修改內容。執行alter system checkpoint;這樣可以導致2個檔案的改動檢查點的計數器不同。
SCOTT@book> alter database  datafile 6 offline ;
Database altered.

SCOTT@book> alter database  datafile 7 offline ;
Database altered.

--做這種offline,最好順手做一個recover datafile N,避免時間上了online時要recover沒有歸檔檔案。當然這裡測試不需要。
SCOTT@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                            TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- ------------------------------- ----------------
    1            6317588 2016-11-11 15:08:38                7           2002065 ONLINE               419 YES /mnt/ramdisk/book/system01.dbf  SYSTEM
    2            6317588 2016-11-11 15:08:38             1834           2002065 ONLINE               416 YES /mnt/ramdisk/book/sysaux01.dbf  SYSAUX
    3            6317588 2016-11-11 15:08:38           923328           2002065 ONLINE               337 YES /mnt/ramdisk/book/undotbs01.dbf UNDOTBS1
    4            6317588 2016-11-11 15:08:38            16143           2002065 ONLINE               415 YES /mnt/ramdisk/book/users01.dbf   USERS
    5            6317588 2016-11-11 15:08:38           952916           2002065 ONLINE               335 YES /mnt/ramdisk/book/example01.dbf EXAMPLE
    6            6317503 2016-11-11 15:07:28          6316871           2002065 OFFLINE                3 NO  /mnt/ramdisk/book/sugar01.dbf   SUGAR
    7            6317588 2016-11-11 15:08:38          6317214           2002065 OFFLINE               11 YES /mnt/ramdisk/book/tea01.dbf     TEA
7 rows selected.

2.備份資料檔案6.

$ cp /mnt/ramdisk/book/sugar01.dbf /u01/backup/sugar01.dbf_20161110
$ bvi -b 8192 -s 8192 /mnt/ramdisk/book/sugar01.dbf
--全部置為0

$ dd if=/dev/zero of=/mnt/ramdisk/book/sugar01.dbf count=1 bs=8192 conv=notrunc seek=1
1+0 records in
1+0 records out
8192 bytes (8.2 kB) copied, 4.0611e-05 seconds, 202 MB/s

BBED> copy dba 7,1 to dba 6,1
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y
File: /mnt/ramdisk/book/sugar01.dbf (6)
Block: 1                                                    Offsets:    0 to  255                                               Dba:0x01800001
------------------------------------------------------------------------------------------------------------------------------------------------
0ba20000 0100c001 00000000 00000104 c54f0000 00000000 0004200b 6e21b74f 424f4f4b 00000000 fc6b0000 00140000 00200000 07000300 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 9e646000 00000000 68bc4a37 e6702c37 918c1e00 00000000 00000000
00000000 00000000 00000400 0b000000 cabc4a37 0a000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
<64 bytes per line>

--這樣使用資料檔案7的檔案頭替換了資料檔案6的檔案頭。

3.開始修改。
1.改動資料的DBA,rdba_kcbh

BBED> set dba 6,1
        DBA             0x01800001 (25165825 6,1)

BBED> p dba 6,1 kcvfh.kcvfhbfh.rdba_kcbh
ub4 rdba_kcbh                               @4        0x01c00001

BBED> assign dba 6,1 kcvfh.kcvfhbfh.rdba_kcbh = 0x01800001
ub4 rdba_kcbh                               @4        0x01800001
--檢視執行set dba 6,1的輸出就知道dba地址。

2 .改動檔案的大小,kccfhfsz
--這個忽略,大小一樣。

BBED> p dba 6,1 kcvfh.kcvfhhdr.kccfhfsz
ub4 kccfhfsz                                @44       0x00001400
-- 0x1400 = 5120, 5120*8192/1024/1024=40M

3 .改動檔案號,kccfhfno
BBED> p dba 6,1 kcvfh.kcvfhhdr.kccfhfno
ub2 kccfhfno                                @52       0x0007

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kccfhfno=0x0006
ub2 kccfhfno                                @52       0x0006

4 .改動檔案建立時SCN,kcvfhcrs
SCOTT@book> SELECT CREATION_CHANGE#,CREATION_TIME ,file#,NAME from v$datafile where file#=6 or file#=7;
CREATION_CHANGE# CREATION_TIME       FILE# NAME
---------------- ------------------- ----- --------------------------------------------------
         6316871 2016-11-11 15:05:44     6 /mnt/ramdisk/book/sugar01.dbf
         6317214 2016-11-11 15:06:16     7 /mnt/ramdisk/book/tea01.dbf

BBED> p /d dba 6,1 kcvfh.kcvfhhdr.kcvfhcrs
struct kcvfhcrs, 8 bytes                    @100
   ub4 kscnbas                              @100      6317214
   ub2 kscnwrp                              @104      0

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhcrs.kscnbas=6316871
ub4 kscnbas                                 @100      0x00606347

5 .改動檔案建立時間,kcvfhcrt
SCOTT@book> SELECT CREATION_CHANGE#,CREATION_TIME ,file#,NAME from v$datafile where file#=6 or file#=7;
CREATION_CHANGE# CREATION_TIME       FILE# NAME
---------------- ------------------- ----- --------------------------------------------------
         6316871 2016-11-11 15:05:44     6 /mnt/ramdisk/book/sugar01.dbf
         6317214 2016-11-11 15:06:16     7 /mnt/ramdisk/book/tea01.dbf

BBED> p /d dba 6,1 kcvfh.kcvfhhdr.kcvfhcrt
ub4 kcvfhcrt                                @108      927644776
--注意現在file#=7的時間。

SCOTT@book> select (to_date('2016-11-11 15:06:16','yyyy-mm-dd hh24:mi:ss')-to_date('2016-11-11 15:05:44','yyyy-mm-dd hh24:mi:ss'))*86400 N10 from dual;
                  N10
---------------------
                   32

-- 927644776-32=927644744

BBED> assign  dba 6,1 kcvfh.kcvfhhdr.kcvfhcrt=927644744
ub4 kcvfhcrt                                @108      0x374abc48

--補充說明:第4,5步我開始忽略,我認為可以不改。但是實際上在recover時報錯。
SCOTT@book> recover datafile 6;
ORA-00283: recovery session canceled due to errors
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-01122: database file 6 failed verification check
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-01202: wrong incarnation of this file - wrong creation time

6 .改動表空間號,kcvfhtsn

SCOTT@book> select ts#,name from sys.ts$ where name in ('SUGAR','TEA');
         TS# NAME
------------ --------------------------------------------------
           7 SUGAR
           8 TEA

BBED> p dba 6,1 kcvfh.kcvfhhdr.kcvfhtsn
sword kcvfhtsn                              @332      8

BBED> assign  dba 6,1 kcvfh.kcvfhhdr.kcvfhtsn=7
sword kcvfhtsn                              @332      7

7 .改動相對檔案號,kcvfhrfn
BBED> p dba 6,1 kcvfh.kcvfhhdr.kcvfhrfn
ub4 kcvfhrfn                                @368      0x00000007

BBED> assign  dba 6,1 kcvfh.kcvfhhdr.kcvfhrfn=0x00000006
ub4 kcvfhrfn                                @368      0x00000006

8 .改動表空間的名稱, kcvfhtnm
BBED> p /c dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm
text kcvfhtnm[0]                            @338     T
text kcvfhtnm[1]                            @339     E
text kcvfhtnm[2]                            @340     A
text kcvfhtnm[3]                            @341     .
text kcvfhtnm[4]                            @342     .

SCOTT@book> select dump('S',16) from dual ;
DUMP('S',16)
----------------
Typ=96 Len=1: 53

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm[0]=0x53
text kcvfhtnm[0]                            @338     S

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm[1]=0x55
text kcvfhtnm[0]                            @339     U

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm[2]=0x47
text kcvfhtnm[0]                            @340     G

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm[3]=0x41
text kcvfhtnm[0]                            @341     A

BBED> assign dba 6,1 kcvfh.kcvfhhdr.kcvfhtnm[4]=0x52
text kcvfhtnm[0]                            @343     R

--檢查略。

9 .改動表空間的長度,kcvfhtln

BBED> p  dba 6,1 kcvfh.kcvfhhdr.kcvfhtln
ub2 kcvfhtln                                @336      0x0003

BBED> assign  dba 6,1 kcvfh.kcvfhhdr.kcvfhtln=0x0005
ub2 kcvfhtln                                @336      0x0005
--修改長度5,SUGAR佔5個字元。

10.改動檢查點的SCN,kcvfh.kcvfhhdr.kcvfhckp.kcvcpscn
SCOTT@book>  SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,status,name FROM v$datafile where file#=6;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------- --------------------------------------------------
    6            6317503 2016-11-11 15:07:28                     0                          6317503 2016-11-11 15:07:28               0              0 OFFLINE /mnt/ramdisk/book/sugar01.dbf

BBED> p /d dba 6,1 kcvfh.kcvfhhdr.kcvfhckp.kcvcpscn
struct kcvcpscn, 8 bytes                    @484
   ub4 kscnbas                              @484      6317588
   ub2 kscnwrp                              @488      0

BBED> assign  /d dba 6,1 kcvfh.kcvfhhdr.kcvfhckp.kcvcpscn.kscnbas=6317503
ub4 kscnbas                                 @484      0x006065bf

11.改動檢查點的時間,kcvcptim
BBED> p /d dba 6,1 kcvfh.kcvfhhdr.kcvfhckp.kcvcptim
ub4 kcvcptim                                @492      927644918

SCOTT@book> SELECT file#, CHECKPOINT_CHANGE#,CHECKPOINT_TIME,UNRECOVERABLE_CHANGE#,UNRECOVERABLE_TIME,LAST_CHANGE#,LAST_TIME, OFFLINE_CHANGE#, ONLINE_CHANGE#,status,name FROM v$datafile where file# in (6,7);
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     UNRECOVERABLE_CHANGE# UNRECOVERABLE_TIME  LAST_CHANGE# LAST_TIME           OFFLINE_CHANGE# ONLINE_CHANGE# STATUS  NAME
----- ------------------ ------------------- --------------------- ------------------- ------------ ------------------- --------------- -------------- ------- --------------------------------------------------
    6            6317503 2016-11-11 15:07:28                     0                          6317503 2016-11-11 15:07:28               0              0 OFFLINE /mnt/ramdisk/book/sugar01.dbf
    7            6317588 2016-11-11 15:08:38                     0                          6317624 2016-11-11 15:10:02               0              0 RECOVER /mnt/ramdisk/book/tea01.dbf
--注意現在file#=7的時間。

SCOTT@book> select (to_date('2016-11-11 15:08:38','yyyy-mm-dd hh24:mi:ss')-to_date('2016-11-11 15:07:28','yyyy-mm-dd hh24:mi:ss'))*86400 N10 from dual;
                  N10
---------------------
                   70

--927644918-70=927644848

BBED> assign /d dba 6,1 kcvfh.kcvfhhdr.kcvfhckp.kcvcptim=927644848
ub4 kcvcptim                                @492      0x374abcb0

12.改動檢查點的計數器,kcvfhcpc

--如果你看前面的查詢v$datafile_header;CHECKPOINT_COUNT=3,現在是11.
BBED> p /d dba 6,1 kcvfh.kcvfhrdb.kcvfhcpc
ub4 kcvfhcpc                                @140      11

--這個資訊來源於控制檔案,你可以轉儲alter session set events 'immediate trace name controlf level 12';
DATA FILE #6:
  name #10: /mnt/ramdisk/book/sugar01.dbf
creation size=5120 block size=8192 status=0xc head=10 tail=10 dup=1
tablespace 7, index=7 krfil=6 prev_file=0
unrecoverable scn: 0x0000.00000000 01/01/1988 00:00:00
Checkpoint cnt:3 scn: 0x0000.006065bf 11/11/2016 15:07:28
Stop scn: 0x0000.006065bf 11/11/2016 15:07:28
Creation Checkpointed at scn:  0x0000.00606347 11/11/2016 15:05:44
thread:1 rba:(0x9f.5897.10)
enabled  threads:  01000000 00000000 00000000 00000000 00000000 00000000
  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

BBED> p /d dba 6,1 kcvfh.kcvfhrdb.kcvfhcpc
ub4 kcvfhcpc                                @140      11

BBED> assign /d dba 6,1 kcvfh.kcvfhrdb.kcvfhcpc=2
ub4 kcvfhcpc                                @140      0x00000002

13.改動檢查點的控制檔案備份的計數器, kcvfhccc
--就是比減少kcvfhcpc-1

BBED> p /d dba 6,1 kcvfh.kcvfhrdb.kcvfhccc
ub4 kcvfhccc                                @148      10

BBED> assign /d dba 6,1 kcvfh.kcvfhrdb.kcvfhccc=1
ub4 kcvfhccc                                @148      0x00000001
--先這樣修改看看。
--補充說明,我開始沒改,但是執行recover時報錯。估計這個是11太大,我設定比原來的3小就ok了。
SCOTT@book> recover datafile 6;
ORA-00283: recovery session canceled due to errors
ORA-01122: database file 6 failed verification check
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-01207: file is more recent than control file - old control file

14.假設你改動是1號檔案的1號塊他的root rdba針指向bootstrap$
--不是系統檔案,這個不修改了。
BBED> p dba 1,1 kcvfh.kcvfhrdb
ub4 kcvfhrdb                                @96       0x00400208

BBED> p dba 6,1 kcvfh.kcvfhrdb
ub4 kcvfhrdb                                @96       0x00000000

BBED> set dba 0x00400208
        DBA             0x00400208 (4194824 1,520)
--這個位置在dba=1,520,大家可以自行驗證。
SCOTT@book> @ &r/which_obj 1 520
OWNER  SEGMENT_NAME          SEGMENT_TYPE       TABLESPACE_NAME                   EXTENT_ID      FILE_ID     BLOCK_ID        BYTES       BLOCKS RELATIVE_FNO
------ --------------------  ------------------ ------------------------------ ------------ ------------ ------------ ------------ ------------ ------------
SYS    BOOTSTRAP$            TABLE              SYSTEM                                    0            1          520        65536            8            1

15.計算檢查和。

BBED> sum apply dba 6,1
Check value for File 6, Block 1:
current = 0x1edf, required = 0x1edf

SCOTT@book> recover datafile 6;
ORA-00283: recovery session canceled due to errors
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-01122: database file 6 failed verification check
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-01203: wrong incarnation of this file - wrong creation SCN

16.執行恢復

SCOTT@book> recover datafile 6;
Media recovery complete.

--ok.
SCOTT@book> alter database datafile 6 online ;

Database altered.

SCOTT@book> select count(*) from t1;
    COUNT(*)
------------
      100000

-- 總結:
1 .改動資料的DBA,rdba_kcbh                                     kcvfh.kcvfhbfh.rdba_kcbh
2 .改動檔案的大小,kccfhfsz                                    kcvfh.kcvfhhdr.kccfhfsz
3 .改動檔案號,kccfhfno                                        kcvfh.kcvfhhdr.kccfhfno
4 .改動檔案建立時SCN,kcvfhcrs                                 kcvfh.kcvfhhdr.kcvfhcrs
5 .改動檔案建立時間,kcvfhcrt                                  kcvfh.kcvfhhdr.kcvfhcrt
6 .改動表空間號,kcvfhtsn                                      kcvfh.kcvfhhdr.kcvfhtsn
7 .改動相對檔案號,kcvfhrfn                                    kcvfh.kcvfhhdr.kcvfhrfn
8 .改動表空間的名稱, kcvfhtnm                                 kcvfh.kcvfhhdr.kcvfhtnm
9 .改動表空間的長度,kcvfhtln                                  kcvfh.kcvfhhdr.kcvfhtln
10.改動檢查點的SCN,kcvfhckp                                   kcvfh.kcvfhhdr.kcvfhckp.kcvcpscn
11.改動檢查點的時間,kcvcptim                                  kcvfh.kcvfhhdr.kcvfhckp.kcvcptim
12.改動檢查點的計數器,kcvfhcpc                                kcvfh.kcvfhrdb.kcvfhcpc
13.改動檢查點的控制檔案備份的計數器, kcvfhccc                 kcvfh.kcvfhrdb.kcvfhccc
14.假設你改動是1號檔案的1號塊他的root rdba針指向bootstrap$     kcvfh.kcvfhrdb
15.計算檢查和。
16.千萬不要在生產系統做這個測試!!

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

相關文章