合 Oracle只读数据文件备份和恢复
简介
只读数据文件是只读表空间的数据文件,其数据块包括文件头在内不允许更改(少数管理性命令除外)。
将表空间设置为只读状态的命令:
SQL> alter tablespace TBS_READ read only;
Tablespace altered.
将表空间重新设置为常规的读/写状态的命令:
SQL> alter tablespace TBS_READ read write;
Tablespace altered.
获得只读表空间及其数据文件的sql语句:
SQL> set line 9999
SQL> col file_name format a50
SQL> select t.TABLESPACE_NAME, d.FILE_ID, d.FILE_NAME
2 from dba_tablespaces t, dba_data_files d
3 WHERE t.TABLESPACE_NAME = d.TABLESPACE_NAME
4 and t.STATUS = 'READ ONLY' ;
TABLESPACE_NAME FILE_ID FILE_NAME
----------------------- ---------- --------------------------------------------------
TBS_READ 5 /u01/app/oracle/oradata/utf8test/tbs_read01.dbf
SQL>
只读表空间的特性
使用只读表空间避免对静态数据的频繁备份
当使用alter tablespace tbs read only时,数据文件会执行检查点进程(将所有脏缓冲区的内容写至磁盘),当前的SCN号会被标注,同时存储了SCN的数据文件头部被冻结.控制文件内也会记录该数据文件的冻结信息。
可以清除只读表空间的对象
只读文件损坏的后果
当一个表空间从读/写状态更改为只读状态时,其数据文件中的脏数据块必须由DBWn进程悉数写回磁盘,完成一次不完整的完全检查点,该表空间内数据文件即称为只读数据文件,其数据块及文件头信息包括检查点在内从此均不再更新,每次打开数据库实例也不会在乎只读文件头的检查点SCN是否和其他数据文件的活在线日志同步,但各种错误(ORA-01116,ORA-01110,ORA-01578,ORA-01157等等错误)仍然会发生。以下图片为从电子书上截取过来的:
不像其他类型的数据文件,在只读文件头损坏后,在发生检查点时,所有进程视其为无物,实例不会崩溃(关键数据文件头损坏的后果),文件也不会自动下线(普通数据文件头损坏的后果),总体上只读文件安然无恙,只是当执行需要访问头部的操作时才在告警日志和追踪文件中留下痕迹而已,比如:
SQL> select checkpoint_change# from v$datafile where file#=5;
CHECKPOINT_CHANGE#
------------------
1865187
但告警日志报错:
ORA-19563: datafile header validation failed for file /u01/app/oracle/oradata/utf8test/tbs_read01.dbf
ORA-01251: Unknown File Header Version read for file number 5
ORA-01578: ORACLE data block corrupted (file # 5, block # 130)
ORA-01110: data file 5: '/u01/app/oracle/oradata/utf8test/tbs_read01.dbf'
此刻其内部的所有队形还是可以查询的(只要对应的数据块没有损坏),但是 alter tablspace ... read write 和 alter tablespace ... offline 之后的online回报ora-01210数据文件头损坏错误。
只读表空间的备份
由于只读数据文件内没有一个数据块能够被修改,所以,一般情况下,只读表空间只需要进行一次备份,尤其是当只读数据文件占用很大空间的时候,这样做可以节省备份数据库的时间。即当表空间状态发生改变时应立即进行备份。可以使用OS系统cp命令来备份或RMAN进行备份只读表空间。备份其他类型数据文件的方式均适用于只读数据文件,比如:
- backup as backupset tablespace TBS_READ;
- backup as copy tablespace TBS_READ;
- backup as backupset datafile 5;
使用RMAN时建议启用备份优化选项,具体保留几份备份由备份保留策略决定:使用冗余度时保留数量为冗余度加1,使用恢复窗口时保留数量为1.
RMAN> CONFIGURE BACKUP OPTIMIZATION ON;
以下例子演示备份优化功能,初始状态下是没有任何备份的:
首先启用备份优化:
[oracle@rhel6_lhr dbs]$ rman target /
Recovery Manager: Release 11.2.0.3.0 - Production on Tue Feb 3 10:02:05 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: lilove (DBID=888888)
RMAN> CONFIGURE BACKUP OPTIMIZATION ON;
using target database control file instead of recovery catalog
new RMAN configuration parameters:
CONFIGURE BACKUP OPTIMIZATION ON;
new RMAN configuration parameters are successfully stored
设置备份保留策略,使用恢复窗口3天:
RMAN> CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 3 DAYS;
new RMAN configuration parameters:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 3 DAYS;
new RMAN configuration parameters are successfully stored
RMAN> show retention policy;
RMAN configuration parameters for database with db_unique_name LILOVE are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 3 DAYS;
使用backup database 命令备份数据库内所有的数据文件,注意有tbs_read01.dbf文件:
RMAN> backup database;
Starting backup at 2015-02-03 10:03:31
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=264 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=00001 name=/u01/app/oracle/oradata/utf8test/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/utf8test/sysaux01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/utf8test/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/utf8test/users01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/utf8test/tbs_read01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-02-03 10:03:32
channel ORA_DISK_1: finished piece 1 at 2015-02-03 10:05:18
piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/1opuba7k_1_1 tag=TAG20150203T100332 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:46
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-02-03 10:05:20
channel ORA_DISK_1: finished piece 1 at 2015-02-03 10:05:21
piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/1ppubaau_1_1 tag=TAG20150203T100332 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-02-03 10:05:21
RMAN>
当第二次执行backup database命令时,输出中是找不到tbs_read01.dbf文件的,rman认为没有必要反复备份只读文件:
RMAN> backup database;
Starting backup at 2015-02-03 10:08:48
using channel ORA_DISK_1
skipping datafile 5; already backed up 1 time(s)
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/utf8test/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/utf8test/sysaux01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/utf8test/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/utf8test/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-02-03 10:08:48
channel ORA_DISK_1: finished piece 1 at 2015-02-03 10:10:13
piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/1qpubahg_1_1 tag=TAG20150203T100848 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-02-03 10:10:15
channel ORA_DISK_1: finished piece 1 at 2015-02-03 10:10:16
piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/1rpubak6_1_1 tag=TAG20150203T100848 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-02-03 10:10:16
若保留策略是冗余度3,则需要等到第5次执行backup database 时才会不带tbs_read01.dbf 文件。另外,如果备份时使用backup tablespace 或 backup datafile命令显式备份只读数据文件,那么RMAN将忽略优化策略。
备份只读数据文件除了可以使用以上备份其他数据文件的方法之外,还可以无需任何准备工作直接使用操作系统的cp复制命令备份,比如:
[oracle@rhel6_lhr dbs]$ cp /u01/app/oracle/oradata/utf8test/tbs_read01.dbf /tmp/tbs_read01.bak
另外:只读表空间不支持热备
SQL> alter tablespace tbs_read begin backup;
alter tablespace tbs_read begin backup
*
ERROR at line 1:
ORA-01642: begin backup not needed for read-only tablespace 'TBS_READ'
SQL>
需要特别注意的是,当一个表空间从只读(READ ONLY)状态改变为读/写(READ WRITE)状态后,应该立即备份其数据文件及当时(成为READ WRITE状态后)的控制文件,否则将来若控制文件连同原来的只读数据文件同时损坏,在恢复流程中可能出现“ORA-01152:数据文件不够旧”的错误。
只读表空间的还原与恢复
其他类型的数据文件恢复过程均包括2个必要步骤:还原(restore)和恢复(recover),对于不可能有更改操作的只读文件来说,重做日志是没有意义的,当然也就没有恢复的必要的,因此,所谓的恢复实际上只有一个步骤:还原。
在mount状态下低可用性恢复策略的步骤如下:
- startup mount
- rman的restore 或switch命令还原数据文件
- alter database open
在open状态下高可用性恢复策略的步骤如下:
- alter database datafile xx offline
- rman的restore force 或switch命令还原数据文件。
- alter database datafile xx online
还原前的准备
只读数据文件的恢复可以采用低可用性恢复策略和高可用性策略,前者是在数据库mount状态下进行,或者是在数据库open状态下进行。低可用性恢复策略要求参数文件和控制文件必须就位,高可用性策略额外要求数据文件必须就位,若不满足条件必须先进行相应的恢复。
控制文件无损情况下的恢复
控制文件无损情况下的恢复指:只读数据文件损坏时控制文件没有损坏
场景1:只读数据文件tbs_read01.dbf 丢失,数据库无法正常启动,停留在mount状态,错误号“ORA-01157:cannot identify/locak data file 5 -see DBWR trace file.”
遇到以上情况只要使用rman执行restore(合适备份集)或switch(合适镜像复制)命令还原数据文件,然后打开数据库即可:
RMAN> restore datafile 5;
Starting restore at 2015-02-03 11:19:45
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00005 to /u01/app/oracle/oradata/utf8test/tbs_read01.dbf
channel ORA_DISK_1: reading from backup piece /u01/app/oracle/product/11.2.0/dbhome_1/dbs/1tpubeef_1_1
channel ORA_DISK_1: piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/1tpubeef_1_1 tag=TAG20150203T111527
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 2015-02-03 11:19:46
RMAN> alter database open;
Database altered.
场景2:只读数据文件tbs_read01.dbf 在实例运行时丢失,导致数据无法访问,错误如下:
SQL> select * from aabbcc;
select * from aabbcc
*
ERROR at line 1:
ORA-01116: error in opening database file 5
ORA-01110: data file 5: '/u01/app/oracle/oradata/utf8test/tbs_read01.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
该情况可以将数据文件下线后通过restore 或switch命令还原,然后上线:
run{
sql 'alter database datafile 5 offline';
restore datafile 5;
sql 'alter database datafile 5 online';
}
场景3:运行时只读数据文件tbs_read01.dbf内部数据块损坏,导致数据无法访问,但是文件依然存在,错误:
SQL> select * from tst;
select * from tst
*
ERROR at line 1:
ORA-01578: ORACLE data block corrupted (file # 5, block # 131)
ORA-01110: data file 5: '/u01/app/oracle/oradata/utf8test/tbs_read01.dbf'
以上情况还原时需要加force关键字,否则不能正确还原数据文件(若在mount状态下执行restore命令则无需force关键字):
run{
sql 'alter database datafile 5 offline';
restore datafile 5 force;
sql 'alter database datafile 5 online';
}
使用镜像复制的switch命令不必使用force关键字:
run{
sql 'alter database datafile 5 offline';
switch datafile 5 to datafilecopy '/tmp/ol_mf_exam.dbf';
sql 'alter database datafile 5 online';
}
场景4:起先数据文件tbs_read01.dbf备份时是只读的,后来修改表空间为读写,但没有备份,现在该文件损坏了:
run{
sql 'alter database datafile 5 offline';
restore datafile 5;
recover datafile 5;
sql 'alter database datafile 5 online';
}
场景5:起先数据文件tbs_read01.dbf备份时是读写状态,后来其所在的表空间为只读状态,但没有备份,现在数据文件损坏了: