归档模式下,使用RMAN的同一备份两次用于不完全恢复

实验理论基础:

SCN的序列的递增性不随数据库的任何操作而改变,即使是RESETLOGS也如此。
RESETLOGS清除所有联机日志文件中未应用的重做记录,RESETLOGS只重置日志文件的序列号为1,但对SCN无影响,SCN仍按原序列递增。

在控制文件中保存resetlogs SCN和计数器,以便唯一地标识用RESETLOGS选项执行的每一次打开数据库的操作。
这个值被写进每个数据文件头以及重做日志文件。
如果重做日志文件的日志序列号与Oracle的要求值不相符,则在恢复中不能应用重做日志文件。
执行不完全恢复后,数据库要求日志序列号为1的日志文件,所以原来的日志序列中剩余的日志文件将不可用。
RESETLOGS操作创建数据库的新形体,即一个拥有从1开始的新的日志序列号流的数据库。

根据以上理论:SCN为顺序数据流,在数据库存在期间始终递增,而日志文件序列流也是递增序列,只不过会因RESETLOGS而重置为1,但日志文件序列流中的SCN序列流却保持递增不变
因此可以用RESETLOGS之前的归档日志流和RESETLOGS之后的归档日志流来连接和延续SCN序列流,这样就实现了用RESETLOGS之前的备份恢复RESETLOGS之后的数据。
前提是:

保证两股日志流(RESETLOGS之前的归档日志流和RESETLOGS之后的归档日志流)完整,并且有相应两股日志流的控制文件。

建议:在RESETLOGS前保证数据库以前备份的数据安全,在创建RESETLOGS之后的第一个一致性备份之前,一定不可删掉在RESETLOGS前创建的一致数据库备份

如空间足够,建议永久保留RESETLOGS前创建的一致数据库备份。

实验思路、步骤:

1.关库做备份--RMAN,记下SCN与时间,记录序号及REDO。
2.打开库,记下时间/SCN,删除一个用户,做基于时间点的不完全恢复。记录不完全恢复后的SCN,归档序号
3.在恢复后的库,记下时间/SCN,再删除一个用户,使用步骤1中备份可以对RESETLOS后的库再次做不完全恢复。
##########################################################################################################

第一步:关库做备份--RMAN,记录归档日志序号。

1.查询当前归档日志序号

SYS@bys1>col name for a50
SYS@bys1>select * from (select name,recid,sequence#,status,applied from v$archived_log order by recid desc) where rownum<5;
NAME RECID SEQUENCE# S APPLIED
-------------------------------------------------- ---------- ---------- - ---------
/u01/archbys1/arc_1_31_829941492.arc 120 31 A NO
/u01/archbys1/arc_1_30_829941492.arc 119 30 A NO
/u01/archbys1/arc_1_29_829941492.arc 118 29 A NO

/u01/archbys1/arc_1_28_829941492.arc 117 28 A NO
BYS@bys1>select incarnation#,resetlogs_change#,resetlogs_time,prior_resetlogs_time,status,resetlogs_id from v$database_incarnation;后来补充-可以从此视图查出历次RESETLOGS情况
INCARNATION# RESETLOGS_CHANGE# RESETLOGS_TIME PRIOR_RESETLOGS_TIM STATUS RESETLOGS_ID
------------ ----------------- ------------------- ------------------- ------- ------------
1 1 2009/08/13 23:00:48 PARENT 694825248
2 754488 2013/08/01 08:58:04 2009/08/13 23:00:48 PARENT 822301084
3 2541692 2013/10/27 19:18:12 2013/08/01 08:58:04 PARENT 829941492
4 3228984 2013/11/06 00:14:08 2013/10/27 19:18:12 PARENT 830736848
5 3229505 2013/11/06 00:27:42 2013/11/06 00:14:08 CURRENT 830737662

2.关库并启动到 MOUNT,做全备备份

SYS@bys1>startup mount;
ORACLE instance started.
Total System Global Area 631914496 bytes
Fixed Size 1338364 bytes
Variable Size 385876996 bytes
Database Buffers 239075328 bytes
Redo Buffers 5623808 bytes
SYS@bys1>exit

3.使用写好的备份脚本做全备备份--注意先用df -h确定备份的目录有足够空间。

[oracle@bys001 ~]$ cat fullback.sh
rman log /home/oracle/backfull-`date +%Y%m%d-%H%M`.log <<EOF
connect target /;
run {
backup full tag 'bys001-full' database
format "/home/oracle/bys001full_%d_%t_%s"
plus archivelog
format "/home/oracle/bys001arch_%d_%t_%s"
delete all input;
}
exit

[oracle@bys001 ~]$ sh fullback.sh
RMAN> RMAN> 2> 3> 4> 5> 6> 7> RMAN>
[oracle@bys001 ~]$ sqlplus / as sysdba
SYS@bys1>alter database open;
Database altered.
###############
4.记录当前系统的SCN,并删除一个用户
SYS@bys1>select open_mode from v$database;
OPEN_MODE
--------------------
READ WRITE
SYS@bys1>select dbid,checkpoint_change# from v$database;
DBID CHECKPOINT_CHANGE#
---------- ------------------
3957527513 3228508
SYS@bys1>col name for a40
SYS@bys1>select file#,name,checkpoint_change#,to_char(checkpoint_time,'yyyy-mm-dd hh24:mi:ss') cptime from v$datafile;
FILE# NAME CHECKPOINT_CHANGE# CPTIME
---------- ---------------------------------------- ------------------ -------------------
1 /u01/oradata/bys1/system01.dbf 3228508 2013-11-05 23:51:15
2 /u01/oradata/bys1/sysaux01.dbf 3228508 2013-11-05 23:51:15
3 /u01/oradata/bys1/undotbs01.dbf 3228508 2013-11-05 23:51:15
4 /u01/oradata/bys1/users01.dbf 3228508 2013-11-05 23:51:15
5 /u01/oradata/bys1/example01.dbf 3228508 2013-11-05 23:51:15
6 /u01/oradata/bys1/test1.dbf 3228508 2013-11-05 23:51:15
SYS@bys1> select GROUP#,sequence#,STATUS,FIRST_CHANGE#,to_char(FIRST_TIME,'yyyy/mm/dd:hh24:mi:ss') time from V$log;
GROUP# SEQUENCE# STATUS FIRST_CHANGE# TIME
---------- ---------- ---------------- ------------- -------------------
1 31 INACTIVE 3222825 2013/11/05:22:08:56
2 32 CURRENT 3228378 2013/11/05:23:38:08 可以看到当前REDO日志的序号是32
3 30 INACTIVE 3218774 2013/11/05:22:00:30
SYS@bys1>set time on
23:55:34 SYS@bys1> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3228983
23:55:35 SYS@bys1>conn test/test
Connected.
23:55:53 TEST@bys1>select * from cat;
TABLE_NAME TABLE_TYPE
------------------------------ -----------
TEST1 SYNONYM
23:56:06 TEST@bys1>conn / as sysdba
Connected.
23:56:18 SYS@bys1>drop user test cascade;
User dropped.
删除用户完成
######################################################################

第二步:执行第一次不完全恢复

1.首先将库启动到MOUNT,然后基于SCN进行不完全恢复

23:57:19 @bys1>conn / as sysdba
Connected.
23:57:25 SYS@bys1>shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
23:57:42 SYS@bys1>startup mount;
ORACLE instance started.

Total System Global Area 631914496 bytes
Fixed Size 1338364 bytes
Variable Size 385876996 bytes
Database Buffers 239075328 bytes
Redo Buffers 5623808 bytes
Database mounted.

##############################

在RMAN中使用的恢复语句如下

restore database;
recover database until scn 3228983;
alter database open resetlogs;
################
RMAN> restore database;

Starting restore at 2013/11/06 00:08:56
using channel ORA_DISK_1

skipping datafile 3; already restored to file /u01/oradata/bys1/undotbs01.dbf
skipping datafile 4; already restored to file /u01/oradata/bys1/users01.dbf
skipping datafile 5; already restored to file /u01/oradata/bys1/example01.dbf
skipping datafile 6; already restored to file /u01/oradata/bys1/test1.dbf
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00001 to /u01/oradata/bys1/system01.dbf
channel ORA_DISK_1: restoring datafile 00002 to /u01/oradata/bys1/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/bys001full_BYS1_830735101_8
channel ORA_DISK_1: piece handle=/home/oracle/bys001full_BYS1_830735101_8 tag=BYS001-FULL
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:04:13
Finished restore at 2013/11/06 00:13:09

RMAN> recover database until scn 3228983;

Starting recover at 2013/11/06 00:13:48
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:03

Finished recover at 2013/11/06 00:13:51

RMAN> alter database open; 使用了不完整恢复,所以打开库时要用RESETLOGS,不然报错。

RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 11/06/2013 00:14:02
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open


RMAN> alter database open resetlogs;
database opened
RMAN>
##########################################################

第三步:登陆系统查看数据是否恢复,并再次删除用户

[oracle@bys001 ~]$ sqlplus / as sysdba
SYS@bys1>select open_mode from v$database;
OPEN_MODE
--------------------
READ WRITE

SYS@bys1>col name for a40

BYS@bys1>show parameter archive_form
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_format string arc_%t_%s_%r.arc

 

初始化参数LOG_ARCHIVE_FORMAT用于指定归档日志的文件名格式,设置该初始化参数时,可以指定以下匹配符:
%s: 日志序列号 %S: 日志序列号(带有前导0) %t: 重做线程编号.%T: 重做线程编号(带有前导0) %a: 活动ID %d: 数据库ID号 %r RESETLOGS的值.

SYS@bys1>select * from (select name,recid,sequence#,status,applied from v$archived_log order by recid desc) where rownum<5;
NAME RECID SEQUENCE# S APPLIED
---------------------------------------- ---------- ---------- - ---------
/u01/archbys1/arc_1_32_829941492.arc 121 32 A NO 这里忘了手动做一次归档,所以看不出归档日志名中日志序列号被重置为1
120 31 D NO
119 30 D NO
118 29 D NO
SYS@bys1>select dbid,checkpoint_change# from v$database;
DBID CHECKPOINT_CHANGE#
---------- ------------------
3957527513 3228987
SYS@bys1>select file#,name,checkpoint_change#,to_char(checkpoint_time,'yyyy-mm-dd hh24:mi:ss') cptime from v$datafile;
FILE# NAME CHECKPOINT_CHANGE# CPTIME
---------- ---------------------------------------- ------------------ -------------------
1 /u01/oradata/bys1/system01.dbf 32289872013-11-06 00:14:27
2 /u01/oradata/bys1/sysaux01.dbf 3228987 2013-11-06 00:14:27
3 /u01/oradata/bys1/undotbs01.dbf 3228987 2013-11-06 00:14:27
4 /u01/oradata/bys1/users01.dbf 3228987 2013-11-06 00:14:27
5 /u01/oradata/bys1/example01.dbf 3228987 2013-11-06 00:14:27
6 /u01/oradata/bys1/test1.dbf 3228987 2013-11-06 00:14:27
SYS@bys1>select GROUP#,sequence#,STATUS,FIRST_CHANGE#,to_char(FIRST_TIME,'yyyy/mm/dd:hh24:mi:ss') time from V$log;
GROUP# SEQUENCE# STATUS FIRST_CHANGE# TIME
---------- ---------- ---------------- ------------- -------------------
1 1 CURRENT 3228984 2013/11/06:00:14:08 可以看到RESETLOGS后的REDO日志的序号是1
2 0 UNUSED 0
3 0 UNUSED 0
SYS@bys1>conn test/test
Connected.
TEST@bys1>set time on
00:16:34 TEST@bys1>select * from cat;
TABLE_NAME TABLE_TYPE
------------------------------ -----------
TEST1 SYNONYM
00:16:39 TEST@bys1>conn / as sysdba
Connected.
00:16:45 SYS@bys1> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3229504
00:16:57 SYS@bys1>drop user test cascade;
User dropped.
00:17:12 SYS@bys1>conn test/test
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
00:17:17 @bys1>conn / as sysdba
Connected.
00:17:21 SYS@bys1>shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
00:17:40 SYS@bys1>startup mount;
ORACLE instance started.

Total System Global Area 631914496 bytes
Fixed Size 1338364 bytes
Variable Size 385876996 bytes
Database Buffers 239075328 bytes
Redo Buffers 5623808 bytes
Database mounted.
############################################################

推荐阅读:

相关推荐