查看: 9052|回复: 8

使用bbed修复oracle数据文件header block损坏的通用方法

[复制链接]
论坛徽章:
5
2010新春纪念徽章
日期:2010-03-01 11:07:242012新春纪念徽章
日期:2012-01-04 11:54:26铁扇公主
日期:2012-02-21 15:03:13奥运会纪念徽章:皮划艇静水
日期:2012-09-17 16:05:02优秀写手
日期:2013-12-18 09:29:09
跳转到指定楼层
1#
发表于 2013-1-6 12:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 ubotutwin 于 2016-8-3 18:38 编辑

   前日,遇到一起ORA-01210: data file header is media corrupt故障,由于datafile header损坏造成数据库不能启动,同时无备份、无归档。网上搜索解决方案未果,因此摸索了一个解决datafile header损坏的通用解决方法。
   思路如下:
   1、复制未损坏的数据文件的datafile header block到损坏的文件头部。
   2、通过bbed修改相关数据结构为合法值。
   首先,模拟datafile header损坏的情况,为重现完全无法启动情况,先停止数据库,
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

使用dd命令模拟datafile header损坏
[oracle@tony orcl]$ cp ./users01.dbf ./users01.dbf.bak
[oracle@tony orcl]$ dd if=/dev/zero of=./users01.dbf bs=8192 count=2
2+0 records in
2+0 records out
16384 bytes (16 kB) copied, 0.000828924 seconds, 19.8 MB/s
[oracle@tony orcl]$ dd if=./users01.dbf.bak of=./users01.dbf bs=8192 skip=2 seek=2
639+0 records in
639+0 records out
5234688 bytes (5.2 MB) copied, 0.115857 seconds, 45.2 MB/s
   启动数据库
SQL> startup
ORACLE instance started.
Total System Global Area  608174080 bytes
Fixed Size                  1275104 bytes
Variable Size             163580704 bytes
Database Buffers          436207616 bytes
Redo Buffers                7110656 bytes
Database mounted.
ORA-01122: database file 4 failed verification check
ORA-01110: data file 4: '/u01/oradata/orcl/users01.dbf'
ORA-01210: data file header is media corrupt
可以看到,故障已经重现。
首先要从数据库内搜集修改datafile header的相关信息,通过暂时将损坏的文件离线,就可以临时打开数据库。
SQL> alter database datafile 4 offline;
Database altered.
SQL> alter database open;
Database altered.
需要收集的是:
1、控制文件中,该数据文件的创建时间。
2、File$字典表中,该数据文件的scn相关信息。
SQL> select file#,to_char(creation_time,'yyyymmddhh24miss') from v$datafile;
     FILE# TO_CHAR(CREATION_TIME,'YYYYM
---------- ----------------------------
         1 20100419102249
         2 20100419104641
         3 20100419102254
         4 20100419102256
         5 20130104195719
SQL> select file#,crscnwrp,crscnbas from file$;
     FILE#   CRSCNWRP   CRSCNBAS
---------- ---------- ----------
         1          0          8
         2          0     381980
         3          0       5782
         4          0       8853
         5          0     412969
使用dd工具,将同一表空间内完好数据文件的header block复制到损坏的文件头部,之所以使用同一表空间下的文件,是因为可以省去表空间相关变量的修改:
[oracle@tony orcl]$ rm ./users01.dbf.bak
[oracle@tony orcl]$ cp ./users01.dbf ./users01.dbf.bak
[oracle@tony orcl]$ dd if=./users02.dbf of=./users01.dbf bs=8192 count=2
2+0 records in
2+0 records out
16384 bytes (16 kB) copied, 0.000442415 seconds, 37.0 MB/s
[oracle@tony orcl]$ dd if=./users01.dbf.bak of=./users01.dbf bs=8192 skip=2 seek=2
639+0 records in
639+0 records out
5234688 bytes (5.2 MB) copied, 0.0560191 seconds, 93.4 MB/s
通过bbed观察数据块内部结构:
BBED> p kcvfh
struct kcvfh, 676 bytes                     @0      
   struct kcvfhbfh, 20 bytes                @0      
      ub1 type_kcbh                         @0        0x0b
      ub1 frmt_kcbh                         @1        0xa2
      ub1 spare1_kcbh                       @2        0x00
      ub1 spare2_kcbh                       @3        0x00
      ub4 rdba_kcbh                         @4        0x01400001
      ub4 bas_kcbh                          @8        0x00000000
      ub2 wrp_kcbh                          @12       0x0000
      ub1 seq_kcbh                          @14       0x01
      ub1 flg_kcbh                          @15       0x04 (KCBHFCKV)
      ub2 chkval_kcbh                       @16       0x0f36
      ub2 spare3_kcbh                       @18       0x0000
   struct kcvfhhdr, 76 bytes                @20      
      ub4 kccfhswv                          @20       0x00000000
      ub4 kccfhcvn                          @24       0x0a200500
      ub4 kccfhdbi                          @28       0x4f5f0ca1
      text kccfhdbn[0]                      @32      O
      text kccfhdbn[1]                      @33      R
      text kccfhdbn[2]                      @34      C
      text kccfhdbn[3]                      @35      L
      text kccfhdbn[4]                      @36      
      text kccfhdbn[5]                      @37      
      text kccfhdbn[6]                      @38      
      text kccfhdbn[7]                      @39      
      ub4 kccfhcsq                          @40       0x000001d6
      ub4 kccfhfsz                          @44       0x00000280
      s_blkz kccfhbsz                       @48       0x00
      ub2 kccfhfno                          @52       0x0005
      ub2 kccfhtyp                          @54       0x0003
      ub4 kccfhacid                         @56       0x00000000
      ub4 kccfhcks                          @60       0x00000000
      text kccfhtag[0]                      @64      
      text kccfhtag[1]                      @65      
      text kccfhtag[2]                      @66      
      text kccfhtag[3]                      @67      
      text kccfhtag[4]                      @68      
      text kccfhtag[5]                      @69      
      text kccfhtag[6]                      @70      
      text kccfhtag[7]                      @71      
      text kccfhtag[8]                      @72      
      text kccfhtag[9]                      @73      
      text kccfhtag[10]                     @74      
      text kccfhtag[11]                     @75      
      text kccfhtag[12]                     @76      
      text kccfhtag[13]                     @77      
      text kccfhtag[14]                     @78      
      text kccfhtag[15]                     @79      
      text kccfhtag[16]                     @80      
      text kccfhtag[17]                     @81      
      text kccfhtag[18]                     @82      
      text kccfhtag[19]                     @83      
      text kccfhtag[20]                     @84      
      text kccfhtag[21]                     @85      
      text kccfhtag[22]                     @86      
      text kccfhtag[23]                     @87      
      text kccfhtag[24]                     @88      
      text kccfhtag[25]                     @89      
      text kccfhtag[26]                     @90      
      text kccfhtag[27]                     @91      
      text kccfhtag[28]                     @92      
      text kccfhtag[29]                     @93      
      text kccfhtag[30]                     @94      
      text kccfhtag[31]                     @95      
   ub4 kcvfhrdb                             @96       0x00000000
   struct kcvfhcrs, 8 bytes                 @100     
      ub4 kscnbas                           @100      0x00064d29
      ub2 kscnwrp                           @104      0x0000
   ub4 kcvfhcrt                             @108      0x2fe9cb1f
   ub4 kcvfhrlc                             @112      0x2fe9c127
   struct kcvfhrls, 8 bytes                 @116     
      ub4 kscnbas                           @116      0x0005da31
      ub2 kscnwrp                           @120      0x0000
   ub4 kcvfhbti                             @124      0x00000000
   struct kcvfhbsc, 8 bytes                 @128     
      ub4 kscnbas                           @128      0x00000000
      ub2 kscnwrp                           @132      0x0000
   ub2 kcvfhbth                             @136      0x0000
   ub2 kcvfhsta                             @138      0x0004 (KCVFHOFZ)
   struct kcvfhckp, 36 bytes                @484     
      struct kcvcpscn, 8 bytes              @484     
         ub4 kscnbas                        @484      0x000681c4
         ub2 kscnwrp                        @488      0x0000
      ub4 kcvcptim                          @492      0x2fea155d
      ub2 kcvcpthr                          @496      0x0001
      union u, 12 bytes                     @500     
         struct kcvcprba, 12 bytes          @500     
            ub4 kcrbaseq                    @500      0x00000002
            ub4 kcrbabno                    @504      0x0000872b
            ub2 kcrbabof                    @508      0x0010
      ub1 kcvcpetb[0]                       @512      0x02
      ub1 kcvcpetb[1]                       @513      0x00
      ub1 kcvcpetb[2]                       @514      0x00
      ub1 kcvcpetb[3]                       @515      0x00
      ub1 kcvcpetb[4]                       @516      0x00
      ub1 kcvcpetb[5]                       @517      0x00
      ub1 kcvcpetb[6]                       @518      0x00
      ub1 kcvcpetb[7]                       @519      0x00
   ub4 kcvfhcpc                             @140      0x00000012
   ub4 kcvfhrts                             @144      0x00000000
   ub4 kcvfhccc                             @148      0x00000011
   struct kcvfhbcp, 36 bytes                @152     
      struct kcvcpscn, 8 bytes              @152     
         ub4 kscnbas                        @152      0x00000000
         ub2 kscnwrp                        @156      0x0000
      ub4 kcvcptim                          @160      0x00000000
      ub2 kcvcpthr                          @164      0x0000
      union u, 12 bytes                     @168     
         struct kcvcprba, 12 bytes          @168     
            ub4 kcrbaseq                    @168      0x00000000
            ub4 kcrbabno                    @172      0x00000000
            ub2 kcrbabof                    @176      0x0000
      ub1 kcvcpetb[0]                       @180      0x00
      ub1 kcvcpetb[1]                       @181      0x00
      ub1 kcvcpetb[2]                       @182      0x00
      ub1 kcvcpetb[3]                       @183      0x00
      ub1 kcvcpetb[4]                       @184      0x00
      ub1 kcvcpetb[5]                       @185      0x00
      ub1 kcvcpetb[6]                       @186      0x00
      ub1 kcvcpetb[7]                       @187      0x00
   ub4 kcvfhbhz                             @312      0x00000000
   struct kcvfhxcd, 16 bytes                @316     
      ub4 space_kcvmxcd[0]                  @316      0x00000000
      ub4 space_kcvmxcd[1]                  @320      0x00000000
      ub4 space_kcvmxcd[2]                  @324      0x00000000
      ub4 space_kcvmxcd[3]                  @328      0x00000000
   word kcvfhtsn                            @332      4
   ub2 kcvfhtln                             @336      0x0005
   text kcvfhtnm[0]                         @338     U
   text kcvfhtnm[1]                         @339     S
   text kcvfhtnm[2]                         @340     E
   text kcvfhtnm[3]                         @341     R
   text kcvfhtnm[4]                         @342     S
   text kcvfhtnm[5]                         @343      
   text kcvfhtnm[6]                         @344      
   text kcvfhtnm[7]                         @345      
   text kcvfhtnm[8]                         @346      
   text kcvfhtnm[9]                         @347      
   text kcvfhtnm[10]                        @348      
   text kcvfhtnm[11]                        @349      
   text kcvfhtnm[12]                        @350      
   text kcvfhtnm[13]                        @351      
   text kcvfhtnm[14]                        @352      
   text kcvfhtnm[15]                        @353      
   text kcvfhtnm[16]                        @354      
   text kcvfhtnm[17]                        @355      
   text kcvfhtnm[18]                        @356      
   text kcvfhtnm[19]                        @357      
   text kcvfhtnm[20]                        @358      
   text kcvfhtnm[21]                        @359      
   text kcvfhtnm[22]                        @360      
   text kcvfhtnm[23]                        @361      
   text kcvfhtnm[24]                        @362      
   text kcvfhtnm[25]                        @363      
   text kcvfhtnm[26]                        @364      
   text kcvfhtnm[27]                        @365      
   text kcvfhtnm[28]                        @366      
   text kcvfhtnm[29]                        @367      
   ub4 kcvfhrfn                             @368      0x00000005
   struct kcvfhrfs, 8 bytes                 @372     
      ub4 kscnbas                           @372      0x00000000
      ub2 kscnwrp                           @376      0x0000
   ub4 kcvfhrft                             @380      0x00000000
   struct kcvfhafs, 8 bytes                 @384     
      ub4 kscnbas                           @384      0x00000000
      ub2 kscnwrp                           @388      0x0000
   ub4 kcvfhbbc                             @392      0x00000000
   ub4 kcvfhncb                             @396      0x00000000
   ub4 kcvfhmcb                             @400      0x00000000
   ub4 kcvfhlcb                             @404      0x00000000
   ub4 kcvfhbcs                             @408      0x00000000
   ub2 kcvfhofb                             @412      0x0000
   ub2 kcvfhnfb                             @414      0x0000
   ub4 kcvfhprc                             @416      0x2ab85c76
   struct kcvfhprs, 8 bytes                 @420     
      ub4 kscnbas                           @420      0x00000001
      ub2 kscnwrp                           @424      0x0000
   struct kcvfhprfs, 8 bytes                @428     
      ub4 kscnbas                           @428      0x00000000
      ub2 kscnwrp                           @432      0x0000
   ub4 kcvfhtrt                             @444      0x00000000
