一.概述
复制数据库即使用RMAN工具的DUPLICATE命令从源数据库拷贝所有或者一部分的数据到目标数据库。复制数据库技术在多种场景都是特别有用的,如创建standby数据库,创建用于不同目的地测试环境,数据库迁移等。
使用DUPLICATE命令复制数据库能自动配置与源数据库不同的DBID,确保目标数据库和源数据库能一起存放在相同的恢复目录数据库中。
RMAN支持两种复制类型:活动数据库复制和基本备份的复制。RMAN能在源数据库和恢复目录数据库存在或者不存在的情况下执行基于备份的复制。
活动数据库复制要求同时连接到源数据库和恢复目标数据库,如果使用恢复目录作为元数据的存储,那么还需要连接到恢复目录数据库。
对于复制技术的选择可以考虑如下因素,活动数据库复制最主要的优势不要求源数据库备份。这个技术的缺点是在复制过程中长时间消耗服务器资源,这对源数据库会产生一定的影响。
二.活动数据库复制准备工作
在活动数据复制中,RMAN连接到作为目标数据库实例和辅助实例,RMAN通过网络拷贝源数据库到辅助实例,由此创建复制数据库,这并不需要先创建源数据库库备份
实际的复制工作是源数据库和辅助实例通过它们之间的网络完成的,源数据库实例和辅助数据库实例之间的网络由Oracle Net实现。
执行数据库复制操作,源数据库和复制数据库必须是相同的平台,在执行活动数据库复制操作之前需要完成以下准备工作:
(1)数据库空间要求
确保目标主机有足够的存储空间存放数据库的数据
(2)源和目标主机通过LAN或者WAN进行连接
在WAN中活动数据库复制的性能可能比LAN(本地区域网络)更慢,如果WAN的性能变得不可接受,那么基于备份的复制可能变成唯一的选择了,如果在多用户活动期间复制数据库,可能导致网络吞吐量下降,这种情况最好的选择基于备份的复制。
(3)复制文件的命名策略
当复制数据库是,RMAN为复制控制文件,数据文件,临时文件和联机REDO日志文件生成名称。因此,必须制定这些文件的命名策略
Oracle推荐简单的复制策略,配置与源数据库相同的名称,使用相同名称意思是环境要符合下列要求:
如果源数据库使用ASM磁盘组,那么复制数据库必须使用相同名称的磁盘组
如果源数据库文件是Oracle管理的文件(OMF),那么辅助实例必须设置DB_FILE_CREATE_DEST参数,指定与源数据库相同的目录位置,源和目标主机的目录相同,Oracle数据库为复制文件创建相应的名称。
源数据库和复制数据库要有相同的路径存放数据库文件
按照以上建议配置环境,就不需要额外的配置工作了
(4)Oracle Net 连通性
要确保源数据库到辅助实例之间的Oracle Net的连通性,如果源数据库是RAC数据库,确保RAC数据库的所有节点都可以建立于辅助实例之间的网络连接
(5) 为辅助实例创建密码文件
手动创建密码文件,活动复制要求原数据库和辅助实例使用相同的密码,默认使用SYS用户
(6)创建初始化参数文件
手动创建初始化参数文件
在DUPLICATE 命令中明确指定SPFILE选项
(7)确保源数据库处于正常状态
(8)基本步骤如下:a.连接到源数据库,b.连接到辅助实例,c.连接到恢复目录数据库(如果有的话)
切换归档redo日志 :ALTER SYSTEM ARCHIVE LOG CURRENT
执行DULICATE 命令复制活动的数据库
DUPLICATE TARGET DATABASE TO "目标数据库实例"
FROM ACTIVE DATABASE
PASSWORD FILE
SPFILE
NOFILENAMECHECK; 源数据库数据文件,密码文件,参数文件和辅助实例具有相同的目录结构;
成功执行上面命令必须确保源数据库在归档模式,以及至少产生一次归档。
三.完整的活动数据库复制实例:
(1).源数据库实例CPP,目标数据库实例MES,Oracle Net配置:
源库:cat listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = orcl1)(PORT = 1521))
)
)
ADR_BASE_LISTENER = /u01/oracle
cat tnsnames.ora
CPP =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.175)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = CPP)
)
)
MES =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.176)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MES)
)
)
目标库: cat listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = orcl2)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/oracle
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=MES)
(SID_NAME=MES)
(ORACLE_HOME=/u01/oracle/product/11.2.0/db_1)
)
)
cat tnsnames.ora
CPP =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.175)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = CPP)
)
)
MES =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.176)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = MES)
)
)
配置完成后用tnsping检测网络连通性
(2)将源库的密码文件拷贝到目标库,并重新命名:
scp orapwCPP 192.168.1.176:/u01/oracle/product/11.2.0/db_1/dbs/orapwMES
(3)拷贝源库的参数文件,根据实际情况修改参数文件
MES.__java_pool_size=4194304
MES.__large_pool_size=12582912
MES.__oracle_base=‘/u01/oracle‘#ORACLE_BASE set from environment
MES.__pga_aggregate_target=314572800
MES.__sga_target=469762048
MES.__shared_io_pool_size=0
MES.__shared_pool_size=184549376
MES.__streams_pool_size=4194304
*.db_create_file_dest=‘/u01/oracle/oradata/‘
*.audit_file_dest=‘/u01/oracle/admin/MES/adump‘ --注意在目标库上创建相应的目录
*.audit_trail=‘db‘
*.compatible=‘11.2.0.4.0‘
*.control_files=‘/u01/oracle/oradata/ MES/control01.ctl‘,‘/u01/oracle/fast_recovery_area/MES/control02.ctl‘
*.db_block_size=8192
*.db_domain=‘‘
*.db_name=‘MES‘
*.db_recovery_file_dest=‘/u01/oracle/fast_recovery_area‘
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest=‘/u01/oracle‘
*.dispatchers=‘(PROTOCOL=TCP) (SERVICE=CPPXDB)‘
*.memory_target=784334848
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile=‘EXCLUSIVE‘
*.undo_tablespace=‘UNDOTBS1‘
(4)启动目标数据到NOMOUNT状态
[email protected] dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Oct 15 12:08:33 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
ORACLE instance started.
Total System Global Area 780824576 bytes
Fixed Size 2257312 bytes
Variable Size 511708768 bytes
Database Buffers 260046848 bytes
Redo Buffers 6811648 bytes
(5)在源数据库上执行复制:
[[email protected] dbs]$ rman target / auxiliary sys/[email protected]/MES
Recovery Manager: Release 11.2.0.4.0 - Production on Wed Oct 15 13:31:54 2014
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: CPP (DBID=3676002607)
connected to auxiliary database: MES (not mounted)
RMAN> duplicate target database to mes from active database nofilenamecheck;
Starting Duplicate Db at 2014-10-15 13:32:20
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=129 device type=DISK
contents of Memory Script:
{
sql clone "create spfile from memory";
}
executing Memory Script
sql statement: create spfile from memory
contents of Memory Script:
{ shutdown clone immediate;
startup clone nomount;
}
executing Memory Script
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 780824576 bytes
Fixed Size 2257312 bytes
Variable Size 515903072 bytes
Database Buffers 255852544 bytes
Redo Buffers 6811648 bytes
contents of Memory Script:
{
sql clone "alter system set db_name =
‘‘CPP‘‘ comment=
‘‘Modified by RMAN duplicate‘‘ scope=spfile";
sql clone "alter system set db_unique_name =
‘‘MES‘‘ comment=
‘‘Modified by RMAN duplicate‘‘ scope=spfile";
shutdown clone immediate;
startup clone force nomount
backup as copy current controlfile auxiliary format ‘/u01/oracle/oradata/MES/control01.ctl‘;
restore clone controlfile to ‘/u01/oracle/fast_recovery_area/MES/control02.ctl‘ from
‘/u01/oracle/oradata/MES/control01.ctl‘;
alter clone database mount;
}
executing Memory Script
sql statement: alter system set db_name = ‘‘CPP‘‘ comment= ‘‘Modified by RMAN duplicate‘‘ scope=spfile
sql statement: alter system set db_unique_name = ‘‘MES‘‘ comment= ‘‘Modified by RMAN duplicate‘‘ scope=spfile
Oracle instance shut down
Oracle instance started
Total System Global Area 780824576 bytes
Fixed Size 2257312 bytes
Variable Size 515903072 bytes
Database Buffers 255852544 bytes
Redo Buffers 6811648 bytes
Starting backup at 2014-10-15 13:32:48
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=134 device type=DISK
channel ORA_DISK_1: starting datafile copy
copying current control file
output file name=/u01/oracle/product/11.2.0/db_1/dbs/snapcf_CPP.f tag=TAG20141015T133249 RECID=8 STAMP=861024770
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 2014-10-15 13:32:52
Starting restore at 2014-10-15 13:32:53
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=63 device type=DISK
channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 2014-10-15 13:32:55
database mounted
contents of Memory Script:
{
set newname for clone datafile 1 to new;
set newname for clone datafile 2 to new;
set newname for clone datafile 3 to new;
set newname for clone datafile 4 to new;
set newname for clone datafile 5 to new;
backup as copy reuse
datafile 1 auxiliary format new
datafile 2 auxiliary format new
datafile 3 auxiliary format new
datafile 4 auxiliary format new
datafile 5 auxiliary format new
;
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 2014-10-15 13:33:00
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/oracle/oradata/CPP/system01.dbf
output file name=/u01/oracle/oradata/MES/datafile/o1_mf_system_0jpl4cgd_.dbf tag=TAG20141015T133301
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:05
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u01/oracle/oradata/CPP/sysaux01.dbf
output file name=/u01/oracle/oradata/MES/datafile/o1_mf_sysaux_0kpl4cif_.dbf tag=TAG20141015T133301
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u01/oracle/oradata/CPP/example01.dbf
output file name=/u01/oracle/oradata/MES/datafile/o1_mf_example_0lpl4cjs_.dbf tag=TAG20141015T133301
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/oracle/oradata/CPP/undotbs01.dbf
output file name=/u01/oracle/oradata/MES/datafile/o1_mf_undotbs1_0mpl4ckv_.dbf tag=TAG20141015T133301
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:16
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/oracle/oradata/CPP/users01.dbf
output file name=/u01/oracle/oradata/MES/datafile/o1_mf_users_0npl4clf_.dbf tag=TAG20141015T133301
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2014-10-15 13:35:44
sql statement: alter system archive log current
contents of Memory Script:
{
backup as copy reuse
archivelog like "/arch/cpp/ARC0000000024_0861010994.0001_db1b592f.log" auxiliary format
"/arch/MES/ARC0000000024_0861010994.0001_db1b592f.log" ;
catalog clone archivelog "/arch/MES/ARC0000000024_0861010994.0001_db1b592f.log"; switch clone datafile all;
}
executing Memory Script
Starting backup at 2014-10-15 13:35:46
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log copy
input archived log thread=1 sequence=24 RECID=18 STAMP=861024945
output file name=/arch/MES/ARC0000000024_0861010994.0001_db1b592f.log RECID=0 STAMP=0
channel ORA_DISK_1: archived log copy complete, elapsed time: 00:00:01
Finished backup at 2014-10-15 13:35:47
cataloged archived log
archived log file name=/arch/MES/ARC0000000024_0861010994.0001_db1b592f.log RECID=18 STAMP=861024947
datafile 1 switched to datafile copy
input datafile copy RECID=8 STAMP=861024947 file name=/u01/oracle/oradata/MES/datafile/o1_mf_system_0jpl4cgd_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=9 STAMP=861024947 file name=/u01/oracle/oradata/MES/datafile/o1_mf_sysaux_0kpl4cif_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=10 STAMP=861024947 file name=/u01/oracle/oradata/MES/datafile/o1_mf_undotbs1_0mpl4ckv_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=11 STAMP=861024947 file name=/u01/oracle/oradata/MES/datafile/o1_mf_users_0npl4clf_.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=12 STAMP=861024947 file name=/u01/oracle/oradata/MES/datafile/o1_mf_example_0lpl4cjs_.dbf
contents of Memory Script:
{
set until scn 1028509;
recover
clone database
delete archivelog
;
}
executing Memory Script
executing command: SET until clause
Starting recover at 2014-10-15 13:35:48
using channel ORA_AUX_DISK_1
starting media recovery
archived log for thread 1 with sequence 24 is already on disk as file /arch/MES/ARC0000000024_0861010994.0001_db1b592f.log
archived log file name=/arch/MES/ARC0000000024_0861010994.0001_db1b592f.log thread=1 sequence=24
media recovery complete, elapsed time: 00:00:00
Finished recover at 2014-10-15 13:35:52
Oracle instance started
Total System Global Area 780824576 bytes
Fixed Size 2257312 bytes
Variable Size 515903072 bytes
Database Buffers 255852544 bytes
Redo Buffers 6811648 bytes
contents of Memory Script:
{
sql clone "alter system set db_name =
‘‘MES‘‘ 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
sql statement: alter system set db_name = ‘‘MES‘‘ 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 780824576 bytes
Fixed Size 2257312 bytes
Variable Size 515903072 bytes
Database Buffers 255852544 bytes
Redo Buffers 6811648 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "MES" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 SIZE 50 M ,
GROUP 2 SIZE 50 M ,
GROUP 3 SIZE 50 M
DATAFILE
‘/u01/oracle/oradata/MES/datafile/o1_mf_system_0jpl4cgd_.dbf‘
CHARACTER SET AL32UTF8
contents of Memory Script:
{
set newname for clone tempfile 1 to new;
switch clone tempfile all;
catalog clone datafilecopy "/u01/oracle/oradata/MES/datafile/o1_mf_sysaux_0kpl4cif_.dbf",
"/u01/oracle/oradata/MES/datafile/o1_mf_undotbs1_0mpl4ckv_.dbf",
"/u01/oracle/oradata/MES/datafile/o1_mf_users_0npl4clf_.dbf",
"/u01/oracle/oradata/MES/datafile/o1_mf_example_0lpl4cjs_.dbf";
switch clone datafile all;
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /u01/oracle/oradata/MES/datafile/o1_mf_temp_%u_.tmp in control file
cataloged datafile copy
datafile copy file name=/u01/oracle/oradata/MES/datafile/o1_mf_sysaux_0kpl4cif_.dbf RECID=1 STAMP=861024971
cataloged datafile copy
datafile copy file name=/u01/oracle/oradata/MES/datafile/o1_mf_undotbs1_0mpl4ckv_.dbf RECID=2 STAMP=861024971
cataloged datafile copy
datafile copy file name=/u01/oracle/oradata/MES/datafile/o1_mf_users_0npl4clf_.dbf RECID=3 STAMP=861024971
cataloged datafile copy
datafile copy file name=/u01/oracle/oradata/MES/datafile/o1_mf_example_0lpl4cjs_.dbf RECID=4 STAMP=861024971
datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=861024971 file name=/u01/oracle/oradata/MES/datafile/o1_mf_sysaux_0kpl4cif_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=861024971 file name=/u01/oracle/oradata/MES/datafile/o1_mf_undotbs1_0mpl4ckv_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=861024971 file name=/u01/oracle/oradata/MES/datafile/o1_mf_users_0npl4clf_.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=4 STAMP=861024971 file name=/u01/oracle/oradata/MES/datafile/o1_mf_example_0lpl4cjs_.dbf
contents of Memory Script:
{
Alter clone database open resetlogs;
}
executing Memory Script
database opened
Finished Duplicate Db at 2014-10-15 13:36:38