Oracle 11gR2 使用 RMAN duplicate from active database 复制数据库

在Oracle 10g下,我们可以使用RMAN duplicate 命令创建一个具有不同DBID 的复制库。到了Oracle 11gR2,RMAN 的duplicate 有2种方法实现:
1. Active database duplication
2. Backup-based duplication

Active database duplication 通过网络,直接copy target 库到auxiliary 库,然后创建复制库。 这种方法就不需要先用RMAN 备份数据库,然后将备份文件发送到auxiliary端。这个功能的作用是非常大的。 尤其是对T级别的库。 因为对这样的库进行备份,然后将备份集发送到备库,在进行duplicate 的代价是非常大的。 一备份要占用时间,二要占用备份空间,三在网络传送的时候,还需要占用带宽和时间。所以Active database duplicate 很好的解决了以上的问题。 它对大库的迁移非常有用。

如果是从RAC duplicate 到单实例,操作是一样的。 如果是从单实例duplicate 到RAC。 那么先duplicate 到 单实例。 然后将单实例转换成RAC。

下面我们看一下11gR2下,Active Database Duplicate的步骤如下:
1. 创建Auxiliary 库的Initialization Parameter:

如果使用spfile,那么在pfile文件里只需要设置一个DB_NAME参数,其他参数会在duplicate 命令中自己设置。

如果使用pfile,那么需要设置如下参数:

DB_NAME
CONTROL_FILES
DB_BLOCK_SIZE
DB_FILE_NAME_CONVERT
LOG_FILE_NAME_CONVERT
DB_RECOVERY_FILE_DEST

2. 在Auxiliary库创建Password File 文件
对于Backup-based duplication,Password File 不是必须的,但是对于Active Database Duplication,Password File是必须的。因为Active Database Duplication 使用相同的SYSDBA 密码直接连接到auxiliary 库。 所以,确保target 和Auxiliary库的SYSDBA 密码一样很重要。

当然,我们也可以在duplicate 命令中加上PASSWORD FILE 选项(也是默认值),这样RMAN 在copy 的时候也会从target 库把密码文件copy过来,如果auxiliary库上已经存在了Password file,那么该操作会重写那个文件。

如:
RMAN> DUPLICATE TARGET DATABASE TO orcl FROM ACTIVE DATABASE NOFILENAMECHECK PASSWORD FILE SPFILE;

3. 如果是windows 平台,还需要创建Database service:
% set ORACLE_SID=DAVE
% set ORACLE_HOME=E:/oracle/product/11.1.0/db_1
% oradim -NEW -SID orcl

4. 配置oracle net,修改listener.ora和tnsnames.ora 文件:
在Target 库和Auxiliary 都要修改。这个也可以使用netca 和netmgr命令配置。
Target: tnsnames.ora
orcl2_au =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.99.92)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl2)
)
)

Auxiliary: listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = standby)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl2)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl2)
)
)

5. 用第一步创建的pfile文件,将Auxiliary启动到nomout 状态。 然后进行Active Database duplicate。

我们看一个完整的复制示例:

我这里复制的目录相同。 如果不同的话需要在pfile里面加入db_file_name_convert和log_file_name_convert.

1. 在Auxiliary 创建pfile 参数文件:
[oracle@rhel5 dbs]$ cat initorcl.ora
DB_NAME=orcl2

2. 在Auxiliary库上创建口令文件
[oracle@rhel5 dbs]$ orapwd file=?/dbs/orapworcl password=oracle

3. 在Auxiliary库创建相关的目录结构:

不然在duplicate时会报如下错误:
ORA-19505: failed to identify file “/u01/app/oracle/oradata/orcl/users01.dbf”
ORA-27040: file create error, unable to create file

4. 启动Auxiliary 到nomout 状态:
SQL> startup nomount pfile=?/dbs/initorcl.ora

5. 在Target 和Auxiliary 都配置Oracle Net(Listener.ora and tnsnames.ora):
Target: tnsnames.ora
orcl2_au =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.99.92)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl2)
)
)

Auxiliary: listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = standby)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl2)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = orcl2)
)
)