需要调整的是:
ub4 rdba_kcbh                         @4      ---data block address
ub2 kccfhfno                          @52     ---file number
ub4 kscnbas                           @100    ---scn bas
ub4 kcvfhcrt                           @108    ---file create time
ub4 kcvfhrfn                           @368    ---reference file number
具体修改方法:
BBED> set offset 4
        OFFSET          4
BBED> modify /x 010000
    4号文件1号块的dba0x01000001
BBED> set offset 52
        OFFSET          52
BBED> modify /x 04
    修改file number4
BBED> set offset 100
        OFFSET          100
BBED> modify /x 952200
     Scn base由之前查询file$sql结果获得,由于linux是小头存储结构,因此最后要把字节序翻转。
BBED> set offset 108
        OFFSET          108
BBED> modify /x 805c
BBED> set offset 110
        OFFSET          110
BBED> modify /x b82a
这里要注意的是,bbedmodify一次最多只可以修改3个字节的值,因此4字节的kcvfhcrt变量要分为两次修改。
Oracle数据文件create time的计算方法为:文件建立时间-198811日零时的秒数,其中每个月都按31天计算。                  
BBED> set offset 368
        OFFSET          368         
BBED> modify /x 04
    修改rfile#4
BBED> sum dba 4,1 apply
Check value for File 4, Block 1:
current = 0xf202, required = 0xf202
    最后重新计算数据块的check sum值并写入。
    尝试在数据库中将该文件online
