模拟BBED修改文件头解决数据库Open验证问题

参考大神 realkid4 的博客 http://blog.itpub.net/17203031/viewspace-2126665/ 做个实验发现点问题 做个记录
环境说明

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production


SQL> set linesize 1000 pagesize 500
SQL> select file#,checkpoint_change#,checkpoint_time,name from v$datafile;

     FILE# CHECKPOINT_CHANGE# CHECKPOIN NAME
---------- ------------------ --------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
         1            1059475 17-MAR-18 /u01/app/oracle/oradata/source/system01.dbf
         2            1059475 17-MAR-18 /u01/app/oracle/oradata/source/sysaux01.dbf
         3            1059475 17-MAR-18 /u01/app/oracle/oradata/source/undotbs01.dbf
         4            1059475 17-MAR-18 /u01/app/oracle/oradata/source/users01.dbf

思路是:在数据库正常运行过程中,抽取数据文件4。Datafile 4对应的是一个过去时间的SCN编号和文件头。之后,通过一系列的Online Redo Log切换、手工检查点操作,最后直接shutdown immediate关闭服务器动作,推动数据库所有数据文件SCN编号前进。

关闭之后,使用过去版本的数据文件4来替换文件。启动数据库进入open状态之后,如果当前online redo log已经乜有接续的文件(被覆盖+非归档),系统报错。

当前文件目录,拷贝留存一个旧版本的datafile 4。

[oracle@dg1 source]$ cp users01.dbf users01bak.dbf 
[oracle@dg1 source]$ ll
total 1760948
-rw-r-----. 1 oracle oinstall   9748480 Mar 17 21:41 control01.ctl
-rw-r-----. 1 oracle oinstall   9748480 Mar 17 21:41 control02.ctl
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:14 redo01.log
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:41 redo02.log
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:14 redo03.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo04.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo05.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo06.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo07.log
-rw-r-----. 1 oracle oinstall 534781952 Mar 17 21:37 sysaux01.dbf
-rw-r-----. 1 oracle oinstall 775954432 Mar 17 21:37 system01.dbf
-rw-r-----. 1 oracle oinstall  30416896 Nov 13 20:49 temp01.dbf
-rw-r-----. 1 oracle oinstall  78651392 Mar 17 21:37 undotbs01.dbf
-rw-r-----. 1 oracle oinstall  13115392 Mar 17 21:41 users01bak.dbf
-rw-r-----. 1 oracle oinstall  13115392 Mar 17 21:37 users01.dbf

多次切换日志,进行检查点操作

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> 
System altered.

SQL> select group#, status, FIRST_CHANGE# from v$log;

    GROUP# STATUS           FIRST_CHANGE#
---------- ---------------- -------------
         1 CURRENT                1059831
         2 INACTIVE               1059825
         3 INACTIVE               1059828

关闭数据库,强行使用旧版本文件替换

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@dg1 source]$ mv users01.dbf users011.dbf
[oracle@dg1 source]$ ll
total 1760948
-rw-r-----. 1 oracle oinstall   9748480 Mar 17 21:43 control01.ctl
-rw-r-----. 1 oracle oinstall   9748480 Mar 17 21:43 control02.ctl
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:42 redo01.log
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:43 redo02.log
-rw-r-----. 1 oracle oinstall  52429312 Mar 17 21:42 redo03.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo04.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo05.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo06.log
-rw-r-----. 1 oracle oinstall  52429312 Nov 13 20:01 std_redo07.log
-rw-r-----. 1 oracle oinstall 534781952 Mar 17 21:43 sysaux01.dbf
-rw-r-----. 1 oracle oinstall 775954432 Mar 17 21:43 system01.dbf
-rw-r-----. 1 oracle oinstall  30416896 Nov 13 20:49 temp01.dbf
-rw-r-----. 1 oracle oinstall  78651392 Mar 17 21:43 undotbs01.dbf
-rw-r-----. 1 oracle oinstall  13115392 Mar 17 21:43 users011.dbf
-rw-r-----. 1 oracle oinstall  13115392 Mar 17 21:41 users01bak.dbf
[oracle@dg1 source]$ mv users01bak.dbf users01.dbf

