1、创建演示环境--当前环境
sys@USBO> select * from v$version where rownum<2;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
--创建表空间
sys@USBO> create tablespace tbs_tmp datafile '/u02/database/usbo/oradata/tbs_tmp.dbf'size 10m autoextend on;
--创建表对象tb_obj及其索引
sys@USBO> createtable tb_obj tablespace tbs_tmp asselect * from dba_objects;
sys@USBO> createindex i_tb_obj on tb_obj(object_id);
--表段上的相关信息
sys@USBO> select segment_name , header_file , header_block,blocks
2 from dba_segments where segment_name ='TB_OBJ';
SEGMENT_NAME HEADER_FILE HEADER_BLOCK BLOCKS
------------------------------ ----------- ------------ ----------
TB_OBJ 6 130 1152
--使用linux自带的dd命令来损坏数据块
[oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=131 <<EOF
> Corrupt me!
> EOF
0+1 records in
0+1 records out
12 bytes (12 B) copied, 0.000209854 seconds, 57.2 kB/s
[oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=141 <<EOF
> Corrupt me!
> EOF
0+1 records in
0+1 records out
12 bytes (12 B) copied, 0.00019939 seconds, 60.2 kB/s
[oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=151 <<EOF
> Corrupt me!
> EOF
0+1 records in
0+1 records out
12 bytes (12 B) copied, 2.1672e-05 seconds, 554 kB/s
sys@USBO> alter system flush buffer_cache;
--下面的查询收到了错误提示
sys@USBO> selectcount(*) from tb_obj;
selectcount(*) from tb_obj
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
2、使用DBMS_REPAIR修复坏块
Step a 创建相应的表对象
--使用DBMS_REPAIR.ADMIN_TABLES过程创建一个表对象,用于记录需要被修复的表
sys@USBO> 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 => 'USERS');
7 END;
8 /
PL/SQL procedure successfully completed.
--使用DBMS_REPAIR.ADMIN_TABLES过程创建一个表对象,用于记录在表块损坏后那些孤立索引,也就是指向坏块的那些索引
sys@USBO> BEGIN
2 DBMS_REPAIR.ADMIN_TABLES
3 (
4 TABLE_NAME => 'ORPHAN_KEY_TABLE',
5 TABLE_TYPE => DBMS_REPAIR.ORPHAN_TABLE,
6 ACTION => DBMS_REPAIR.CREATE_ACTION,
7 TABLESPACE => 'USERS'
8 );
9 END;
10 /
PL/SQL procedure successfully completed.
Step b 校验受损的对象
--使用DBMS_REPAIR.CHECK_OBJECT来检测对象上受损的情形,并返回受损块数
sys@USBO> SET SERVEROUTPUT ON
sys@USBO> DECLARE num_corrupt INT;
2 BEGIN
3 num_corrupt := 0;
4 DBMS_REPAIR.CHECK_OBJECT (
5 SCHEMA_NAME => 'SYS',
6 OBJECT_NAME => 'TB_OBJ',
7 REPAIR_TABLE_NAME => 'REPAIR_TABLE',
8 CORRUPT_COUNT => num_corrupt);
9 DBMS_OUTPUT.PUT_LINE('number corrupt: ' TO_CHAR (num_corrupt));
10 END;
11 /
number corrupt: 3
PL/SQL procedure successfully completed.
--下面我们可以从repair_table查询到受损的块
--从下面的查询中可以看出列marked_corrupt全部为true,表明我们在CHECK_OBJECT已经标注了坏块
--有一点不太清楚的是为什么块131在查询中被列出3次?
sys@USBO> COLUMN object_name FORMAT a10
sys@USBO> COLUMN repair_description FORMAT a28
sys@USBO> SET LINES 10000
sys@USBO> SELECT object_name, block_id, corrupt_type,marked_corrupt,repair_description FROM repair_table;
OBJECT_NAM BLOCK_ID CORRUPT_TYPE MARKED_COR REPAIR_DESCRIPTION
---------- ---------- ------------ ---------- ----------------------------
TB_OBJ 131 6148 TRUE mark block software corrupt
TB_OBJ 131 6148 TRUE mark block software corrupt
TB_OBJ 131 6148 TRUE mark block software corrupt
TB_OBJ 141 6148 TRUE mark block software corrupt
TB_OBJ 151 6148 TRUE mark block software corrupt
Step c 标记坏块
--过程FIX_CORRUPT_BLOCKS用于标记坏块,在这个演示中,我们在CHECK_OBJECT已经被标注了,如没有执行下面的过程
--由于上一步已经标注,所以下面的输出为0
sys@USBO> SET SERVEROUTPUT ON
sys@USBO> DECLARE num_fix INT;
2 BEGIN
3 num_fix := 0;
4 DBMS_REPAIR.FIX_CORRUPT_BLOCKS (
5 SCHEMA_NAME => 'SYS',
6 OBJECT_NAME=> 'TB_OBJ',
7 OBJECT_TYPE => dbms_repair.table_object,
8 REPAIR_TABLE_NAME => 'REPAIR_TABLE',
9 FIX_COUNT=> num_fix);
10 DBMS_OUTPUT.PUT_LINE('num fix: ' TO_CHAR(num_fix));
11 END;
12 /
num fix: 0
PL/SQL procedure successfully completed.
Step d DUMP孤立的索引键值
--使用DUMP_ORPHAN_KEYS过程将那些指向坏块的索引键值填充到ORPHAN_KEY_TABLE
sys@USBO> SET SERVEROUTPUT ON
sys@USBO> DECLARE num_orphans INT;
2 BEGIN
3 num_orphans := 0;
4 DBMS_REPAIR.DUMP_ORPHAN_KEYS (
5 SCHEMA_NAME => 'SYS',
6 OBJECT_NAME => 'I_TB_OBJ',
7 OBJECT_TYPE => dbms_repair.index_object,
8 REPAIR_TABLE_NAME => 'REPAIR_TABLE',
9 ORPHAN_TABLE_NAME=> 'ORPHAN_KEY_TABLE',
10 KEY_COUNT => num_orphans);
11 DBMS_OUTPUT.PUT_LINE('orphan key count: ' TO_CHAR(num_orphans));
12 END;
13 /
orphan keycount: 242
PL/SQL procedure successfully completed.
--下面的查询可以看到正好等于上面返回的数量也就是242条记录
sys@USBO> selectcount(*) from orphan_key_table;
COUNT(*)
----------
242
--验证对象是否可以查询,下面的结果显示依旧无法查询
sys@USBO> selectcount(*) from tb_obj;
selectcount(*) from tb_obj
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 6, block # 131)
ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
Step e 跳过坏块
--使用SKIP_CORRUPT_BLOCKS来告知Oracle哪些坏块需要被跳过
sys@USBO> BEGIN
2 DBMS_REPAIR.SKIP_CORRUPT_BLOCKS (
3 SCHEMA_NAME => 'SYS',
4 OBJECT_NAME => 'TB_OBJ',
5 OBJECT_TYPE => dbms_repair.table_object,
6 FLAGS => dbms_repair.skip_flag);
7 END;
8 /
PL/SQL procedure successfully completed.
--由于索引键上存在孤立索引,因此我们重建索引
sys@USBO> alterindex i_tb_obj rebuild;
Index altered.
--验证结果
sys@USBO> selectcount(*) from tb_obj;
COUNT(*)
----------
72211
3、后记
a、提醒,备份重于一切,因此无论何时应保留可用的备份。
b、DBMS_REPAIR包并不是真正意思上的坏块修复,而是标记坏块,损坏的这部分数据被丢失。
c、DBMS_REPAIR包的几个步骤,先创建相应的表用于存储修复表及孤立索引,其次CHECK_OBJECT,FIX_CORRUPT_BLOCKS,DUMP_ORPHAN_KEYS,SKIP_CORRUPT_BLOCKS。
d、完整DBMS_REPAIR上面提到的几个步骤后,建议重建索引。
e、注,如果受损表对象被其他对象参照,建议先disable这些约束,那些在子表上孤立的记录可根据情形决定后再enable约束。
--转自