SQL> /
alter database datafile 4 online
*
ERROR at line 1:
ORA-01113: file 4 needs media recovery if it was restored from backup, or END
BACKUP if it was not
ORA-01110: data file 4: '/u01/oradata/orcl/users01.dbf'
SQL> recover datafile 4;
Media recovery complete.
SQL> alter database datafile 4 online;
Database altered.
    该文件已经成功恢复。
SQL> select owner,table_name from dba_tables a where a.tablespace_name='USERS';
OWNER
------------------------------------------------------------
TABLE_NAME
------------------------------------------------------------
SCOTT
DEPT
SCOTT
EMP
SCOTT
BONUS
OWNER
------------------------------------------------------------
TABLE_NAME
------------------------------------------------------------
SCOTT
SALGRADE
SQL> select * from scott.dept;
    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
    查询该表空间内的数据,完全正常。
    在oracle数据库损坏而造成不能启动的情况下,只要不是system表空间的损坏,是有很大可能性通过分析、调整来打开数据库并挽救数据的。只要对oracle的内部原理、结构有足够的了解。

论坛徽章:
42
ITPUB季度 技术新星
日期:2012-05-22 15:10:11祖母绿
日期:2013-09-13 21:16:10蓝锆石
日期:2013-09-13 21:15:34海蓝宝石
日期:2013-09-13 21:13:45最佳人气徽章
日期:2012-03-13 17:39:18优秀写手
日期:2013-12-18 09:29:11ITPUB社区12周年站庆徽章
日期:2013-10-17 13:56:592013年新春福章
日期:2013-02-25 14:51:24玉石琵琶
日期:2012-02-21 15:04:38ITPUB 11周年纪念徽章
日期:2012-10-09 18:08:15
2#
发表于 2013-1-6 12:40 | 只看该作者
dul 比odu功能弱吗?

