ITPub博客

首页 > 数据库 > Oracle > rman完全恢复演练

rman完全恢复演练

原创 Oracle 作者:pwz1688 时间:2014-05-27 21:58:46 0 删除 编辑
本文主要记述10个完全恢复的演示案例,每个案例均可与之前手工完全恢复演练相对应,操作时可复习下之前手工备份的内容。在案例演示之前,先要做好冷热备份,下面开始做rman冷热备。
--执行冷备
RMAN> run{
2> shutdown immediate;
3> startup mount;
allocate channel c1 type disk;
allocate channel c2 type disk;
4> 5> 6> backup database format'/home/oracle/rman_bak/cold_bak/%d_%s_%p.bak';
7> alter database open;
8> }
database closed
database dismounted
Oracle instance shut down
connected to target database (not started)
Oracle instance started
database mounted
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
allocated channel: c1
channel c1: SID=19 device type=DISK
allocated channel: c2
channel c2: SID=21 device type=DISK
Starting backup at 18-APR-14
channel c1: starting compressed full datafile backup set
channel c1: specifying datafile(s) in backup set
input datafile file number=00004 name=/u01/app/oracle/oradata/PROD/disk3/users01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
input datafile file number=00006 name=/u01/app/oracle/oradata/PROD/disk3/fba01.dbf
channel c1: starting piece 1 at 18-APR-14
channel c2: starting compressed full datafile backup set
channel c2: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/PROD/disk3/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel c2: starting piece 1 at 18-APR-14
channel c1: finished piece 1 at 18-APR-14
piece handle=/home/oracle/rman_bak/cold_bak/PROD_55_1.bak tag=TAG20140418T174338 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:17
channel c2: finished piece 1 at 18-APR-14
piece handle=/home/oracle/rman_bak/cold_bak/PROD_56_1.bak tag=TAG20140418T174338 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:39
Finished backup at 18-APR-14
Starting Control File Autobackup at 18-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140418-00 comment=NONE
Finished Control File Autobackup at 18-APR-14
database opened
released channel: c1
released channel: c2
--执行热备
RMAN> run{
2> sql 'alter system switch logfile';
3> allocate channel c1 type disk;
4> allocate channel c2 type disk;
5> backup database format'/home/oracle/rman_bak/hot_bak/%d_%s_%u.bak' filesperset 3;
sql 'alter system switch logfile';
}
6> 7>
sql statement: alter system switch logfile
allocated channel: c1
channel c1: SID=19 device type=DISK
allocated channel: c2
channel c2: SID=21 device type=DISK
Starting backup at 18-APR-14
channel c1: starting compressed full datafile backup set
channel c1: specifying datafile(s) in backup set
input datafile file number=00004 name=/u01/app/oracle/oradata/PROD/disk3/users01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
input datafile file number=00006 name=/u01/app/oracle/oradata/PROD/disk3/fba01.dbf
channel c1: starting piece 1 at 18-APR-14
channel c2: starting compressed full datafile backup set
channel c2: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/PROD/disk3/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel c2: starting piece 1 at 18-APR-14
channel c1: finished piece 1 at 18-APR-14
piece handle=/home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak tag=TAG20140418T174636 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:08
channel c2: finished piece 1 at 18-APR-14
piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak tag=TAG20140418T174636 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:26
Finished backup at 18-APR-14
Starting Control File Autobackup at 18-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140418-01 comment=NONE
Finished Control File Autobackup at 18-APR-14
sql statement: alter system switch logfile
released channel: c1
released channel: c2
一、归档模式,执行rman的恢复
案例一:所有数据都丢失(控制文件丢失)
场景:对scott.lxtb1表分三次插入数据(如之前手工完全恢复演练一样,第一次提交归档、第二次只提交不归档,第三次不提交不归档),之后突然断电,然后删除所有的控制文件及数据文件,在此情况,对数据库做rman完全恢复;
--控制文件破坏之前,先对其做转储
SQL> show parameter dump;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
background_core_dump string partial
background_dump_dest string /u01/app/oracle/oradata/PROD/d
                                                 ump/diag/rdbms/prod/PROD/trace
core_dump_dest string /u01/app/oracle/oradata/PROD/d
                                                 ump/diag/rdbms/prod/PROD/cdump
max_dump_file_size string unlimited
shadow_core_dump string partial
user_dump_dest string /u01/app/oracle/oradata/PROD/d
                                                 ump/diag/rdbms/prod/PROD/trace
SQL> alter database backup controlfile to trace;
Database altered
注意:转储控制文件时,还可指定文件名,方便查找,如:
alter database backup controlfile to trace as '/u01/app/oracle/oradata/PROD/dump/diag/rdbms/prod/PROD/trace/prod_ora_20140526.tra';
[oracle@gc1 ~]$ cd /u01/app/oracle/oradata/PROD/dump/diag/rdbms/prod/PROD/trace
[oracle@gc1 trace]$ ls -lt | grep PROD_ora
-rw-r----- 1 oracle oinstall 10177 Apr 18 18:02 PROD_ora_2612.trc
-rw-r----- 1 oracle oinstall 110 Apr 18 18:02 PROD_ora_2612.trm
-rw-r----- 1 oracle oinstall 2106 Apr 18 17:47 PROD_ora_2528.trc
-rw-r----- 1 oracle oinstall 488 Apr 18 17:47 PROD_ora_2528.trm
-rw-r----- 1 oracle oinstall 1004 Apr 18 17:44 PROD_ora_2526.trc
-rw-r----- 1 oracle oinstall 78 Apr 18 17:44 PROD_ora_2526.trm
-rw-r----- 1 oracle oinstall 1384 Apr 18 17:43 PROD_ora_2529.trc
-rw-r----- 1 oracle oinstall 82 Apr 18 17:43 PROD_ora_2529.trm
-rw-r----- 1 oracle oinstall 14428 Apr 18 17:43 PROD_ora_2330.trc
-rw-r----- 1 oracle oinstall 258 Apr 18 17:43 PROD_ora_2330.trm
-rw-r----- 1 oracle oinstall 1553 Apr 18 17:42 PROD_ora_2165.trc
……
--查看转储下来的PROD_ora_2612.trc控制文件信息
[oracle@gc1 trace]$ more PROD_ora_2612.trc
找到noresetlogs方式的重建控制文件语句,如下:
--开始做破坏性实验操作,实验表为scott.lxtb1
SQL> alter database backup controlfile to trace;
Database altered.
SQL> select count(1) from scott.lxtb1;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb1 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> select count(1) from scott.lxtb1;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb1 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb1;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb1 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb1;
  COUNT(1)
----------
        56
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
6 rows selected.
--模拟断电
SQL> shutdown abort;
ORACLE instance shut down.
--手工破坏所有数据文件及控制文件
[oracle@gc1 rman_bak]$ cd /u01/app/oracle/oradata/PROD/
[oracle@gc1 PROD]$ ls
disk1 disk2 disk3 disk4 disk5 dump
[oracle@gc1 PROD]$ cd disk1;
[oracle@gc1 disk1]$ ls
control01.ctl redo01.log redo02.log redo03.log
[oracle@gc1 disk1]$ rm *.ctl
[oracle@gc1 disk1]$ cd ../disk2
[oracle@gc1 disk2]$ ls
control01.ctl redo01.log redo02.log redo03.log
[oracle@gc1 disk2]$ rm *.ctl
[oracle@gc1 disk2]$ ls
redo01.log redo02.log redo03.log
[oracle@gc1 disk2]$ cd ../disk3
[oracle@gc1 disk3]$ ls
control01.ctl fba01.dbf lxtbs01.dbf system01.dbf users01.dbf
[oracle@gc1 disk3]$ rm *
[oracle@gc1 disk3]$ cd ../disk4
[oracle@gc1 disk4]$ ls
sysaux01.dbf temp01.dbf
[oracle@gc1 disk4]$ rm *
[oracle@gc1 disk4]$ cd ../disk5
[oracle@gc1 disk5]$ ls
undotbs01.dbf
[oracle@gc1 disk5]$ rm *
[oracle@gc1 disk5]$
--此时启库报错
SQL> startup;
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
ORA-00205: error in identifying control file, check alert log for more info
--开始恢复
--查看控制文件备份信息,并转储最新的控制文件
[oracle@gc1 disk5]$ cd /home/oracle/backup/control
[oracle@gc1 control]$ ls -lt
total 15448
-rw-r----- 1 oracle oinstall 7897088 Apr 18 17:47 c-239015079-20140418-01
-rw-r----- 1 oracle oinstall 7897088 Apr 18 17:44 c-239015079-20140418-00
[oracle@gc1 ~]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Fri Apr 18 19:18:51 2014
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: PROD (not mounted)
RMAN> restore controlfile from '/home/oracle/backup/control/c-239015079-20140418-01';
Starting restore at 18-APR-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
output file name=/u01/app/oracle/oradata/PROD/disk1/control01.ctl
output file name=/u01/app/oracle/oradata/PROD/disk2/control01.ctl
output file name=/u01/app/oracle/oradata/PROD/disk3/control01.ctl
Finished restore at 18-APR-14
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
--执行数据库的恢复脚本
RMAN> run{
2> allocate channel c1 type disk;
3> allocate channel c2 type disk;
4> restore database;
5> recover database;
6> alter database open;
7> }
allocated channel: c1
channel c1: SID=21 device type=DISK
allocated channel: c2
channel c2: SID=19 device type=DISK
Starting restore at 18-APR-14
Starting implicit crosscheck backup at 18-APR-14
Crosschecked 5 objects
Finished implicit crosscheck backup at 18-APR-14
Starting implicit crosscheck copy at 18-APR-14
Finished implicit crosscheck copy at 18-APR-14
searching for all files in the recovery area
cataloging files...
no files cataloged
channel c1: starting datafile backup set restore
channel c1: specifying datafile(s) to restore from backup set
channel c1: restoring datafile 00003 to /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
channel c1: restoring datafile 00004 to /u01/app/oracle/oradata/PROD/disk3/users01.dbf
channel c1: restoring datafile 00006 to /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
channel c1: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak
channel c2: starting datafile backup set restore
channel c2: specifying datafile(s) to restore from backup set
channel c2: restoring datafile 00001 to /u01/app/oracle/oradata/PROD/disk3/system01.dbf
channel c2: restoring datafile 00002 to /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
channel c2: restoring datafile 00005 to /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel c2: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak
channel c1: piece handle=/home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak tag=TAG20140418T174636
channel c1: restored backup piece 1
channel c1: restore complete, elapsed time: 00:00:36
channel c2: piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak tag=TAG20140418T174636
channel c2: restored backup piece 1
channel c2: restore complete, elapsed time: 00:00:57
Finished restore at 18-APR-14
Starting recover at 18-APR-14
starting media recovery
archived log for thread 1 with sequence 14 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo02.log
archived log for thread 1 with sequence 15 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo03.log
archived log for thread 1 with sequence 16 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo01.log
archived log file name=/u01/app/oracle/oradata/PROD/disk1/redo02.log thread=1 sequence=14
archived log file name=/u01/app/oracle/oradata/PROD/disk1/redo03.log thread=1 sequence=15
archived log file name=/u01/app/oracle/oradata/PROD/disk1/redo01.log thread=1 sequence=16
media recovery complete, elapsed time: 00:00:06
Finished recover at 18-APR-14
released channel: c1
released channel: c2
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 04/18/2014 19:29:03
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
RMAN> alter database open resetlogs;
database opened
到此,数据库恢复成功。
--查看恢复后,scott.lxtb1表的记录数。
SQL> select count(1) from scott.lxtb1;
  COUNT(1)
----------
        42
由此可见,恢复到最后一次提交。
总结:当所有数据都丢失(控制文件、数据文件)时,采用rman完全恢复的步骤为:
1、通过restore controlfile from方式转储最新的控制文件备份
2、alter database mount
3、restore database
4、recover databse
5、alter database open  resetlogs;
其中3、4、5步骤直接写在一个run语句中,一步执行即可。而转储的文件信息由rman自己决定最优选择,不需要我们人为指定(这里有冷热备的情况,rman优先选择了热备,若想人为干预,你也可以指定用哪个备份)。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例一)步骤为:
1、os级cp转储所有的数据文件
2、采用noresetlogs方式重建控制文件(重建语句从控制文件的trace文件获取)
3、recover databse
4、alter database open
二者对比可知,rman方式更便捷。
案例二、所有数据文件都丢失(控制文件还在)
场景:对scott.lxtb2表分三次插入数据(操作流程同案例一),之后突然断电,然后删除所有的数据文件,在此情况,对数据库做rman完全恢复;
--对scott.lxtb2实验表分三次插入数据
SQL> select count(1) from scott.lxtb2;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb2 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> insert into scott.lxtb2 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb2;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb2 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb2;
  COUNT(1)
----------
        56
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
6 rows selected.
--模拟断电即非法关库
SQL> shutdown abort;
ORACLE instance shut down.
--手工破坏所有数据文件
[oracle@gc1 ~]$ cd /u01/app/oracle/oradata/PROD/disk3
[oracle@gc1 disk3]$ ls
control01.ctl fba01.dbf lxtbs01.dbf system01.dbf users01.dbf
[oracle@gc1 disk3]$ rm *.dbf
[oracle@gc1 disk3]$ cd ../disk4;
[oracle@gc1 disk4]$ ls
sysaux01.dbf temp01.dbf
[oracle@gc1 disk4]$ rm *
[oracle@gc1 disk4]$ cd ../disk5
[oracle@gc1 disk5]$ ls
undotbs01.dbf
[oracle@gc1 disk5]$ rm *
[oracle@gc1 disk5]$ 
--启库报错,查看具体的文件错误信息
SQL> startup;
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u01/app/oracle/oradata/PROD/disk3/system01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 FILE NOT FOUND
         2 FILE NOT FOUND
         3 FILE NOT FOUND
         4 FILE NOT FOUND
         5 FILE NOT FOUND
         6 FILE NOT FOUND
6 rows selected.
--执行rman恢复数据库
RMAN> run{
2> allocate channel c1 type disk;
3> allocate channel c2 type disk;
4> restore database;
5> recover database;
6> alter database open;
7> }
using target database control file instead of recovery catalog
allocated channel: c1
channel c1: SID=18 device type=DISK
allocated channel: c2
channel c2: SID=21 device type=DISK
Starting restore at 19-APR-14
channel c1: starting datafile backup set restore
channel c1: specifying datafile(s) to restore from backup set
channel c1: restoring datafile 00003 to /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
channel c1: restoring datafile 00004 to /u01/app/oracle/oradata/PROD/disk3/users01.dbf
channel c1: restoring datafile 00006 to /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
channel c1: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak
channel c2: starting datafile backup set restore
channel c2: specifying datafile(s) to restore from backup set
channel c2: restoring datafile 00001 to /u01/app/oracle/oradata/PROD/disk3/system01.dbf
channel c2: restoring datafile 00002 to /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
channel c2: restoring datafile 00005 to /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel c2: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak
channel c1: piece handle=/home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak tag=TAG20140418T174636
channel c1: restored backup piece 1
channel c1: restore complete, elapsed time: 00:00:52
channel c2: piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak tag=TAG20140418T174636
channel c2: restored backup piece 1
channel c2: restore complete, elapsed time: 00:01:01
Finished restore at 19-APR-14
Starting recover at 19-APR-14
starting media recovery
archived log for thread 1 with sequence 14 is already on disk as file /home/oracle/PROD/1_14_844605031.dbf
archived log for thread 1 with sequence 15 is already on disk as file /home/oracle/PROD/1_15_844605031.dbf
archived log for thread 1 with sequence 16 is already on disk as file /home/oracle/PROD/1_16_844605031.dbf
archived log for thread 1 with sequence 1 is already on disk as file /home/oracle/PROD/1_1_845235054.dbf
archived log for thread 1 with sequence 2 is already on disk as file /home/oracle/PROD/1_2_845235054.dbf
archived log file name=/home/oracle/PROD/1_14_844605031.dbf thread=1 sequence=14
archived log file name=/home/oracle/PROD/1_15_844605031.dbf thread=1 sequence=15
archived log file name=/home/oracle/PROD/1_16_844605031.dbf thread=1 sequence=16
media recovery complete, elapsed time: 00:00:13
Finished recover at 19-APR-14
database opened
released channel: c1
released channel: c2
RMAN> 
--查看恢复后scott.lxtb2表的记录数
SQL> select count(1) from scott.lxtb2;
  COUNT(1)
----------
        42
由此可见,也是恢复至数据库的最后一次提交。
总结:当所有数据文件都丢失时,采用rman完全恢复的步骤为:
1、restore database
2、recover databse
3、alter database open;
而且这三步放在一个run语句中,直接执行即可。同上例一样,这里不需人为指定要转储的数据文件信息(当然,若想人为干预,可指定转储信息),rman会自己选择最优的转储方式。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例二)步骤为:
1、os级cp转储所有的数据文件
2、recover databse
3、alter database open
二者对比可知,rman方式更便捷。
案例三、部分数据文件丢失,rman完全恢复演练
场景:对scott.lxtb3表分三次插入数据(操作流程同案例一),之后突然断电,然后删除某个数据文件(lxtb01.dbf),在此情况,对数据库做rman完全恢复;
--对scott.lxtb3表分三次插入数据
SQL> select count(1) from scott.lxtb3;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb3 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> select count(1) from scott.lxtb3;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb3 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb3;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb3 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb3;
  COUNT(1)
----------
        56
--查看scott.lxtb3表所在表空间
SQL> select table_name,owner,tablespace_name from dba_tables where table_name like 'LXTB%';
TABLE_NAME OWNER TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
LXTB7 SCOTT LXTBS
LXTB6 SCOTT LXTBS
LXTB5 SCOTT LXTBS
LXTB4 SCOTT LXTBS
LXTB3 SCOTT LXTBS
LXTB1 SCOTT LXTBS
LXTB2 SCOTT LXTBS
LXTB9 SCOTT LXTBS
LXTB8 SCOTT LXTBS
9 rows selected.
--查看数据文件信息
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
6 rows selected.
--模拟断电,非法关库
SQL> shutdown abort;
ORACLE instance shut down.
--删除lxtbs01.dbf
[oracle@gc1 disk5]$ rm /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
--启库报错,查看具体的文件错误信息
SQL> startup;
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复数据库(这次采用指定转储方式恢复被破坏的数据文件)
RMAN> run{
2> restore datafile 5 from tag 'TAG20140418T174338';
3> recover datafile 5;
4> alter database open;
5> }
Starting restore at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
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 00005 to /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/cold_bak/PROD_56_1.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/cold_bak/PROD_56_1.bak tag=TAG20140418T174338
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
archived log for thread 1 with sequence 13 is already on disk as file /home/oracle/PROD/1_13_844605031.dbf
archived log for thread 1 with sequence 14 is already on disk as file /home/oracle/PROD/1_14_844605031.dbf
archived log for thread 1 with sequence 15 is already on disk as file /home/oracle/PROD/1_15_844605031.dbf
archived log for thread 1 with sequence 16 is already on disk as file /home/oracle/PROD/1_16_844605031.dbf
archived log for thread 1 with sequence 1 is already on disk as file /home/oracle/PROD/1_1_845235054.dbf
archived log for thread 1 with sequence 2 is already on disk as file /home/oracle/PROD/1_2_845235054.dbf
archived log for thread 1 with sequence 3 is already on disk as file /home/oracle/PROD/1_3_845235054.dbf
archived log for thread 1 with sequence 4 is already on disk as file /home/oracle/PROD/1_4_845235054.dbf
archived log file name=/home/oracle/PROD/1_13_844605031.dbf thread=1 sequence=13
archived log file name=/home/oracle/PROD/1_14_844605031.dbf thread=1 sequence=14
archived log file name=/home/oracle/PROD/1_15_844605031.dbf thread=1 sequence=15
archived log file name=/home/oracle/PROD/1_16_844605031.dbf thread=1 sequence=16
archived log file name=/home/oracle/PROD/1_1_845235054.dbf thread=1 sequence=1
archived log file name=/home/oracle/PROD/1_2_845235054.dbf thread=1 sequence=2
media recovery complete, elapsed time: 00:00:02
Finished recover at 19-APR-14
database opened
注意:这次转储数据文件时,指定了转储冷备的信息,从打印的恢复日志可知,用的是冷备(前面rman自选的是热备)。当然,如果不指定的话,直接用restore datafile 5即可。
--查看恢复后scott.lxtb3的记录数
SQL> select count(1) from scott.lxtb3;
  COUNT(1)
----------
        42
由此可见,也是恢复到最后一次提交。
总结:当某个数据文件丢失时,采用rman完全恢复的步骤为:
1、restore datafile 数据文件号(通过select file#,error from v$recover_file查询获取)
2、recover datafile 数据文件号
3、alter database open;
而且这三步放在一个run语句中,直接执行即可。还可使用restore/recover tablespace方式进行恢复,上面案例恢复演示过程中,指定了转储的备份信息,通常我们可以不指定,rman会自己选择最优方式转储,在这只是为了演示可以采取指定的方式。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例三)步骤为:
1、os级cp转储被破坏的数据文件
2、recover tablespace 表空间名/recover datafile 数据文件号(注意:若此表空间对应多个数据文件则采用recover tablespace方式,否则选recover datafile方式)
3、alter database open
二者对比可知,优选rman方式。
案例四、部分数据文件丢失,rman完全恢复演练(高可用模式)
场景:此场景同案例三(对scott.lxtb4表分三次插入数据(操作流程同案例一),之后突然断电,然后删除数据文件lxtb01.dbf),只是恢复时采用高可用模式(即先开库,再恢复);
--对scott.lxtb4表分三次插入数据
SQL> select count(1) from scott.lxtb4;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb4 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> select count(1) from scott.lxtb4;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb4 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb4;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb4 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb4;
  COUNT(1)
----------
        56
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
6 rows selected.
--非法关库
SQL> shutdown abort;
ORACLE instance shut down.
--手工删除lxtb01.dbf数据文件
[oracle@gc1 disk5]$ rm /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
--启库失败,查看文件错误信息
SQL> startup
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复
RMAN> run{
2> sql 'alter database datafile 5 offline';
3> alter database open;
4> restore datafile 5;
5> recover datafile 5;
6> sql 'alter database datafile 5 online';
7> }
using target database control file instead of recovery catalog
sql statement: alter database datafile 5 offline
database opened
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=18 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=35 device type=DISK
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 00005 to /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak tag=TAG20140418T174636
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
archived log for thread 1 with sequence 14 is already on disk as file /home/oracle/PROD/1_14_844605031.dbf
archived log for thread 1 with sequence 15 is already on disk as file /home/oracle/PROD/1_15_844605031.dbf
archived log for thread 1 with sequence 16 is already on disk as file /home/oracle/PROD/1_16_844605031.dbf
archived log for thread 1 with sequence 1 is already on disk as file /home/oracle/PROD/1_1_845235054.dbf
archived log for thread 1 with sequence 2 is already on disk as file /home/oracle/PROD/1_2_845235054.dbf
archived log for thread 1 with sequence 3 is already on disk as file /home/oracle/PROD/1_3_845235054.dbf
archived log for thread 1 with sequence 4 is already on disk as file /home/oracle/PROD/1_4_845235054.dbf
archived log for thread 1 with sequence 5 is already on disk as file /home/oracle/PROD/1_5_845235054.dbf
archived log for thread 1 with sequence 6 is already on disk as file /home/oracle/PROD/1_6_845235054.dbf
archived log for thread 1 with sequence 7 is already on disk as file /home/oracle/PROD/1_7_845235054.dbf
archived log file name=/home/oracle/PROD/1_14_844605031.dbf thread=1 sequence=14
archived log file name=/home/oracle/PROD/1_15_844605031.dbf thread=1 sequence=15
archived log file name=/home/oracle/PROD/1_16_844605031.dbf thread=1 sequence=16
archived log file name=/home/oracle/PROD/1_1_845235054.dbf thread=1 sequence=1
archived log file name=/home/oracle/PROD/1_2_845235054.dbf thread=1 sequence=2
archived log file name=/home/oracle/PROD/1_3_845235054.dbf thread=1 sequence=3
archived log file name=/home/oracle/PROD/1_4_845235054.dbf thread=1 sequence=4
archived log file name=/home/oracle/PROD/1_5_845235054.dbf thread=1 sequence=5
media recovery complete, elapsed time: 00:00:04
Finished recover at 19-APR-14
sql statement: alter database datafile 5 online
RMAN>
--查看恢复后,scott.lxtb4表的记录数
SQL> select count(1) from scott.lxtb4;
  COUNT(1)
