使用RMAN備份與恢復數據庫(10)——修復損壞的數據塊

使用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

修復成功!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章