Oracle中模拟及修复数据块损坏

来源:互联网 发布:ubuntu 16.04分区教程 编辑:程序博客网 时间:2024/05/21 19:33
1.插入数据

E:oracleora92in>sqlplus "/ as sysdba"
sql*Plus: Release 9.2.0.4.0 - Production on 星期一 3月 8 20:27:15 2004
Copyright (c) 1982, 2002, oracle Corporation. All rights reserved.
连接到:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
sql> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
E:ORACLEORADATAEYGLESYSTEM01.DBF
E:ORACLEORADATAEYGLEUNDOTBS01.DBF
E:ORACLEORADATAEYGLEEYGLE01.DBF
sql> create tablespace block
2 datafile 'e:oracleoradataeyglelock.dbf'
3 size 1M
4 extent management local;
表空间已创建。
sql> alter user eygle default tablespace block;
用户已更改。
sql> alter user eygle quota unlimited on block;
用户已更改。
sql> connect eygle/eygle
已连接。
sql> create table t as select * from dba_users;
表已创建。
sql> insert into t select * from t;
已创建8行。
sql> /
已创建16行。
sql> /
已创建32行。
sql> /
已创建64行。
sql> /
已创建128行。
sql> /
已创建256行。
sql> /
已创建512行。
sql> /
已创建1024行。
sql> /
已创建2048行。
sql> /
已创建4096行。
sql> /
insert into t select * from t
*
ERROR 位于第 1 行:
ORA-01653: 表EYGLE.T无法通过8(在表空间BLOCK中)扩展
sql> commit;
提交完成。
sql> alter system checkpoint;
系统已更改。
sql> select count(*) from t;
COUNT(*)
----------
8192
sql> connect / as sysdba
已连接。
sql> shutdown immediate
数据库已经关闭。
已经卸载数据库。
oracle 例程已经关闭。
 

2.损坏数据文件
关闭数据库后用Ultredit编辑数据文件,随便更改几个字符.然后启动数据库.

sql> startup
oracle 例程已经启动。
Total system Global Area 101785252 bytes
Fixed Size 454308 bytes
Variable Size 75497472 bytes
Database Buffers 25165824 bytes
Redo Buffers 667648 bytes
数据库装载完毕。
数据库已经打开。
sql> select count(*) from eygle.t;
select count(*) from eygle.t
*
ERROR 位于第 1 行:
ORA-01578: oracle 数据块损坏(文件号4,块号35)
ORA-01110: 数据文件 4: 'E:ORACLEORADATAEYGLEBLOCK.DBF'
sql>
 
此时如果查询该表,会出现错误,数据块损坏无法读取.
有个内部工具BBED也可以用来编辑数据块的内容
3.使用DBV检查数据文件
 
E:Oracleoradataeygle>dbv file=block.dbf blocksize=8192
DBVERIFY: Release 9.2.0.4.0 - Production on 星期一 3月 8 20:48:50 2004
Copyright (c) 1982, 2002, oracle Corporation. All rights reserved.
DBVERIFY - 验证正在开始 : FILE = block.dbf
标记为损坏的页35
***
Corrupt block relative dba: 0x01000023 (file 4, block 35)
Bad check value found during dbv:
Data in bad block -
type: 6 format: 2 rdba: 0x01000023
last change scn: 0x0000.00049097 seq: 0x1 flg: 0x06
consistency value in tail: 0x90970601
check value in block header: 0xd6cb, computed block checksum: 0x2c0a
spare1: 0x0, spare2: 0x0, spare3: 0x0
***
标记为损坏的页69
***
Corrupt block relative dba: 0x01000045 (file 4, block 69)
Bad check value found during dbv:
Data in bad block -
type: 6 format: 2 rdba: 0x01000045
last change scn: 0x0000.00049097 seq: 0x1 flg: 0x06
consistency value in tail: 0x90970601
check value in block header: 0x33d1, computed block checksum: 0x653
spare1: 0x0, spare2: 0x0, spare3: 0x0
***

DBVERIFY - 验证完成
检查的页总数 :128
处理的页总数(数据):117
失败的页总数(数据):0
处理的页总数(索引):0
失败的页总数(索引):0
处理的页总数(其它):9
处理的总页数 (段) : 0
失败的总页数 (段) : 0
空的页总数 :0
标记为损坏的总页数:2
汇入的页总数 :0

 
dbv检测到坏块.
4.寻求恢复
在这种情况下,如果有备份,需要从备份中恢复
如果没有备份,那么坏块部分的数据肯定要丢失了
在这个时候导出是不允许的:

E:>exp eygle/eygle file=t.dmp tables=t
Export: Release 9.2.0.4.0 - Production on 星期一 3月 8 20:54:15 2004
Copyright (c) 1982, 2002, oracle Corporation. All rights reserved.
连接到: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集
即将导出指定的表通过常规路径 ...
. . 正在导出表 T
EXP-00056: 遇到 oracle 错误 1578
ORA-01578: oracle 数据块损坏(文件号4,块号35)
ORA-01110: 数据文件 4: 'E:ORACLEORADATAEYGLEBLOCK.DBF'
导出成功终止,但出现警告。
 
5.恢复步骤
当然,对于不同的情况需要区别对待
首先你需要检查损坏的对象,使用以下SQL:

sql> SELECT tablespace_name, segment_type, owner, segment_name
2 FROM dba_extents
3 WHERE file_id = 4
4 and 35 between block_id AND block_id + blocks - 1
5 ;
TABLESPACE_NAME SEGMENT_TYPE OWNER SEGMENT_NAME
---------------------------------------------------------------------------
BLOCK table EYGLE T
 
如果损失的是数据,ok
我们可以设置内部事件,使exp跳过这些损坏的block

sql> ALTER system SET EVENTS='10231 trace name context forever,level 10';
系统已更改。
 
note:
ALTER system SET EVENTS='10231 trace name context forever,level 10' ;
内部事件,设置在全表扫描时跳过损坏的数据块.

然后我们可以导出未损坏的数据

sql> host
microsoft windows xp [版本 5.1.2600]
(C) 版权所有 1985-2001 microsoft Corp.
E:
E:>exp eygle/eygle file=t.dmp tables=t
Export: Release 9.2.0.4.0 - Production on 星期一 3月 8 20:57:13 2004
Copyright (c) 1982, 2002, oracle Corporation. All rights reserved.
连接到: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集
即将导出指定的表通过常规路径 ...
. . 正在导出表 T 8036 行被导出
在没有警告的情况下成功终止导出。
 
这时候数据成功导出.
然后我们可以drop table,recreate,然后导入数据
本例中
我们损失了
8192 - 8036 = 156 行数据

sql> connect eygle/eygle
已连接。
sql> drop table t;
表已丢弃。
sql> host
microsoft windows xp [版本 5.1.2600]
(C) 版权所有 1985-2001 microsoft Corp.
E:Oracleora92in>cd
E:>imp eygle/eygle file=t.dmp tables=t
Import: Release 9.2.0.4.0 - Production on 星期一 3月 8 21:12:38 2004
Copyright (c) 1982, 2002, oracle Corporation. All rights reserved.
连接到: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and oracle Data Mining options
JServer Release 9.2.0.4.0 - Production
经由常规路径导出由EXPORT:V09.02.00创建的文件
已经完成ZHS16GBK字符集和AL16UTF16 NCHAR 字符集中的导入
. 正在将EYGLE的对象导入到 EYGLE
. . 正在导入表 "T" 8036行被导入
成功终止导入,但出现警告。
E:>exit
sql> select count(*) from t;
COUNT(*)
----------
8036
 
完成数据恢复