----------
        42
注意:本案例的恢复方式同上一案例差不多,只是先将破坏的数据文件offline,开库后再做恢复。但要注意若system与undo表空间对应的数据文件破坏了,不能采用此方式进行恢复,因为他们不能offline。 
另外,此案例可以在若在不关库的情况下,做数据文件的删除破坏,则要体现报错,需要执行alter system flush buffer_cache清理缓存。恢复时,只要去掉开库命令即可,在此不演示了。
总结:当某个数据文件丢失时,采用rman高可用模式进行完全恢复的步骤为:
1、sql 'alter database datafile 数据文件号 offline';
2、alter database open;
3、restore datafile 数据文件号(通过select file#,error from v$recover_file查询获取)
4、recover datafile 数据文件号
5、sql 'alter database datafile 数据文件号 online';
所有步骤放一个run语句中,直接执行即可。system、undo表空间数据文件损坏不能采用此方式进行恢复,只能用案例三的一般模式进行恢复。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例四)步骤为:
1、alter database datafile 数据文件号 offline;
2、alter database open;
3、os级cp转储被破坏的数据文件;
4、recover  datafile 数据文件号;
5、alter database datafile 数据文件号 online;
二者对比可知,优选rman方式。
案例五、新建表空间对应的数据文件丢失,执行rman完全恢复
场景:新建表空间zhentbs,然后在其上新建表zhentb,并对zhentb表分三次插入数据,然后模拟断电,非法关库,删除zhentbs对应的数据文件,在此情况下,做rman的完全恢复(因是新建的表空间,故没有相关的数据文件备份)。
--查看当前表空间及数据文件信息
SQL> select tablespace_name,contents,status from dba_tablespaces;
TABLESPACE_NAME CONTENTS STATUS
------------------------------ --------- ---------
SYSTEM PERMANENT ONLINE
SYSAUX PERMANENT ONLINE
UNDOTBS UNDO ONLINE
TEMPTS TEMPORARY ONLINE
USERS PERMANENT ONLINE
LXTBS PERMANENT ONLINE
FBA PERMANENT ONLINE
7 rows selected.
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
6 rows selected.
--创建表空间zhentbs,并在其上面新建表zhentb
SQL> create tablespace zhentbs datafile '/u01/app/oracle/oradata/PROD/disk4/zhen01.dbf' size 50m;
Tablespace created.
SQL> create table zhentb tablespace zhentbs as select * from scott.emp;
Table created.
SQL> select count(1) from zhentb;
  COUNT(1)
----------
        14
--对zhentbs表分三次插入数据
SQL> insert into zhentb select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> select count(1) from zhentb;
  COUNT(1)
----------
        28
SQL> insert into zhentb select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from zhentb;
  COUNT(1)
----------
        42
SQL> insert into zhentb select * from scott.emp;
14 rows created.
SQL> select count(1) from zhentb;
  COUNT(1)
----------
        56
--非法关库,并删除刚新建表空间对应的数据文件zhen01.dbf
SQL> shutdown abort;
ORACLE instance shut down.
[oracle@gc1 disk5]$ rm /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
--启库失败,查看具体数据文件错误信息
SQL> startup
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 7 - see DBWR trace file
ORA-01110: data file 7: '/u01/app/oracle/oradata/PROD/disk4/zhen01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 UNKNOWN ERROR
         6 UNKNOWN ERROR
         7 FILE NOT FOUND
7 rows selected.
--采用高可用模式执行rman恢复数据库
RMAN> run{
2> sql 'alter database datafile 7 offline';
3> alter database open;
4> restore datafile 7;
5> recover datafile 7;
6> sql 'alter database datafile 7 online';
7> }
using target database control file instead of recovery catalog
sql statement: alter database datafile 7 offline
database opened
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=18 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=33 device type=DISK
creating datafile file number=7 name=/u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
restore not done; all files read only, offline, or already restored
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
media recovery complete, elapsed time: 00:00:05
Finished recover at 19-APR-14
sql statement: alter database datafile 7 online
RMAN> 
从恢复日志可知,虽然没有新建的zhen01.dbf的备份信息,但使用restore datafile语句时,rman实际上是做了create datafile 操作,与之前的手工恢复类似。
--查看恢复后zhentb表的记录数
SQL> select count(1) from zhentb;
  COUNT(1)
----------
        42
总结:当新建表空间对应数据文件丢失时,采用rman恢复步骤:
1、sql 'alter database datafile 数据文件号 offline';
2、alter database open;
3、restore datafile 数据文件号;
4、recover datafile 数据文件号;
5、sql 'alter database datafile 数据文件号 online';
注意:此案例恢复方式实际与上一案例的高可用模式进行完全恢复的步骤一样,只是restore datafile时并非真正转储备份的数据文件,而是通过create datafile方式新建数据文件。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例五)步骤为:
1、alter database datafile 数据文件号 offline;
2、alter database open;
3、alter database create datafile 数据文件存放路径(与之前指定的路径一样);
4、recover  datafile 数据文件号;
5、alter database datafile 数据文件号 online;
二者对比可知,rman方式实际也是转储时做了create datafile操作,只不过交由rman来做,不需要人工创建,更省事些。
案例六:某个数据文件丢失且所在磁盘坏掉时,执行rman的完全恢复
场景:对scott.lxtb5表分三次插入数据(操作流程同案例一),之后突然断电,然后删除对应数据文件(lxtb01.dbf)且其所在碰盘坏掉了,此情况下,做rman的完全恢复。
--对scott.lxtb5表分三次插入数据
SQL> select count(1) from scott.lxtb5;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb5 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.

SQL> select count(1) from scott.lxtb5;
  COUNT(1)
----------
        28 
SQL> insert into scott.lxtb5 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb5;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb5 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb5;
  COUNT(1)
----------
        56
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
         7 /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
