这篇文章给大家分享的是有关Oracle数据库恢复教程之resetlogs操作的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

实验环境:RHEL 5.4 + Oracle 11.2.0.3

如果是一名合格的Oracle DBA,对resetlogs这种关键字都应该是极其敏感的,当确认需要这种操作时一定要三思而后行,如果自己不是特别确认,哪怕多花些时间申请去让高级DBA人员协助你一起确认,也不要擅自去尝试执行,避免误操作造成既定损失后追悔莫及。

1.哪些场景可以resetlogs

首先要明确resetlogs操作非常危险的,也只有在进行不完全恢复开库时会使用到。

SQL>alterdatabaseopenresetlogs;->openthedatabaseandresettheonlinelogs

官方的描述如下:

Incomplete recovery, also called database point-in-time recovery, results in a noncurrent version of the database. In this case, you do not apply all of the redo generated after the restored backup. Typically, you perform point-in-time database recovery to undo a user error when Flashback Database is not possible.
To perform incomplete recovery, you must restore all data files from backups created before the time to which you want to recover and then open the database with the RESETLOGS option when recovery completes. Resetting the logs creates a new stream of log sequence numbers starting with log sequence 1.

官方的描述其实很清晰,但是实际很多初级DBA小伙伴们在实际工作中遇到这样的场景时却总是有些困惑,甚至误操作引发灾难。

我这里以一个实验来具体说明常见场景:

需求:A机数据库PROD1,现需在B机不同目录下用A机的备份集恢复出来;

A机:

--A机当前currentredolog的sequence是57:SQL>select*fromv$log;GROUP#THREAD#SEQUENCE#BYTESBLOCKSIZEMEMBERSARCSTATUSFIRST_CHANGE#FIRST_TIMNEXT_CHANGE#NEXT_TIME--------------------------------------------------------------------------------------------------------------------------1155524288005121YESINACTIVE205157219-MAY-19206036119-MAY-192156524288005121YESINACTIVE206036119-MAY-19206043619-MAY-193157524288005121NOCURRENT206043619-MAY-192.8147E+14--A机做了一次数据库备份:RMAN>backupdatabaseincludecurrentcontrolfileplusarchivelogdeleteallinput;Startingbackupat19-MAY-19currentlogarchivedallocatedchannel:ORA_DISK_1channelORA_DISK_1:SID=23devicetype=DISKallocatedchannel:ORA_DISK_2channelORA_DISK_2:SID=189devicetype=DISKallocatedchannel:ORA_DISK_3channelORA_DISK_3:SID=21devicetype=DISKchannelORA_DISK_1:startingcompressedarchivedlogbackupsetchannelORA_DISK_1:specifyingarchivedlog(s)inbackupsetinputarchivedlogthread=1sequence=57RECID=3STAMP=1008670991channelORA_DISK_1:startingpiece1at19-MAY-19channelORA_DISK_1:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0cu1u68l_1_1.baktag=TAG20190519T102315comment=NONEchannelORA_DISK_1:backupsetcomplete,elapsedtime:00:00:01channelORA_DISK_1:deletingarchivedlog(s)archivedlogfilename=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch2_57_860888149.dbfRECID=3STAMP=1008670991Finishedbackupat19-MAY-19Startingbackupat19-MAY-19usingchannelORA_DISK_1usingchannelORA_DISK_2usingchannelORA_DISK_3channelORA_DISK_1:startingcompressedfulldatafilebackupsetchannelORA_DISK_1:specifyingdatafile(s)inbackupsetinputdatafilefilenumber=00002name=/u01/app/oracle/oradata/PROD1/sysaux01.dbfchannelORA_DISK_1:startingpiece1at19-MAY-19channelORA_DISK_2:startingcompressedfulldatafilebackupsetchannelORA_DISK_2:specifyingdatafile(s)inbackupsetinputdatafilefilenumber=00001name=/u01/app/oracle/oradata/PROD1/system01.dbfinputdatafilefilenumber=00004name=/u01/app/oracle/oradata/PROD1/users01.dbfchannelORA_DISK_2:startingpiece1at19-MAY-19channelORA_DISK_3:startingcompressedfulldatafilebackupsetchannelORA_DISK_3:specifyingdatafile(s)inbackupsetinputdatafilefilenumber=00005name=/u01/app/oracle/oradata/PROD1/example01.dbfinputdatafilefilenumber=00003name=/u01/app/oracle/oradata/PROD1/undotbs01.dbfchannelORA_DISK_3:startingpiece1at19-MAY-19channelORA_DISK_3:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0fu1u68p_1_1.baktag=TAG20190519T102319comment=NONEchannelORA_DISK_3:backupsetcomplete,elapsedtime:00:00:26channelORA_DISK_3:startingcompressedfulldatafilebackupsetchannelORA_DISK_3:specifyingdatafile(s)inbackupsetincludingcurrentcontrolfileinbackupsetchannelORA_DISK_3:startingpiece1at19-MAY-19channelORA_DISK_3:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0gu1u69j_1_1.baktag=TAG20190519T102319comment=NONEchannelORA_DISK_3:backupsetcomplete,elapsedtime:00:00:01channelORA_DISK_1:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0du1u68p_1_1.baktag=TAG20190519T102319comment=NONEchannelORA_DISK_1:backupsetcomplete,elapsedtime:00:01:03channelORA_DISK_2:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0eu1u68p_1_1.baktag=TAG20190519T102319comment=NONEchannelORA_DISK_2:backupsetcomplete,elapsedtime:00:01:23Finishedbackupat19-MAY-19Startingbackupat19-MAY-19currentlogarchivedusingchannelORA_DISK_1usingchannelORA_DISK_2usingchannelORA_DISK_3channelORA_DISK_1:startingcompressedarchivedlogbackupsetchannelORA_DISK_1:specifyingarchivedlog(s)inbackupsetinputarchivedlogthread=1sequence=58RECID=4STAMP=1008671084channelORA_DISK_1:startingpiece1at19-MAY-19channelORA_DISK_1:finishedpiece1at19-MAY-19piecehandle=/home/oracle/backup/0hu1u6bg_1_1.baktag=TAG20190519T102446comment=NONEchannelORA_DISK_1:backupsetcomplete,elapsedtime:00:00:01channelORA_DISK_1:deletingarchivedlog(s)archivedlogfilename=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch2_58_860888149.dbfRECID=4STAMP=1008671084Finishedbackupat19-MAY-19StartingControlFileandSPFILEAutobackupat19-MAY-19piecehandle=/home/oracle/backup/control/c-2082231315-20190519-01comment=NONEFinishedControlFileandSPFILEAutobackupat19-MAY-19RMAN>--可以看到备份数据库的日志前后都自动归档了当前的redolog(57和58),所以备份完成后,当前日志sequence变为59.SQL>select*fromv$log;GROUP#THREAD#SEQUENCE#BYTESBLOCKSIZEMEMBERSARCSTATUSFIRST_CHANGE#FIRST_TIMNEXT_CHANGE#NEXT_TIME--------------------------------------------------------------------------------------------------------------------------1158524288005121YESINACTIVE206069119-MAY-19206076719-MAY-192159524288005121NOCURRENT206076719-MAY-192.8147E+143157524288005121YESINACTIVE206043619-MAY-19206069119-MAY-19

