实践RMAN Duplicate功能异机复制数据库【10gR2】

  使用RMAN的DUPLICATE DATABASE功能可以,方便快捷的实现数据库的复制工作,它既可以实现完整的数据库克隆也可以只复制部分表空间,复制目的地可以本地也可以是异机,对于构建一个产品数据库的测试副本确实是个方便的选择,复制命令需要一条duplicate target database to …。

  现在实践一下使用duplicate功能将rac1机器上的trgdb数据库复制到rac2机器上并命名为dupdb,文件结构不相同。rac1:trgdb => rac2:dupdb。

我的环境:Redhat 5.2 + Oracle 10.2.0.1

1.首先需要在rac2机上建立相关的目录,最好和参数文件中参数对上。

[oracle@rac2 ~]$ mkdir -p /u01/app/oracle/oradata/dupdb/
[oracle@rac2 ~]$ mkdir -p /u01/app/oracle/admin/dupdb/adump
[oracle@rac2 ~]$ mkdir -p /u01/app/oracle/admin/dupdb/bdump
[oracle@rac2 ~]$ mkdir -p /u01/app/oracle/admin/dupdb/cdump
[oracle@rac2 ~]$ mkdir -p /u01/app/oracle/admin/dupdb/udump

2.在rac2上为dupdb数据库产生一个密码文件,以便后面从rac1上使用sysdba远程连接。

[oracle@rac2 dbs]$ orapwd file=orapwdupdb password=oracle entries=20;

3.复制trgdb的参数文件到rac2的$ORACLE_HOME/dbs目录下。

SYS@trgdb>create pfile from spfile;

File created.

[oracle@rac1 dbs]$ scp inittrgdb.ora rac2:$ORACLE_HOME/dbs/initdupdb.ora
oracle@rac2's password:
inittrgdb.ora                                 100%  904     0.9KB/s   00:00   

4.修改复制的参数文件,主要是修改db_name,control_files,和有关的目录位置,还有就是LOG_FILE_NAME_CONVERT,DB_FILE_NAME_CONVERT参数实现复制之后的数据文件的位置映射,当然如果不配置这两个参数也可以在rman的复制选项中指定。

[oracle@rac2 dbs]$ vi initdupdb.ora
*.audit_file_dest='/u01/app/oracle/admin/dupdb/adump'
*.background_dump_dest='/u01/app/oracle/admin/dupdb/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/u01/app/oracle/oradata/dupdb/control01.ctl','/u01/app/oracle/oradata/dupdb/control02.ctl','/u01/app/oracle/oradata/dupdb/control03.ctl'
*.core_dump_dest='/u01/app/oracle/admin/dupdb/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='dupdb'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=dupdbXDB)'
*.job_queue_processes=10
*.open_cursors=300
*.pga_aggregate_target=94371840
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=285212672
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/u01/app/oracle/admin/dupdb/udump'
DB_FILE_NAME_CONVERT=(/u01/app/oracle/oradata/trgdb/,/u01/app/oracle/oradata/dupdb/)
LOG_FILE_NAME_CONVERT=(/u01/app/oracle/oradata/trgdb/,/u01/app/oracle/oradata/dupdb/)

5.启动辅助实例到nomount

[oracle@rac2 ~]$ export ORACLE_SID=dupdb
[oracle@rac2 ~]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on Sun Sep 23 10:08:33 2012 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to an idle instance. SYS@dupdb> create spfile from pfile; File created. SYS@dupdb> startup nomount; ORACLE instance started. Total System Global Area 285212672 bytes Fixed Size 1218992 bytes Variable Size 92276304 bytes Database Buffers 188743680 bytes Redo Buffers 2973696 bytes

6.配置监听和连接串。

1 ) rac2机上的listener.ora中添加如下部分,否则nomount下的dupdb将无法连接。

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = dupdb)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0.1/db_1)
      (SID_NAME = dupdb)
    )
  )

2 ) 添加连接串到tnsnames.ora。

TRGDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = trgdb)
    )
  )

DUPDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = rac2)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = dupdb)
    )
  )

7.测试就可以开始连接辅助实例开始复制了,不过此处的前提是有一个可用的trgdb的完整性备份。

RMAN> list backup of database;

using target database control file instead of recovery catalog

List of Backup Sets
===================

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
7       Full    509.97M    DISK        00:00:18     23-SEP-12     
        BP Key: 7   Status: AVAILABLE  Compressed: NO  Tag: TAG20120923T103154
        Piece Name: /u01/app/oracle/backup/full_08nltm8r18.rmn
  List of Datafiles in backup set 7
  File LV Type Ckp SCN    Ckp Time  Name
---- -- ---- ---------- --------- ----
  1       Full 456254     23-SEP-12 /u01/app/oracle/oradata/trgdb/system01.dbf
  2       Full 456254     23-SEP-12 /u01/app/oracle/oradata/trgdb/undotbs01.dbf
  3       Full 456254     23-SEP-12 /u01/app/oracle/oradata/trgdb/sysaux01.dbf
  4       Full 456254     23-SEP-12 /u01/app/oracle/oradata/trgdb/users01.dbf

注:备份集是复制成功的重要环节,建议备份的时候就将数据文件和日志一块备份了,一下是我的备份方式:

RMAN> backup full database format '/u01/app/oracle/backup/full_%u%p%s.rmn' include current controlfile plus archivelog format '/u01/app/oracle/backup/arch_%u%p%s.rmn' delete all input;

8.复制备份集到rac2机相同目录中~,如果没有执行这步的会收到RMAN-06023: no backup or copy of datafile报错,如果是本地复制数据库的话也就没有这步了。

[oracle@rac1 oracle]$ scp -r backup/ rac2:/u01/app/oracle/

9.连接辅助实例和目标库。

[oracle@rac1 ~]$ rman target sys/oracle@trgdb auxiliary sys/oracle@dupdb

Recovery Manager: Release 10.2.0.1.0 - Production on Sun Sep 23 10:21:51 2012

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

connected to target database: TRGDB (DBID=1663074066)
connected to auxiliary database: DUPDB (not mounted)

10.执行复制语句,因为之前我配置了LOG_FILE_NAME_CONVERT,DB_FILE_NAME_CONVERT参数,所以这里命令很短,注意命令的输出中可以看到所有的转换过程。

RMAN> duplicate target database to dupdb;

Starting Duplicate Db at 23-SEP-12
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=159 devtype=DISK

contents of Memory Script:
{
   set until scn  456265;
   set newname for datafile  1 to 
 "/u01/app/oracle/oradata/dupdb/system01.dbf";
   set newname for datafile  2 to 
 "/u01/app/oracle/oradata/dupdb/undotbs01.dbf";
   set newname for datafile  3 to 
 "/u01/app/oracle/oradata/dupdb/sysaux01.dbf";
   set newname for datafile  4 to 
 "/u01/app/oracle/oradata/dupdb/users01.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

Starting restore at 23-SEP-12
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 /u01/app/oracle/oradata/dupdb/system01.dbf
restoring datafile 00002 to /u01/app/oracle/oradata/dupdb/undotbs01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/dupdb/sysaux01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/dupdb/users01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/backup/full_08nltm8r18.rmn
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/backup/full_08nltm8r18.rmn tag=TAG20120923T103154
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 23-SEP-12
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "DUPDB" RESETLOGS ARCHIVELOG 
  MAXLOGFILES     16
  MAXLOGMEMBERS      3
  MAXDATAFILES      100
  MAXINSTANCES     8
  MAXLOGHISTORY      292
 LOGFILE
  GROUP  1 ( '/u01/app/oracle/oradata/dupdb/redo01.log' ) SIZE 50 M  REUSE,
  GROUP  2 ( '/u01/app/oracle/oradata/dupdb/redo02.log' ) SIZE 50 M  REUSE,
  GROUP  3 ( '/u01/app/oracle/oradata/dupdb/redo03.log' ) SIZE 50 M  REUSE
 DATAFILE
  '/u01/app/oracle/oradata/dupdb/system01.dbf'
 CHARACTER SET AL32UTF8


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=794745571 filename=/u01/app/oracle/oradata/dupdb/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=794745571 filename=/u01/app/oracle/oradata/dupdb/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=794745571 filename=/u01/app/oracle/oradata/dupdb/users01.dbf

contents of Memory Script:
{
   set until scn  456265;
   recover
   clone database
    delete archivelog
   ;
}
executing Memory Script

executing command: SET until clause

Starting recover at 23-SEP-12
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=159 devtype=DISK

starting media recovery

channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=4
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/backup/arch_0anltm9m110.rmn
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/backup/arch_0anltm9m110.rmn tag=TAG20120923T103222
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
archive log filename=/u01/app/oracle/product/10.2.0.1/db_1/dbs/arch1_4_794741396.dbf thread=1 sequence=4
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0.1/db_1/dbs/arch1_4_794741396.dbf recid=1 stamp=794745572
media recovery complete, elapsed time: 00:00:03
Finished recover at 23-SEP-12

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     285212672 bytes

Fixed Size                     1218992 bytes
Variable Size                 92276304 bytes
Database Buffers             188743680 bytes
Redo Buffers                   2973696 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "DUPDB" RESETLOGS ARCHIVELOG 
  MAXLOGFILES     16
  MAXLOGMEMBERS      3
  MAXDATAFILES      100
  MAXINSTANCES     8
  MAXLOGHISTORY      292
 LOGFILE
  GROUP  1 ( '/u01/app/oracle/oradata/dupdb/redo01.log' ) SIZE 50 M  REUSE,
  GROUP  2 ( '/u01/app/oracle/oradata/dupdb/redo02.log' ) SIZE 50 M  REUSE,
  GROUP  3 ( '/u01/app/oracle/oradata/dupdb/redo03.log' ) SIZE 50 M  REUSE
 DATAFILE
  '/u01/app/oracle/oradata/dupdb/system01.dbf'
 CHARACTER SET AL32UTF8


contents of Memory Script:
{
   set newname for tempfile  1 to 
 "/u01/app/oracle/oradata/dupdb/temp01.dbf";
   switch clone tempfile all;
   catalog clone datafilecopy  "/u01/app/oracle/oradata/dupdb/undotbs01.dbf";
   catalog clone datafilecopy  "/u01/app/oracle/oradata/dupdb/sysaux01.dbf";
   catalog clone datafilecopy  "/u01/app/oracle/oradata/dupdb/users01.dbf";
   switch clone datafile all;
}
executing Memory Script

executing command: SET NEWNAME

renamed temporary file 1 to /u01/app/oracle/oradata/dupdb/temp01.dbf in control file

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/dupdb/undotbs01.dbf recid=1 stamp=794745582

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/dupdb/sysaux01.dbf recid=2 stamp=794745582

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/dupdb/users01.dbf recid=3 stamp=794745605

datafile 2 switched to datafile copy
input datafile copy recid=1 stamp=794745582 filename=/u01/app/oracle/oradata/dupdb/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=794745582 filename=/u01/app/oracle/oradata/dupdb/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=794745605 filename=/u01/app/oracle/oradata/dupdb/users01.dbf

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

database opened
Finished Duplicate Db at 23-SEP-12

11.复制完成,查看两个数据库的状态,可以看到产生的dbid是不同的。

SYS@trgdb>select instance_name,name db_name,status,dbid from v$instance,v$database;

INSTANCE_NAME    DB_NAME   STATUS             DBID
---------------- --------- ------------ ----------
trgdb            TRGDB     OPEN         1663074066

SYS@dupdb>select instance_name,name db_name,status,dbid from v$instance,v$database;

INSTANCE_NAME    DB_NAME   STATUS             DBID
---------------- --------- ------------ ----------
dupdb            DUPDB     OPEN          519959661

  尽管duplicate功能复制数据库很方便,不过如上方式创建的数据库和standby数据库还是完全不同的,duplicate数据库只是target database某个时刻的一个备份而已,无法像standby一样和主库传输日志完成数据同步一致,也不存在故障切换,当然duplicate也可以创建一个standby,只是在语法上有一定的区别,相对来说会比手动方式简单一点。

posted @ 2012-09-23 12:29  beanbee  阅读(535)  评论(0编辑  收藏  举报