Oracle从9i以后把SPFILE也纳入备份的策略当中,如果启用了控制文件自动备份配置,则进行控制文件自动备份的时候,会自动把SPFILE备份到备份集当中

[oracle@021Y-SH-BKAP ~]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Tue Jan 27 10:11:36 2015
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
connected to target database: ORCL (DBID=1394247230)
RMAN> configure controlfile autobackup on;
using target database control file instead of recovery catalog
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored

 

切换回到sqlplus查看v$rman_configuration视图

SQL> col name for a30
SQL> col value for a30
SQL> select * from v$rman_configuration;
     CONF# NAME                           VALUE
---------- ------------------------------ --------------------------
         1 CONTROLFILE AUTOBACKUP         ON
SQL> create tablespace mydata datafile '/DBBK/oracle/oradata/orcl/mydata.dbf' size 5m

通过RMAN备份查看

RMAN> run
 {
 allocate channel c1 type disk format='/DBBK/oracle/backup/original%t.bak';
 backup archivelog all delete all input;
 release channel c1;
 }
allocated channel: c1
channel c1: SID=35 device type=DISK
Starting backup at 2015-01-27 10:27:50
current log archived
channel c1: starting archived log backup set
channel c1: specifying archived log(s) in backup set
input archived log thread=1 sequence=58 RECID=1 STAMP=867794417
input archived log thread=1 sequence=59 RECID=2 STAMP=867794437
input archived log thread=1 sequence=60 RECID=3 STAMP=867816048
......
channel c1: starting piece 1 at 2015-01-27 10:27:52
channel c1: finished piece 1 at 2015-01-27 10:28:06
piece handle=/DBBK/oracle/backup/original870085672.bak tag=TAG20150127T102751 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:14
channel c1: deleting archived log(s)
archived log file name=/DBBK/oracle/flash_recovery_area/ORCL/archivelog/2014_12_31/o1_mf_1_58_bb80cktj_.arc RECID=1 STAMP=867794417
archived log file name=/DBBK/oracle/flash_recovery_area/ORCL/archivelog/2014_12_31/o1_mf_1_59_bb80d5oy_.arc RECID=2 STAMP=867794437
......
Finished backup at 2015-01-27 10:28:39
Starting Control File and SPFILE Autobackup at 2015-01-27 10:28:39
piece handle=/DBBK/oracle/flash_recovery_area/ORCL/autobackup/2015_01_27/o1_mf_s_870085719_bdfxyqxf_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 2015-01-27 10:28:40
released channel: c1

可以看到在备份进入到最后阶段,自动备份了CONTROLFILE和SPFILE,因为这里没有指定目录,控制文件和参数文件的备份集被存放到了默认的闪回区目录中

piece handle=/DBBK/oracle/flash_recovery_area/ORCL/autobackup/2015_01_27/o1_mf_s_870085719_bdfxyqxf_.bkp comment=NONE

当然也可以在RMAN中指明控制文件备份的路径

RMAN> configure controlfile autobackup format for device type disk to '/DBBK/oracle/backup/control%F.bkp';
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/DBBK/oracle/backup/control%F.bkp';
new RMAN configuration parameters are successfully stored
执行上方的备份命令
......
Starting Control File and SPFILE Autobackup at 2015-01-27 10:48:12
piece handle=/DBBK/oracle/backup/controlc-1394247230-20150127-02.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 2015-01-27 10:48:13
released channel: c1

可以看到控制文件%F的命名方式如下:

controlc-1394247230-20150127-02.bkp

C 表示controlfile

1394247230 表示DBID (connected to target database: ORCL (DBID=1394247230))

20150127 表示date

02 是从00~FF的十六进制数,这里说明是第二次备份控制文件

 

通过该自动备份恢复SPFILE

RMAN> restore spfile to '/DBBK/oracle/backup/spfileorcl.ora' from autobackup;
Starting restore at 2015-01-27 10:54:05
using channel ORA_DISK_1
recovery area destination: /DBBK/oracle/flash_recovery_area
database name (or database unique name) used for search: ORCL
channel ORA_DISK_1: AUTOBACKUP /DBBK/oracle/flash_recovery_area/ORCL/autobackup/2015_01_27/o1_mf_s_870085719_bdfxyqxf_.bkp found in the recovery area
channel ORA_DISK_1: looking for AUTOBACKUP on day: 20150127
channel ORA_DISK_1: AUTOBACKUP found: /DBBK/oracle/backup/controlc-1394247230-20150127-02.bkp
channel ORA_DISK_1: restoring spfile from AUTOBACKUP /DBBK/oracle/backup/controlc-1394247230-20150127-02.bkp
channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
Finished restore at 2015-01-27 10:54:07

查看恢复的spfile

$ cd /DBBK/oracle/backup/
-rw-r----- 1 oracle oinstall      3584 Jan 27 10:54 spfileorcl.ora

同样的也可以恢复controlfile

RMAN> restore controlfile to '/DBBK/oracle/backup/controlf01.dbf' from autobackup;

另外,如果数据库无法mount,则自动备份集的恢复时无法进行。

RMAN> restore spfile to '/DBBK/oracle/backup/spfileorcl.ora' from autobackup;
Starting restore at 2015-01-27 11:04:03
using channel ORA_DISK_1
recovery area destination: /DBBK/oracle/flash_recovery_area
database name (or database unique name) used for search: ORCL
channel ORA_DISK_1: no AUTOBACKUPS found in the recovery area
AUTOBACKUP search with format "%F" not attempted because DBID was not set
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 01/27/2015 11:04:03
RMAN-06172: no AUTOBACKUP found or specified handle is not a valid copy or piece

不过这里我发现报错的意思是在AUTOBACKUP下找不到指定的备份集,因为我之前开启configure controlfile autobackup之后没有指定控制文件自动备份路径,第一次备份控制文件片存放在闪回区中。因此即使在nomount的状态下也能进行控制文件和spfile的恢复,可以观察以下恢复输出:

RMAN> restore spfile to '/DBBK/oracle/backup/spfileorcl.ora' from autobackup;
Starting restore at 2015-01-27 11:02:10
using channel ORA_DISK_1
recovery area destination: /DBBK/oracle/flash_recovery_area
database name (or database unique name) used for search: ORCL
channel ORA_DISK_1: AUTOBACKUP /DBBK/oracle/flash_recovery_area/ORCL/autobackup/2015_01_27/o1_mf_s_870085719_bdfxyqxf_.bkp found in the recovery area
AUTOBACKUP search with format "%F" not attempted because DBID was not set
channel ORA_DISK_1: restoring spfile from AUTOBACKUP /DBBK/oracle/flash_recovery_area/ORCL/autobackup/2015_01_27/o1_mf_s_870085719_bdfxyqxf_.bkp
channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
Finished restore at 2015-01-27 11:02:11

我这里尝试指定恢复时使用的控制文件备份集:

RMAN> restore spfile to '/DBBK/oracle/backup/spfileorcl.ora' from '/DBBK/oracle/backup/controlc-1394247230-20150127-02.bkp';
Starting restore at 2015-01-27 11:12:05
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=156 device type=DISK
channel ORA_DISK_1: restoring spfile from AUTOBACKUP /DBBK/oracle/backup/controlc-1394247230-20150127-02.bkp
channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
Finished restore at 2015-01-27 11:12:07

可以看到在nomount状态下一样能恢复,因为是在11G下验证,RMAN的功能上可能与早期的版本有所不同。