此时把备份集传输到B机,比如/u03/backup目录下,期望恢复到/u03/oradata/PROD1目录下。如果最终只是根据这个备份集去恢复,那最多恢复完sequence 58就结束了,找不到sequence 59(因为59还是当前current的redolog)。Oracle认为这就是最基本的不完全恢复,需要resetlogs操作。

--指定恢复到/u03/oradata/RMAN>run{2>setnewnamefordatabaseto'/u03/oradata/PROD1/%U';3>restoredatabase;4>}--切换到上步恢复出来的copy复本:RMAN>switchdatabasetocopy;datafile1switchedtodatafilecopy"/u03/oradata/PROD1/data_D-PROD1_TS-SYSTEM_FNO-1"datafile2switchedtodatafilecopy"/u03/oradata/PROD1/data_D-PROD1_TS-SYSAUX_FNO-2"datafile3switchedtodatafilecopy"/u03/oradata/PROD1/data_D-PROD1_TS-UNDOTBS1_FNO-3"datafile4switchedtodatafilecopy"/u03/oradata/PROD1/data_D-PROD1_TS-USERS_FNO-4"datafile5switchedtodatafilecopy"/u03/oradata/PROD1/data_D-PROD1_TS-EXAMPLE_FNO-5"--尝试恢复数据库:RMAN>recoverdatabase;Startingrecoverat19-MAY-19usingtargetdatabasecontrolfileinsteadofrecoverycatalogallocatedchannel:ORA_DISK_1channelORA_DISK_1:SID=102devicetype=DISKallocatedchannel:ORA_DISK_2channelORA_DISK_2:SID=9devicetype=DISKallocatedchannel:ORA_DISK_3channelORA_DISK_3:SID=112devicetype=DISKstartingmediarecoverychannelORA_DISK_1:startingarchivedlogrestoretodefaultdestinationchannelORA_DISK_1:restoringarchivedlogarchivedlogthread=1sequence=58channelORA_DISK_1:readingfrombackuppiece/home/oracle/backup/0hu1u6bg_1_1.bakchannelORA_DISK_1:errorsfoundreadingpiecehandle=/home/oracle/backup/0hu1u6bg_1_1.bakchannelORA_DISK_1:failovertopiecehandle=/u03/backup/0hu1u6bg_1_1.baktag=TAG20190519T102446channelORA_DISK_1:restoredbackuppiece1channelORA_DISK_1:restorecomplete,elapsedtime:00:00:01archivedlogfilename=/u01/app/oracle/product/11.2.0/db_1/dbs/arch2_58_860888149.dbfthread=1sequence=58unabletofindarchivedlogarchivedlogthread=1sequence=59RMAN-00571:===========================================================RMAN-00569:===============ERRORMESSAGESTACKFOLLOWS===============RMAN-00571:===========================================================RMAN-03002:failureofrecovercommandat05/19/201911:04:21RMAN-06054:mediarecoveryrequestingunknownarchivedlogforthread1withsequence59andstartingSCNof2060767RMAN>

