使用BBED模拟Oracle数据库坏块

      BBED(OracleBlockBrowerandEDitor Tool),用来直接查看和修改数据文件数据的一个工具,是Oracle一款内部工具,可以直接修改Oracle数据文件块的内容,在一些极端恢复场景下比较有用。该工具不受Oracle支持,所以默认是没有生成可执行文件的,在使用前需要重新连接。

1.安装BBED

[oracle@std lib]$ pwd
/u02/app/product/10.2.0/db_1/rdbms/lib
[oracle@std lib]$ make-f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed
-bash: make-f: command not found
[oracle@std lib]$ make -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed

Linking BBED utility (bbed)
rm -f /u02/app/product/10.2.0/db_1/rdbms/lib/bbed
gcc -o /u02/app/product/10.2.0/db_1/rdbms/lib/bbed -L/u02/app/product/10.2.0/db_1/rdbms/lib/ -L/u02/app/product/10.2.0/db_1/lib/ -L/u02/app/product/10.2.0/db_1/lib/stubs/ -L/usr/lib -lirc  /u02/app/product/10.2.0/db_1/lib/s0main.o /u02/app/product/10.2.0/db_1/rdbms/lib/ssbbded.o /u02/app/product/10.2.0/db_1/rdbms/lib/sbbdpt.o `cat /u02/app/product/10.2.0/db_1/lib/ldflags`    -lnsslb10 -lncrypt10 -lnsgr10 -lnzjs10 -ln10 -lnnz10 -lnl10 /u02/app/product/10.2.0/db_1/rdbms/lib/defopt.o -ldbtools10 -lclntsh  `cat /u02/app/product/10.2.0/db_1/lib/ldflags`    -lnsslb10 -lncrypt10 -lnsgr10 -lnzjs10 -ln10 -lnnz10 -lnl10 -lnro10 `cat /u02/app/product/10.2.0/db_1/lib/ldflags`    -lnsslb10 -lncrypt10 -lnsgr10 -lnzjs10 -ln10 -lnnz10 -lnl10 -lclient10 -lnnetd10  -lvsn10 -lcommon10 -lgeneric10 -lmm -lsnls10 -lnls10  -lcore10 -lsnls10 -lnls10 -lcore10 -lsnls10 -lnls10 -lxml10 -lcore10 -lunls10 -lsnls10 -lnls10 -lcore10 -lnls10 `cat /u02/app/product/10.2.0/db_1/lib/ldflags`    -lnsslb10 -lncrypt10 -lnsgr10 -lnzjs10 -ln10 -lnnz10 -lnl10 -lnro10 `cat /u02/app/product/10.2.0/db_1/lib/ldflags`    -lnsslb10 -lncrypt10 -lnsgr10 -lnzjs10 -ln10 -lnnz10 -lnl10 -lclient10 -lnnetd10  -lvsn10 -lcommon10 -lgeneric10   -lsnls10 -lnls10  -lcore10 -lsnls10 -lnls10 -lcore10 -lsnls10 -lnls10 -lxml10 -lcore10 -lunls10 -lsnls10 -lnls10 -lcore10 -lnls10 -lclient10 -lnnetd10  -lvsn10 -lcommon10 -lgeneric10 -lsnls10 -lnls10  -lcore10 -lsnls10 -lnls10 -lcore10 -lsnls10 -lnls10 -lxml10 -lcore10 -lunls10 -lsnls10 -lnls10 -lcore10 -lnls10   `cat /u02/app/product/10.2.0/db_1/lib/sysliblist` -Wl,-rpath,/u02/app/product/10.2.0/db_1/lib -lm    `cat /u02/app/product/10.2.0/db_1/lib/sysliblist` -ldl -lm   -L/u02/app/product/10.2.0/db_1/lib
[oracle@std lib]$ ll bbed
-rwxr-xr-x 1 oracle oinstall 550734 Feb  5 10:34 bbed
[oracle@std lib]$ ./bbed
Password:     --密码默认为blockedit

BBED: Release 2.0.0.0.0 - Limited Production on Thu Feb 5 11:12:15 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> info
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------

 

OracleDatabase 11g中缺省的未提供BBED库文件,但是可以用10g的文件编译出来,需要先从10g中复制如下文件到相应目录,然后再执行上述连接命令。

$ORACLE_HOME/rdbms/lib/ssbbded.o

$ORACLE_HOME/rdbms/lib/sbbdpt.o

$ORACLE_HOME/rdbms/msg/bbedus.msb

 

2.使用参数文件连接BBED

--查询出当前库的数据文件并保存在文本文件中

 

SQL> select file#|| ||name|| ||bytes from v$datafile ;

FILE#||‘‘||NAME||‘‘||BYTES
--------------------------------------------------------------------------------
1 /u02/app/oradata/PSDB/system01.dbf 503316480
2 /u02/app/oradata/PSDB/undotbs01.dbf 36700160
3 /u02/app/oradata/PSDB/sysaux01.dbf 262144000
4 /u02/app/oradata/PSDB/users01.dbf 5242880
5 /u02/app/oradata/PSDB/example01.dbf 104857600
6 /u02/app/oradata/PSDB/livan_tbs01.dbf 314572800

6 rows selected.

--注意保存在文件里的文件号要与我们数据库查询出来的FILE#相同

[oracle@std lib]$ cat ~/psdb_file.txt 
1 /u02/app/oradata/PSDB/system01.dbf 503316480
2 /u02/app/oradata/PSDB/undotbs01.dbf 36700160
3 /u02/app/oradata/PSDB/sysaux01.dbf 262144000
4 /u02/app/oradata/PSDB/users01.dbf 5242880
5 /u02/app/oradata/PSDB/example01.dbf 104857600
6 /u02/app/oradata/PSDB/livan_tbs01.dbf 314572800

 

--BBED使用参数文件登录

 

oracle@std lib]$ cat ~/bbed_parameter.txt 
blocksize=8192
listfile=/home/oracle/psdb_file.txt
mode=edit
[oracle@std lib]$ ./bbed parfile=/home/oracle/bbed_parameter.txt 
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Thu Feb 5 11:39:23 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> info
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------
     1  /u02/app/oradata/PSDB/system01.dbf                               61440
     2  /u02/app/oradata/PSDB/undotbs01.dbf                               4480
     3  /u02/app/oradata/PSDB/sysaux01.dbf                               32000
     4  /u02/app/oradata/PSDB/users01.dbf                                  640
     5  /u02/app/oradata/PSDB/example01.dbf                              12800
     6  /u02/app/oradata/PSDB/livan_tbs01.dbf                            38400

BBED> 

 

 

3.创建测试对象

[oracle@std lib]$ sqlplus livan/livan

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 5 11:42:36 2015

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> create table test(id int,name varchar2(30));

Table created.

SQL> insert into test values(1,beijing);

1 row created.

SQL> insert into test values(2,shanghai);

1 row created.

SQL> insert into test values(3,shandong);

1 row created.

SQL> commit;

Commit complete.

 

--查看测试对象的数据块分布

 

SQL> select 
  2  dbms_rowid.ROWID_RELATIVE_FNO(rowid) fno,
  3  dbms_rowid.ROWID_BLOCK_NUMBER(rowid) bno,
  4  dbms_rowid.ROWID_ROW_NUMBER(rowid) rowno,
  5  name   from test;

       FNO        BNO      ROWNO NAME
---------- ---------- ---------- ------------------------------
         6         12          0 beijing
         6         12          1 shanghai
         6         12          2 shandong

 

4.使用BBED编辑数据块

[oracle@std lib]$ ./bbed parfile=/home/oracle/bbed_parameter.txt 
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Thu Feb 5 12:33:32 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> info
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------
     1  /u02/app/oradata/PSDB/system01.dbf                               61440
     2  /u02/app/oradata/PSDB/undotbs01.dbf                               4480
     3  /u02/app/oradata/PSDB/sysaux01.dbf                               32000
     4  /u02/app/oradata/PSDB/users01.dbf                                  640
     5  /u02/app/oradata/PSDB/example01.dbf                              12800
     6  /u02/app/oradata/PSDB/livan_tbs01.dbf                            38400

BBED> set dba 6,12
        DBA             0x0180000c (25165836 6,12)

BBED> dump /v dba 6,12 offset 0
 File: /u02/app/oradata/PSDB/livan_tbs01.dbf (6)
 Block: 12      Offsets:    0 to  127  Dba:0x0180000c
-------------------------------------------------------
 06a20000 0c008001 60d40700 00000506 l .?.....`?.....
 5c4d0000 01000000 c9ce0000 51d40700 l \M......晌..Q?.
 00000000 02003200 09008001 05002c00 l ......2.......,.
 1c010000 b1058000 f1000500 03200000 l ....?..?... ..
 60d40700 00000000 00000000 00000000 l `?.............
 00000000 00000000 00000000 00000000 l ................
 00000000 00010300 ffff1800 6c1f541f l ............l.T.
 541f0000 03008a1f 7b1f6c1f 00000000 l T.......{.l.....

 <16 bytes per line>

BBED> modify /x 12345678 dba 6,12 offset 0
 File: /u02/app/oradata/PSDB/livan_tbs01.dbf (6)
 Block: 12               Offsets:    0 to  127           Dba:0x0180000c
------------------------------------------------------------------------
 12345678 0c008001 60d40700 00000506 5c4d0000 01000000 c9ce0000 51d40700 
 00000000 02003200 09008001 05002c00 1c010000 b1058000 f1000500 03200000 
 60d40700 00000000 00000000 00000000 00000000 00000000 00000000 00000000 
 00000000 00010300 ffff1800 6c1f541f 541f0000 03008a1f 7b1f6c1f 00000000 

 <32 bytes per line>

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

 

 

5.登录SQLPLUS验证表

[oracle@std lib]$ sqlplus /as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Feb 5 12:50:22 2015

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> conn livan/livan
Connected.
SQL> select * from test;

        ID NAME
---------- ------------------------------
         1 beijing
         2 shanghai
         3 shandong

SQL> conn /as sysdba
Connected.
SQL> alter system flush buffer_cache;

System altered.

SQL> conn livan/livan
Connected.
SQL> select * from test;
select * from test
              *
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 12)
ORA-01110: data file 6: /u02/app/oradata/PSDB/livan_tbs01.dbf

 

6.使用DBV工具检查数据坏块

[oracle@std lib]$ dbv file=/u02/app/oradata/PSDB/livan_tbs01.dbf

DBVERIFY: Release 10.2.0.4.0 - Production on Thu Feb 5 12:50:05 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

DBVERIFY - Verification starting : FILE = /u02/app/oradata/PSDB/livan_tbs01.dbf
Page 12 is marked corrupt
Corrupt block relative dba: 0x0180000c (file 6, block 12)
Bad check value found during dbv: 
Data in bad block:
 type: 6 format: 2 rdba: 0x0180000c
 last change scn: 0x0000.0007d460 seq: 0x5 flg: 0x06
 spare1: 0x0 spare2: 0x0 spare3: 0x0
 consistency value in tail: 0xd4600605
 check value in block header: 0x4d5c
 computed block checksum: 0x1607



DBVERIFY - Verification complete

Total Pages Examined         : 38400
Total Pages Processed (Data) : 4
Total Pages Failing   (Data) : 0
Total Pages Processed (Index): 0
Total Pages Failing   (Index): 0
Total Pages Processed (Other): 11
Total Pages Processed (Seg)  : 0
Total Pages Failing   (Seg)  : 0
Total Pages Empty            : 38384
Total Pages Marked Corrupt   : 1    --发现坏块
Total Pages Influx           : 0
Highest block SCN            : 513120 (0.513120)

 

7.使用rman工具检查数据文件验证坏块

[oracle@std lib]$ rman target /

Recovery Manager: Release 10.2.0.4.0 - Production on Thu Feb 5 12:54:21 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: PSDB (DBID=1410134833)

RMAN> backup check logical validate database;

Starting backup at 05-FEB-15
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=141 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/u02/app/oradata/PSDB/system01.dbf
input datafile fno=00006 name=/u02/app/oradata/PSDB/livan_tbs01.dbf
input datafile fno=00003 name=/u02/app/oradata/PSDB/sysaux01.dbf
input datafile fno=00005 name=/u02/app/oradata/PSDB/example01.dbf
input datafile fno=00002 name=/u02/app/oradata/PSDB/undotbs01.dbf
input datafile fno=00004 name=/u02/app/oradata/PSDB/users01.dbf
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 05-FEB-15

 

----只对某个数据文件检查

RMAN> backup check logical validate datafile 6;

Starting backup at 05-FEB-15
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=159 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00006 name=/u02/app/oradata/PSDB/livan_tbs01.dbf
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 05-FEB-15

 

--RMAN的检查结果放在v$database_block_corruption

SQL> select file#,block#,blocks from v$database_block_corruption;

     FILE#     BLOCK#     BLOCKS
---------- ---------- ----------
         6         12          1

 

8.使用exp导出工具验证坏块

 

[oracle@std lib]$ exp livan/livan file=/home/oracle/livan_table.dmp tables=test

Export: Release 10.2.0.4.0 - Production on Thu Feb 5 13:00:27 2015

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                           TEST
EXP-00056: ORACLE error 1578 encountered
ORA-01578: ORACLE data block corrupted (file # 6, block # 12)
ORA-01110: data file 6: /u02/app/oradata/PSDB/livan_tbs01.dbf
Export terminated successfully with warnings.

 

 9.使用dbms_repair.check_object检验坏块

SQL> BEGIN 
  2    DBMS_REPAIR.ADMIN_TABLES ( 
  3    TABLE_NAME => REPAIR_TABLE, 
  4    TABLE_TYPE => dbms_repair.repair_table, 
  5    ACTION => dbms_repair.create_action, 
  6    TABLESPACE => livan_tbs); 
  7  END;
  8  /

PL/SQL procedure successfully completed.



SQL> set serveroutput on 
SQL> DECLARE 
  2  num_corrupt INT; 
  3  BEGIN 
  4    num_corrupt := 0; 
  5    DBMS_REPAIR.CHECK_OBJECT ( 
  6    SCHEMA_NAME => LIVAN, 
  7    OBJECT_NAME => TEST, 
  8    REPAIR_TABLE_NAME => REPAIR_TABLE, 
  9    corrupt_count => num_corrupt); 
 10    DBMS_OUTPUT.PUT_LINE(number corrupt:  || TO_CHAR (num_corrupt)); 
 11  END;
 12  /
number corrupt: 1

PL/SQL procedure successfully completed.



SQL> select RELATIVE_FILE_ID,BLOCK_ID,CORRUPT_TYPE,OBJECT_NAME     
  2  from REPAIR_TABLE;

RELATIVE_FILE_ID   BLOCK_ID CORRUPT_TYPE OBJECT_NAME
---------------- ---------- ------------ ------------------------------
               6         12         6148 TEST

 

10.使用analyze命令检验坏块

SQL> analyze table test validate structure cascade online;
analyze table test validate structure cascade online
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 12)
ORA-01110: data file 6: /u02/app/oradata/PSDB/livan_tbs01.dbf


 

11.使用bbed自带的命令verify检验坏块

BBED> verify dba 6,12
DBVERIFY - Verification starting
FILE = /u02/app/oradata/PSDB/livan_tbs01.dbf
BLOCK = 12

Block 12 is corrupt
Corrupt block relative dba: 0x0180000c (file 0, block 12)
Bad check value found during verification
Data in bad block:
 type: 6 format: 2 rdba: 0x0180000c
 last change scn: 0x0000.0007d460 seq: 0x5 flg: 0x06
 spare1: 0x0 spare2: 0x0 spare3: 0x0
 consistency value in tail: 0xd4600605
 check value in block header: 0x4d5c
 computed block checksum: 0x1607


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   : 1
Total Blocks Influx           : 0

 

 

参考:张晓明<<大话Oracle RAC>>
http://blog.csdn.net/tianlesoftware/article/details/5006580
http://blog.itpub.net/8494287/viewspace-1357457/
http://blog.csdn.net/haiross/article/details/15340489
http://blog.sina.com.cn/s/blog_a57562c801015pzr.html

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。