欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle数据库恢复教程之resetlogs操作

程序员文章站 2023-12-13 20:19:34
实验环境:rhel 5.4 + oracle 11.2.0.3 如果是一名合格的oracle dba,对resetlogs这种关键字都应该是极其敏感的,当确认需要这种...

实验环境:rhel 5.4 + oracle 11.2.0.3

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

1.哪些场景可以resetlogs

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

sql> alter database open resetlogs;
-> open the database and reset the online logs

官方的描述如下:

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机当前current redolog的sequence是57:
sql> select * from v$log;

 group# thread# sequence#  bytes blocksize members arc status   first_change# first_tim next_change# next_time
---------- ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- ------------ ---------
   1   1   55 52428800  512   1 yes inactive    2051572 19-may-19  2060361 19-may-19
   2   1   56 52428800  512   1 yes inactive    2060361 19-may-19  2060436 19-may-19
   3   1   57 52428800  512   1 no current    2060436 19-may-19 2.8147e+14

--a机做了一次数据库备份:
rman> backup database include current controlfile plus archivelog delete all input;


starting backup at 19-may-19
current log archived
allocated channel: ora_disk_1
channel ora_disk_1: sid=23 device type=disk
allocated channel: ora_disk_2
channel ora_disk_2: sid=189 device type=disk
allocated channel: ora_disk_3
channel ora_disk_3: sid=21 device type=disk
channel ora_disk_1: starting compressed archived log backup set
channel ora_disk_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=57 recid=3 stamp=1008670991
channel ora_disk_1: starting piece 1 at 19-may-19
channel ora_disk_1: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0cu1u68l_1_1.bak tag=tag20190519t102315 comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:00:01
channel ora_disk_1: deleting archived log(s)
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_57_860888149.dbf recid=3 stamp=1008670991
finished backup at 19-may-19

starting backup at 19-may-19
using channel ora_disk_1
using channel ora_disk_2
using channel ora_disk_3
channel ora_disk_1: starting compressed full datafile backup set
channel ora_disk_1: specifying datafile(s) in backup set
input datafile file number=00002 name=/u01/app/oracle/oradata/prod1/sysaux01.dbf
channel ora_disk_1: starting piece 1 at 19-may-19
channel ora_disk_2: starting compressed full datafile backup set
channel ora_disk_2: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/prod1/system01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/prod1/users01.dbf
channel ora_disk_2: starting piece 1 at 19-may-19
channel ora_disk_3: starting compressed full datafile backup set
channel ora_disk_3: specifying datafile(s) in backup set
input datafile file number=00005 name=/u01/app/oracle/oradata/prod1/example01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/prod1/undotbs01.dbf
channel ora_disk_3: starting piece 1 at 19-may-19
channel ora_disk_3: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0fu1u68p_1_1.bak tag=tag20190519t102319 comment=none
channel ora_disk_3: backup set complete, elapsed time: 00:00:26
channel ora_disk_3: starting compressed full datafile backup set
channel ora_disk_3: specifying datafile(s) in backup set
including current control file in backup set
channel ora_disk_3: starting piece 1 at 19-may-19
channel ora_disk_3: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0gu1u69j_1_1.bak tag=tag20190519t102319 comment=none
channel ora_disk_3: backup set complete, elapsed time: 00:00:01
channel ora_disk_1: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0du1u68p_1_1.bak tag=tag20190519t102319 comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:01:03
channel ora_disk_2: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0eu1u68p_1_1.bak tag=tag20190519t102319 comment=none
channel ora_disk_2: backup set complete, elapsed time: 00:01:23
finished backup at 19-may-19

starting backup at 19-may-19
current log archived
using channel ora_disk_1
using channel ora_disk_2
using channel ora_disk_3
channel ora_disk_1: starting compressed archived log backup set
channel ora_disk_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=58 recid=4 stamp=1008671084
channel ora_disk_1: starting piece 1 at 19-may-19
channel ora_disk_1: finished piece 1 at 19-may-19
piece handle=/home/oracle/backup/0hu1u6bg_1_1.bak tag=tag20190519t102446 comment=none
channel ora_disk_1: backup set complete, elapsed time: 00:00:01
channel ora_disk_1: deleting archived log(s)
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_58_860888149.dbf recid=4 stamp=1008671084
finished backup at 19-may-19

starting control file and spfile autobackup at 19-may-19
piece handle=/home/oracle/backup/control/c-2082231315-20190519-01 comment=none
finished control file and spfile autobackup at 19-may-19

rman> 

--可以看到备份数据库的日志前后都自动归档了当前的redolog(57和58),所以备份完成后,当前日志sequence变为59.
sql> select * from v$log;

 group# thread# sequence#  bytes blocksize members arc status   first_change# first_tim next_change# next_time
---------- ---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- --------- ------------ ---------
   1   1   58 52428800  512   1 yes inactive    2060691 19-may-19  2060767 19-may-19
   2   1   59 52428800  512   1 no current    2060767 19-may-19 2.8147e+14
   3   1   57 52428800  512   1 yes inactive    2060436 19-may-19  2060691 19-may-19

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