6. 开始RMAN duplicate from active database:
注意:如果target 和 Auxiliary库的目录结构相同,记得加上nofilenamecheck参数,不然会报如下错误:
RMAN-05001: auxiliary file name /u01/app/oracle/oradata/orcl/users01.dbf conflicts with a file used by the target database

如果目录不同,在pfile里加如下2个参数进行转换:
db_file_name_convert
log_file_name_convert.

[oracle@rhel5 dbs]$ rman target / auxiliary sys/oracle@orcl2_au
Recovery Manager: Release 11.2.0.1.0 – Production on Mon Oct 21 21:33:44 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
connected to target database: ORCL2 (DBID=810247392)
connected to auxiliary database: ORCL2 (not mounted)

RMAN> run {
2> allocate channel prmy1 type disk;
3> allocate channel prmy2 type disk;
4> allocate channel prmy3 type disk;
5> allocate auxiliary channel stby1 type disk;
6> duplicate target database to orcl2 from active database nofilenamecheck spfile;
7> }

using target database control file instead of recovery catalog
allocated channel: prmy1
channel prmy1: SID=31 device type=DISK

allocated channel: prmy2
channel prmy2: SID=36 device type=DISK

allocated channel: prmy3
channel prmy3: SID=40 device type=DISK

allocated channel: stby1
channel stby1: SID=20 device type=DISK

Starting Duplicate Db at 21-OCT-13

contents of Memory Script:
{
backup as copy reuse
targetfile  ‘/u01/app/oracle/product/11.2.0/db_1/dbs/spfileorcl2.ora’ auxiliary format
‘/u01/app/oracle/product/11.2.0/db_1/dbs/spfileorcl2.ora’   ;
sql clone “alter system set spfile= ”/u01/app/oracle/product/11.2.0/db_1/dbs/spfileorcl2.ora””;
}
executing Memory Script

Starting backup at 21-OCT-13
Finished backup at 21-OCT-13

sql statement: alter system set spfile= ”/u01/app/oracle/product/11.2.0/db_1/dbs/spfileorcl2.ora”

contents of Memory Script:
{
sql clone “alter system set  db_name =
”ORCL2” comment=
”duplicate” scope=spfile”;
shutdown clone immediate;
startup clone nomount;
}
executing Memory Script
sql statement: alter system set  db_name =  ”ORCL2” comment= ”duplicate” scope=spfile
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started

Total System Global Area     849530880 bytes

Fixed Size                     1339824 bytes
Variable Size                637537872 bytes
Database Buffers             205520896 bytes
Redo Buffers                   5132288 bytes
allocated channel: stby1
channel stby1: SID=19 device type=DISK

contents of Memory Script:
{
sql clone “alter system set  db_name =
”ORCL2” comment=
”Modified by RMAN duplicate” scope=spfile”;
sql clone “alter system set  db_unique_name =
”ORCL2” comment=
”Modified by RMAN duplicate” scope=spfile”;
shutdown clone immediate;
startup clone force nomount
backup as copy current controlfile auxiliary format  ‘/oradata/orcl2/datafiles/control01.ctl’;
restore clone controlfile to  ‘/oradata/orcl2/datafiles/control02.ctl’ from
‘/oradata/orcl2/datafiles/control01.ctl’;
alter clone database mount;
}
executing Memory Script
sql statement: alter system set  db_name =  ”ORCL2” comment= ”Modified by RMAN duplicate” scope=spfile
sql statement: alter system set  db_unique_name =  ”ORCL2” comment= ”Modified by RMAN duplicate” scope=spfile
Oracle instance shut down
Oracle instance started

Total System Global Area     849530880 bytes

Fixed Size                     1339824 bytes
Variable Size                637537872 bytes
Database Buffers             205520896 bytes
Redo Buffers                   5132288 bytes
allocated channel: stby1
channel stby1: SID=19 device type=DISK

Starting backup at 21-OCT-13
channel prmy1: starting datafile copy
copying current control file
output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_orcl2.f tag=TAG20131021T213753 RECID=4 STAMP=829431485
channel prmy1: datafile copy complete, elapsed time: 00:00:25
Finished backup at 21-OCT-13