使用道具 举报

回复
论坛徽章:
5
2010新春纪念徽章
日期:2010-03-01 11:07:242012新春纪念徽章
日期:2012-01-04 11:54:26铁扇公主
日期:2012-02-21 15:03:13奥运会纪念徽章:皮划艇静水
日期:2012-09-17 16:05:02优秀写手
日期:2013-12-18 09:29:09
3#
 楼主| 发表于 2013-1-6 12:59 | 只看该作者
xifenfei 发表于 2013-1-6 12:40
dul 比odu功能弱吗?

没用过dul,不知道啊

使用道具 举报

回复
求职 : 数据库管理员
论坛徽章:
45
祖国60周年纪念徽章
日期:2015-05-19 13:02:04itpub13周年纪念徽章
日期:2014-12-30 09:02:122010数据库技术大会纪念徽章
日期:2015-04-23 10:33:192011数据库大会纪念章
日期:2015-04-23 10:33:192012数据库大会纪念章
日期:2015-04-23 10:33:192013数据库大会纪念章
日期:2015-04-23 10:33:192014数据库大会纪念章
日期:2015-04-23 10:33:192015中国数据库技术大会纪念徽章
日期:2015-04-24 16:04:24暖羊羊
日期:2015-05-13 18:24:182015年新春福章
日期:2015-05-30 17:02:05
4#
发表于 2013-5-16 13:42 | 只看该作者
支持一下

使用道具 举报

回复
论坛徽章:
0
5#
发表于 2013-5-21 08:51 | 只看该作者
执行下面2条
SQL> alter database datafile 4 offline;
SQL> alter database open;
命令后

再执行
SQL> select file#,to_char(creation_time,'yyyymmddhh24miss') from v$datafile;
直接就报错了
ERROR at line 1:
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/dba/app/oracle/oradata/orcl/users01.dbf'

使用道具 举报

回复
论坛徽章:
4
2012新春纪念徽章
日期:2012-01-04 11:53:54ITPUB 11周年纪念徽章
日期:2012-10-09 18:08:152013年新春福章
日期:2013-02-25 14:51:24ITPUB社区OCM联盟徽章
日期:2013-03-21 15:37:32
6#
发表于 2013-6-6 11:08 | 只看该作者
三教罪人 发表于 2013-5-21 08:51
执行下面2条
SQL> alter database datafile 4 offline;
SQL> alter database open;

什么数据库版本?

使用道具 举报

回复
论坛徽章:
0
7#
发表于 2013-6-6 12:57 | 只看该作者
dcswinner 发表于 2013-6-6 11:08
什么数据库版本?

11.2.0.1

使用道具 举报

回复
论坛徽章:
0
8#
发表于 2013-6-6 13:25 | 只看该作者
楼主使用的什么版本?

使用道具 举报

回复
论坛徽章:
4
2014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11暖羊羊
日期:2015-03-04 14:50:372015年新春福章
日期:2015-03-06 11:57:31
9#
发表于 2013-11-27 14:23 | 只看该作者
收藏,下次阅读

使用道具 举报

回复
论坛徽章:
10
2013年新春福章
日期:2013-02-25 14:51:242015年新春福章
日期:2015-03-06 11:58:18懒羊羊
日期:2015-03-04 14:52:11沸羊羊
日期:2015-02-26 14:39:08优秀写手
日期:2014-04-17 06:00:16马上有对象
日期:2014-03-08 12:29:16红旗
日期:2013-12-09 20:02:28问答徽章
日期:2013-12-04 22:17:38紫蛋头
日期:2013-04-03 16:14:01天蝎座
日期:2015-07-27 15:42:40
10#
发表于 2014-5-9 16:13 | 只看该作者
4号文件1号块的dba是0x01000001 是如何得知的?

set offset 100
modify /x 952200是如何算的呢?

set offset 108
modify /x 805c 又是如何算的?

还有offset 110和368都是如何得出的,求指教

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表