使用RMAN备份与恢复数据库(10)——修复损坏的数据块
如果某个数据文件正常,但数据文件中的某些数据块损坏,可以使用RMAN修复。
一、模拟数据块损坏
1、新建表空间data03
新建一个表空间data03,文件大小5M。
SQL> create tablespace data03 datafile '/usr/local/oradata/orcl/data03.dbf' size 2m;
Tablespace created.
Elapsed: 00:00:00.40
SQL> select name from v$datafile;
NAME
-----------------------------------------------------------------------------------
/usr/local/oradata/orcl/system01.dbf
/usr/local/oradata/orcl/sysaux01.dbf
/usr/local/oradata/orcl/undotbs01.dbf
/usr/local/oradata/orcl/users01.dbf
/usr/local/oradata/orcl/data01.dbf
/usr/local/oradata/orcl/data02.dbf
/usr/local/oradata/orcl/data03.dbf
2、在表空间data03中创建表
在表空间中创建一个scott.t_emp表,并添加数据,充满整个表空间。
SQL> create table scott.t_emp tablespace data03 as select * from scott.e02 where rownum<20000;
Table created.
3、使用RMAN备份表空间
RMAN> recover tablespace data03;
Starting recover at 2020-04-12 14:45:55
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 2020-04-12 14:45:56
RMAN> backup tablespace data03 format '/home/oracle/rmanbak/data03.bak';
Starting backup at 2020-04-12 14:47:50
using channel ORA_DISK_1
using channel ORA_DISK_2
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/usr/local/oradata/orcl/data03.dbf
channel ORA_DISK_1: starting piece 1 at 2020-04-12 14:47:51
channel ORA_DISK_1: finished piece 1 at 2020-04-12 14:47:52
piece handle=/home/oracle/rmanbak/data03.bak tag=TAG20200412T144751 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2020-04-12 14:47:52
4、设置表空间data03为offline
SQL> alter tablespace data03 offline;
Tablespace altered.
5、用vim编辑data03.dbf文件,然后保存。
[oracle@wgx orcl]$ vim -b data03.dbf
6、设置表空间data03为online
SQL> alter tablespace data03 online;
Tablespace altered.
二、查看t_emp表的数据
提示出现坏的数据块,结果如下:
SQL> select count(*) from scott.t_emp;
select count(*) from scott.t_emp
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 7, block # 158)
ORA-01110: data file 7: '/usr/local/oradata/orcl/data03.dbf'
三、使用RMAN修复损坏的数据块
1、查看损坏的数据块的信息
可以看到,有两个数据块发生损坏。
SQL> select file#,block# from v$database_block_corruption;
FILE# BLOCK#
---------- ----------
7 163
7 158
Elapsed: 00:00:00.04
2、使用RMAN修复损坏的数据块
RMAN> blockrecover corruption list;
Starting recover at 2020-04-12 14:53:35
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=9 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=133 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 00007
channel ORA_DISK_1: reading from backup piece /home/oracle/rmanbak/data03.bak
channel ORA_DISK_1: piece handle=/home/oracle/rmanbak/data03.bak tag=TAG20200412T144751
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 2020-04-12 14:53:40
四、查看t_emp表的数据
SQL> select count(*) from scott.t_emp;
COUNT(*)
----------
19999
五、查看损坏的数据块的信息
SQL> select file#,block# from v$database_block_corruption;
no rows selected