控制文件的备份和恢复在数据库的备份恢复过程中十分关键。
备份控制文件的原则是:在数据库物理结构发生变化后备份控制文件。
备份控制文件包括三种方法:
通过操作系统命令在数据库关闭时对控制文件进行COPY;
利用ALTER DATABASE BACKUP CONTROLFILE TO命令将控制文件备份到二进制文件;
利用ALTER DATABASE BACKUP CONTROLFILE TO TRACE命令将重建控制文件备份的脚本备份到后台trace文件中。
Oracle推荐使用第二种方式来备份控制文件。第一种方式备份的控制文件,一般用于全库一致性恢复。而第三种方式备份控制文件会丢失归档日志历史等许多信息。在重建控制文件时,只读表空间和脱机的数据文件的处理相对比较复杂。
第二种方式备份也是最简单的方法,只需要执行下面的步骤:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO 'F:ORACLEBACKUPTEST20060202CONTROL.BAK';
数据库已更改。
采用第三种方式备份控制文件:
SQL> SELECT TABLESPACE_NAME, STATUS FROM DBA_TABLESPACES;
TABLESPACE_NAME STATUS
------------------------------ ---------
SYSTEM ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
INDX READ ONLY
TOOLS ONLINE
USERS ONLINE
YANGTK ONLINE
已选择7行。
SQL> SELECT NAME, STATUS FROM V$DATAFILE;
NAME STATUS
-------------------------------------------------- -------
F:ORACLEORADATATESTSYSTEM01.DBF SYSTEM
F:ORACLEORADATATESTUNDOTBS01.DBF ONLINE
F:ORACLEORADATATESTINDX01.DBF ONLINE
F:ORACLEORADATATESTTOOLS01.DBF ONLINE
F:ORACLEORADATATESTUSERS01.DBF RECOVER
F:ORACLEORADATATESTYANGTK01.DBF ONLINE
已选择6行。
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE;
数据库已更改。
SQL> SELECT SPID FROM V$PROCESS
2 WHERE ADDR =
3 (
4 SELECT PADDR FROM V$SESSION
5 WHERE SID = (SELECT SID FROM V$MYSTAT WHERE ROWNUM = 1)
6 )
7 ;
SPID
------------
3308
打开f:oracleadmintestudumptest_ora_3308.trc文件:
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "TEST" NORESETLOGS ARCHIVELOG
-- SET STANDBY TO MAXIMIZE PERFORMANCE
MAXLOGFILES 50
MAXLOGMEMBERS 5
MAXDATAFILES 100
MAXINSTANCES 1
MAXLOGHISTORY 226
LOGFILE
GROUP 1 'F:ORACLEORADATATESTREDO01.LOG' SIZE 100M,
GROUP 2 'F:ORACLEORADATATESTREDO02.LOG' SIZE 100M,
GROUP 3 'F:ORACLEORADATATESTREDO03.LOG' SIZE 100M
-- STANDBY LOGFILE
DATAFILE
'F:ORACLEORADATATESTSYSTEM01.DBF',
'F:ORACLEORADATATESTUNDOTBS01.DBF',
'F:ORACLEORADATATESTTOOLS01.DBF',
'F:ORACLEORADATATESTUSERS01.DBF',
'F:ORACLEORADATATESTYANGTK01.DBF'
CHARACTER SET ZHS16GBK
;
# Take files offline to match current control file.
ALTER DATABASE DATAFILE 'F:ORACLEORADATATESTUSERS01.DBF' OFFLINE;
# Recovery is required if any of the datafiles are restored backups,
# or if the last shutdown was not normal or immediate.
RECOVER DATABASE
# All logs need archiving and a log switch is needed.
ALTER SYSTEM ARCHIVE LOG ALL;
# Database can now be opened normally.
ALTER DATABASE OPEN;
# Files in read-only tablespaces are now named.
ALTER DATABASE RENAME FILE 'MISSING00003'
TO 'F:ORACLEORADATATESTINDX01.DBF';
# Online the files in read-only tablespaces.
ALTER TABLESPACE "INDX" ONLINE;
# Commands to add tempfiles to temporary tablespaces.
# Online tempfiles have complete space information.
# Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE 'F:ORACLEORADATATESTTEMP01.DBF'
SIZE 41943040 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;