RMAN恢复原理首先还是得理解Oracle数据库恢复的一个原理。数据库恢复是指将数据库恢复到一个一致性的状态,整个恢复操作可以分为两个步骤,数据库修复(RESTORE)和恢复(RECOVER)。Oracle官方文档上关于Restore和Recover的解释: The two most important RMAN commands used in database recovery are:RESTORE, which retrieves files from RMAN backups based on the contents of the RMAN repositoryRECOVER, which performs complete or point-in-time media recovery using available datafiles and redo logs.Restore是指将要恢复的文件从备份集中读取出来。Recover是指应用所有重做日志,将数据库恢复到崩溃前的状态,或者应用部分REDO,将数据库恢复到指定的时间点。上图是一个最典型的恢复过程,数据库在SCN=100时执行了数据库备份,在SCN=500时数据库出现介质错误,需要通过备份进行恢复。 操作步骤:1.首先使用SCN=100时创建的数据库备份将数据库恢复到备份时的状态,这个操作就是修复(RESTORE)。2.然后重新应用SCN 100~500之间生成的所有重做日志,这个操作就是恢复(RECOVER),最后数据库被恢复到崩溃前的状态。由上可知,Oracle数据库中的备份恢复必然是先有备份,然后才能做恢复的操作。不管是RMAN备份恢复,或者是用户管理的备份和恢复,恢复操作都是从备份时间点向前进行恢复,不可能直接将当前状态向后恢复到之前的某个时间点(Oracle 10g R2新推出的Flashback Database特性开始支持,直接从当前时间点向之前的时间点恢复)。RMAN如何选择备份集很多新接触RMAN的朋友都会有这样的疑问:我执行过很多次备份,早也备份是晚也备份,昨天也备份今天也备份,在恢复的时候,RMAN怎么知道从哪个备份集中选择文件用来恢复呢? 答案其实很简单,RMAN一开始也不知道,它只是遵循固定的规则,按照你的要求去寻找合适的备份集罢了,首先是寻找最近的可用的备份集(如果没有指定UNTIL子句则从最近的备份开始,如果指定了UNTIL,则从满足UNTIL的备份集开始),如果能找到,那么运气不错,恢复将会继续正常进行,如果找到了多个,还有更加细致的规则,从中选择一个RMAN觉着最优的备份集来做恢复(如优先选择镜像复制,因为RMAN认为这样恢复时会更快)。如果最终一个备份集都没找到,嘿嘿,那就只有一个选择了:报错。RMAN能做哪些恢复在之前的文章Oracle数据库备份与恢复 - RMAN备份 里,写到了RMAN可以做多种备份,包括:1.整库备份、2.表空间的备份、3.数据文件的备份、4.控制文件备份、5.归档文件的备份、6.初始化参数文件的备份、7.对备份集备份对于恢复,RMAN中提供了多种不同级别的恢复方式,你既可以恢复整个数据库,也可以只恢复某个或某几个表空间,或某个或某几个数据文件,还可以单独恢复控制文件、初始化参数文件,或者归档文件。说白了就一条,用RMAN备份的就都能被恢复。1.对数据库进行完全恢复如果当前数据库只剩下控制文件和SPFILE,其他数据文件因为某些原因导致全部丢失,不过幸运的是之前创建过整库的备份,并且执行备份操作之后,所有的归档文件和重做日志文件都还在,这种情况下你就可以将数据库恢复到崩溃前那一刻的状态,而这种恢复方式,就叫做完全介质恢复。做数据库完全恢复,需要数据库处于MOUNT状态(如果数据库处于OPEN状态会报ORA-19573错误)启动数据库到加载状态:RMAN> STARTUP MOUNT;connected to target database (not started)Oracle instance starteddatabase mountedTotal System Global Area 1071333376 bytesFixed Size 1375792 bytesVariable Size 637534672 bytesDatabase Buffers 427819008 bytesRedo Buffers 4603904 bytes执行恢复操作(恢复分两步,有先有后的):RMAN> restore database;Starting restore at 15-JUL-15allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=63 device type=DISKchannel ORA_DISK_1: restoring datafile 00001input datafile copy RECID=2 STAMP=884555266 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_SYSTEM_BSTBH904_.DBFdestination for restore of datafile 00001: E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00001output file name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF RECID=0 STAMP=0channel ORA_DISK_1: restoring datafile 00002input datafile copy RECID=3 STAMP=884555292 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_SYSAUX_BSTBJDFQ_.DBFdestination for restore of datafile 00002: E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00002output file name=E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF RECID=0 STAMP=0channel ORA_DISK_1: restoring datafile 00003input datafile copy RECID=4 STAMP=884555305 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_UNDOTBS1_BSTBK5V1_.DBFdestination for restore of datafile 00003: E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00003output file name=E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF RECID=0 STAMP=0channel ORA_DISK_1: restoring datafile 00004input datafile copy RECID=7 STAMP=884555314 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_USERS_BSTBKKVB_.DBFdestination for restore of datafile 00004: E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00004output file name=E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF RECID=0 STAMP=0channel ORA_DISK_1: restoring datafile 00005input datafile copy RECID=5 STAMP=884555312 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_EXAMPLE_BSTBKF80_.DBFdestination for restore of datafile 00005: E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00005output file name=E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF RECID=0 STAMP=0Finished restore at 15-JUL-15RMAN> RECOVER DATABASE;Starting recover at 15-JUL-15using channel ORA_DISK_1starting media recoveryarchived log for thread 1 with sequence 105 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARCarchived log for thread 1 with sequence 106 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARCarchived log for thread 1 with sequence 107 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARCarchived log for thread 1 with sequence 108 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_108_BT1M9PW5_.ARCarchived log for thread 1 with sequence 109 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_15\O1_MF_1_109_BTCH29LR_.ARCarchived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARC thread=1 sequence=105archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARC thread=1 sequence=106archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARC thread=1 sequence=107media recovery complete, elapsed time: 00:00:22Finished recover at 15-JUL-15RMAN>打开数据库: RMAN> ALTER DATABASE OPEN;database openedRMAN>2.表空间恢复在执行恢复之前,如果被操作的表空间未处于OFFLINE状态,必须首先通过ALTER TABLESPACE ... OFFLINE语句将其置为脱机。SQL> select * from v$tablespace; TS# NAME INCLUDED_IN_DATABASE_BACKUP BIGFILE FLASHBACK_ON ENCRYPT_IN_BACKUP---------- ------------------------------ --------------------------- ------- ------------ ----------------- 0 SYSTEM YES NO YES 1 SYSAUX YES NO YES 2 UNDOTBS1 YES NO YES 4 USERS YES NO YES 3 TEMP NO NO YES 6 EXAMPLE YES NO YES 6 rows selected以恢复Example表空间为例。操作步骤如下: RMAN> SQL 'ALTER TABLESPACE EXAMPLE OFFLINE IMMEDIATE'; RMAN> RESTORE TABLESPACE EXAMPLE; RMAN> RECOVER TABLESPACE EXAMPLE; RMAN> SQL 'ALTER TABLESPACE EXAMPLE ONLINE'; 上述执行的四个脚本,表示将表空间置于OFFLINE状态,执行RESTORE命令,执行RECOVER命令,最后将表空间置为ONLINE状态。RMAN> SQL 'ALTER TABLESPACE EXAMPLE OFFLINE IMMEDIATE';sql statement: ALTER TABLESPACE EXAMPLE OFFLINE IMMEDIATERMAN> RESTORE TABLESPACE EXAMPLE;Starting restore at 15-JUL-15using channel ORA_DISK_1channel ORA_DISK_1: restoring datafile 00005input datafile copy RECID=5 STAMP=884555312 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_EXAMPLE_BSTBKF80_.DBFdestination for restore of datafile 00005: E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00005output file name=E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF RECID=0 STAMP=0Finished restore at 15-JUL-15RMAN> RECOVER TABLESPACE EXAMPLE;Starting recover at 15-JUL-15using channel ORA_DISK_1starting media recoveryarchived log for thread 1 with sequence 105 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARCarchived log for thread 1 with sequence 106 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARCarchived log for thread 1 with sequence 107 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARCarchived log for thread 1 with sequence 108 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_108_BT1M9PW5_.ARCarchived log for thread 1 with sequence 109 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_15\O1_MF_1_109_BTCH29LR_.ARCarchived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARC thread=1 sequence=105archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARC thread=1 sequence=106archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARC thread=1 sequence=107media recovery complete, elapsed time: 00:00:03Finished recover at 15-JUL-15RMAN> SQL 'ALTER TABLESPACE EXAMPLE ONLINE';sql statement: ALTER TABLESPACE EXAMPLE ONLINERMAN>3.数据文件的恢复恢复表空间实际就是恢复其所对应的数据文件(一个表空间可能对应多个数据文件),因此恢复数据文件的操作步骤与上极其相似。 同样在恢复操作之前,如果需要被恢复的数据文件未处于OFFLINE状态,需要通过ALTER DATABASE DATAFILE ... OFFLINE语句(注意哟,执行的语句不一样了)将其置为脱机。SQL> select file#, name,status, enabled from v$datafile; FILE# NAME STATUS ENABLED---------- -------------------------------------------------------------------------------- ------- ---------- 1 E:\APP\TIANPAN\ORADATA\PTIAN\SYSTEM01.DBF SYSTEM READ WRITE 2 E:\APP\TIANPAN\ORADATA\PTIAN\SYSAUX01.DBF ONLINE READ WRITE 3 E:\APP\TIANPAN\ORADATA\PTIAN\UNDOTBS01.DBF ONLINE READ WRITE 4 E:\APP\TIANPAN\ORADATA\PTIAN\USERS01.DBF ONLINE READ WRITE 5 E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF ONLINE READ WRITESQL> 以恢复datafile 5为例。操作步骤如下:RMAN> SQL 'ALTER DATABASE DATAFILE 5 OFFLINE'; RMAN> RESTORE DATAFILE 5; RMAN> RECOVER DATAFILE 5; RMAN> SQL 'ALTER DATABASE DATAFILE 5 ONLINE';实例如下:RMAN> SQL 'ALTER DATABASE DATAFILE 5 OFFLINE';sql statement: ALTER DATABASE DATAFILE 5 OFFLINERMAN> RESTORE DATAFILE 5;Starting restore at 15-JUL-15using channel ORA_DISK_1channel ORA_DISK_1: restoring datafile 00005input datafile copy RECID=5 STAMP=884555312 file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\DATAFILE\O1_MF_EXAMPLE_BSTBKF80_.DBFdestination for restore of datafile 00005: E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBFchannel ORA_DISK_1: copied datafile copy of datafile 00005output file name=E:\APP\TIANPAN\ORADATA\PTIAN\EXAMPLE01.DBF RECID=0 STAMP=0Finished restore at 15-JUL-15RMAN> RECOVER DATAFILE 5;Starting recover at 15-JUL-15using channel ORA_DISK_1starting media recoveryarchived log for thread 1 with sequence 105 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARCarchived log for thread 1 with sequence 106 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARCarchived log for thread 1 with sequence 107 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARCarchived log for thread 1 with sequence 108 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_108_BT1M9PW5_.ARCarchived log for thread 1 with sequence 109 is already on disk as file E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_15\O1_MF_1_109_BTCH29LR_.ARCarchived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_09\O1_MF_1_105_BSWC39YH_.ARC thread=1 sequence=105archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_10\O1_MF_1_106_BSYCRLNG_.ARC thread=1 sequence=106archived log file name=E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\ARCHIVELOG\2015_07_11\O1_MF_1_107_BT10F9Y9_.ARC thread=1 sequence=107media recovery complete, elapsed time: 00:00:04Finished recover at 15-JUL-15RMAN> SQL 'ALTER DATABASE DATAFILE 5 ONLINE';sql statement: ALTER DATABASE DATAFILE 5 ONLINERMAN>4.归档日志文件的恢复恢复归档文件也是使用RESTORE命令,如果只是为了在恢复数据文件后应用归档文件,那并不需要手动对归档文件进行恢复,RMAN会在RECOVER的时候自动对适当的归档进行恢复。恢复归档文件也非常灵活,RMAN的RECOVER命令中提供了多种限定条件,可以精确指定恢复哪些备份的归档文件,例如,恢复归档序号为20到30之间的归档文件: RMAN> RESTORE ARCHIVELOG SEQUENCE BETWEEN 20 AND 30;示例:RMAN> RUN{ 2> SET ARCHIVELOG DESTINATION TO 'F:\ORACLE\BACKUP\ARCLOG1'; 3> RESTORE ARCHIVELOG SEQUENCE BETWEEN 15 AND 20; 4> SET ARCHIVELOG DESTINATION TO 'F:\ORACLE\BACKUP\ARCLOG2'; 5> RESTORE ARCHIVELOG SEQUENCE BETWEEN 21 AND 30; 6> SET ARCHIVELOG DESTINATION TO 'F:\ORACLE\BACKUP\ARCLOG3'; 7> RESTORE ARCHIVELOG SEQUENCE BETWEEN 31 AND 40; 8> }5.控制文件的恢复看到控制两字就该知道,这类事物一般都处于领导阶层,Oracle数据库的控制文件虽然个头不大(最大不超过20000个数据块),但是地位非凡,这倒正应了那句话:浓缩的都是精华。Oracle数据库实例启动后(即启动到NOMOUNT模式),要通过加载控制文件确定数据文件、重做日志文件的路径(进入到MOUNT模式),然后才能打开数据库(Finally,OPEN数据库)。因此,没有了控制文件,想顺利启动Oracle数据库是不可能的。当然,控制文件中并不是只有数据文件和重做日志文件的路径(如果仅有这些,这个导字早就被别人领了),还包括数据库名称、数据库创建信息、表空间信息、数据文件状态、日志文件信息、备份信息、检查点信息等。该文件不仅在数据库启动过程中需要,在Oracle数据库运行过程中,控制文件也需要发挥重要作用,如记录检查点的相关信息、归档文件路径、备份信息(假如采用RMAN执行备份的话),以及数据库发生结构修改(流行词汇形容叫领导班子调整,类似添加删除表空间、数据文件、日志文件)等操作都需要同步到控制文件。如果在Oracle数据库运行过程中,由于某些原因导致控制文件无法访问,则数据库也无法继续正常工作。控制文件是一个二进制文件,不能直接通过文本编辑工具修改,一般这个文件中的内容都是由Oracle自行维护。一个Oracle数据库至少要拥有一个控制文件,鉴于其重要地位(领导嘛),Oracle对其的保护有加,在默认情况下,控制文件就会有两、三份冗余(就是一模一样的文件存在两、三份),这几份文件的一致性由Oracle自动维护,当然啦,冗余的数量和冗余文件的位置可以由DBA指定,Oracle建议控制文件至少要有两份冗余,并且存储在不同的磁盘中,以提高该文件的可用性。查询当前数据库拥有的控制文件,最常用的是通过下列两种方式进行。通过初始化参数CONTROL_FILES查询:SQL> SELECT NAME FROM V$CONTROLFILE; NAME--------------------------------------------------------------------------------E:\APP\TIANPAN\ORADATA\PTIAN\CONTROL01.CTLE:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\CONTROL02.CTLSQL> SHOW PARAMETER CONTROL_FILES; NAME TYPE VALUE------------------------------------ ----------- ------------------------------control_files string E:\APP\TIANPAN\ORADATA\PTIAN\CONTROL01.CTL, E:\APP\TIANPAN\FLASH_RECOVERY_AREA\PTIAN\CONTROL02.CTLSQL> 虽说控制文件个头很小,但丢了确实是件很麻烦的事情,注意,这个麻烦不是指恢复控制文件需要很高深的技术、较高难度的操作,而是由于控制文件在Oracle数据库中的重要地位,因此连恢复方式都被特别得看重,以至于恢复控制文件的方法灵活,多种多样,变幻莫测。1.从自动备份中恢复由于没有了控制文件,目标数据库只能启动到NOMOUNT状态,不过在启动数据库之前,必须首先通过SET命令设置DBID:示例:RMAN> SET DBID=1415261003;RMAN> STARTUP NOMOUNT;RMAN> RESTORE CONTROLFILE FROM AUTOBACKUP;2.从备份集中恢复在启动数据库之前,必须首先通过SET命令设置DBID: RMAN> SET DBID=1415261003; 启动数据库到NOMOUNT状态:RMAN> STARTUP NOMOUNT; 执行RESTORE命令时指定控制文件所在备份片段的详细路径: RMAN> RESTORE CONTROLFILE FROM 'f:\oracle\backup\c- 2. 1415261003-20090413-00';6.SPFILE初始化参数文件虽然DBA可以通过BACKUP SPFILE命令手动备份服务器端的初始化参数文件,不过一般都不会主动执行,因为RMAN在备份控制文件时会自动备份SPFILE。相对于其他文件的备份,SPFILE最不重要(或者说最容易被恢复)。除了可以通过备份方式保障拥有可用的SPFILE外,数据库在运行过程中也会在Alert文件中留下数据库启动时的初始化参数信息。甚至即使这些都丢失了也没有关系,如果你对数据库足够了解,还是能够手动地创建出一份SPFILE出来,只是麻烦一些罢了。即使是运行中的数据库丢失了SPFILE也不会导致数据库崩溃(只不过下次启动时如果还没有能创建出一份来,数据库就起不来了,嘿嘿),因此SPFILE的恢复相对来说,可以更加从容。通过RMAN恢复初始化参数的过程与恢复控制文件极其类似。在启动数据库之前,必须首先通过SET命令设置DBID: RMAN> SET DBID=1415261003;启动数据库到NOMOUNT状态: RMAN> STARTUP NOMOUNT;执行恢复命令,将SPFILE恢复到默认路径下:RMAN> RESTORE SPFILE FROM AUTOBACKUP;参考&整理《涂抹Oracle》 控制文件(Control Files)