[20171115]恢復資料檔案塊頭3補充.txt

lfree發表於2017-11-15

[20171115]恢復資料檔案塊頭3補充.txt

--// 昨天做了恢復資料檔案塊頭,透過備份檔案直接取出檔案塊頭,覆蓋原來的資料塊,然後修復.
--//補充幾點:
--1.檔案頭損壞,無法使用rman的塊恢復功能.
--2.檔案頭損壞,dbv檢查發現都是壞塊.我感覺主要檔案塊頭損壞,dbv無法定位其它剩下的塊.

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

2.建立測試環境:

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;

create table t1 tablespace tea as select rownum id ,lpad('A',32,'A') name from dual connect by level<=1e5;

--//建立rman備份:
backup database filesperset=1 format '/home/oracle/backup/20171114_%U';
--//注:這是我個人主張的備份方式設定filesperset=1,可惜我們團隊一直不建議使用.這樣備份檔案顯得太多了.實際上這樣做與我後面的恢復有關.

create table t2 tablespace tea as select rownum id ,lpad('B',32,'B') name from dual connect by level<=1e5;
--//建立大小5M的表。注:說明一點,我是先做資料庫備份,再建立t2表.
--//破壞資料檔案頭,注:資料檔案頭在資料檔案的第2塊.第1塊OS頭.

update t2 set name=lpad('C',32,'C') where id <=5;
alter system checkpoint;
alter system checkpoint;
alter system checkpoint;
rollback ;
update t2 set name=lpad('D',32,'D') where id between 5 and 9;
commit ;

alter database  datafile 6 offline ;

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

--//再次提醒不要忘記conv=notrunc引數,不然資料檔案會被截斷.我自己在以前測試中遇到這種情況!!

--//在以前rman備份的疑問中發現rman會修改塊的資訊,比如:
rdba_kcbh,spare3_kcbh,tailchk,當然還包括檢查和.

--//拿當時備份的一個資料塊分析:
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

SCOTT@book> select rowid,t1.* from t1 where id=100000;
ROWID                      ID NAME
------------------ ---------- ---------------------------------
AAAWITAAGAAAALSACM     100000 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


SCOTT@book> select count(*) from t1 where rowid between 'AAAWITAAGAAAALSAAA' and 'AAAWITAAGAAAALSBBB';
  COUNT(*)
----------
       141

SCOTT@book> @ &r/rowid AAAWITAAGAAAALSACM
    OBJECT       FILE      BLOCK        ROW ROWID_DBA            DBA                  TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
     90643          6        722        140  0x18002D2           6,722                alter system dump datafile 6 block 722 ;

--//這個塊僅僅141條記錄.

$ cat filelist.txt
4 /mnt/ramdisk/book/users01.dbf
1 /mnt/ramdisk/book/system01.dbf
2 /mnt/ramdisk/book/sysaux01.dbf
3 /mnt/ramdisk/book/undotbs01.dbf
5 /mnt/ramdisk/book/example01.dbf
6 /mnt/ramdisk/book/tea01.dbf
100 /home/oracle/backup/20171114_fcsji7ti_1_1

BBED> map  dba 100,723
File: /home/oracle/backup/20171114_fcsji7ti_1_1 (100)
Block: 723                                   Dba:0x190002d3
------------------------------------------------------------
KTB Data Block (Table/Cluster)
struct kcbh, 20 bytes                      @0
struct ktbbh, 96 bytes                     @20
struct kdbh, 14 bytes                      @116
struct kdbt[1], 4 bytes                    @130
sb2 kdbr[141]                              @134
ub1 freespace[1994]                        @416
ub1 rowdata[5778]                          @2410
ub4 tailchk                                @8188

--//備份集合

BBED> set dba 100,723
        DBA             0x190002d3 (419431123 100,723)

BBED> p dba 100,723 kcbh.rdba_kcbh
ub4 rdba_kcbh                               @4        0x018002d3