启动数据库,在open过程中报错

Total System Global Area 1603411968 bytes
Fixed Size                  2253664 bytes
Variable Size             989858976 bytes
Database Buffers          603979776 bytes
Redo Buffers                7319552 bytes
Database mounted.
ORA-01113: file 4 needs media recovery
ORA-01110: data file 4: '/u01/app/oracle/oradata/source/users01.dbf'

故障出现,尝试进行修复

总体修复的思路是:使用BBED,将文件头的SCN等关键信息修改到与控制文件control file相匹配即可。

当前,控制文件各个文件SCN相同,而数据文件上SCN不同。

--控制文件上信息

SQL>  select file#, CHECKPOINT_CHANGE# from v$datafile;

     FILE# CHECKPOINT_CHANGE#
---------- ------------------
         1            1059997
         2            1059997
         3            1059997
         4            1059997
 

SQL> select CHECKPOINT_CHANGE# from v$database;

CHECKPOINT_CHANGE#
------------------
           1059997
-数据文件头信息

SQL>  select file#, recover, fuzzy, CHECKPOINT_CHANGE# from v$datafile_header;

     FILE# REC FUZ CHECKPOINT_CHANGE#
---------- --- --- ------------------
         1 NO  NO             1059997
         2 NO  NO             1059997
         3 NO  NO             1059997
         4 YES YES            1059475
 

一种最简单的策略,是将File 4对应的SCN修改为其他文件相同。首先,我们可以先使用BBED看一下那些正确文件的内容是什么。

BBED> set filename '/u01/app/oracle/oradata/source/system01.dbf'
        FILENAME        /u01/app/oracle/oradata/source/system01.dbf

BBED> set block 1
        BLOCK#          1

BBED> map
 File: /u01/app/oracle/oradata/source/system01.dbf (1)
 Block: 1                                     Dba:0x00400001
------------------------------------------------------------
 Data File Header

 struct kcvfh, 860 bytes                    @0       

 ub4 tailchk                                @8188  

针对这个案例,我们通常需要关注四个偏移量offset点,分别为484、492、140和148。

分别抓取1号文件 与4号文件对比  

1 号文件

4 号文件 

1号文件偏移量 484 102c9d转换10进制 为1059997 与scn文件一致

偏移量492 为最后一次Check Point Time

偏移量140  Check Point Count

偏移量148  比检查点计数少1

其中,位于484和488偏移量的是数据文件对应的SCN编号。在Oracle内部,SCN是使用wrap*4*1024*1024*1024+base来进行标示的。通常我们看到的数据库wrap都是0。位于492偏移量的是最后一次检查点对应的时间信息。位于140和148偏移量的是检查点次数。这些信息都是会由于时间推动和检查点动作引起变化,我们严格情况下,需要保证文件头块的信息和控制文件信息一致。

另外一点,由于Linux是Little字节系统,要关注写入时候的格式问题。最简单的方式是dump一下偏移量,看看是怎么保存的。

参考1号文件

BBED> set offset 484
        OFFSET          484

BBED> dump
 File: /u01/app/oracle/oradata/source/system01.dbf (1)
 Block: 1                Offsets:  484 to  995           Dba:0x00400001
------------------------------------------------------------------------
 9d2c1000 00000000 43e6e039 01000000 28000000 20010000 100051bf 02000000 
 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 00000000 00000000 00000000 00000000 00000000 00000000 
 0d000d00 0d000100 00000000 00000000 00000000 02004000 94180e00 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 00000000 00000000 02089b3d 9b7de6d3 1a96005b b4f19eb0 ff000000 
 00000000 00000000 00000000 00000000 00000000 00300000 00000000 0027f76c 
 1f748a40 20489c47 0b463176 e0010600 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 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>

查看4号文件并修改

BBED> set offset 484
        OFFSET          484

BBED> dump
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  484 to  995           Dba:0x01000001
------------------------------------------------------------------------
 932a1000 00000000 a4e5e039 01000000 18000000 02000000 10000000 02000000 
 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 00000000 00000000 00000000 00000000 00000000 00000000 
 0d000d00 0d000100 00000000 00000000 00000000 02000001 03000000 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 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 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 00000000 00000000 00000000 

 <32 bytes per line>

修改 

关键一步


BBED> modify /x 9d2c1000 offset 484
BBED-00209: invalid number (9d2c1000) 

这里显示不是有效位数 第一次做实验scnwarp 位进了一位,以为scnwarp位没有带入到一起计算,后来使用9d2c10 ,应该是位数自动补齐,这里感谢杰哥指点

BBED> modify /x 9d2c10 offset 484
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  484 to  995           Dba:0x01000001
------------------------------------------------------------------------
 9d2c1000 00000000 a4e5e039 01000000 18000000 02000000 10000000 02000000 
 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 00000000 00000000 00000000 00000000 00000000 00000000 
 0d000d00 0d000100 00000000 00000000 00000000 02000001 03000000 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 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 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 00000000 00000000 00000000 

 <32 bytes per line>


BBED> sum apply
Check value for File 4, Block 1:
current = 0xc34f, required = 0xc34f

修改成功

接下来参考1号块依次修改492 ,140,148

BBED> map
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                                     Dba:0x01000001
------------------------------------------------------------
 Data File Header

 struct kcvfh, 860 bytes                    @0       

 ub4 tailchk                                @8188    


BBED> p kcvfh

BBED> set offset 492
        OFFSET          492

BBED> dump 
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  492 to 1003           Dba:0x01000001
------------------------------------------------------------------------
 a4e5e039 01000000 18000000 02000000 10000000 02000000 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 
 00000000 00000000 00000000 00000000 00000000 00000000 0d000d00 0d000100 
 00000000 00000000 00000000 02000001 03000000 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 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 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 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>


修改 

BBED> modify /x 43e6e039 offset 492
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  492 to 1003           Dba:0x01000001
------------------------------------------------------------------------
 43e6e039 01000000 18000000 02000000 10000000 02000000 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 
 00000000 00000000 00000000 00000000 00000000 00000000 0d000d00 0d000100 
 00000000 00000000 00000000 02000001 03000000 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 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 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 00000000 00000000 00000000 00000000 00000000 

 <32 bytes per line>

BBED> sum apply
Check value for File 4, Block 1:
current = 0xc34f, required = 0xc34f

注意:Oracle数据块中使用冗余校验功能,修改数据块之后,要使用sum apply重新计算校验位 

修改140

BBED> modify /x 8c offset 140
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  140 to  651           Dba:0x01000001
------------------------------------------------------------------------
 8c000000 cae3e039 79000000 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 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 04000000 05005553 45525300 00000000 00000000 00000000 00000000 00000000 
 00000000 04000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 7ac92131 01000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 9d2c1000 00000000 
 43e6e039 01000000 18000000 02000000 10000000 02000000 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 
 00000000 00000000 00000000 00000000 00000000 00000000 0d000d00 0d000100 

 <32 bytes per line>

BBED> sum apply
Check value for File 4, Block 1:
current = 0xc3b9, required = 0xc3b9
修改148

BBED> modify /x 8b offset  148
 File: /u01/app/oracle/oradata/source/users01.dbf (4)
 Block: 1                Offsets:  148 to  659           Dba:0x01000001
------------------------------------------------------------------------
 8b000000 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 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 04000000 05005553 
 45525300 00000000 00000000 00000000 00000000 00000000 00000000 04000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 7ac92131 01000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00000000 00000000 00000000 9d2c1000 00000000 43e6e039 01000000 
 18000000 02000000 10000000 02000000 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 00000000 00000000 
 00000000 00000000 00000000 00000000 0d000d00 0d000100 00000000 00000000 

 <32 bytes per line>