Starting restore at 21-OCT-13

channel stby1: copied control file copy
Finished restore at 21-OCT-13

database mounted
RMAN-05538: WARNING: implicitly using DB_FILE_NAME_CONVERT

contents of Memory Script:
{
set newname for datafile  1 to
“/oradata/orcl2/datafilessystem01.dbf”;
set newname for datafile  2 to
“/oradata/orcl2/datafilessysaux01.dbf”;
set newname for datafile  3 to
“/oradata/orcl2/datafilesundotbs01.dbf”;
set newname for datafile  4 to
“/oradata/orcl2/datafilesusers01.dbf”;
set newname for datafile  5 to
“/oradata/orcl2/datafilesexample01.dbf”;
backup as copy reuse
datafile  1 auxiliary format
“/oradata/orcl2/datafilessystem01.dbf”   datafile
2 auxiliary format
“/oradata/orcl2/datafilessysaux01.dbf”   datafile
3 auxiliary format
“/oradata/orcl2/datafilesundotbs01.dbf”   datafile
4 auxiliary format
“/oradata/orcl2/datafilesusers01.dbf”   datafile
5 auxiliary format
“/oradata/orcl2/datafilesexample01.dbf”   ;
sql ‘alter system archive log current’;
}
executing Memory Script
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME

Starting backup at 21-OCT-13
channel prmy1: starting datafile copy
input datafile file number=00001 name=/oradata/orcl2/datafilessystem01.dbf
channel prmy2: starting datafile copy
input datafile file number=00002 name=/oradata/orcl2/datafilessysaux01.dbf
channel prmy3: starting datafile copy
input datafile file number=00005 name=/oradata/orcl2/datafilesexample01.dbf
output file name=/oradata/orcl2/datafilesexample01.dbf tag=TAG20131021T213825
channel prmy3: datafile copy complete, elapsed time: 00:00:26
channel prmy3: starting datafile copy
input datafile file number=00003 name=/oradata/orcl2/datafilesundotbs01.dbf
output file name=/oradata/orcl2/datafilessysaux01.dbf tag=TAG20131021T213825
channel prmy2: datafile copy complete, elapsed time: 00:01:03
channel prmy2: starting datafile copy
input datafile file number=00004 name=/oradata/orcl2/datafilesusers01.dbf
output file name=/oradata/orcl2/datafilesundotbs01.dbf tag=TAG20131021T213825
channel prmy3: datafile copy complete, elapsed time: 00:00:37
output file name=/oradata/orcl2/datafilesusers01.dbf tag=TAG20131021T213825
channel prmy2: datafile copy complete, elapsed time: 00:00:15
output file name=/oradata/orcl2/datafilessystem01.dbf tag=TAG20131021T213825
channel prmy1: datafile copy complete, elapsed time: 00:01:49
Finished backup at 21-OCT-13

sql statement: alter system archive log current

contents of Memory Script:
{
backup as copy reuse
archivelog like  “/oradata/orcl2/arch/1_15_829415524.arc” auxiliary format
“/oradata/orcl2/arch/1_15_829415524.arc”   ;
catalog clone archivelog  “/oradata/orcl2/arch/1_15_829415524.arc”;
switch clone datafile all;
}
executing Memory Script

Starting backup at 21-OCT-13
channel prmy1: starting archived log copy
input archived log thread=1 sequence=15 RECID=11 STAMP=829431615
output file name=/oradata/orcl2/arch/1_15_829415524.arc RECID=0 STAMP=0
channel prmy1: archived log copy complete, elapsed time: 00:00:16
Finished backup at 21-OCT-13

cataloged archived log
archived log file name=/oradata/orcl2/arch/1_15_829415524.arc RECID=11 STAMP=829430002

datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=829430002 file name=/oradata/orcl2/datafilessystem01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=5 STAMP=829430002 file name=/oradata/orcl2/datafilessysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=6 STAMP=829430002 file name=/oradata/orcl2/datafilesundotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=7 STAMP=829430002 file name=/oradata/orcl2/datafilesusers01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=8 STAMP=829430002 file name=/oradata/orcl2/datafilesexample01.dbf

contents of Memory Script:
{
set until scn  854661;
recover
clone database
delete archivelog
;
}
executing Memory Script
executing command: SET until clause
Starting recover at 21-OCT-13
starting media recovery

archived log for thread 1 with sequence 15 is already on disk as file /oradata/orcl2/arch/1_15_829415524.arc
archived log file name=/oradata/orcl2/arch/1_15_829415524.arc thread=1 sequence=15
media recovery complete, elapsed time: 00:00:00
Finished recover at 21-OCT-13

contents of Memory Script:
{
shutdown clone immediate;
startup clone nomount;
sql clone “alter system set  db_name =
”ORCL2” comment=
”Reset to original value by RMAN” scope=spfile”;
sql clone “alter system reset  db_unique_name scope=spfile”;
shutdown clone immediate;
startup clone nomount;
}
executing Memory Script
database dismounted
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started

Total System Global Area     849530880 bytes

Fixed Size                     1339824 bytes
Variable Size                637537872 bytes
Database Buffers             205520896 bytes
Redo Buffers                   5132288 bytes
allocated channel: stby1
channel stby1: SID=19 device type=DISK

sql statement: alter system set  db_name =  ”ORCL2” comment= ”Reset to original value by RMAN” scope=spfile
sql statement: alter system reset  db_unique_name scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area     849530880 bytes

Fixed Size                     1339824 bytes
Variable Size                637537872 bytes
Database Buffers             205520896 bytes
Redo Buffers                   5132288 bytes
allocated channel: stby1
channel stby1: SID=19 device type=DISK
sql statement: CREATE CONTROLFILE REUSE SET DATABASE “ORCL2” RESETLOGS ARCHIVELOG
MAXLOGFILES     16
MAXLOGMEMBERS      3
MAXDATAFILES      100
MAXINSTANCES     8
MAXLOGHISTORY      292
LOGFILE
GROUP  1 ( ‘/oradata/orcl2/datafiles/redo01.log’ ) SIZE 50 M  REUSE,
GROUP  2 ( ‘/oradata/orcl2/datafiles/redo02.log’ ) SIZE 50 M  REUSE,
GROUP  3 ( ‘/oradata/orcl2/datafiles/redo03.log’ ) SIZE 50 M  REUSE
DATAFILE
‘/oradata/orcl2/datafilessystem01.dbf’
CHARACTER SET ZHS16GBK
contents of Memory Script:
{
set newname for tempfile  1 to
“/oradata/orcl2/datafilestemp01.dbf”;
switch clone tempfile all;
catalog clone datafilecopy  “/oradata/orcl2/datafilessysaux01.dbf”,
“/oradata/orcl2/datafilesundotbs01.dbf”,
“/oradata/orcl2/datafilesusers01.dbf”,
“/oradata/orcl2/datafilesexample01.dbf”;
switch clone datafile all;
}
executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to /oradata/orcl2/datafilestemp01.dbf in control file

cataloged datafile copy
datafile copy file name=/oradata/orcl2/datafilessysaux01.dbf RECID=1 STAMP=829430181
cataloged datafile copy
datafile copy file name=/oradata/orcl2/datafilesundotbs01.dbf RECID=2 STAMP=829430181
cataloged datafile copy
datafile copy file name=/oradata/orcl2/datafilesusers01.dbf RECID=3 STAMP=829430181
cataloged datafile copy
datafile copy file name=/oradata/orcl2/datafilesexample01.dbf RECID=4 STAMP=829430181

datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=829430181 file name=/oradata/orcl2/datafilessysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=829430181 file name=/oradata/orcl2/datafilesundotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=829430181 file name=/oradata/orcl2/datafilesusers01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=4 STAMP=829430181 file name=/oradata/orcl2/datafilesexample01.dbf

contents of Memory Script:
{
Alter clone database open resetlogs;
}
executing Memory Script

database opened
Finished Duplicate Db at 21-OCT-13
released channel: prmy1
released channel: prmy2
released channel: prmy3
released channel: stby1

标签