This paper refers to: Le Shami's World
For physically damaged data blocks, we can complete the recovery of damaged blocks through the RMAN Block Media Recovery (BLOCK MEDIA RECOVERY) function without requiring the whole recovery. data base Or all files to repair these small amounts of damaged data blocks. Restoring the entire database or data file is not a cannon for mosquitoes, it's not worth it! But the prerequisite is that you have an available RMAN backup, so backup is everything at any time. This paper demonstrates the whole process of recovering bad blocks by using RMAN when bad blocks are generated.
1. Create a demo environment
- SQL> select * from v$version where rownum<2;
- BANNER
- --------------------------------------------------------------------------------
- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
- --Create a demo data file
- SQL> create tablespace tbs_tmp datafile '/u02/database/usbo/oradata/tbs_tmp.dbf' size 10m autoextend on;
- SQL> conn scott/tiger;
- --Creating Objects Based on New Data Files tb_tmp
- SQL> create table tb_tmp tablespace tbs_tmp as select * from dba_objects;
- SQL> col file_name format a60
- SQL> select file_id,file_name from dba_data_files where tablespace_name='TBS_TMP';
- FILE_ID FILE_NAME
- ---------- ------------------------------------------------------------
- 6 /u02/database/usbo/oradata/tbs_tmp.dbf
- --Table object tb_tmp The information above, including the corresponding file information, header block, total number of blocks
- SQL> select segment_name , header_file , header_block,blocks
- 2 from dba_segments
- 3 where segment_name = 'TB_TMP' and owner='SCOTT';
- SEGMENT_NAME HEADER_FILE HEADER_BLOCK BLOCKS
- ------------------------------ ----------- ------------ ----------
- TB_TMP 6 130 1152
- --use first rman Back up the corresponding data files
- $ $ORACLE_HOME/bin/rman target /
- RMAN> backup datafile 6 tag=health;
- Starting backup at 2013/08/28 17:03:15
- allocated channel: ORA_DISK_1
- channel ORA_DISK_1: SID=24 device type=DISK
- channel ORA_DISK_1: starting full datafile backup set
- channel ORA_DISK_1: specifying datafile(s) in backup set
- input datafile file number=00006 name=/u02/database/usbo/oradata/tbs_tmp.dbf
- channel ORA_DISK_1: starting piece 1 at 2013/08/28 17:03:16
- channel ORA_DISK_1: finished piece 1 at 2013/08/28 17:03:17
- piece handle=/u02/database/usbo/fr_area/USBO/backupset/2013_08_28/o1_mf_nnndf_HEALTH_91vh6ntb_.bkp tag=HEALTH comment=NONE
- channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
- Finished backup at 2013/08/28 17:03:17
- RMAN> exit
2. Recovery Processing of Damaged Single Data Block
- --The following uses linux Self contained dd Command to damage a single block of data
- [oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=130 <<EOF
- > Corrupted block!
- > EOF
- 0+1 records in
- 0+1 records out
- 17 bytes (17 B) copied, 0.000184519 seconds, 92.1 kB/s
- --empty buffer cache
- SQL> alter system flush buffer_cache;
- --Query Table Relativity tb_tmp,Received ORA-01578
- SQL> select count(*) from tb_tmp;
- select count(*) from tb_tmp
- *
- ERROR at line 1:
- ORA-01578: ORACLE data block corrupted (file # 6, block # 130)
- ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
- --Query view v$database_block_corruption,Note that the view may not return any data. If no data is returned, execute first. backup validate
- SQL> select * from v$database_block_corruption;
- FILE# BLOCK# BLOCKS CORRUPTION_CHANGE# CORRUPTIO
- ---------- ---------- ---------- ------------------ ---------
- 6 129 1 0 CORRUPT
- --It can also be used dbv Tools to check bad blocks, reference: http://blog.csdn.net/robinson_0612/article/details/6530890
- --Use below blockrecover To recover the bad block
- RMAN> blockrecover datafile 6 block 130;
- Starting recover at 2013/08/28 17:22:25
- using target database control file instead of recovery catalog
- allocated channel: ORA_DISK_1
- channel ORA_DISK_1: SID=24 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 00006
- channel ORA_DISK_1: reading from backup piece /u02/database/usbo/fr_area/USBO/backupset/2013_08_28/o1_mf_nnndf_HEALTH_91vh6ntb_.bkp
- channel ORA_DISK_1: piece handle=/u02/database/usbo/fr_area/USBO/backupset/2013_08_28/o1_mf_nnndf_HEALTH_91vh6ntb_.bkp tag=HEALTH
- channel ORA_DISK_1: restored block(s) from backup piece 1
- channel ORA_DISK_1: block restore complete, elapsed time: 00:00:01
- starting media recovery
- media recovery complete, elapsed time: 00:00:03
- Finished recover at 2013/08/28 17:22:31
- --Query the table again tb_emp normal
- SQL> select count(*) from tb_tmp;
- COUNT(*)
- ----------
- 72449
3. Recovery Processing of Damaged Multi-Block Data
- --Use below linux dd Command Damage to Discontinuous Blocks
- [oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=133 <<EOF
- > New corrupted block!
- > EOF
- 0+1 records in
- 0+1 records out
- 21 bytes (21 B) copied, 0.000182835 seconds, 115 kB/s
- [oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=143 <<EOF
- > New corrupted block!
- > EOF
- 0+1 records in
- 0+1 records out
- 21 bytes (21 B) copied, 0.000115527 seconds, 182 kB/s
- [oracle@linux1 ~]$ dd of=/u02/database/usbo/oradata/tbs_tmp.dbf bs=8192 conv=notrunc seek=153 <<EOF
- > New corrupted block!
- > EOF
- 0+1 records in
- 0+1 records out
- 21 bytes (21 B) copied, 0.000335781 seconds, 62.5 kB/s
- SQL> alter system flush buffer_cache;
- --The following prompt block 133 is damaged. Note that we have damaged many data blocks, but when querying, we start with the smallest number of blocks. If there are bad blocks after 133 is repaired, we continue to prompt the bad blocks after 133.
- SQL> select count(*) from scott.tb_tmp;
- select count(*) from scott.tb_tmp
- *
- ERROR at line 1:
- ORA-01578: ORACLE data block corrupted (file # 6, block # 133)
- ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
- --Query view v$database_block_corruption No record
- SQL> select * from v$database_block_corruption;
- no rows selected
- --Use below backup validate To verify data files
- RMAN> backup validate datafile 6;
- Starting backup at 2013/08/29 09:42:04
- using target database control file instead of recovery catalog
- allocated channel: ORA_DISK_1
- channel ORA_DISK_1: SID=22 device type=DISK
- channel ORA_DISK_1: starting full datafile backup set
- channel ORA_DISK_1: specifying datafile(s) in backup set
- input datafile file number=00006 name=/u02/database/usbo/oradata/tbs_tmp.dbf
- channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
- List of Datafiles
- =================
- File Status Marked Corrupt Empty Blocks Blocks Examined High SCN
- ---- ------ -------------- ------------ --------------- ----------
- 6 FAILED 0 223 1408 838489 --field Status by FAILED
- File Name: /u02/database/usbo/oradata/tbs_tmp.dbf
- Block Type Blocks Failing Blocks Processed
- ---------- -------------- ----------------
- Data 0 1029
- Index 0 0
- Other 3 156 --There are 3. Blocks Failing
- validate found one or more corrupt blocks
- See trace file /u02/database/usbo/diag/rdbms/usbo/usbo/trace/usbo_ora_27874.trc for details
- Finished backup at 2013/08/29 09:42:06
- --Query again v$database_block_corruption,It shows three damaged blocks.
- SQL> select * from v$database_block_corruption;
- FILE# BLOCK# BLOCKS CORRUPTION_CHANGE# CORRUPTIO
- ---------- ---------- ---------- ------------------ ---------
- 6 153 1 0 CORRUPT
- 6 143 1 0 CORRUPT
- 6 133 1 0 CORRUPT
- --The following is used directly blockrecover corruption list To restore, all the bad blocks that have just been checked will be restored
- RMAN> blockrecover corruption list;
- Starting recover at 2013/08/29 10:05:24
- using channel ORA_DISK_1
- channel ORA_DISK_1: restoring block(s)
- channel ORA_DISK_1: specifying block(s) to restore from backup set
- restoring blocks of datafile 00006
- channel ORA_DISK_1: reading from backup piece /u02/database/usbo/fr_area/USBO/backupset/2013_08_28/o1_mf_nnndf_HEALTH_91vh6ntb_.bkp
- channel ORA_DISK_1: piece handle=/u02/database/usbo/fr_area/USBO/backupset/2013_08_28/o1_mf_nnndf_HEALTH_91vh6ntb_.bkp tag=HEALTH
- channel ORA_DISK_1: restored block(s) from backup piece 1
- channel ORA_DISK_1: block restore complete, elapsed time: 00:00:01
- starting media recovery
- media recovery complete, elapsed time: 00:00:03
- Finished recover at 2013/08/29 10:05:28
- --Verification result
- SQL> select count(*) from scott.tb_tmp;
- COUNT(*)
- ----------
- 72449
4. Object Location and Impact of Bad Blocks
- --Next, we query the object on block number 163.
- SQL> select dbms_rowid.rowid_object(rowid) object_id,dbms_rowid.rowid_relative_fno(rowid) file_id,
- 2 dbms_rowid.rowid_block_number(rowid) block_id,owner,object_name,object_id
- 3 from scott.tb_tmp where dbms_rowid.rowid_block_number(rowid)=163 and rownum<=2;
- OBJECT_ID FILE_ID BLOCK_ID OWNER OBJECT_NAME OBJECT_ID
- ---------- ---------- ---------- ------------ ------------------------------ ----------
- 74555 6 163 SYS GV_$QUEUEING_MTH 2439
- 74555 6 163 PUBLIC GV$QUEUEING_MTH 2440
- --Using the above method, we lost blocks 163, 173, which are not listed here.
- a,It is impossible to aggregate and aggregate bad block objects.
- SQL> select count(*) from scott.tb_tmp;
- select count(*) from scott.tb_tmp
- *
- ERROR at line 1:
- ORA-01578: ORACLE data block corrupted (file # 6, block # 163)
- ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
- b,Records on bad blocks cannot be queried
- --We use a query based on previous queries OBJECT_ID To query
- SQL> select owner,object_name,object_id from scott.tb_tmp where object_id in(2439,2440);
- select owner,object_name,object_id from scott.tb_tmp where object_id in(2439,2440)
- *
- ERROR at line 1:
- ORA-01578: ORACLE data block corrupted (file # 6, block # 163)
- ORA-01110: data file 6: '/u02/database/usbo/oradata/tbs_tmp.dbf'
- --In the following queries, the data on the damaged block can not be queried, but it can still be queried for the undamaged block. Objects on block 161 for the following query
- SQL> select owner,object_name,object_id from scott.tb_tmp
- 2 where dbms_rowid.rowid_block_number(rowid)=161 and rownum<3;
- OWNER OBJECT_NAME OBJECT_ID
- ------------------------------ ------------------------------ ----------
- PUBLIC GV$RECOVERY_LOG 2285
- SYS GV_$ARCHIVE_GAP 2286
- --Author : Robinson Cheng
- --Blog : http://blog.csdn.net/robinson_0612
- c,Locate the object corresponding to the damaged block
- SQL> run get_obj_name_from_corrupt_block
- 1 SELECT tablespace_name,
- 2 segment_type,
- 3 owner,
- 4 segment_name,
- 5 partition_name
- 6 FROM dba_extents
- 7* WHERE file_id = &file_id AND &block_id BETWEEN block_id AND block_id + blocks - 1
- Enter value for file_id: 6
- Enter value for block_id: 133
- old 7: WHERE file_id = &file_id AND &block_id BETWEEN block_id AND block_id + blocks - 1
- new 7: WHERE file_id = 6 AND 133 BETWEEN block_id AND block_id + blocks - 1
- TABLESPACE_NAME SEGMENT_TYPE OWNER SEGMENT_NAME PARTITION_NAME
- ------------------------------ ------------------ -------------- ----------------- -----------------
- TBS_TMP TABLE SCOTT TB_TMP
- d,Damaged data files cannot be backed up by default, as follows
- RMAN> backup datafile 6 tag='corruption';
- Starting backup at 2013/08/29 10:37:32
- using channel ORA_DISK_1
- channel ORA_DISK_1: starting full datafile backup set
- channel ORA_DISK_1: specifying datafile(s) in backup set
- input datafile file number=00006 name=/u02/database/usbo/oradata/tbs_tmp.dbf
- channel ORA_DISK_1: starting piece 1 at 2013/08/29 10:37:32
- RMAN-00571: ===========================================================
- RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
- RMAN-00571: ===========================================================
- RMAN-03009: failure of backup command on ORA_DISK_1 channel at 08/29/2013 10:37:33
- ORA-19566: exceeded limit of 0 corrupt blocks for file /u02/database/usbo/oradata/tbs_tmp.dbf
- --You need to set the number of blocks allowed to be damaged before backing up
- RMAN> run{
- 2> set maxcorrupt for datafile 6 to 2;
- 3> backup datafile 6 tag='corruption';
- 4> }
- executing command: SET MAX CORRUPT
- Starting backup at 2013/08/29 10:41:24
- using channel ORA_DISK_1
- channel ORA_DISK_1: starting full datafile backup set
- channel ORA_DISK_1: specifying datafile(s) in backup set
- input datafile file number=00006 name=/u02/database/usbo/oradata/tbs_tmp.dbf
- channel ORA_DISK_1: starting piece 1 at 2013/08/29 10:41:25
- channel ORA_DISK_1: finished piece 1 at 2013/08/29 10:41:26
- piece handle=/u02/database/usbo/fr_area/USBO/backupset/2013_08_29/o1_mf_nnndf_CORRUPTION_91xf6o18_.bkp tag=CORRUPTION comment=NONE
- channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
- Finished backup at 2013/08/29 10:41:26
- --Looking at the backup information below, you should restore the backup after repairing the bad block to avoid the previously available backup being caused by the retention policy. aged out
- RMAN> list backup summary;
- List of Backups
- ===============
- Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
- ------- -- -- - ----------- ------------------- ------- ------- ---------- ---
- 1 B F A DISK 2013/08/28 17:03:17 1 1 NO HEALTH
- 3 B F A DISK 2013/08/29 10:41:25 1 1 NO CORRUPTION
5, postscript
a. For damaged data blocks, only the data on the bad blocks can not be queried or read, and the data on the other normal blocks can still be used.
b. When aggregating damaged table objects and other related operations, errors are received, because the data on the bad block can not be counted. If you aggregate index columns, the index is returned normally if it is not damaged.
c. Block media recovery can be realized based on the backup files available in RMAN. Its data files need no offline, and have the least overhead and impact.
d. For damage to multiple data blocks, first perform backup validate to verify the database or the corresponding data file to mark the damaged block, then fill in v$database_block_corruption and subsequent recovery.
e. In the case of using backup validate check, the block recovery corruption list can be used directly to recover all bad blocks at one time.
f. By default, data files with bad blocks can not be backed up successfully, which will also lead to the failure of automatic backup scripts.
g. By default, data files with bad blocks can not be backed up successfully, which will also lead to the failure of automatic backup scripts.