7 rows selected.
--非法关库
SQL> shutdown abort;
ORACLE instance shut down.
--删除此表所在的数据文件
[oracle@gc1 disk5]$ rm /u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf
--启库失败,查看具体错误信息
SQL> startup
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk3/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复
RMAN> run{
2> sql 'alter database datafile 5 offline';
3> alter database open;
4> set newname for datafile 5 to '/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf';
5> restore datafile 5;
6> switch datafile 5;
7> recover datafile 5;
8> sql 'alter database datafile 5 online';
9> }
using target database control file instead of recovery catalog
sql statement: alter database datafile 5 offline
database opened
executing command: SET NEWNAME
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=30 device type=DISK
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 00005 to /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak tag=TAG20140418T174636
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:09
Finished restore at 19-APR-14
datafile 5 switched to datafile copy
input datafile copy RECID=6 STAMP=845286991 file name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
archived log for thread 1 with sequence 14 is already on disk as file /home/oracle/PROD/1_14_844605031.dbf
archived log for thread 1 with sequence 15 is already on disk as file /home/oracle/PROD/1_15_844605031.dbf
archived log for thread 1 with sequence 16 is already on disk as file /home/oracle/PROD/1_16_844605031.dbf
archived log for thread 1 with sequence 1 is already on disk as file /home/oracle/PROD/1_1_845235054.dbf
archived log for thread 1 with sequence 2 is already on disk as file /home/oracle/PROD/1_2_845235054.dbf
archived log for thread 1 with sequence 3 is already on disk as file /home/oracle/PROD/1_3_845235054.dbf
archived log for thread 1 with sequence 4 is already on disk as file /home/oracle/PROD/1_4_845235054.dbf
archived log for thread 1 with sequence 5 is already on disk as file /home/oracle/PROD/1_5_845235054.dbf
archived log for thread 1 with sequence 6 is already on disk as file /home/oracle/PROD/1_6_845235054.dbf
archived log for thread 1 with sequence 7 is already on disk as file /home/oracle/PROD/1_7_845235054.dbf
archived log for thread 1 with sequence 8 is already on disk as file /home/oracle/PROD/1_8_845235054.dbf
archived log for thread 1 with sequence 9 is already on disk as file /home/oracle/PROD/1_9_845235054.dbf
archived log for thread 1 with sequence 10 is already on disk as file /home/oracle/PROD/1_10_845235054.dbf
archived log for thread 1 with sequence 11 is already on disk as file /home/oracle/PROD/1_11_845235054.dbf
archived log for thread 1 with sequence 12 is already on disk as file /home/oracle/PROD/1_12_845235054.dbf
archived log for thread 1 with sequence 13 is already on disk as file /home/oracle/PROD/1_13_845235054.dbf
archived log for thread 1 with sequence 14 is already on disk as file /home/oracle/PROD/1_14_845235054.dbf
archived log for thread 1 with sequence 15 is already on disk as file /home/oracle/PROD/1_15_845235054.dbf
archived log file name=/home/oracle/PROD/1_14_844605031.dbf thread=1 sequence=14
archived log file name=/home/oracle/PROD/1_15_844605031.dbf thread=1 sequence=15
archived log file name=/home/oracle/PROD/1_16_844605031.dbf thread=1 sequence=16
archived log file name=/home/oracle/PROD/1_1_845235054.dbf thread=1 sequence=1
archived log file name=/home/oracle/PROD/1_2_845235054.dbf thread=1 sequence=2
archived log file name=/home/oracle/PROD/1_3_845235054.dbf thread=1 sequence=3
archived log file name=/home/oracle/PROD/1_4_845235054.dbf thread=1 sequence=4
archived log file name=/home/oracle/PROD/1_5_845235054.dbf thread=1 sequence=5
archived log file name=/home/oracle/PROD/1_6_845235054.dbf thread=1 sequence=6
archived log file name=/home/oracle/PROD/1_7_845235054.dbf thread=1 sequence=7
archived log file name=/home/oracle/PROD/1_8_845235054.dbf thread=1 sequence=8
archived log file name=/home/oracle/PROD/1_9_845235054.dbf thread=1 sequence=9
archived log file name=/home/oracle/PROD/1_10_845235054.dbf thread=1 sequence=10
archived log file name=/home/oracle/PROD/1_11_845235054.dbf thread=1 sequence=11
archived log file name=/home/oracle/PROD/1_12_845235054.dbf thread=1 sequence=12
media recovery complete, elapsed time: 00:00:05
Finished recover at 19-APR-14
sql statement: alter database datafile 5 online
RMAN> 
注意:这里转储数据文件至新位置是采用set newname 的方式,然后更新控制文件记录信息是用switch datafile方式。
--查看恢复后scott.lxtb5表的记录数
SQL> select count(1) from scott.lxtb5;
  COUNT(1)
----------
        42
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
         7 /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
7 rows selected.
总结:当被破坏的数据文件其所在磁盘都坏掉时,采用rman恢复步骤为:
1、sql 'alter database datafile 数据文件号 offline';
2、alter database open;
3、set newname for datafile 数据文件号 to ’数据文件新存放路径‘;
4、restore datafile 数据文件号;
5、switch datafile 数据文件号;
4、recover datafile 数据文件号;
5、sql 'alter database datafile 数据文件号 online';
注意:此案例恢复方式实际也是采用高可用模式,只是转储数据文件时采用set newname将其定义为新位置,后又使用switch datafile来更改控制文件记录的数据文件信息。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例六)步骤为:
1、alter database datafile 数据文件号 offline;
2、alter database open;
3、os级cp转储被破坏数据文件至新位置
4、alter database rename file 旧位置 to 新位置;
4、recover  datafile 数据文件号;
5、alter database datafile 数据文件号 online;
由此可见,rman与手工恢复方式,其原理是一样的,都是先将数据文件转储至新位置,然后更改控制文件记录的相关数据文件信息。再做recover操作。

接下来演示二个案例分别是采用copy与增量备份信息进行恢复,这二案例与手工完全恢复部分内容无对应案例相比较。因此只是单纯的演示,不做太多讲解。在这二案例演示这前,先做copy镜像备份与增量备份。
--copy备份
RMAN> backup as copy datafile 5 format '/home/oracle/rman_bak/copy/lxtb01.bak';
Starting backup at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
output file name=/home/oracle/rman_bak/copy/lxtb01.bak tag=TAG20140419T101545 RECID=7 STAMP=845288152
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:08
Finished backup at 19-APR-14
Starting Control File Autobackup at 19-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140419-04 comment=NONE
Finished Control File Autobackup at 19-APR-14
RMAN> list copy;
specification does not match any control file copy in the repository
List of Datafile Copies
=======================
Key File S Completion Time Ckp SCN Ckp Time
------- ---- - --------------- ---------- ---------------
7 5 A 19-APR-14 899645 19-APR-14
        Name: /home/oracle/rman_bak/copy/lxtb01.bak
        Tag: TAG20140419T101545
List of Archived Log Copies for database with db_unique_name PROD
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
38 1 13 A 18-APR-14
        Name: /home/oracle/PROD/1_13_844605031.dbf
46 1 14 A 18-APR-14
        Name: /home/oracle/PROD/1_14_844605031.dbf
47 1 15 A 18-APR-14
        Name: /home/oracle/PROD/1_15_844605031.dbf
45 1 16 A 18-APR-14
        Name: /home/oracle/PROD/1_16_844605031.dbf
……
注意:以上备份命令还可采用copy datafile 5 to '/home/oracle/rman_bak/copy/lxtb01.bak';简单方式进行镜像备份。
案例七、在有镜像备份的前提下,删除某个数据文件,执行rman备份。
场景:此场景与案例四一模一样,只是看rman恢复时,在有冷备、热备及镜像备份时,会选择哪种备份信息进行恢复,着重关注恢复打印信息。
SQL> select count(1) from scott.lxtb6;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb6 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system archive log current;
System altered.
SQL> select count(1) from scott.lxtb6;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb6 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb6;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb6 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb6;
  COUNT(1)
----------
        56
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
         7 /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
7 rows selected.
SQL> shutdown abort;
ORACLE instance shut down.
[oracle@gc1 copy]$ rm /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
SQL> startup
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复
RMAN> run{
2> sql 'alter database datafile 5 offline';
3> alter database open;
4> restore datafile 5;
5> recover datafile 5;
6> sql 'alter database datafile 5 online';
7> }
using target database control file instead of recovery catalog
sql statement: alter database datafile 5 offline
database opened
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=33 device type=DISK
channel ORA_DISK_1: restoring datafile 00005
input datafile copy RECID=7 STAMP=845288152 file name=/home/oracle/rman_bak/copy/lxtb01.bak
destination for restore of datafile 00005: /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: copied datafile copy of datafile 00005
output file name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf RECID=0 STAMP=0
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
media recovery complete, elapsed time: 00:00:03
Finished recover at 19-APR-14
sql statement: alter database datafile 5 online
从以上恢复信息可知,在有冷备、热备及copy镜像备份信息时,rman会优先选择copy备份信息进行恢复,这是因为利用copy备份信息进行恢复时会比较快。
--查看恢复后scott.lxtb6的记录数
SQL> select count(1) from scott.lxtb6;
  COUNT(1)
----------
        42
案例八、在有增量备份的情况下,删除某个数据文件,执行rman完全恢复。
场景:与案例7一样,只是多了增量备份信息。下面先做增量备份(上个案例的copy备份作为0级备份)
--差异增量备份
RMAN> backup incremental level 2 format '/home/oracle/rman_bak/hot_bak/lxtbs_%s_2.bak' tablespace lxtbs;
Starting backup at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
channel ORA_DISK_1: starting compressed incremental level 2 datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: starting piece 1 at 19-APR-14
channel ORA_DISK_1: finished piece 1 at 19-APR-14
piece handle=/home/oracle/rman_bak/hot_bak/lxtbs_70_2.bak tag=TAG20140419T103525 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 19-APR-14
Starting Control File Autobackup at 19-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140419-06 comment=NONE
Finished Control File Autobackup at 19-APR-14。
--累计增量备份
RMAN> backup incremental level 1 cumulative format '/home/oracle/rman_bak/hot_bak/lxtbs_%s_cu1.bak' datafile 5;
Starting backup at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
channel ORA_DISK_1: starting compressed incremental level 1 datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: starting piece 1 at 19-APR-14
channel ORA_DISK_1: finished piece 1 at 19-APR-14
piece handle=/home/oracle/rman_bak/hot_bak/lxtbs_72_cu1.bak tag=TAG20140419T104127 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 19-APR-14
Starting Control File Autobackup at 19-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140419-07 comment=NONE
Finished Control File Autobackup at 19-APR-14