可以看到最后有报错信息,就是告诉你找不到sequence 59的日志,这是必然的,因为59还是A机current的redo日志。

2.resetlogs前必须确认路径正确

2.1 先查看控制文件和数据文件头记录的scn是否一致

SQL>selectcheckpoint_change#fromv$datafile;CHECKPOINT_CHANGE#------------------20607672060767206076720607672060767SQL>selectcheckpoint_change#fromv$datafile_header;CHECKPOINT_CHANGE#------------------20607672060767206076720607672060767

2.2 此时如果尝试直接OPEN会报错

SQL>alterdatabaseopen;alterdatabaseopen*ERRORatline1:ORA-01589:mustuseRESETLOGSorNORESETLOGSoptionfordatabaseopen

提示我们开库必须使用RESETLOGS或者NORESETLOGS选项。

2.3 重点来了,现在可以open resetlogs吗?

当然不行!记得一定要确认好路径!!

--查询发现临时文件以及redo日志的路径都不是我们所期望的:SQL>selectnamefromv$datafile;NAME--------------------------------------------------------------------------------/u03/oradata/PROD1/data_D-PROD1_TS-SYSTEM_FNO-1/u03/oradata/PROD1/data_D-PROD1_TS-SYSAUX_FNO-2/u03/oradata/PROD1/data_D-PROD1_TS-UNDOTBS1_FNO-3/u03/oradata/PROD1/data_D-PROD1_TS-USERS_FNO-4/u03/oradata/PROD1/data_D-PROD1_TS-EXAMPLE_FNO-5SQL>selectnamefromv$tempfile;NAME--------------------------------------------------------------------------------/u01/app/oracle/oradata/PROD1/temp01.dbfSQL>selectmemberfromv$logfile;MEMBER--------------------------------------------------------------------------------/u01/app/oracle/oradata/PROD1/redo03.log/u01/app/oracle/oradata/PROD1/redo02.log/u01/app/oracle/oradata/PROD1/redo01.log--rename重命名为我们期望的目录:SQL>alterdatabaserenamefile'/u01/app/oracle/oradata/PROD1/temp01.dbf'to'/u03/oradata/PROD1/temp01.dbf';Databasealtered.SQL>alterdatabaserenamefile'/u01/app/oracle/oradata/PROD1/redo01.log'to'/u03/oradata/PROD1/redo01.log';Databasealtered.SQL>alterdatabaserenamefile'/u01/app/oracle/oradata/PROD1/redo02.log'to'/u03/oradata/PROD1/redo02.log';Databasealtered.SQL>alterdatabaserenamefile'/u01/app/oracle/oradata/PROD1/redo03.log'to'/u03/oradata/PROD1/redo03.log';Databasealtered.--再次检查确认:SQL>selectnamefromv$tempfile;NAME--------------------------------------------------------------------------------/u03/oradata/PROD1/temp01.dbfSQL>selectmemberfromv$logfile;MEMBER--------------------------------------------------------------------------------/u03/oradata/PROD1/redo03.log/u03/oradata/PROD1/redo02.log/u03/oradata/PROD1/redo01.log--最终尝试open开库:SQL>alterdatabaseopen;alterdatabaseopen*ERRORatline1:ORA-01589:mustuseRESETLOGSorNORESETLOGSoptionfordatabaseopenSQL>alterdatabaseopenresetlogs;Databasealtered.

感谢各位的阅读!关于“Oracle数据库恢复教程之resetlogs操作的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!