--指定恢复到/u03/oradata/
rman> run {
2> set newname for database to '/u03/oradata/prod1/%u';
3> restore database;
4> }

--切换到上步恢复出来的copy复本:
rman> switch database to copy;

datafile 1 switched to datafile copy "/u03/oradata/prod1/data_d-prod1_ts-system_fno-1"
datafile 2 switched to datafile copy "/u03/oradata/prod1/data_d-prod1_ts-sysaux_fno-2"
datafile 3 switched to datafile copy "/u03/oradata/prod1/data_d-prod1_ts-undotbs1_fno-3"
datafile 4 switched to datafile copy "/u03/oradata/prod1/data_d-prod1_ts-users_fno-4"
datafile 5 switched to datafile copy "/u03/oradata/prod1/data_d-prod1_ts-example_fno-5"

--尝试恢复数据库:
rman> recover database;

starting recover at 19-may-19
using target database control file instead of recovery catalog
allocated channel: ora_disk_1
channel ora_disk_1: sid=102 device type=disk
allocated channel: ora_disk_2
channel ora_disk_2: sid=9 device type=disk
allocated channel: ora_disk_3
channel ora_disk_3: sid=112 device type=disk

starting media recovery

channel ora_disk_1: starting archived log restore to default destination
channel ora_disk_1: restoring archived log
archived log thread=1 sequence=58
channel ora_disk_1: reading from backup piece /home/oracle/backup/0hu1u6bg_1_1.bak
channel ora_disk_1: errors found reading piece handle=/home/oracle/backup/0hu1u6bg_1_1.bak
channel ora_disk_1: failover to piece handle=/u03/backup/0hu1u6bg_1_1.bak tag=tag20190519t102446
channel ora_disk_1: restored backup piece 1
channel ora_disk_1: restore complete, elapsed time: 00:00:01
archived log file name=/u01/app/oracle/product/11.2.0/db_1/dbs/arch1_58_860888149.dbf thread=1 sequence=58
unable to find archived log
archived log thread=1 sequence=59
rman-00571: ===========================================================
rman-00569: =============== error message stack follows ===============
rman-00571: ===========================================================
rman-03002: failure of recover command at 05/19/2019 11:04:21
rman-06054: media recovery requesting unknown archived log for thread 1 with sequence 59 and starting scn of 2060767

rman>

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

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

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

sql> select checkpoint_change# from v$datafile;

checkpoint_change#
------------------
   2060767
   2060767
   2060767
   2060767
   2060767

sql> select checkpoint_change# from v$datafile_header;

checkpoint_change#
------------------
   2060767
   2060767
   2060767
   2060767
   2060767

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

sql> alter database open;
alter database open
*
error at line 1:
ora-01589: must use resetlogs or noresetlogs option for database open

提示我们开库必须使用resetlogs或者noresetlogs选项。

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

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

--查询发现临时文件以及redo日志的路径都不是我们所期望的:
sql> select name from v$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-5

sql> select name from v$tempfile;

name
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/prod1/temp01.dbf

sql> select member from v$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> alter database rename file '/u01/app/oracle/oradata/prod1/temp01.dbf' to '/u03/oradata/prod1/temp01.dbf';

database altered.

sql> alter database rename file '/u01/app/oracle/oradata/prod1/redo01.log' to '/u03/oradata/prod1/redo01.log';

database altered.

sql> alter database rename file '/u01/app/oracle/oradata/prod1/redo02.log' to '/u03/oradata/prod1/redo02.log';

database altered.

sql> alter database rename file '/u01/app/oracle/oradata/prod1/redo03.log' to '/u03/oradata/prod1/redo03.log';

database altered.

--再次检查确认:
sql> select name from v$tempfile;

name
--------------------------------------------------------------------------------
/u03/oradata/prod1/temp01.dbf

sql> select member from v$logfile;

member
--------------------------------------------------------------------------------
/u03/oradata/prod1/redo03.log
/u03/oradata/prod1/redo02.log
/u03/oradata/prod1/redo01.log

--最终尝试open开库:
sql> alter database open;
alter database open
*
error at line 1:
ora-01589: must use resetlogs or noresetlogs option for database open


sql> alter database open resetlogs;

database altered.

总结:

很多初级人员有可能是对set newname for database这个有误解,以为这里的database包括了临时文件,redo日志文件,误以为自己已经把新库所有路径都指向到了期望位置。但实际并不是这样,这也说明了不确认的操作一定要在测试环境测试验证后才可以在生产环境操作。大家可以想象一下,如果是理解有误没确认日志路径直接执行了resetlogs,那么如果b机正好有别的库用到同名的这些路径,亦或是整个恢复操作就是直接在a机的本机其他目录临时基于某个时间点恢复出一套库,那将会是一场大的生产事故。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。

上一篇:

下一篇: