有时候可能需要去创建一个跟target数据库一样的一个数据库副本, 用RMAN可以很方便地通过Target数据库的备份文件(backup sets)来clone一个同样的数据库。
本文主要讲述如何用RMAN在同一个Host上duplicate一个数据库, (target database and duplicated database reside in the same machine).
因为RMAN是通过target数据库的备份文件来创建数据库副本,因此首先需要同RMAN来给target数据库做一个全面的备份,如下
RMAN> backup database plus archivelog;
Step 1:
Target数据库叫orcl, 我们想创建一个副本叫orcl3, 该怎么做呢? 首先需要建一个数据库instance启动的参数文件,暂命名为initorcl3.ora, 与initorcl.ora放在同一个目录(默认数目-<ORACLE_HOME>\database, 我的机器上路径是E:\oracle\product\10.2.0\db_1\database).
文件initorcl3.ora的内容如下,
db_name=orcl3
db_block_size=8192
compatible=10.2.0.4
remote_login_passwordfile=exclusive
#sga_target=500m
#sga_max_size=600m
control_files=('E:\oracle\product\10.2.0\oradata\orcl3\control01.ctl',
'E:\oracle\product\10.2.0\oradata\orcl3\control02.ctl',
'E:\oracle\product\10.2.0\oradata\orcl3\control03.ctl')
db_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')
log_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')
这些参数里面有些是必须的,比如db_name, control_files, db_file_name_convert, log_file_name_convert.
control_files设置了orcl3数据库的控制文件的名字和位置(RMAN在duplicate database的时候会自动创建)
db_file_name_convert和log_file_name_convert用来设置Target数据库(orcl)和duplicated database(orcl3)中数据文件和日志文件的目录对应关系,因为默认情况下rman在duplicate database的时候只是把target database(从backup set)中的datafile, redo logfile拷贝到指定的目录下,不会改名字, 因此目录必须不一样才行。这里就放在了同target 数据库orcl相同的“父”目录下—E:\oracle\product\10.2.0\oradata.
另外,注意里面有两个参数被注释了(#)
sga_target=500m
sga_max_size=600m
这两个参数用来控制instance的内存分配,如果不设置这两个参数,可能会出现问题,下面会见到。
Step 2:
接下来在listener.ora和tnsname.ora中加入如下内容,这个主要是用在后面rman来连接这个auxiliary数据库时用到。
-- listener.ora
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = E:\oracle\product\10.2.0\db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME = E:\oracle\product\10.2.0\db_1)
(SID_NAME=orcl)
)
(SID_DESC =
(GLOBAL_DBNAME=orcl2)
(ORACLE_HOME = E:\oracle\product\10.2.0\db_1)
(SID_NAME=orcl2)
)
(SID_DESC =
(GLOBAL_DBNAME=orcl3)
(ORACLE_HOME = E:\oracle\product\10.2.0\db_1)
(SID_NAME=orcl3)
)
)
-- tnsname.ora
ORCL3 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.80.15.117)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl3)
)
)
Step 3:
用oradim来新建instance – orcl3, 如下
C:\Documents and Settings\szuser>oradim -new -sid orcl3
Instance created.
启动orcl3到nomount阶段 (因为这个时候还没有Control文件,因此只能启动到nomount阶段)
C:\Documents and Settings\szuser>sqlplus /nolog
SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jan 18 16:17:13 2010
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
SQL> conn /@orcl3 as sysdba
ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect
descriptor
SQL> conn sys/sys@orcl3 as sysdba
ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect
descriptor
SQL> exit
C:\Documents and Settings\szuser>set ORACLE_SID=orcl3
C:\Documents and Settings\szuser>sqlplus /nolog
SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jan 18 16:18:58 2010
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
SQL> conn / as sysdba
Connected to an idle instance.
SQL> startup force nomount pfile=E:\oracle\product\10.2.0\db_1\database\initorcl
3.ora
ORACLE instance started.
Total System Global Area 117440512 bytes
Fixed Size 1295320 bytes
Variable Size 58723368 bytes
Database Buffers 50331648 bytes
Redo Buffers 7090176 bytes
SQL> select name from v$database;
select name from v$database
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 2
ORA-04031: unable to allocate 64 bytes of shared memory ("shared pool","select
di.inst_id,di.didbi,d...","sql area","opn: qkexrInitOpn")
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Pr
oduction
With the Partitioning, OLAP, Data Mining and Real Application Testing options
注意在这个过程中做了一个测试 select name from v$database, 因为数据库还没有到open阶段,很显然这条SQL语句会出现问题,但是出现的问题显然不应该是ORA-04031, 从错误信息来看就以为shared pool分配的空间不够!暂时先不管这个错误,继续。
Step 4:
用RMAN连接到target 数据库orcl和auxiliary数据库(orcl3)来duplicate orcl to orcl3
C:\Documents and Settings\szuser>rman target /@orcl auxiliary /@orcl3
Recovery Manager: Release 10.2.0.4.0 - Production on Mon Jan 18 16:20:37 2010
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: ORCL (DBID=1235521622)
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-04006: error from auxiliary database: ORA-12528: TNS:listener: all appropri
ate instances are blocking new connections
C:\Documents and Settings\szuser>
但是很不幸,出现了一些问题,这是因为我们之前改了listener.ora文件,需要重启下tns listener.
Step 5: Restart TNS Listener
C:\Documents and Settings\szuser>lsnrctl stop
LSNRCTL for 32-bit Windows: Version 10.2.0.4.0 - Production on 18-JAN-2010 16:23
:52
Copyright (c) 1991, 2007, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
The command completed successfully
C:\Documents and Settings\szuser>lsnrctl start
LSNRCTL for 32-bit Windows: Version 10.2.0.4.0 - Production on 18-JAN-2010 16:24
:01
Copyright (c) 1991, 2007, Oracle. All rights reserved.
Starting tnslsnr: please wait...
TNSLSNR for 32-bit Windows: Version 10.2.0.4.0 - Production
System parameter file is E:\oracle\product\10.2.0\db_1\network\admin\listener.or
a
Log messages written to E:\oracle\product\10.2.0\db_1\network\log\listener.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1ipc
)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.80.15.117)(PORT=1521)
))
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for 32-bit Windows: Version 10.2.0.4.0 - Produ
ction
Start Date 18-JAN-2010 16:24:02
Uptime 0 days 0 hr. 0 min. 2 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File E:\oracle\product\10.2.0\db_1\network\admin\listener.o
ra
Listener Log File E:\oracle\product\10.2.0\db_1\network\log\listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.80.15.117)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
Instance "orcl", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl2" has 1 instance(s).
Instance "orcl2", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl3" has 1 instance(s).
Instance "orcl3", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
C:\Documents and Settings\szuser>
再来尝试下看看,
C:\Documents and Settings\szuser>rman target /@orcl auxiliary /@orcl3
Recovery Manager: Release 10.2.0.4.0 - Production on Mon Jan 18 16:25:01 2010
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: ORCL (DBID=1235521622)
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-04006: error from auxiliary database: ORA-00600: internal error code, argum
ents: [15435], [SYS], [X$DBMS_BACKUP_RESTORE], [], [], [], [], []
ORA-04031: unable to allocate 1040 bytes of shared memory ("shared pool","X$DBMS
_BACKUP_RESTORE","PL/SQL DIANA","PAR.C:parapt:Page")
RMAN-04015: error setting target database character set to WE8MSWIN1252
这个时候会发现得到的错误信息跟之前执行select * from v$database得到的错误类似,也是因为shared pool空间不够。怎么办呢,我们可以修改initorcl3.ora, 设置下sga_target参数值
db_name=orcl3
db_block_size=8192
compatible=10.2.0.4
remote_login_passwordfile=exclusive
sga_target=500m
sga_max_size=600m
control_files=('E:\oracle\product\10.2.0\oradata\orcl3\control01.ctl', 'E:\oracle\product\10.2.0\oradata\orcl3\control02.ctl', 'E:\oracle\product\10.2.0\oradata\orcl3\control03.ctl')
db_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')
log_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')
然后重新启动instance orcl3,
C:\Documents and Settings\szuser>set ORACLE_SID=orcl3
C:\Documents and Settings\szuser>sqlplus /nolog
SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jan 18 16:31:21 2010
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
SQL> conn / as sysdba
Connected.
SQL> startup force nomount pfile=E:\oracle\product\10.2.0\db_1\database\initorcl
3.ora
ORACLE instance started.
Total System Global Area 629145600 bytes
Fixed Size 1298304 bytes
Variable Size 251658368 bytes
Database Buffers 369098752 bytes
Redo Buffers 7090176 bytes
SQL> select * from v$database;
select * from v$database
*
ERROR at line 1:
ORA-01507: database not mounted
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Pr
oduction
With the Partitioning, OLAP, Data Mining and Real Application Testing options
C:\Documents and Settings\szuser>rman target /@orcl auxiliary /@orcl3
Recovery Manager: Release 10.2.0.4.0 - Production on Mon Jan 18 16:32:25 2010
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: ORCL (DBID=1235521622)
connected to auxiliary database: ORCL3 (not mounted)
RMAN>
可以看出RMAN可以成功连接到target database和auxiliary database了!
Step 6:
执行 RMAN>duplicate target database to orcl3
但是遇到如下问题,
ORA-27040: file create error, unable to create file
OSD-04002: unable to open file
O/S-Error: (OS 3) The system cannot find the path specified.
failover to previous backup
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of Duplicate Db command at 01/18/2010 16:33:45
RMAN-03015: error occurred in stored script Memory Script
RMAN-06026: some targets not found - aborting restore
RMAN-06023: no backup or copy of datafile 5 found to restore
RMAN-06023: no backup or copy of datafile 4 found to restore
RMAN-06023: no backup or copy of datafile 3 found to restore
RMAN-06023: no backup or copy of datafile 2 found to restore
RMAN-06023: no backup or copy of datafile 1 found to restore
原因是我们没有手动创建文件夹orcl3 (E:\oracle\product\10.2.0\oradata\orcl3)
手动创建文件夹orcl3之后才尝试就可以成功了!如下
RMAN> duplicate target database to orcl3;
Starting Duplicate Db at 18-JAN-10
using channel ORA_AUX_DISK_1
contents of Memory Script:
{
set until scn 9158499;
set newname for datafile 1 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSTEM01.DBF";
set newname for datafile 2 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\UNDOTBS01.DBF";
set newname for datafile 3 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSAUX01.DBF";
set newname for datafile 4 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\USERS01.DBF";
set newname for datafile 5 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\EXAMPLE02.DBF";
restore
check readonly
clone database
;
}
executing Memory Script
executing command: SET until clause
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 18-JAN-10
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSTEM01.DBF
restoring datafile 00002 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\UNDOTBS01.DBF
restoring datafile 00003 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSAUX01.DBF
restoring datafile 00004 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\USERS01.DBF
restoring datafile 00005 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\EXAMPLE02.DBF
channel ORA_AUX_DISK_1: reading from backup piece E:\ORACLE\PRODUCT\10.2.0\FLASH
_RECOVERY_AREA\ORCL\BACKUPSET\2010_01_18\O1_MF_NNNDF_TAG20100118T101131_5O7JTMGD
_.BKP
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=E:\ORACLE\PRODUCT\10.2.0\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2010_01
_18\O1_MF_NNNDF_TAG20100118T101131_5O7JTMGD_.BKP tag=TAG20100118T101131
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:06
Finished restore at 18-JAN-10
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "ORCL3" RESETLOGS ARCHIVELO
G
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO01.LOG' ) SIZE 50 M RE
USE,
GROUP 2 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO02.LOG' ) SIZE 50 M RE
USE,
GROUP 3 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO03.LOG' ) SIZE 50 M RE
USE
DATAFILE
'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSTEM01.DBF'
CHARACTER SET WE8MSWIN1252
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
released channel: ORA_AUX_DISK_1
datafile 2 switched to datafile copy
input datafile copy recid=1 stamp=708626171 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\UNDOTBS01.DBF
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=708626171 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\SYSAUX01.DBF
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=708626171 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\USERS01.DBF
datafile 5 switched to datafile copy
input datafile copy recid=4 stamp=708626171 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\EXAMPLE02.DBF
contents of Memory Script:
{
set until scn 9158499;
recover
clone database
delete archivelog
;
}
executing Memory Script
executing command: SET until clause
Starting recover at 18-JAN-10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=36 devtype=DISK
starting media recovery
archive log thread 1 sequence 182 is already on disk as file E:\ORACLE\PRODUCT\1
0.2.0\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2010_01_18\O1_MF_1_182_5O7JVDHV_.ARC
archive log filename=E:\ORACLE\PRODUCT\10.2.0\FLASH_RECOVERY_AREA\ORCL\ARCHIVELO
G\2010_01_18\O1_MF_1_182_5O7JVDHV_.ARC thread=1 sequence=182
media recovery complete, elapsed time: 00:00:03
Finished recover at 18-JAN-10
contents of Memory Script:
{
shutdown clone;
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 629145600 bytes
Fixed Size 1298304 bytes
Variable Size 251658368 bytes
Database Buffers 369098752 bytes
Redo Buffers 7090176 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "ORCL3" RESETLOGS ARCHIVELO
G
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO01.LOG' ) SIZE 50 M RE
USE,
GROUP 2 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO02.LOG' ) SIZE 50 M RE
USE,
GROUP 3 ( 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\REDO03.LOG' ) SIZE 50 M RE
USE
DATAFILE
'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSTEM01.DBF'
CHARACTER SET WE8MSWIN1252
contents of Memory Script:
{
set newname for tempfile 1 to
"E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\TEMP01.DBF";
switch clone tempfile all;
catalog clone datafilecopy "E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\UNDOTBS01
.DBF";
catalog clone datafilecopy "E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSAUX01.
DBF";
catalog clone datafilecopy "E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\USERS01.D
BF";
catalog clone datafilecopy "E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\EXAMPLE02
.DBF";
switch clone datafile all;
}
executing Memory Script
executing command: SET NEWNAME
renamed temporary file 1 to E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\TEMP01.DBF in
control file
cataloged datafile copy
datafile copy filename=E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\UNDOTBS01.DBF reci
d=1 stamp=708626184
cataloged datafile copy
datafile copy filename=E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\SYSAUX01.DBF recid
=2 stamp=708626185
cataloged datafile copy
datafile copy filename=E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\USERS01.DBF recid=
3 stamp=708626185
cataloged datafile copy
datafile copy filename=E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL3\EXAMPLE02.DBF reci
d=4 stamp=708626185
datafile 2 switched to datafile copy
input datafile copy recid=1 stamp=708626184 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\UNDOTBS01.DBF
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=708626185 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\SYSAUX01.DBF
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=708626185 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\USERS01.DBF
datafile 5 switched to datafile copy
input datafile copy recid=4 stamp=708626185 filename=E:\ORACLE\PRODUCT\10.2.0\OR
ADATA\ORCL3\EXAMPLE02.DBF
contents of Memory Script:
{
Alter clone database open resetlogs;
}
executing Memory Script
database opened
Finished Duplicate Db at 18-JAN-10
最后生成的结果目录如下图,可以看到所有的文件都创建出来了,
下面总结下整个过程:
1. Backup target database using RMAN
2. Create parameter file (remember to set the sga size)and directory (e.g. orcl3) wherein the database will reside
3. Add entries to tnsname.ora and listener.ora
4. Restart TNS listerner
5. Use ORADIM to create new instance
6. Start the newly created instance using the parameter file to nomount state.
7. Connect to both Target database and Auxiliary database using RMAN
8. Duplicate target database to auxiliary database
9. Remove the following parameters from the pfile initorcl3.ora
db_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')
log_file_name_convert=('E:\oracle\product\10.2.0\oradata\orcl', 'E:\oracle\product\10.2.0\oradata\orcl3')