--演示表为scott.lxtb7,数据插入及数据文件破坏与上一案例一模一样,在此省略,只附上rman恢复部分内容,如下:
RMAN> run{
2> sql 'alter database datafile 5 offline';
3> alter database open;
4> restore datafile 5;
5> recover datafile 5;
6> sql 'alter database datafile 5 online';
7> }
using target database control file instead of recovery catalog
sql statement: alter database datafile 5 offline
database opened
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=35 device type=DISK
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 00005 to /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/hot_bak/lxtbs_72_cu1.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/hot_bak/lxtbs_72_cu1.bak tag=TAG20140419T104127
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 19-APR-14
sql statement: alter database datafile 5 online
--查看恢复后scott.lxtb7的记录数
SQL> select count(1) from scott.lxtb7;
  COUNT(1)
----------
        42
由此可见,在有冷备、热备、copy备份及差异增量备份与累计增量备份时,rman会优先选择累计增量备份来做恢复。
以上八个案例都是在归档模式下执行rman的恢复,下面讲二个例子是在非归档模式下,进行rman的恢复。
一、非归档模式,执行rman的恢复
案例演示前,先关归档
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
SQL> alter database noarchivelog;
alter database noarchivelog
*
ERROR at line 1:
ORA-38774: cannot disable media recovery - flashback database is enabled
注意:从以上错误信息可知,在开闪回的情况下,要关归档的话,必须先关闪回再关归档。
SQL> alter database flashback off;
Database altered.
SQL> alter database noarchivelog;
Database altered.
SQL> alter database open;
Database altered.
--二种方式查看归档模式
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination /home/oracle/PROD
Oldest online log sequence 18
Current log sequence 20
SQL> select log_mode from v$database;
LOG_MODE
------------
NOARCHIVELOG
--因切换了归档模式,在此需要重新做备份(先删原备份,后做冷备,注意非归档模式不能做热备)
RMAN> delete noprompt backup;
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=35 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=38 device type=DISK
List of Backup Pieces
BP Key BS Key Pc# Cp# Status Device Type Piece Name
------- ------- --- --- ----------- ----------- ----------
48 48 1 1 AVAILABLE DISK /home/oracle/rman_bak/cold_bak/PROD_55_1.bak
49 49 1 1 AVAILABLE DISK /home/oracle/rman_bak/cold_bak/PROD_56_1.bak
50 50 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140418-00
51 51 1 1 AVAILABLE DISK /home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak
52 52 1 1 AVAILABLE DISK /home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak
53 53 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140418-02
54 54 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-00
55 55 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-01
56 56 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-02
57 57 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-03
58 58 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-04
59 59 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-05
60 60 1 1 AVAILABLE DISK /home/oracle/rman_bak/hot_bak/lxtbs_70_2.bak
61 61 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-06
62 62 1 1 AVAILABLE DISK /home/oracle/rman_bak/hot_bak/lxtbs_72_cu1.bak
63 63 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-07
64 64 1 1 AVAILABLE DISK /home/oracle/backup/control/c-239015079-20140419-08
deleted backup piece
backup piece handle=/home/oracle/rman_bak/cold_bak/PROD_55_1.bak RECID=48 STAMP=845228619
deleted backup piece
backup piece handle=/home/oracle/rman_bak/cold_bak/PROD_56_1.bak RECID=49 STAMP=845228620
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140418-00 RECID=50 STAMP=845228660
deleted backup piece
backup piece handle=/home/oracle/rman_bak/hot_bak/PROD_58_1qp62ant.bak RECID=51 STAMP=845228798
deleted backup piece
backup piece handle=/home/oracle/rman_bak/hot_bak/PROD_59_1rp62ant.bak RECID=52 STAMP=845228798
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140418-02 RECID=53 STAMP=845235110
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-00 RECID=54 STAMP=845276866
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-01 RECID=55 STAMP=845283600
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-02 RECID=56 STAMP=845284193
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-03 RECID=57 STAMP=845287393
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-04 RECID=58 STAMP=845288155
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-05 RECID=59 STAMP=845289322
deleted backup piece
backup piece handle=/home/oracle/rman_bak/hot_bak/lxtbs_70_2.bak RECID=60 STAMP=845289326
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-06 RECID=61 STAMP=845289329
deleted backup piece
backup piece handle=/home/oracle/rman_bak/hot_bak/lxtbs_72_cu1.bak RECID=62 STAMP=845289687
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-07 RECID=63 STAMP=845289688
deleted backup piece
backup piece handle=/home/oracle/backup/control/c-239015079-20140419-08 RECID=64 STAMP=845290442
Deleted 17 objects
RMAN> delete noprompt copy;
released channel: ORA_DISK_1
released channel: ORA_DISK_2
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=35 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=38 device type=DISK
specification does not match any control file copy in the repository
List of Datafile Copies
=======================
Key File S Completion Time Ckp SCN Ckp Time
------- ---- - --------------- ---------- ---------------
7 5 A 19-APR-14 899645 19-APR-14
        Name: /home/oracle/rman_bak/copy/lxtb01.bak
        Tag: TAG20140419T101545
List of Archived Log Copies for database with db_unique_name PROD
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
38 1 13 A 18-APR-14
        Name: /home/oracle/PROD/1_13_844605031.dbf
46 1 14 A 18-APR-14
        Name: /home/oracle/PROD/1_14_844605031.dbf
47 1 15 A 18-APR-14
        Name: /home/oracle/PROD/1_15_844605031.dbf
45 1 16 A 18-APR-14
        Name: /home/oracle/PROD/1_16_844605031.dbf
48 1 1 A 18-APR-14
        Name: /home/oracle/PROD/1_1_845235054.dbf
49 1 2 A 19-APR-14
        Name: /home/oracle/PROD/1_2_845235054.dbf
50 1 3 A 19-APR-14
        Name: /home/oracle/PROD/1_3_845235054.dbf
51 1 4 A 19-APR-14
        Name: /home/oracle/PROD/1_4_845235054.dbf
52 1 5 A 19-APR-14
        Name: /home/oracle/PROD/1_5_845235054.dbf
53 1 6 A 19-APR-14
        Name: /home/oracle/PROD/1_6_845235054.dbf
54 1 7 A 19-APR-14
        Name: /home/oracle/PROD/1_7_845235054.dbf
55 1 8 A 19-APR-14
        Name: /home/oracle/PROD/1_8_845235054.dbf
56 1 9 A 19-APR-14
        Name: /home/oracle/PROD/1_9_845235054.dbf
57 1 10 A 19-APR-14
        Name: /home/oracle/PROD/1_10_845235054.dbf
58 1 11 A 19-APR-14
        Name: /home/oracle/PROD/1_11_845235054.dbf
59 1 12 A 19-APR-14
        Name: /home/oracle/PROD/1_12_845235054.dbf
60 1 13 A 19-APR-14
        Name: /home/oracle/PROD/1_13_845235054.dbf
61 1 14 A 19-APR-14
        Name: /home/oracle/PROD/1_14_845235054.dbf
62 1 15 A 19-APR-14
        Name: /home/oracle/PROD/1_15_845235054.dbf
63 1 16 A 19-APR-14
        Name: /home/oracle/PROD/1_16_845235054.dbf
64 1 17 A 19-APR-14
        Name: /home/oracle/PROD/1_17_845235054.dbf
65 1 18 A 19-APR-14
        Name: /home/oracle/PROD/1_18_845235054.dbf
66 1 19 A 19-APR-14
        Name: /home/oracle/PROD/1_19_845235054.dbf