BBED> p dba 100,723 kcbh.spare3_kcbh
ub2 spare3_kcbh                             @18       0x0001

BBED> p dba 100,723 tailchk
ub4 tailchk                                 @8188     0x73fd06d2


--//資料塊
BBED> set dba 6,722
        DBA             0x018002d2 (25166546 6,722)

BBED> p dba 6,722 kcbh.rdba_kcbh
ub4 rdba_kcbh                               @4        0x018002d2

BBED> p dba 6,722 kcbh.spare3_kcbh
ub2 spare3_kcbh                             @18       0x0000

BBED> p dba 6,722 tailchk
ub4 tailchk                                 @8188     0x73fd0602

--//可以看出備份修改kcbh.spare3_kcbh=0x0001,rdba_kcbh修改為備份塊的rdba地址,把原來rdba地址的低2位儲存在tailchk.
--//我這裡備份集合塊與資料塊差別不大,如果差距很大會出現什麼情況呢?

3.繼續測試:
--// drop T1,這樣前面就空出一段空間沒有使用,rman備份也不會備份這些塊.在看看這個備份檔案.
--//分析前準備:

1.記下表T2 id=1e5在那個資料塊:

SCOTT@book> drop table t1 purge;
Table dropped.

SCOTT@book> select rowid,t2.* from t2 where id=1e5;
ROWID                      ID NAME
------------------ ---------- ---------------------------------
AAAWIUAAGAAAAVSACM     100000 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

SCOTT@book> @ &r/rowid AAAWIUAAGAAAAVSACM
    OBJECT       FILE      BLOCK        ROW ROWID_DBA            DBA                  TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
     90644          6       1362        140  0x1800552           6,1362               alter system dump datafile 6 block 1362

SCOTT@book> select count(*) from t2 where rowid between 'AAAWIUAAGAAAAVSAAA' and 'AAAWIUAAGAAAAVSACM';
  COUNT(*)
----------
       141
--//說明該塊有141條記錄.

2.使用rman備份資料檔案6:

RMAN> backup  datafile 6 format '/home/oracle/backup/datafile6_20171115_%U' ;
Starting backup at 2017-11-15 15:42:38
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/mnt/ramdisk/book/tea01.dbf
channel ORA_DISK_1: starting piece 1 at 2017-11-15 15:42:38
channel ORA_DISK_1: finished piece 1 at 2017-11-15 15:42:39
piece handle=/home/oracle/backup/datafile6_20171115_fqsjku7e_1_1 tag=TAG20171115T154238 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2017-11-15 15:42:39

Starting Control File and SPFILE Autobackup at 2017-11-15 15:42:39
piece handle=/u01/app/oracle/fast_recovery_area/BOOK/autobackup/2017_11_15/o1_mf_s_960133359_f0qvzhdt_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 2017-11-15 15:42:40

--//備份檔案=/home/oracle/backup/datafile6_20171115_fqsjku7e_1_1

BBED> info
File#  Name                                                 Size(blks)
-----  ----                                                 ----------
     1  /mnt/ramdisk/book/system01.dbf                                0
     2  /mnt/ramdisk/book/sysaux01.dbf                                0
     3  /mnt/ramdisk/book/undotbs01.dbf                               0
     4  /mnt/ramdisk/book/users01.dbf                                 0
     5  /mnt/ramdisk/book/example01.dbf                               0
     6  /mnt/ramdisk/book/tea01.dbf                                   0
   206  /home/oracle/backup/datafile6_20171115_fqsjku7e_1_1           0

$ ls -l /home/oracle/backup/datafile6_20171115_fqsjku7e_1_1
-rw-r----- 1 oracle oinstall 5980160 2017-11-15 15:42:38 /home/oracle/backup/datafile6_20171115_fqsjku7e_1_1

--//備份不到6M.
--//佔用5980160/8192=730塊.最大塊號729(從0開始記數).
--//前面測試已經說明728備份的檔案頭.依次往前面看.

BBED> map  dba 206,728
File: /home/oracle/backup/datafile6_20171115_fqsjku7e_1_1 (206)
Block: 728                                   Dba:0x338002d8
------------------------------------------------------------
Data File Header
struct kcvfh, 860 bytes                    @0
ub4 tailchk                                @8188

--OK,判斷正確.

BBED> map  dba 206,724
File: /home/oracle/backup/datafile6_20171115_fqsjku7e_1_1 (206)
Block: 724                                   Dba:0x338002d4
------------------------------------------------------------
KTB Data Block (Table/Cluster)
struct kcbh, 20 bytes                      @0
struct ktbbh, 96 bytes                     @20
struct kdbh, 14 bytes                      @116
struct kdbt[1], 4 bytes                    @130
sb2 kdbr[141]                              @134
ub1 freespace[1994]                        @416
ub1 rowdata[5778]                          @2410
ub4 tailchk                                @8188

BBED> set dba 206,724
        DBA             0x338002d4 (864027348 206,724)

BBED> x  /rnc   *kdbr[140] dba 206,724
rowdata[0]                                  @2410
----------
flag@2410: 0x2c (KDRHFL, KDRHFF, KDRHFH)
lock@2411: 0x00
cols@2412:    2

col    0[2] @2413: 100000
col   1[32] @2416: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
--//說明判斷正確,就是這塊.
--//備份檔案中塊724對於就是dba=6,1362的備份.對比備份與對於資料塊的不同:

p dba 6,1362 kcbh.rdba_kcbh
p dba 6,1362 kcbh.spare3_kcbh
p dba 6,1362 tailchk

p dba 206,724 kcbh.rdba_kcbh
p dba 206,724 kcbh.spare3_kcbh
p dba 206,724 tailchk

--//資料塊
BBED> p dba 6,1362 kcbh.rdba_kcbh
ub4 rdba_kcbh                               @4        0x01800552

BBED> p dba 6,1362 kcbh.spare3_kcbh
ub2 spare3_kcbh                             @18       0x0000

BBED> p dba 6,1362 tailchk
ub4 tailchk                                 @8188     0x75570602

BBED> p dba 6,1362 kcbh.chkval_kcbh
ub2 chkval_kcbh                             @16       0x30c7


--//備份集
BBED> p dba 206,724 kcbh.rdba_kcbh
ub4 rdba_kcbh                               @4        0x018005d4

BBED> p dba 206,724 kcbh.spare3_kcbh
ub2 spare3_kcbh                             @18       0x0001

BBED> p dba 206,724 tailchk
ub4 tailchk                                 @8188     0x75570652

BBED> p dba 206,724 kcbh.chkval_kcbh
ub2 chkval_kcbh                             @16       0x3010

--//kcbh.spare3_kcbh 從 0x0000 => 0x0001.
--724=0x2d4
--1362=0x552

--//注意看原資料塊 kcbh.rdba_kcbh=0x01800552 備份後變成了 kcbh.rdba_kcbh= 0x018005d4.
--//也就是將資料塊的dba地址的低2位0x52儲存到tailchk的低2位.
--//同時將備份時資料塊的kcbh.rdba_kcbh的低2位0x52 變成備份塊的塊地址的低2位(前面0x02丟棄了)0xd4.
--//當然這樣檢查和也會發生變化.

--//將變化的低2位做異或操作.

$ echo 52 00 02 c7 d4 01 52 |xor.sh
52
00
02
C7
D4
01
52
xor result: 10

$ echo 0552 0000 0602 30c7 05d4 0001 0652 |xor.sh
0552
0000
0602
30C7
05D4
0001
0652
xor result: 3010

--//正好是備份對應塊檢查和的低2位.(dba 206,724 kcbh.chkval_kcbh=0x3010)
--//感覺自己好無聊^_^,放棄不再探究了.

--//關於做異或的指令碼可以參考如下連結:
http://blog.itpub.net/267265/viewspace-2134945/

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

相關文章