BBED> sum apply
Check value for File 4, Block 1:
current = 0xc34b, required = 0xc34b

最后验证

BBED> verify
DBVERIFY - Verification starting
FILE = /u01/app/oracle/oradata/source/users01.dbf
BLOCK = 1


DBVERIFY - Verification complete

Total Blocks Examined         : 1
Total Blocks Processed (Data) : 0
Total Blocks Failing   (Data) : 0
Total Blocks Processed (Index): 0
Total Blocks Failing   (Index): 0
Total Blocks Empty            : 0
Total Blocks Marked Corrupt   : 0
Total Blocks Influx           : 0
Message 531 not found;  product=RDBMS; facility=BBED
 

[oracle@dg1 source]$ dbv file=/u01/app/oracle/oradata/source/users01.dbf 

DBVERIFY: Release 11.2.0.4.0 - Production on Sat Mar 17 22:06:08 2018

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

DBVERIFY - Verification starting : FILE = /u01/app/oracle/oradata/source/users01.dbf


DBVERIFY - Verification complete

Total Pages Examined         : 1600
Total Pages Processed (Data) : 1247
Total Pages Failing   (Data) : 0
Total Pages Processed (Index): 2
Total Pages Failing   (Index): 0
Total Pages Processed (Other): 258
Total Pages Processed (Seg)  : 0
Total Pages Failing   (Seg)  : 0
Total Pages Empty            : 93
Total Pages Marked Corrupt   : 0
Total Pages Influx           : 0
Total Pages Encrypted        : 0
Highest block SCN            : 994786 (0.994786)
[oracle@dg1 source]$ 

准备起库


Total System Global Area 1603411968 bytes
Fixed Size                  2253664 bytes
Variable Size             989858976 bytes
Database Buffers          603979776 bytes
Redo Buffers                7319552 bytes
Database mounted.

SQL> alter database open;  

Database altered.
 

alter.log 中未报错

ALTER DATABASE   MOUNT
Successful mount of redo thread 1, with mount id 3089374267
Database mounted in Exclusive Mode
Lost write protection disabled
Completed: ALTER DATABASE   MOUNT
Sat Mar 17 22:07:49 2018
alter database open
Sat Mar 17 22:07:49 2018
Thread 1 advanced to log sequence 41 (thread open)
Thread 1 opened at log sequence 41
  Current log# 2 seq# 41 mem# 0: /u01/app/oracle/oradata/source/redo02.log
Successful open of redo thread 1
MTTR advisory is disabled because FAST_START_MTTR_TARGET is not set
Sat Mar 17 22:07:49 2018
SMON: enabling cache recovery
[9089] Successfully onlined Undo Tablespace 2.
Undo initialization finished serial:0 start:5739044 end:5739074 diff:30 (0 seconds)
Verifying file header compatibility for 11g tablespace encryption..
Verifying 11g file header compatibility for tablespace encryption completed
SMON: enabling tx recovery
Database Characterset is ZHS16GBK
No Resource Manager plan active
replication_dependency_tracking turned off (no async multimaster replication found)
Starting background process QMNC
Sat Mar 17 22:07:49 2018
QMNC started with pid=20, OS id=9099 
Completed: alter database open
Sat Mar 17 22:07:50 2018
Starting background process CJQ0
Sat Mar 17 22:07:50 2018
CJQ0 started with pid=22, OS id=9111 
 

SQL> select file#, recover, fuzzy, CHECKPOINT_CHANGE# from v$datafile_header;

     FILE# REC FUZ CHECKPOINT_CHANGE#
---------- --- --- ------------------
         1 NO  YES            1060001
         2 NO  YES            1060001
         3 NO  YES            1060001
         4 NO  YES            1060001

猜你喜欢

转载自blog.csdn.net/ljl_name/article/details/88353282
今日推荐