deleted datafile copy
datafile copy file name=/home/oracle/rman_bak/copy/lxtb01.bak RECID=7 STAMP=845288152
deleted archived log
archived log file name=/home/oracle/PROD/1_13_844605031.dbf RECID=38 STAMP=845228797
deleted archived log
archived log file name=/home/oracle/PROD/1_14_844605031.dbf RECID=46 STAMP=845235054
deleted archived log
archived log file name=/home/oracle/PROD/1_15_844605031.dbf RECID=47 STAMP=845235054
deleted archived log
archived log file name=/home/oracle/PROD/1_16_844605031.dbf RECID=45 STAMP=845235054
deleted archived log
archived log file name=/home/oracle/PROD/1_1_845235054.dbf RECID=48 STAMP=845270033
deleted archived log
archived log file name=/home/oracle/PROD/1_2_845235054.dbf RECID=49 STAMP=845270150
deleted archived log
archived log file name=/home/oracle/PROD/1_3_845235054.dbf RECID=50 STAMP=845270642
deleted archived log
archived log file name=/home/oracle/PROD/1_4_845235054.dbf RECID=51 STAMP=845273384
deleted archived log
archived log file name=/home/oracle/PROD/1_5_845235054.dbf RECID=52 STAMP=845274247
deleted archived log
archived log file name=/home/oracle/PROD/1_6_845235054.dbf RECID=53 STAMP=845275733
deleted archived log
archived log file name=/home/oracle/PROD/1_7_845235054.dbf RECID=54 STAMP=845276179
deleted archived log
archived log file name=/home/oracle/PROD/1_8_845235054.dbf RECID=55 STAMP=845282921
deleted archived log
archived log file name=/home/oracle/PROD/1_9_845235054.dbf RECID=56 STAMP=845283570
deleted archived log
archived log file name=/home/oracle/PROD/1_10_845235054.dbf RECID=57 STAMP=845285618
deleted archived log
archived log file name=/home/oracle/PROD/1_11_845235054.dbf RECID=58 STAMP=845286345
deleted archived log
archived log file name=/home/oracle/PROD/1_12_845235054.dbf RECID=59 STAMP=845286560
deleted archived log
archived log file name=/home/oracle/PROD/1_13_845235054.dbf RECID=60 STAMP=845286711
deleted archived log
archived log file name=/home/oracle/PROD/1_14_845235054.dbf RECID=61 STAMP=845286827
deleted archived log
archived log file name=/home/oracle/PROD/1_15_845235054.dbf RECID=62 STAMP=845286955
deleted archived log
archived log file name=/home/oracle/PROD/1_16_845235054.dbf RECID=63 STAMP=845288665
deleted archived log
archived log file name=/home/oracle/PROD/1_17_845235054.dbf RECID=64 STAMP=845288956
deleted archived log
archived log file name=/home/oracle/PROD/1_18_845235054.dbf RECID=65 STAMP=845289795
deleted archived log
archived log file name=/home/oracle/PROD/1_19_845235054.dbf RECID=66 STAMP=845290001
Deleted 24 objects
RMAN> list backup;
specification does not match any backup in the repository
RMAN> list copy;
specification does not match any datafile copy in the repository
specification does not match any control file copy in the repository
specification does not match any archived log in the repository
--执行冷备
RMAN> run{
2> shutdown immediate;
3> startup mount;
4> allocate channel c1 type disk;
allocate channel c2 type disk;
backup database format'/home/oracle/rman_bak/cold_bak/%d_%s_%p.bak';
5> 6> 7> alter database open;
8> }
database closed
database dismounted
Oracle instance shut down
connected to target database (not started)
Oracle instance started
database mounted
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
allocated channel: c1
channel c1: SID=21 device type=DISK
allocated channel: c2
channel c2: SID=19 device type=DISK
Starting backup at 19-APR-14
channel c1: starting compressed full datafile backup set
channel c1: specifying datafile(s) in backup set
input datafile file number=00004 name=/u01/app/oracle/oradata/PROD/disk3/users01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
input datafile file number=00006 name=/u01/app/oracle/oradata/PROD/disk3/fba01.dbf
channel c1: starting piece 1 at 19-APR-14
channel c2: starting compressed full datafile backup set
channel c2: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/PROD/disk3/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
input datafile file number=00007 name=/u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
channel c2: starting piece 1 at 19-APR-14
channel c1: finished piece 1 at 19-APR-14
piece handle=/home/oracle/rman_bak/cold_bak/PROD_75_1.bak tag=TAG20140419T110458 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:17
channel c2: finished piece 1 at 19-APR-14
piece handle=/home/oracle/rman_bak/cold_bak/PROD_76_1.bak tag=TAG20140419T110458 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:37
Finished backup at 19-APR-14
Starting Control File Autobackup at 19-APR-14
piece handle=/home/oracle/backup/control/c-239015079-20140419-09 comment=NONE
Finished Control File Autobackup at 19-APR-14
database opened
released channel: c1
released channel: c2
到此,非归档模式,rman恢复的准备工作已做完。下面开始演练二种场景的rman恢复。
案例九、非归档模式下,某个数据文件丢失,日志未覆盖情况下做rman完全恢复。
场景:与归档模式下,某个数据文件丢失,采用rman一般模式恢复场景一样,同以上案例三。
--对scott.lxtb8分三次插入数据
SQL> select count(1) from scott.lxtb8;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb8 select * from scott.emp;
c
14 rows created.
SQL>commit;
Commit complete.
SQL> alter database archive log current;
alter database archive log current
                       *
ERROR at line 1:
ORA-02231: missing or invalid option to ALTER DATABASE
注意:非归档模式,不能采用此方式进行归档,改用switch logfile方式切换日志
SQL> alter system switch logfile;
System altered.
SQL> select count(1) from scott.lxtb8;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb8 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb8;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb8 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb8;
  COUNT(1)
----------
        56
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
         7 /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
7 rows selected.
SQL> shutdown abort;
ORACLE instance shut down.
SQL> 
--删除数据文件后启库失败,并查看具体错误信息
[oracle@gc1 rman_bak]$ rm /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
SQL> startup;
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复
RMAN> run{
2> restore datafile 5;
3> recover datafile 5;
4> alter database open;
5> }
Starting restore at 19-APR-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=18 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=20 device type=DISK
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 00005 to /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/cold_bak/PROD_76_1.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/cold_bak/PROD_76_1.bak tag=TAG20140419T110458
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
Finished restore at 19-APR-14
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 19-APR-14
database opened
由此可见,此恢复方式,与归档模式下,某数据文件丢失,一般模式恢复一样,只是这里不指定具体的转储备份信息。
总结:非归档模式下,当某个数据文件丢失时且日志没有被覆盖的情况下,采用rman完全恢复的步骤为:
1、restore datafile 数据文件号(通过select file#,error from v$recover_file查询获取)
2、recover datafile 数据文件号
3、alter database open;
恢复方式,与归档模式下的一般恢复模式一样。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例七)步骤为:
1、os级cp转储被破坏的数据文件
2、recover datafile 数据文件号
3、alter database open
注意:此误操作场景,无论是rman还是手工恢复,若是在归档模式下,还可使用高可用模式进行恢复,但非归档模式下,只能利用一般模式进行恢复,因为非归档模式无法对数据文件进行offline操作。
案例十、非归档模式下,某个数据文件丢失,日志被覆盖情况下做rman完全恢复。
场景:与上一案例场景一样,只是对实验表三次插入数据后,进行四次日志组切换,从而达到日志被覆盖的目的,此情况下执行rman恢复
SQL> select count(1) from scott.lxtb9;
  COUNT(1)
----------
        14
SQL> insert into scott.lxtb9 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> alter system switch logfile;
System altered.
SQL> select count(1) from scott.lxtb9;
  COUNT(1)
----------
        28
SQL> insert into scott.lxtb9 select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(1) from scott.lxtb9;
  COUNT(1)
----------
        42
SQL> insert into scott.lxtb9 select * from scott.emp;
14 rows created.
SQL> select count(1) from scott.lxtb9;
  COUNT(1)
----------
        56
SQL> select * from v$log;
    GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC STATUS FIRST_CHANGE#
---------- ---------- ---------- ---------- ---------- ---------- --- ---------------- -------------
FIRST_TIM NEXT_CHANGE# NEXT_TIME
--------- ------------ ---------
         1 1 22 104857600 512 2 NO ACTIVE 961286
