Oracle备份恢复场景总结一、数据文件、表空间恢复1、数据库文件恢复1场景一:问题描述:test.dbf丢失,有RMAN备份。重启报错:Startup;ORA-01157:cannotidentify/lockdatafile5-seeDBWRtracefileORA-01110:datafile5:'/oracle/app/oracle/oradata/orcl/test01.dbf'run{startupmount;allocatechannelch00type'SBT_TAPE';restoredatafile5;recoverdatafile5;alterdatabaseopen;releasechannelch00;}2场景二问题描述:模拟test表空间损坏,删除数据文件(恢复到新目录)旧目录:/oracle/app/oracle/oradata新目录:/home/oraclerun{allocatechannelch00type'SBT_TAPE';sql'alterdatabasedatafile5offline';setnewnamefordatafile5to'/home/oracle/test01.dbf';restoredatafile5;switchdatafile5;recoverdatafile5;sql'alterdatabasedatafile4online';releasechannelch00;}说明:setnewnamefor告诉RMAN还原数据文件的新位置在哪里。这个命令在restore前出现。switchdatafile更新controlfile,记录这个新位置。这个命令要在recover前出现2、表空间恢复恢复误删除表空间(已备份),RMAN必须通过备份的控制文件(与之配套的老控制文件)进行恢复。run{startupnomount;allocatechanneld1typesbt;restorecontrolfilefrom'/bk_43_1_920726015';sql'alterdatabasemount';setuntiltime'2016-08-2413:17:16';restoredatabase;recoverdatabase;alterdatabaseopenreadonly;releasechanneld1;}确保没问题在:shutdownabort;startupmount;alterdatabaseopenresetlogs;二、控制文件恢复1、场景一:控制文件自动备份的恢复startupnomountsetdbidxxxx需指定DBIDrestorecontrolfilefromautobackup;alterdatabasemount;recoverdatabase;alterdatabaseopenresetlogs;如何寻找DBID?1.既然是默认路径,那么就去默认路径寻找,一般在$ORACLE_HOME/dbs目录下,例如c-361654012-20150312-04,其中361654012就是DBID2.在数据文件的头部含有DBID2、场景一:控制文件从非默认地方恢复这里控制文件从nbu备份的磁带上恢复备份路径startupnomount#从本地恢复restorecontrolfilefrom‘/u01/c-361654012-20150312-04’;在此之前无需指定DBID或者#从SBT_TAPE恢复restorecontrolfilefrom'/cntrl_23_1_920645270';run{allocatechanneld1typesbt;restorecontrolfilefrom'/cntrl_23_1_920645270';sql'alterdatabasemount';recoverdatabase;sql'alterdatabaseopenresetlogs';releasechanneld1;}注释:1、restorecontrolfile必须写在run块了,否则报错:RMAN-06172:noAUTOBACKUPfoundorspecifiedhandleisnotavalidcopyorpiece2sbt的磁带号-/cntrl_23_1_920645270可以从已备份成功的任务里查看。三、Redo文件恢复联机重做日志的3种状态:current当前inactive非活动active活动观察数据恢复是否需要联机重做日志来恢复。1.inactive日志丢失的恢复—实例恢复不需要2.active日志丢失的恢复:日志组对应的脏块还没有从databuffer写入到datafile,不能被覆盖(但已写入到归档里)—实例恢复需要3.current日志丢失的恢复—实例恢复需要1、场景一:inactive日志丢失的恢复1正常关闭数据库后(redo的丢失就属于inactive日志丢失的恢复)shutdownimmediatermredo*startupmountalterdatabaseclearlogfile'/u01/app/oracle/oradata/ortest/redo01.log';##alterdatabaseclearlogfilegroupn;也行alterdatabaseclearlogfile'/u01/app/oracle/oradata/ortest/redo03.log';alterdatabaseclearunarchivedlogfile'/u01/app/oracle/oradata/ortest/redo02.log';alterdatabaseopen;此情况不会丢失数据。起来后多切几遍日志,看是否正常:altersystemswitchlogfile;注意事项:必须clear完非currentlog之后,才能clearunarchivedcurrentlog(当你不带参数unarchived而clear当前日志的时候会提示你日志需要归档)。2非关闭数据库后(inactive日志丢失)shutdownabortrmredo01.logstartupmountalterdatabaseclearlogfile'/u01/app/oracle/oradata/ortest/redo01.log';alterdatabaseopen;此种情况不会丢失数据2、场景二:active日志丢失非正常关闭数据库,active日志丢失shutdownabortrmredo01.logstartupmountrecoverdatabaseuntilcancel;alterdatabaseopenresetlogs;此种情况不会丢失数据。(active的日志已写入归档)3、场景三:current日志丢失此种情况会丢失数据:shutdownabortrmredo01.logstartupmountaltersystemset_allow_resetlogs_corruption=truescope=spfile;添加隐藏参数recoverdatabaseuntilcancel;alterdatabaseopenresetlogs;四、不完全恢复RMAN不完全恢复:三个标准模式:基于time、基于scn和基于sequence还有一种是基于cancel的恢复(注意是在SQL下执行)在不完全恢复时,有时为了保险往往一步把数据库openreadonly,确保没问题再openresetlogs:alterdatabaseopenreadonly;启动到mount下执行:run{allocatechannelc1typedisk;allocatechannelc2typedisk;setuntiltime'2016-06-0611:54:05';restoredatabase;recoverdatabase;alterdatabaseopenresetlogs;}//基于scn的方法与基于time相似,语法是setuntilscn6689163;//基于日志的方法类似手工恢复的例子,语法是setuntilsequence3;//有时报错格式不对恢复前执行://linux操作系统:exportNLS_DATE_FORMAT='YYYY-MM-DDHH24:MI:SS'五、database恢复1、原机器恢复1恢复spfile文件-到nomount状态:可以从数据库模板里随便找个pfile起到mount下,再恢复spfile。因为分配channel必须得在nomount下面:rmanrun{allocatechannelc1typesbt;restorespfilefrom'/al_63_1_920727874';releasechannelc1;}2恢复控制文件-到mount状态run{allocatechannelc1typesbt;restorecontrolfilefrom'/cntrl_64_1_920727890';sql'alterdatabasemount';releasechannelc1;}3恢复数据文件-以readonly打开run{allocatechannelc1typesbt;allocatechannelc2typesbt;allocatechannelc3typesbt;allocatechannelc4typesbt;setuntiltime'2016-08-2413:40:00';restoredatabase;recoverdatabase;sql'alterdatabaseopenreadonly';releasechannelc1;releasechannelc2;releasechannelc3;releasechannelc4;}注释:查看恢复用到的截止时间戳:用listbackup;查看备份的归档最后的时间:4打开数据库确定无误后,已resetlogs打开数据库:shutdownabort;startupmount;alterdatabaseopenresetlogs;2、异机恢复:参考原机器恢复,主要的就是,再每个run里面写着一条:run{….SEND'NB_ORA_CLIENT=rac1;…}rac1是nbu上原机器的client名字。具体参考另外一个work文档。附录1、rman里allocate的单独用法:RMANallocatechannelformaintenancedevicetypesbtparms'SBT_LIBRARY=oracle.disksbt,ENV=(BACKUP_DIR=/tmp)';allocatedchannel:ORA_MAINT_SBT_TAPE_1channelORA_MAINT_SBT_TAPE_1:SID=63devicetype=SBT_TAPEchannelORA_MAINT_SBT_TAPE_1:WARNING:OracleTestDiskAPI2、手工注册备份集到控制文件:rmantarget/configureCHANNELdevicetype'SBT_TAPE'PARMS'ENV=(NB_ORA_CLIENT=rac1)';catalogdevicetype'SBT_TAPE'backuppiece'/bk_6177_1_923273904';3、不完全恢复后的注意事项不完全恢复后归档肯定有丢失,这是需要执行以下操作:RMANcrosscheckarchivelogall;RMANdeleteexpiredarchivelogall;最后再做全备份。