本文档风哥主要介绍Oracle块级恢复相关知识,包括块级恢复的概念、块级恢复方法、块级恢复使用、块级恢复配置、块级恢复监控、块级恢复故障处理等内容,由风哥教程参考Oracle官方文档Troubleshooting内容编写,适合DBA人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 块级恢复的概念
Oracle块级恢复(Block Media Recovery)是使用RMAN恢复单个或多个损坏数据块的技术。块级恢复只恢复损坏的块,而不需要恢复整个数据文件,大大减少了恢复时间。块级恢复可以在数据库运行时进行,不影响其他数据块的访问。块级恢复是处理数据块损坏的首选方法。更多视频教程www.fgedu.net.cn
- 快速恢复:只恢复损坏的块,恢复速度快
- 在线恢复:数据库可以保持运行状态
- 精确恢复:只恢复损坏的块,不影响其他数据
- 减少停机:减少数据库停机时间
- 灵活恢复:可以选择恢复特定的块
1.2 块级恢复方法
Oracle块级恢复方法:
- RMAN BLOCKRECOVER:使用RMAN恢复损坏块
- 恢复损坏列表:恢复V$DATABASE_BLOCK_CORRUPTION中的所有损坏块
- 恢复指定块:恢复指定的数据文件和块号
- 恢复表空间块:恢复表空间中的损坏块
- 恢复数据文件块:恢复数据文件中的损坏块
BLOCKRECOVER DATAFILE file# BLOCK block#
恢复指定数据文件的指定块
示例:BLOCKRECOVER DATAFILE 4 BLOCK 1024;
BLOCKRECOVER DATAFILE file# BLOCK block# TO block#
恢复指定数据文件的块范围
示例:BLOCKRECOVER DATAFILE 4 BLOCK 1024 TO 1030;
BLOCKRECOVER CORRUPTION LIST
恢复V$DATABASE_BLOCK_CORRUPTION中的所有损坏块
示例:BLOCKRECOVER CORRUPTION LIST;
BLOCKRECOVER TABLESPACE tablespace
恢复表空间中的损坏块
示例:BLOCKRECOVER TABLESPACE users;
BLOCKRECOVER DATAFILE file#
恢复数据文件中的损坏块
示例:BLOCKRECOVER DATAFILE 4;
# 块级恢复前提条件
– 必须有可用的RMAN备份
– 备份必须包含损坏的块
– 归档日志必须可用
– 数据库必须在归档模式下
1.3 块级恢复使用
Oracle块级恢复使用:
- 损坏检测:检测数据块损坏
- 损坏定位:定位损坏块的位置
- 备份验证:验证备份是否包含损坏块
- 块级恢复:执行块级恢复
- 恢复验证:验证恢复结果
Part02-生产环境规划与建议
2.1 块级恢复规划
Oracle块级恢复规划要点:
– 备份策略:确保有可用的RMAN备份
– 归档模式:数据库必须在归档模式下
– 监控机制:建立数据块损坏监控
– 恢复流程:建立块级恢复流程
# 备份策略规划
– 定期备份:定期进行RMAN备份
– 备份验证:验证备份的可用性
– 备份保留:保留足够的备份
– 归档日志:保留足够的归档日志
# 监控机制规划
– 定期检测:定期检测数据块损坏
– 告警机制:建立损坏告警机制
– 记录保存:保存损坏记录
– 分析报告:定期生成分析报告
# 恢复流程规划
– 损坏检测:检测数据块损坏
– 损坏定位:定位损坏块位置
– 备份验证:验证备份可用性
– 块级恢复:执行块级恢复
– 恢复验证:验证恢复结果
# 恢复时间规划
– 响应时间:快速响应损坏告警
– 恢复时间:快速完成块级恢复
– 验证时间:验证恢复结果
– 记录时间:记录恢复过程
2.2 块级恢复场景
Oracle块级恢复场景:
- 物理损坏:物理损坏导致的数据块损坏
- 逻辑损坏:逻辑损坏导致的数据块损坏
- 存储故障:存储故障导致的数据块损坏
- 介质损坏:介质损坏导致的数据块损坏
- 系统崩溃:系统崩溃导致的数据块损坏
1. 物理损坏
– 存储介质故障
– 磁盘坏块
– IO错误
– 数据文件损坏
2. 逻辑损坏
– Oracle内部错误
– 数据不一致
– 索引损坏
– 段损坏
3. 存储故障
– 存储系统故障
– 存储控制器故障
– 存储网络故障
– 存储配置错误
4. 介质损坏
– 磁盘介质损坏
– SSD介质损坏
– 磁带介质损坏
– 光盘介质损坏
5. 系统崩溃
– 操作系统崩溃
– 系统断电
– 系统重启
– 系统故障
2.3 块级恢复最佳实践
Oracle块级恢复最佳实践:
- 定期备份:定期进行RMAN备份
- 定期检测:定期检测数据块损坏
- 快速响应:快速响应损坏告警
- 验证恢复:验证恢复结果
- 记录过程:记录恢复过程
Part03-生产环境项目实施方案
3.1 块级恢复配置
3.1.1 配置块级恢复检测
RMAN> validate datafile 4;
Starting validate at 2026-03-31 10:00:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: starting validation of datafile
channel ORA_DISK_1: specifying datafile(s) for validation
input datafile file number=00004 name=/oracle/app/oracle/oradata/FGEDUDB/users01.dbf
channel ORA_DISK_1: validation complete, elapsed time: 00:00:10
List of Datafiles
=================
File Status Marked Corrupt Empty Blocks Blocks Examined High SCN
—- —— ————– ———— ————— ———-
4 OK 0 2559 12800 12345678
File Name: /oracle/app/oracle/oradata/FGEDUDB/users01.dbf
Block Type Blocks Failing Blocks Processed
———- ————– —————-
Data 0 8192
Index 0 2048
Other 0 2559
Finished validate at 2026-03-31 10:00:10
# 2. 使用RMAN验证数据库
RMAN> validate database;
Starting validate at 2026-03-31 10:00:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: starting validation of datafile
channel ORA_DISK_1: specifying datafile(s) for validation
input datafile file number=00001 name=/oracle/app/oracle/oradata/FGEDUDB/system01.dbf
input datafile file number=00002 name=/oracle/app/oracle/oradata/FGEDUDB/sysaux01.dbf
input datafile file number=00003 name=/oracle/app/oracle/oradata/FGEDUDB/undotbs01.dbf
input datafile file number=00004 name=/oracle/app/oracle/oradata/FGEDUDB/users01.dbf
channel ORA_DISK_1: validation complete, elapsed time: 00:00:30
List of Datafiles
=================
File Status Marked Corrupt Empty Blocks Blocks Examined High SCN
—- —— ————– ———— ————— ———-
1 OK 0 10240 51200 12345678
2 OK 0 8192 40960 12345678
3 OK 0 2048 25600 12345678
4 OK 0 2559 12800 12345678
Finished validate at 2026-03-31 10:00:30
# 3. 查看损坏块信息
SQL> select * from v$database_block_corruption;
FILE# BLOCK# BLOCKS CORRUPTION_CHANGE# CORRUPTION_TYPE
———- ———- ———- —————— —————
4 1024 1 0.00 ALL ZERO
# 4. 使用DBV验证数据文件
$ dbv file=/oracle/app/oracle/oradata/FGEDUDB/users01.dbf
DBVERIFY: Release 19.0.0.0.0 – Production on Tue Mar 31 10:00:00 2026
DBVERIFY – Verification starting : FILE = /oracle/app/oracle/oradata/FGEDUDB/users01.dbf
Page 1024 is marked corrupt
Corrupt block relative dba: 0x01000400, file 4, block 1024
Completely zero block found during dbv:
DBVERIFY – Verification complete
Total Pages Examined : 12800
Total Pages Processed (Data) : 8192
Total Pages Failing (Data) : 0
Total Pages Processed (Index): 2048
Total Pages Failing (Index): 0
Total Pages Processed (Other): 2559
Total Pages Processed (Seg) : 0
Total Pages Failing (Seg) : 0
Total Pages Empty : 0
Total Pages Marked Corrupt : 1
Total Pages Influx : 0
Total Pages Encrypted : 0
Highest block SCN : 12345678 (0.12345678)
# 5. 查看损坏块所属对象
SQL> select
segment_type,
owner,
segment_name
from dba_extents
where file_id = 4
and 1024 between block_id and block_id + blocks – 1;
SEGMENT_TYPE OWNER SEGMENT_NAME
————– ——- ————
TABLE SCOTT EMP
3.1.2 配置块级恢复执行
RMAN> blockrecover datafile 4 block 1024;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 2. 恢复多个损坏块
RMAN> blockrecover datafile 4 block 1024, 1025, 1026;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:15
starting media recovery
media recovery complete, elapsed time: 00:00:10
Finished blockrecover at 2026-03-31 10:10:25
# 3. 恢复块范围
RMAN> blockrecover datafile 4 block 1024 to 1030;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:20
starting media recovery
media recovery complete, elapsed time: 00:00:15
Finished blockrecover at 2026-03-31 10:10:35
# 4. 恢复损坏列表中的所有块
RMAN> blockrecover corruption list;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 5. 恢复表空间中的损坏块
RMAN> blockrecover tablespace users;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 6. 恢复数据文件中的损坏块
RMAN> blockrecover datafile 4;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 7. 从指定备份恢复块
RMAN> blockrecover datafile 4 block 1024 from backupset 12345;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 8. 验证恢复结果
SQL> select * from v$database_block_corruption;
no rows selected
SQL> select count(*) from scott.emp;
COUNT(*)
———-
14
3.2 块级恢复监控
3.2.1 配置块级恢复监控脚本
$ vi /home/oracle/scripts/monitor_block_corruption.sh
#!/bin/bash
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 块级恢复监控脚本
export ORACLE_HOME=/oracle/app/oracle/product/19c/dbhome_1
export ORACLE_SID=FGEDUDB
export PATH=$ORACLE_HOME/bin:$PATH
LOG_DIR=”/home/oracle/scripts/block_recovery_logs”
mkdir -p $LOG_DIR
echo “$(date): Starting block corruption monitoring…” > $LOG_DIR/block_monitor.log
# 使用RMAN验证数据库
rman target / <
run {
allocate channel ch1 type disk;
validate database;
release channel ch1;
}
exit
EOF
# 检查损坏块
sqlplus -s / as sysdba <
set heading on
set feedback on
column corruption_type format a20
select file#, block#, blocks, corruption_type
from v\$database_block_corruption
order by file#, block#;
exit
EOF
# 检查是否有损坏
CORRUPT_COUNT=$(sqlplus -s / as sysdba <
EOF
)
if [ “$CORRUPT_COUNT” -gt 0 ]; then
echo “$(date): CORRUPTION DETECTED: $CORRUPT_COUNT corrupt blocks found” >> $LOG_DIR/block_monitor.log
# 发送告警邮件
mail -s “Block Corruption Alert: $CORRUPT_COUNT corrupt blocks found” admin@fgedu.net.cn < $LOG_DIR/block_monitor.log
# 执行块级恢复
echo "$(date): Starting block recovery..." >> $LOG_DIR/block_monitor.log
rman target / <
blockrecover corruption list;
exit
EOF
echo “$(date): Block recovery completed.” >> $LOG_DIR/block_monitor.log
else
echo “$(date): No corruption detected.” >> $LOG_DIR/block_monitor.log
fi
echo “$(date): Block corruption monitoring completed.” >> $LOG_DIR/block_monitor.log
# 2. 设置脚本权限
$ chmod +x /home/oracle/scripts/monitor_block_corruption.sh
# 3. 测试脚本
$ /home/oracle/scripts/monitor_block_corruption.sh
# 4. 设置定期监控
$ crontab -e
# 每天凌晨2点监控块损坏
0 2 * * * /home/oracle/scripts/monitor_block_corruption.sh >> /home/oracle/scripts/block_monitor.log 2>&1
3.2.2 配置块级恢复报告
$ vi /home/oracle/scripts/block_recovery_report.sh
#!/bin/bash
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 块级恢复报告脚本
export ORACLE_HOME=/oracle/app/oracle/product/19c/dbhome_1
export ORACLE_SID=FGEDUDB
export PATH=$ORACLE_HOME/bin:$PATH
LOG_DIR=”/home/oracle/scripts/block_recovery_logs”
REPORT_FILE=”$LOG_DIR/block_recovery_report.txt”
echo “Block Recovery Report” > $REPORT_FILE
echo “Generated: $(date)” >> $REPORT_FILE
echo “========================================” >> $REPORT_FILE
# 获取损坏块信息
sqlplus -s / as sysdba <
set heading on
set feedback on
set linesize 200
column file_name format a50
column corruption_type format a20
column owner format a10
column segment_name format a30
prompt
prompt Corrupt Blocks Summary:
prompt ———————-
select
d.file_name,
c.file#,
c.block#,
c.blocks,
c.corruption_type
from v\$database_block_corruption c, dba_data_files d
where c.file# = d.file_id
order by c.file#, c.block#;
prompt
prompt Corrupt Blocks Details:
prompt ———————–
select
e.owner,
e.segment_name,
e.segment_type,
c.file#,
c.block#,
c.corruption_type
from v\$database_block_corruption c, dba_extents e
where c.file# = e.file_id
and c.block# between e.block_id and e.block_id + e.blocks – 1
order by c.file#, c.block#;
exit
EOF
# 发送报告
mail -s “Block Recovery Report” admin@fgedu.net.cn < $REPORT_FILE
# 2. 设置脚本权限
$ chmod +x /home/oracle/scripts/block_recovery_report.sh
# 3. 测试脚本
$ /home/oracle/scripts/block_recovery_report.sh
# 4. 查看报告
$ cat /home/oracle/scripts/block_recovery_logs/block_recovery_report.txt
Block Recovery Report
Generated: Tue Mar 31 10:00:00 CST 2026
========================================
Corrupt Blocks Summary:
----------------------
FILE_NAME FILE# BLOCK# BLOCKS CORRUPTION_TYPE
-------------------------------------------------- ---------- ---------- ---------- --------------------
/oracle/app/oracle/oradata/FGEDUDB/users01.dbf 4 1024 1 ALL ZERO
Corrupt Blocks Details:
-----------------------
OWNER SEGMENT_NAME SEGMENT_TYPE FILE# BLOCK# CORRUPTION_TYPE
---------- ------------------------------ ---------------- ---------- ---------- --------------------
SCOTT EMP TABLE 4 1024 ALL ZERO
3.3 块级恢复故障处理
3.3.1 块级恢复常见错误处理
# 原因:备份不包含损坏的块
# 解决:查找包含损坏块的备份
RMAN> blockrecover datafile 4 block 1024;
RMAN-06026: some targets not found – aborting restore
RMAN-06023: no backup or copy of datafile 4 found to restore
# 查找可用备份
RMAN> list backup of datafile 4;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
——- —- — ———- ———– ———— —————
12345 Full 1024.00M DISK 00:05:00 2026-03-30 10:00:00
# 使用指定备份恢复
RMAN> run {
allocate channel ch1 type disk;
blockrecover datafile 4 block 1024 from backupset 12345;
release channel ch1;
}
Starting blockrecover at 2026-03-31 10:10:00
allocated channel: ch1
channel ch1: SID=150 device type=DISK
channel ch1: restoring block(s)
channel ch1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ch1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ch1: restored block(s) from backup piece 1
channel ch1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 2. 错误:RMAN-06054: media recovery requesting unknown archived log
# 原因:归档日志缺失
# 解决:恢复归档日志或使用备份控制文件
RMAN> blockrecover datafile 4 block 1024;
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 12345 and starting SCN of 12345678
# 查看归档日志状态
RMAN> list archivelog all;
# 恢复归档日志
RMAN> restore archivelog sequence 12345;
# 重新执行块级恢复
RMAN> blockrecover datafile 4 block 1024;
# 3. 错误:ORA-01178: file 4 created before last CREATE CONTROLFILE
# 原因:控制文件不包含数据文件信息
# 解决:重建控制文件或使用备份控制文件
RMAN> blockrecover datafile 4 block 1024;
ORA-01178: file 4 created before last CREATE CONTROLFILE
# 使用备份控制文件
RMAN> restore controlfile from ‘/backup/FGEDUDB/control_12345.bak’;
Starting restore at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:05
output file name=/oracle/app/oracle/oradata/FGEDUDB/control01.ctl
output file name=/oracle/app/oracle/oradata/FGEDUDB/control02.ctl
Finished restore at 2026-03-31 10:10:05
RMAN> alter database mount;
Statement processed
RMAN> blockrecover datafile 4 block 1024;
# 4. 错误:ORA-19573: cannot obtain exclusive enqueue for datafile 4
# 原因:数据文件正在被访问
# 解决:等待或终止访问
RMAN> blockrecover datafile 4 block 1024;
ORA-19573: cannot obtain exclusive enqueue for datafile 4
# 查看访问会话
SQL> select sid, serial#, status from v$session where sid in (
select sid from v$lock where id1 = 4
);
SID SERIAL# STATUS
———- ———- ——–
150 1234 ACTIVE
# 终止会话
SQL> alter system kill session ‘150,1234’;
System altered.
# 重新执行块级恢复
RMAN> blockrecover datafile 4 block 1024;
Part04-生产案例与实战讲解
4.1 块级恢复分析案例
在生产环境中使用块级恢复的完整案例:
4.1.1 场景描述
某企业生产数据库出现数据块损坏,需要使用RMAN块级恢复进行修复。
4.1.2 分析步骤
SQL> select * from scott.emp where empno = 7369;
select * from scott.emp where empno = 7369
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 4, block # 1024)
ORA-01110: data file 4: ‘/oracle/app/oracle/oradata/FGEDUDB/users01.dbf’
# 2. 确认损坏信息
SQL> select file#, block#, blocks, corruption_type
from v$database_block_corruption;
FILE# BLOCK# BLOCKS CORRUPTION_TYPE
———- ———- ———- ——————–
4 1024 1 ALL ZERO
# 3. 确认损坏对象
SQL> select owner, segment_name, segment_type
from dba_extents
where file_id = 4
and 1024 between block_id and block_id + blocks – 1;
OWNER SEGMENT_NAME SEGMENT_TYPE
———- —————————— ——————
SCOTT EMP TABLE
# 4. 查看可用备份
RMAN> list backup of datafile 4;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
——- —- — ———- ———– ———— —————
12345 Full 1024.00M DISK 00:05:00 2026-03-30 10:00:00
BP Key: 12346 Status: AVAILABLE Compressed: NO Tag: TAG20260330T100000
Piece Name: /backup/FGEDUDB/backup_12345.bak
List of Datafiles in backup set 12345
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
—- — —- ———- ——— ———– ——- —-
4 Full 12345678 2026-03-30 10:00:00 /oracle/app/oracle/oradata/FGEDUDB/users01.dbf
# 5. 执行块级恢复
RMAN> blockrecover datafile 4 block 1024;
Starting blockrecover at 2026-03-31 10:10:00
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=150 device type=DISK
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/FGEDUDB/backup_12345.bak
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:10
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished blockrecover at 2026-03-31 10:10:15
# 6. 验证恢复结果
SQL> select * from v$database_block_corruption;
no rows selected
SQL> select * from scott.emp where empno = 7369;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
———- ———- ——— ———- ——— ———- ———- ———-
7369 SMITH CLERK 7902 17-DEC-80 800 20
# 7. 验证数据完整性
SQL> select count(*) from scott.emp;
COUNT(*)
———-
14
4.2 块级恢复故障处理
在块级恢复故障处理过程中的方法和技巧:
4.2.1 故障处理流程
# 1. 故障识别
# – 检测数据块损坏
# – 确认损坏位置
# – 确认损坏类型
# 2. 故障分析
# – 确认损坏对象
# – 分析损坏原因
# – 评估影响范围
# 3. 故障处理
# – 准备恢复环境
# – 执行块级恢复
# – 验证恢复结果
# 4. 故障预防
# – 定期备份数据
# – 定期检测损坏
# – 监控存储状态
# 示例:块级恢复故障处理
# 1. 故障识别
SQL> select * from v$database_block_corruption;
FILE# BLOCK# BLOCKS CORRUPTION_TYPE
———- ———- ———- ——————–
4 1024 1 ALL ZERO
4 1025 1 ALL ZERO
# 2. 故障分析
SQL> select owner, segment_name, segment_type
from dba_extents
where file_id = 4
and 1024 between block_id and block_id + blocks – 1;
OWNER SEGMENT_NAME SEGMENT_TYPE
———- —————————— ——————
SCOTT EMP TABLE
# 3. 故障处理
RMAN> blockrecover datafile 4 block 1024, 1025;
Starting blockrecover at 2026-03-31 10:10:00
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:15
starting media recovery
media recovery complete, elapsed time: 00:00:10
Finished blockrecover at 2026-03-31 10:10:25
# 4. 验证处理结果
SQL> select * from v$database_block_corruption;
no rows selected
4.3 块级恢复优化
优化块级恢复配置的最佳实践:
4.3.1 优化块级恢复管理
SQL> create or replace procedure auto_block_recovery as
v_corrupt_count number;
begin
dbms_output.put_line(‘— Auto Block Recovery —‘);
— 检查损坏块
select count(*) into v_corrupt_count from v$database_block_corruption;
if v_corrupt_count > 0 then
dbms_output.put_line(‘Found ‘ || v_corrupt_count || ‘ corrupt blocks’);
— 显示损坏块信息
for rec in (
select file#, block#, blocks, corruption_type
from v$database_block_corruption
order by file#, block#
) loop
dbms_output.put_line(‘File ‘ || rec.file# ||
‘, Block ‘ || rec.block# ||
‘, Type: ‘ || rec.corruption_type);
end loop;
dbms_output.put_line(‘Please run RMAN blockrecover to fix corrupt blocks’);
else
dbms_output.put_line(‘No corrupt blocks found’);
end if;
end auto_block_recovery;
/
Procedure created.
# 2. 执行自动化存储过程
SQL> set serveroutput on
SQL> exec auto_block_recovery;
— Auto Block Recovery —
Found 1 corrupt blocks
File 4, Block 1024, Type: ALL ZERO
Please run RMAN blockrecover to fix corrupt blocks
PL/SQL procedure successfully completed.
# 3. 创建块级恢复监控表
SQL> create table block_corruption_history (
id number primary key,
file# number,
block# number,
corruption_type varchar2(20),
detection_time date,
recovery_time date,
status varchar2(20)
);
Table created.
SQL> create sequence block_corruption_seq;
Sequence created.
# 4. 创建块级恢复记录存储过程
SQL> create or replace procedure record_corruption(
p_file# in number,
p_block# in number,
p_corruption_type in varchar2
) as
begin
insert into block_corruption_history (
id, file#, block#, corruption_type, detection_time, status
) values (
block_corruption_seq.nextval, p_file#, p_block#, p_corruption_type, sysdate, ‘DETECTED’
);
commit;
end record_corruption;
/
Procedure created.
# 5. 创建块级恢复更新存储过程
SQL> create or replace procedure update_recovery_status(
p_file# in number,
p_block# in number
) as
begin
update block_corruption_history
set recovery_time = sysdate, status = ‘RECOVERED’
where file# = p_file# and block# = p_block# and status = ‘DETECTED’;
commit;
end update_recovery_status;
/
Procedure created.
# 6. 查看块级恢复历史
SQL> column detection_time format a20
SQL> column recovery_time format a20
SQL> select * from block_corruption_history order by detection_time desc;
ID FILE# BLOCK# CORRUPTION_TYPE DETECTION_TIME RECOVERY_TIME STATUS
———- ———- ———- ——————– ——————– ——————– ——————–
1 4 1024 ALL ZERO 2026-03-31 10:00:00 2026-03-31 10:10:00 RECOVERED
Part05-风哥经验总结与分享
5.1 块级恢复总结
Oracle块级恢复是处理数据块损坏的首选方法,具有以下特点:
- 快速恢复:只恢复损坏的块,恢复速度快
- 在线恢复:数据库可以保持运行状态
- 精确恢复:只恢复损坏的块,不影响其他数据
- 减少停机:减少数据库停机时间
- 灵活恢复:可以选择恢复特定的块
5.2 块级恢复检查清单
Oracle块级恢复检查清单:
- 备份可用:确保有可用的RMAN备份
- 归档日志:确保归档日志可用
- 损坏检测:定期检测数据块损坏
- 快速响应:快速响应损坏告警
- 恢复验证:验证恢复结果
- 记录过程:记录恢复过程
5.3 块级恢复工具推荐
Oracle块级恢复工具推荐:
- RMAN:执行块级恢复
- DBVERIFY:验证数据文件完整性
- ANALYZE:验证对象结构
- DBMS_REPAIR:修复损坏块
- SQL*Plus:执行SQL命令
- Shell脚本:自动化恢复过程
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
