对比两者 1.使用create controlfile命令时,datafile中未列出只读表空间的数据文件 2.成功创建控制文件并打开后,使用alter database rename file命令重命名只读表空间的数据文件 3.使用alter tablespace readonly_tablespacename online 将只读表空间联机 四、演示只读表空间变化的恢复过程 1. 演示整个过程为只读表空间的情况(对应前面描述的case 1)SQL> alter database backup controlfile to trace as '/tmp/rectl1.sql'; SQL> alter tablespace tbs1 read only; SQL> alter database backup controlfile to trace as '/tmp/rectl2.sql'; SQL> ho diff /tmp/rectl1.sql /tmp/rectl2.sql 69,70c69 < '/u01/app/oracle/oradata/orcl/example01.dbf', < '/u01/app/oracle/oradata/orcl/tbs01.dbf' --- > '/u01/app/oracle/oradata/orcl/example01.dbf' 97a97,102 > -- Files in read-only tablespaces are now named. > ALTER DATABASE RENAME FILE 'MISSING00006' > TO '/u01/app/oracle/oradata/orcl/tbs01.dbf'; > > -- Online the files in read-only tablespaces. > ALTER TABLESPACE "TBS1" ONLINE;
从上面的演示可以看出对只读表空间内的数据作任何DML操作均不可用 在 一文中,可以对只读表空间作delete操作(版本是10.2.0.1.0),应该是数 据库补丁的问题, 此版本为10.2.0.4.0。SQL> create table scott.tb1 tablespace tbs1 2 as select * from scott.emp; SQL> commit; SQL> alter tablespace tbs1 read only; SQL> select file#,name,enabled from v$datafile where file#=6; FILE# NAME ENABLED ---------- --------------------------------------------- ---------- 6 /u01/app/oracle/oradata/orcl/tbs01.dbf READ ONLY SQL> ho cp /u01/app/oracle/oradata/orcl/tbs01.dbf /tmp/tbs01.dbf SQL> insert into scott.tb1(empno,ename) values(3333,'Thomas'); SQL> update scott.tb1 set sal=sal*1.2 where ename='SCOTT'; SQL> delete from scott.tb1 where ename='SCOTT'; --执行上述三条命令,收到下列同样的错误提示 ORA-00372: file 6 cannot be modified at this time ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/tbs01.dbf'
2. 演示由只读变为读写后发生损坏且只有只读备份的恢复情况(对应前面描述的case 2)--使用vim 打开?/oradata/orcl/tbs01.dbf文件做任意操作来模拟破坏该只读表空间的数据文件 --重启数据库后收到下面的错误提示 ORA-01157: cannot identify/lock data file 6 - see DBWR trace file ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/tbs01.dbf' SQL> ho cp /tmp/tbs01.dbf /u01/app/oracle/oradata/orcl/tbs01.dbf SQL> alter database open; SQL> select count(1) from scott.tb1; COUNT(1) ---------- 16
3. 演示由读写表空间变为只读表空间,且仅有读写表空间备份的恢复(对应前面描述的case 3)SQL> alter tablespace tbs1 read write; SQL> insert into scott.tb1(empno,ename) values(3333,'Thomas'); SQL> commit; --使用vim 打开/u01/app/oracle/oradata/orcl/tbs01.dbf文件做任意操作来模拟破坏该读写表空间的数据文件 --重启数据库后未收到错误提示 SQL> insert into scott.tb1(empno,ename) values(4444,'Jackson'); insert into scott.tb1(empno,ename) values(4444,'Jackson') * ERROR at line 1: ORA-00376: file 6 cannot be read at this time ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/tbs01.dbf' SQL> select * from v$recover_file; FILE# ONLINE ONLINE_ ERROR CHANGE# TIME ---------- ------- ------- ---------------- ---------- --------- 6 OFFLINE OFFLINE FILE NOT FOUND 0 SQL> ho cp /tmp/tbs01.dbf /u01/app/oracle/oradata/orcl/tbs01.dbf SQL> recover datafile 6; Media recovery complete. SQL> alter tablespace tbs1 online; Tablespace altered. SQL> select * from scott.tb1 where ename='Thomas'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 3333 Thomas
4. 演示数据文件状态发生多次变化且在变化时没有任何备份的恢复处理(实际上使用日志来重新构造该数据文件)SQL> select file#,name,enabled from v$datafile where file#=6; FILE# NAME ENABLED ---------- --------------------------------------------- ---------- 6 /u01/app/oracle/oradata/orcl/tbs01.dbf READ WRITE SQL> alter tablespace tbs1 begin backup; SQL> ho cp /u01/app/oracle/oradata/orcl/tbs01.dbf /tmp/tbs01.dbf SQL> alter tablespace tbs1 end backup; SQL> delete from scott.tb1 where empno=3333; SQL> commit; SQL> alter tablespace tbs1 read only; --使用vim 打开?/oradata/orcl/tbs01.dbf文件做任意操作来模拟破坏该只读表空间的数据文件 --重启数据库后收到下面的错误提示 ORA-01157: cannot identify/lock data file 6 - see DBWR trace file ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/tbs01.dbf' SQL> ho cp /tmp/tbs01.dbf /u01/app/oracle/oradata/orcl/tbs01.dbf SQL> recover datafile 6; SQL> alter database open; SQL> select * from scott.tb1 where ename='Thomas'; no rows selected
5. 演示删除只读表空间上的对象SQL> select file#,name,enabled from v$datafile where file#=6; FILE# NAME ENABLED ---------- --------------------------------------------- ---------- 6 /u01/app/oracle/oradata/orcl/tbs01.dbf READ WRITE SQL> drop table scott.tb1; SQL> commit; SQL> alter tablespace tbs1 read only; SQL> alter tablespace tbs1 read write; SQL> create table scott.tb2 tablespace tbs1 as select * from scott.emp; SQL> commit; SQL> alter system checkpoint; [oracle@oradb orcl]$tail -n 50 $ORACLE_BASE/admin/orcl/bdump/alert_orcl.log Errors in file /u01/app/oracle/admin/orcl/bdump/orcl_ckpt_4064.trc: ORA-01171: datafile 6 going offline due to error advancing checkpoint ORA-01122: database file 6 failed verification check ORA-01110: data file 6: '/u01/app/oracle/oradata/orcl/tbs01.dbf' ORA-01251: Unknown File Header Version read for file number 6 SQL> select * from v$recover_file; FILE# ONLINE ONLINE_ ERROR CHANGE# TIME ---------- ------- ------- --------------- ---------- --------- 6 OFFLINE OFFLINE FILE NOT FOUND 0 SQL> select file#,name,status from v$datafile where file#=6; FILE# NAME STATUS ---------- --------------------------------------------- ------- 6 /u01/app/oracle/oradata/orcl/tbs01.dbf RECOVER SQL> alter database create datafile 6; SQL> recover datafile 6; Media recovery complete. SQL> alter database datafile 6 online; SQL> select count(1) from scott.tb2; COUNT(1) ---------- 16
五、总结 1. 表空间置为只读后将减少数据的备份量 2. 表空间置为只读后,不能对其中的对象执行任何DML操作 3. 只读表空间内的对象可以被清除,因为drop命令更新了数据字典,而不更新对象本身 4. 当表空间的状态发生变化时,应立即备份该表空间,以减少恢复工作 5. 对于状态多次发生改变且未及时备份的情况,日志未损坏时,可以使用联机重做、归档日志来进行恢复 使用下列命令来实现: 删除受损的数据文件(rm dbfile.dbf) 重建受损的数据文件(alter database create datafile n) 进行介质恢复(recover datafile n) 使受损的数据文件联机(alter database datafile n online) 6. 演示中多为在mount状态下来恢复,生产环境中多在open状态下恢复,可以按下列步骤实现 先将受损的只读表空间(数据文件)脱机(offline) 使用备份的表空间(数据文件)来还原(restore) 使用归档、联机日志进行介质恢复(recover) 使恢复成功的表空间(数据文件)联机(online) 7. 对于原始介质受损,不能恢复到原始位置的情况下,使用下面的命令实现转移 alter database rename file '<dir1>' to '<dir2>';SQL> select file#,name,enabled from v$datafile where file#=6; FILE# NAME ENABLED ---------- --------------------------------------------- ---------- 6 /u01/app/oracle/oradata/orcl/tbs01.dbf READ ONLY SQL> select segment_name,segment_type,tablespace_name,owner from dba_segments where 2 tablespace_name='TBS1' and segment_name='TB2'; SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME OWNER -------------------- ------------------ ------------------------------ ---------- TB2 TABLE TBS1 SCOTT SQL> drop table scott.tb2; Table dropped.
六、 快捷参考
有关性能优化请参考
有关ORACLE体系结构请参考
有关闪回特性请参考
有关基于用户管理的备份和备份恢复的概念请参考
(详细描述了介质恢复及其处理)
有关RMAN的备份恢复与管理请参考
有关ORACLE故障请参考
有关ASM请参考
有关SQL/PLSQL请参考
有关ORACLE其它特性
原文链接: