[20190226]测试使用bbed恢复索引.txt
[20190226]测试使用bbed恢复索引.txt
--//上午做tab$删除恢复测试时发现,tab$的索引i_tab1很小.可以尝试使用bbed解决这个问题.
--//首先在普通表上做一个测试看看.
1.环境:
scott@book> @ 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> create table empy as select * from emp ;
table created.
scott@book> create index i_empy_empno on empy(empno);
index created.
scott@book> select rowid,empy.* from empy where rownum=1;
rowid empno ename job mgr hiredate sal comm deptno
------------------ ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
aaawpzaaeaaaailaaa 7369 smith clerk 7902 1980-12-17 00:00:00 800 20
scott@book> @ rowid aaawpzaaeaaaailaaa
object file block row rowid_dba dba text
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
91097 4 523 0 0x100020b 4,523 alter system dump datafile 4 block 523 ;
scott@book> select header_file,header_block,segment_name from dba_segments where owner=user and segment_name='i_empy_empno';
header_file header_block segment_name
----------- ------------ --------------------
4 530 i_empy_empno
--//索引的roor节点在dba=4,531.
2.删除记录看看:
scott@book> delete from empy where empno not in (7369);
13 rows deleted.
scott@book> commit ;
commit complete.
--//仅仅保留1条.并且这条是empy表最小的empno号.
scott@book> alter system checkpoint ;
system altered.
3.先恢复表:
bbed> set dba 4,523
dba 0x0100020b (16777739 4,523)
bbed> x /rnccntnnn *kdbr[0]
rowdata[529] @8150
------------
flag@8150: 0x2c (kdrhfl, kdrhff, kdrhfh)
lock@8151: 0x00
cols@8152: 8
col 0[3] @8153: 7369
col 1[5] @8157: smith
col 2[5] @8163: clerk
col 3[3] @8169: 7902
col 4[7] @8173: 1980-12-17 00:00:00
col 5[2] @8181: 800
col 6[0] @8184: *null*
col 7[2] @8185: 20
--//这条记录保留.
bbed> x /rnccntnnn *kdbr[1]
rowdata[486] @8107
------------
flag@8107: 0x3c (kdrhfl, kdrhff, kdrhfd, kdrhfh)
lock@8108: 0x02
cols@8109: 0
--//执行如下生成bbed脚本:
$ seq 1 13 | xargs -i{} echo 'x /rnccntnnn dba 4,523 *kdbr[{}]' | rlbbed | grep -b1 "^lock@" | grep "^flag@.*=0x3c" | cut -d: -f1| cut -d"@" -f2 | xargs -i{} echo assign dba 4,523 offset {}=0x2c
assign dba 4,523 offset 8107=0x2c
assign dba 4,523 offset 8064=0x2c
assign dba 4,523 offset 8023=0x2c
assign dba 4,523 offset 7978=0x2c
assign dba 4,523 offset 7937=0x2c
assign dba 4,523 offset 7896=0x2c
assign dba 4,523 offset 7856=0x2c
assign dba 4,523 offset 7818=0x2c
assign dba 4,523 offset 7775=0x2c
assign dba 4,523 offset 7737=0x2c
assign dba 4,523 offset 7699=0x2c
assign dba 4,523 offset 7660=0x2c
assign dba 4,523 offset 7621=0x2c
--//执行如上脚本,去除删除标识.
bbed> sum apply
check value for file 4, block 523:
current = 0x8c05, required = 0x8c05
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 523
block checking: dba = 16777739, block type = ktb-managed data block
data header at 0x7f6d56b9427c
kdbchk: the amount of space used is not equal to block size
used=613 fsc=503 avsp=7451 dtl=8064
block 523 failed with check code 6110
--//dtl-used-fsc = avsp
bbed> p ktbbh.ktbbhitl[1]._ktbitun._ktbitfsc
sb2 _ktbitfsc @86 503
bbed> assign ktbbh.ktbbhitl[1]._ktbitun._ktbitfsc=0
sb2 _ktbitfsc @86 0
bbed> sum apply
check value for file 4, block 523:
current = 0x8df2, required = 0x8df2
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 523
block checking: dba = 16777739, block type = ktb-managed data block
data header at 0xe9427c
kdbchk: space available on commit is incorrect
tosp=7980 fsc=0 stb=0 avsp=7451
block 523 failed with check code 6111
--//avsp+fsc+stb=tops.
bbed> assign kdbh.kdbhtosp=kdbh.kdbhavsp
sb2 kdbhtosp @136 7451
bbed> sum apply
check value for file 4, block 523:
current = 0x8fc5, required = 0x8fc5
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 523
--//ok, 现在对应块已经修复.
scott@book> alter system flush buffer_cache;
system altered.
scott@book> select * from empy ;
empno ename job mgr hiredate sal comm deptno
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 smith clerk 7902 1980-12-17 00:00:00 800 20
7499 allen salesman 7698 1981-02-20 00:00:00 1600 300 30
7521 ward salesman 7698 1981-02-22 00:00:00 1250 500 30
7566 jones manager 7839 1981-04-02 00:00:00 2975 20
7654 martin salesman 7698 1981-09-28 00:00:00 1250 1400 30
7698 blake manager 7839 1981-05-01 00:00:00 2850 30
7782 clark manager 7839 1981-06-09 00:00:00 2450 10
7788 scott analyst 7566 1987-04-19 00:00:00 3000 20
7839 king president 1981-11-17 00:00:00 5000 10
7844 turner salesman 7698 1981-09-08 00:00:00 1500 0 30
7876 adams clerk 7788 1987-05-23 00:00:00 1100 20
7900 james clerk 7698 1981-12-03 00:00:00 950 30
7902 ford analyst 7566 1981-12-03 00:00:00 3000 20
7934 miller clerk 7782 1982-01-23 00:00:00 1300 10
14 rows selected.
scott@book> select * from empy where empno in(7499,7369);
empno ename job mgr hiredate sal comm deptno
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 smith clerk 7902 1980-12-17 00:00:00 800 20
--//可以发现走索引仅仅找到1条.
scott@book> analyze table empy validate structure cascade;
analyze table empy validate structure cascade
*
error at line 1:
ora-01499: table/index cross reference failure - see trace file
--//一般普通表的索引不用恢复,根据表重建就ok了.但是对于sys.tab$的索引不行,这也是做这个测试的原因.
4.恢复索引看看.
bbed> set dba 4,531
dba 0x01000213 (16777747 4,531)
bbed> p kd_off
sb2 kd_off[0] @132 8032
sb2 kd_off[1] @134 0
sb2 kd_off[2] @136 8019
sb2 kd_off[3] @138 8006
sb2 kd_off[4] @140 7993
sb2 kd_off[5] @142 7980
sb2 kd_off[6] @144 7967
sb2 kd_off[7] @146 7954
sb2 kd_off[8] @148 7941
sb2 kd_off[9] @150 7928
sb2 kd_off[10] @152 7915
sb2 kd_off[11] @154 7902
sb2 kd_off[12] @156 7889
sb2 kd_off[13] @158 7877
--//可以发现bbed查看索引有问题,kd_off[0],kd_off[1]看到的偏移是不对的.实际上kd_off偏移从136开始kd_off[2].
bbed> x /rnx *kd_off[2]
rowdata[172] @8119
------------
flag@8119: 0x00 (none)
lock@8120: 0x00
data key:
col 0[3] @8122: 7369
col 1[6] @8126: 0x01 0x00 0x02 0x0b 0x00 0x00
--//这个表的第一条记录(最小值),我没有删除.empno=7369.
bbed> x /rnx *kd_off[3]
rowdata[159] @8106
------------
flag@8106: 0x01 (kdxrdel)
lock@8107: 0x02
data key:
col 0[3] @8109: 7499
col 1[6] @8113: 0x01 0x00 0x02 0x0b 0x00 0x01
--//对比删除与不删除的可以看出.flag不同,删除的flag=0x01.
$ seq 2 13 | xargs -i{} echo 'x /rnx dba 4,531 *kd_off[{}]' | rlbbed | grep -b1 "^lock@.*:*0x02$" | grep "flag@.*0x01" | cut -d: -f1 | cut -d@ -f2 |xargs -i{} echo assign dba 4,531 offset {}=0x00
assign dba 4,531 offset 8106=0x00
assign dba 4,531 offset 8093=0x00
assign dba 4,531 offset 8080=0x00
assign dba 4,531 offset 8067=0x00
assign dba 4,531 offset 8054=0x00
assign dba 4,531 offset 8041=0x00
assign dba 4,531 offset 8028=0x00
assign dba 4,531 offset 8015=0x00
assign dba 4,531 offset 8002=0x00
assign dba 4,531 offset 7989=0x00
assign dba 4,531 offset 7977=0x00
--//仅仅修改11条,还有2条需要修改标识.
bbed> dump offset 160 count 4
file: /mnt/ramdisk/book/users01.dbf (4)
block: 531 offsets: 160 to 163 dba:0x01000213
---------------------------------------------------------------
b81eab1e
<64 bytes per line>
--// 说明: kd_off[13]的偏移量记录在偏移158,下面2条记录的偏移记录从160算起.
--// b81e 颠倒过来就是 1eb8=7864 , 记录相对偏移从kdxle算起(当前是100), 7864+100 = 7964
--// ab1e 颠倒过来就是 1eab=7851 , 记录相对偏移从kdxle算起(当前是100), 7851+100 = 7951
bbed> x /rnx offset 7964
rowdata[17] @7964
-----------
flag@7964: 0x01 (kdxrdel)
lock@7965: 0x02
data key:
col 0[3] @7967: 7902
col 1[6] @7971: 0x01 0x00 0x02 0x0b 0x00 0x0c
bbed> x /rnx offset 7951
rowdata[4] @7951
----------
flag@7951: 0x01 (kdxrdel)
lock@7952: 0x02
data key:
col 0[3] @7954: 7934
col 1[6] @7958: 0x01 0x00 0x02 0x0b 0x00 0x0d
--//也就是要补充执行如下:
assign dba 4,531 offset 7964=0x00
assign dba 4,531 offset 7951=0x00
--//执行如下脚本:
--//补充如果你仔细看就可以发现键值是按照行目录排序的.如果有记录插入,oracle是通过2分法定位插入行目录的位置,
--//你可以发现后面的偏移不是有序的,这也就是索引的块内无序,块间有序,但是如果索引分裂,oracle会重新排序.
assign dba 4,531 offset 8106=0x00
assign dba 4,531 offset 8093=0x00
assign dba 4,531 offset 8080=0x00
assign dba 4,531 offset 8067=0x00
assign dba 4,531 offset 8054=0x00
assign dba 4,531 offset 8041=0x00
assign dba 4,531 offset 8028=0x00
assign dba 4,531 offset 8015=0x00
assign dba 4,531 offset 8002=0x00
assign dba 4,531 offset 7989=0x00
assign dba 4,531 offset 7977=0x00
assign dba 4,531 offset 7964=0x00
assign dba 4,531 offset 7951=0x00
bbed> sum apply
check value for file 4, block 531:
current = 0x91b5, required = 0x91b5
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 531
block checking: dba = 16777747, block type = ktb-managed data block
**** actual free space = 7593 < kdxcoavs = 7787
**** actual rows marked deleted = 0 != kdxlende = 13
---- end index block validation
block 531 failed with check code 6401
bbed> p kdxle.kdxlende
sb2 kdxlende @118 13
bbed> assign kdxle.kdxlende=0
sb2 kdxlende @118 0
bbed> sum apply
check value for file 4, block 531:
current = 0x91b8, required = 0x91b8
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 531
block checking: dba = 16777747, block type = ktb-managed data block
**** actual free space = 7593 < kdxcoavs = 7787
---- end index block validation
block 531 failed with check code 6401
bbed> p kdxle.kdxlexco.kdxcoavs
sb2 kdxcoavs @114 7787
bbed> assign kdxle.kdxlexco.kdxcoavs=7593
sb2 kdxcoavs @114 7593
bbed> sum apply
check value for file 4, block 531:
current = 0x927a, required = 0x927a
bbed> verify
dbverify - verification starting
file = /mnt/ramdisk/book/users01.dbf
block = 531
--//ok,现在完全修复了.
3.验证看看:
scott@book> alter system flush buffer_cache;
system altered.
scott@book> select * from empy where empno in(7499,7369);
empno ename job mgr hiredate sal comm deptno
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 smith clerk 7902 1980-12-17 00:00:00 800 20
7499 allen salesman 7698 1981-02-20 00:00:00 1600 300 30
--//通过索引能定位行记录.
scott@book> analyze table empy validate structure cascade;
table analyzed.
--//analyze table empy validate structure cascade;一切ok,证明修复没有任何问题.
总结:
--//使用bbed修复索引有点繁琐,主要kd_off记录的偏移不对,从kd_off[2]算起.并且遗漏2条键值记录.