19-APR-14 961500 19-APR-14
         2 1 23 104857600 512 2 NO CURRENT 961500
19-APR-14 2.8147E+14
         3 1 21 104857600 512 2 NO INACTIVE 941275
19-APR-14 961286 19-APR-14
SQL> col member for a50;
SQL> select * from v$logfile;
    GROUP# STATUS TYPE MEMBER IS_
---------- ------- ------- -------------------------------------------------- ---
         1 ONLINE /u01/app/oracle/oradata/PROD/disk1/redo01.log NO
         1 ONLINE /u01/app/oracle/oradata/PROD/disk2/redo01.log NO
         2 ONLINE /u01/app/oracle/oradata/PROD/disk1/redo02.log NO
         2 ONLINE /u01/app/oracle/oradata/PROD/disk2/redo02.log NO
         3 ONLINE /u01/app/oracle/oradata/PROD/disk1/redo03.log NO
         3 ONLINE /u01/app/oracle/oradata/PROD/disk2/redo03.log NO
6 rows selected.
SQL> col name for a50;
SQL> select file#,name from v$datafile;
     FILE# NAME
---------- --------------------------------------------------
         1 /u01/app/oracle/oradata/PROD/disk3/system01.dbf
         2 /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
         3 /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
         4 /u01/app/oracle/oradata/PROD/disk3/users01.dbf
         5 /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
         6 /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
         7 /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
7 rows selected.
--做四次日志切换操作(共三组日志,四次切换就可覆盖当前日志组)
SQL> alter system switch logfile;
System altered.
SQL> /
System altered.
SQL> /
System altered.
SQL> /
System altered.
SQL> shutdown abort;
ORACLE instance shut down
--删除数据文件
[oracle@gc1 rman_bak]$ rm /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
--启库失败,查看具体错误信息
SQL> startup
ORACLE instance started.
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf'
SQL> select file#,error from v$recover_file;
     FILE# ERROR
---------- -----------------------------------------------------------------
         1 UNKNOWN ERROR
         2 UNKNOWN ERROR
         3 UNKNOWN ERROR
         4 UNKNOWN ERROR
         5 FILE NOT FOUND
--执行rman恢复,先尝试用一般模式进行完全恢复,分步执行,先转储datafile 5
RMAN> restore datafile 5;
Starting restore at 19-APR-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=18 device type=DISK
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 00005 to /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/rman_bak/cold_bak/PROD_76_1.bak
channel ORA_DISK_1: piece handle=/home/oracle/rman_bak/cold_bak/PROD_76_1.bak tag=TAG20140419T110458
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
Finished restore at 19-APR-14
可见,转储成功,接下来执行recover datafile 5
RMAN> recover datafile 5;
Starting recover at 19-APR-14
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
archived log for thread 1 with sequence 25 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo01.log
archived log for thread 1 with sequence 26 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo02.log
archived log for thread 1 with sequence 27 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo03.log
RMAN-08187: WARNING: media recovery until SCN 941081 complete
Finished recover at 19-APR-14
以上红色字体标识内容,可知recover失败了,提示信息不够明确,其实就是指日志被覆盖,不连续了,在此我们只能做整个数据库的恢复,如同案例一,先转储控制文件,再对整个数据库进行恢复,具体操作如下:
--先关库,再启至nomount
RMAN> shutdown abort;
Oracle instance shut down
RMAN> startup nomount;
connected to target database (not started)
Oracle instance started
Total System Global Area 836976640 bytes
Fixed Size 1339740 bytes
Variable Size 490737316 bytes
Database Buffers 339738624 bytes
Redo Buffers 5160960 bytes
RMAN> restore controlfile from '/home/oracle/backup/control/c-239015079-20140419-09';
Starting restore at 19-APR-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=22 device type=DISK
channel ORA_DISK_2: skipped, AUTOBACKUP already found
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:13
output file name=/u01/app/oracle/oradata/PROD/disk1/control01.ctl
output file name=/u01/app/oracle/oradata/PROD/disk2/control01.ctl
output file name=/u01/app/oracle/oradata/PROD/disk3/control01.ctl
Finished restore at 19-APR-14
--启库至mount后,执行数据库的转储及恢复操作,最后开库
RMAN> run{
2> allocate channel c1 type disk;
3> allocate channel c2 type disk;
4> restore database;
5> recover database;
6> }
released channel: ORA_DISK_1
released channel: ORA_DISK_2
allocated channel: c1
channel c1: SID=22 device type=DISK
allocated channel: c2
channel c2: SID=19 device type=DISK
Starting restore at 19-APR-14
skipping datafile 1; already restored to file /u01/app/oracle/oradata/PROD/disk3/system01.dbf
skipping datafile 2; already restored to file /u01/app/oracle/oradata/PROD/disk4/sysaux01.dbf
skipping datafile 5; already restored to file /u01/app/oracle/oradata/PROD/disk4/lxtbs01.dbf
skipping datafile 7; already restored to file /u01/app/oracle/oradata/PROD/disk4/zhen01.dbf
skipping datafile 3; already restored to file /u01/app/oracle/oradata/PROD/disk5/undotbs01.dbf
skipping datafile 4; already restored to file /u01/app/oracle/oradata/PROD/disk3/users01.dbf
skipping datafile 6; already restored to file /u01/app/oracle/oradata/PROD/disk3/fba01.dbf
restore not done; all files read only, offline, or already restored
Finished restore at 19-APR-14
Starting recover at 19-APR-14
starting media recovery
archived log for thread 1 with sequence 25 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo01.log
archived log for thread 1 with sequence 26 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo02.log
archived log for thread 1 with sequence 27 is already on disk as file /u01/app/oracle/oradata/PROD/disk1/redo03.log
RMAN-08187: WARNING: media recovery until SCN 941081 complete
Finished recover at 19-APR-14
released channel: c1
released channel: c2
注意:这里提示转储时文件已存在,是因为我执行这个run之前,手动执行了restore database做了一次转储,属于误操作,正常情况这里会显示转储信息。
--这里是转储整个数据库的冷备信息,按理scn号是相同的,在此我们查看验证一下
SQL> select checkpoint_change# from v$database;
CHECKPOINT_CHANGE#
------------------
            941081
SQL> select file#,checkpoint_change# from v$datafile;
     FILE# CHECKPOINT_CHANGE#
---------- ------------------
         1 941081
         2 941081
         3 941081
         4 941081
         5 941081
         6 941081
         7 941081
7 rows selected.
SQL> select file#,checkpoint_change# from v$datafile_header;
     FILE# CHECKPOINT_CHANGE#
---------- ------------------
         1 941081
         2 941081
         3 941081
         4 941081
         5 941081
         6 941081
         7 941081
7 rows selected.
由此可见,整个数据库通过冷备转储后,scn号确实一致。需要说明的是这里虽然三个scn号一样,但不能正常开库,因为日志不一致,需要使用open resetlogs方式开库,如下:
RMAN> alter database open resetlogs;
database opened
--查看恢复成功后,
SQL> select count(1) from scott.lxtb9;
  COUNT(1)
----------
        14
由此可见,这里只能恢复至备份状态,因为日志被丢失不能做完全恢复。
总结:非归档模式下,当某个数据文件丢失时且日志被覆盖的情况下,采用rman完全恢复的步骤为::
1、通过restore controlfile from方式转储最新的控制文件备份
2、alter database mount
3、restore database
4、recover databse
5、alter database open  resetlogs;
其中3、4、5步骤直接写在一个run语句中,一步执行即可。此恢复方式与案例一一样,只是因为日志被丢失,只能做不完全恢复。
而采用手工完全恢复(详见“手工完全恢复演练”章节案例一)步骤为:
1、os级cp转储所有的数据文件与控制文件
2、startup mount;
2、recover databse until cancel;
4、alter database open resetlogs;
二者对比可知,手工恢复时,转储了所有数据文件信息后,需要执行recover database until cancel方式进行恢复,意为日志能连到哪就连到哪,而rman恢复时最后只需执行open resetlogs即可。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21251711/viewspace-1171306/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-03-30

  • 博文量
    106
  • 访问量
    736055