oracle 11g duplicate from active database 复制数据库(三)

下面介绍将数据库复制到远程主机相同目录结构(且数据库名与原数据库名相同)的操作步骤
1.创建辅助实例的密码文件(这里辅助实例名为db) ,在目的主机上为辅助实例创建密码文件可以有以下选项:
.手动创建密码文件,对于duplicate … from active database有额外的要求。必须使用SYS用户ID并且密码必须与原数据库的密码相匹配。当想要使用单独的密码来创建密码文件时因此可以启动辅助实例并使用它来连接原数据库。

.在执行duplicate … from active database命令时指定password file选项,在这种情况下,RMAN将原数据库的密码文件复制到目的主机上并且覆盖辅助实例已经存在的密码文件。如果原数据库密码文件有多个密码且你想让它们在副本数据库中使用时这种技术是非常有用的。

[oracle11@jingyong1 dbs]$ orapwd file=/u03/app/oracle/11.2.0/db/dbs/orapwdb password=system entries=10;

2.创建辅助实例网络连接,修改监听文件,使用静态监听来监听辅助实例

[oracle11@jingyong1 admin]$ vi listener.ora


# listener.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = jingyong1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =

    (SID_DESC =
     (SID_NAME = db)
      (ORACLE_HOME =/u03/app/oracle/11.2.0/db)
    )
  )
[oracle11@jingyong1 admin]$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 05-JUN-2015 11:25:35

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Starting /u03/app/oracle/11.2.0/db/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Log messages written to /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jingyong1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                05-JUN-2015 11:25:35
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Listener Log File         /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "cs" has 1 instance(s).
  Instance "cs", status UNKNOWN, has 1 handler(s) for this service...
Service "db" has 1 instance(s).
  Instance "db", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

给辅助实例增加网络服务名

[oracle11@jingyong1 admin]$ vi tnsnames.ora
# tnsnames.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

db =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = db)
      (UR = A)
    )
  )

3.创建辅助实例的参数文件,初始化参数文件的目录和内容依赖于给复制文件命名选择的策略。可以选择建议的技术来对原主机和目的主机使用相同的命名策略。因此对于Oracle RAC环境,对于原主机和目的主机可以使用相同的ORACLE_SID。在参数文件中将db_name设置为任意值,db_name是唯一必须设置的参数。创建参数文件有以下选项:
.为辅助实例来创建文本参数文件,初始化参数文件的存储目录是在主机上操作系统特定的缺省目录。例如,在Linux和UNIX中缺省参数文件名是ORACLE_HOME/dbs/initORACLE_SID.ora,在Windows平台上参数文件名是ORACLE_HOME\database\initORACLE_SID.ora

.在执行duplicate命令时指定spfile子句。duplicate … spfile技术最简单,因为在执行复制时RMAN自动将原数据库的spfile文件复制到辅助实例或从备份中还原spfile文件。如果在辅助实例上存在spfile文件,那么RMAN就会覆盖它。
[oracle11@jingyong1 dbs]$ vi initdb.ora
db_name=db

上面只设置了db_name参数,因为我们在复制时使用spfile选项来复制原数据库的SPFILE参数文件

4.启动辅助实例,启动SQL*Plus并使用sysdba权限连接到辅助实例。将辅助实例启动到nomount状态(如果参数文件在缺省目录中startup命令不需要pfile参数)。注意:确保辅助实例使用文本参数文件来启动而不是SPFILE参数文件。不要创建控制文件或试图mount或open辅助实例。

[oracle11@jingyong1 dbs]$ export ORACLE_SID=db
[oracle11@jingyong1 dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 11:30:03 2015

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

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area  150654976 bytes
Fixed Size                  1363216 bytes
Variable Size              96469744 bytes
Database Buffers           50331648 bytes
Redo Buffers                2490368 bytes


5.在目标主机(运行被复制数据库的主机)配置辅助实例的网络服务名
[oracle11@oracle11g admin]$ vi tnsnames.ora
db =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME =db)
      (UR=A)
    )
  )

[oracle11@oracle11g admin]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 11:35:55 2015

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

SQL> conn sys/system@dest_db as sysdba
Connected.

6.加载或打开目标数据库,如果RMAN连接到原数据库作为目标数据库,那么原数据库为了执行复制必须设置为合适的状态。如果原数据库实例没有mount或open,那么将原数据库mount或open。如果执行active database复制,那么确保满足下面额外的条件:
-如果原数据库open,那么必须启用归档
-如果原数据库没有open,那么数据库不需要执行实例恢复
由于原数据库启用了归档所以可以将原数据库启动到open状态

[oracle11@jingyong1 dbs]$ export ORACLE_SID=db
[oracle11@jingyong1 dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 11:36:03 2015

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

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area  150654976 bytes
Fixed Size                  1363216 bytes
Variable Size              96469744 bytes
Database Buffers           50331648 bytes
Redo Buffers                2490368 bytes

7.使用duplicate命令来执行数据库复制。因为我们这里复制后数据库名为dup与原数据库名不相同,所以不用指定spfile子句从原数据库复制SPFILE参数文件。因为复制后的副本数据库与原数据库的文件拥有相同文件名,所以在执行duplicate命令时要使用nofilenamecheck选项。如果目录不同,在pfile里加这个2个参数进行转换:db_file_name_convert,log_file_name_convert。如果指定password file选项那么RMAN将会从原数据库复制密码文件到目的主机上。RMAN会自动复制SPFILE参数文件到目的主机,使用SPFILE文件来启动辅助实例,复制所有需要的数据库文件和归档重做日志文件到目的主机,并恢复数据库。最终,RMAN将使用resetlogs选项来创建联机重做日志。

[oracle11@oracle11g ~]$ rman target sys/system@db catalog rman/rman@cs auxiliary sys/system@dest_db

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Jun 5 11:55:56 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: DB (DBID=1644809111)
connected to recovery catalog database
connected to auxiliary database: DB (not mounted)

RMAN> duplicate target database to db from active database password file spfile nofilenamecheck;

Starting Duplicate Db at 2015-06-05 11:56:10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=19 device type=DISK

contents of Memory Script:
{
   backup as copy reuse
   targetfile  '/u03/app/oracle/11.2.0/db/dbs/orapwdb' auxiliary format
 '/u03/app/oracle/11.2.0/db/dbs/orapwdb'   targetfile
 '/u03/app/oracle/11.2.0/db/dbs/spfiledb.ora' auxiliary format
 '/u03/app/oracle/11.2.0/db/dbs/spfiledb.ora'   ;
   sql clone "alter system set spfile= ''/u03/app/oracle/11.2.0/db/dbs/spfiledb.ora''";
}
executing Memory Script

Starting backup at 2015-06-05 11:56:10
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=33 device type=DISK
Finished backup at 2015-06-05 11:56:13

sql statement: alter system set spfile= ''/u03/app/oracle/11.2.0/db/dbs/spfiledb.ora''

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DB'' comment=
 ''duplicate'' scope=spfile";
   shutdown clone immediate;
   startup clone nomount;
}
executing Memory Script

sql statement: alter system set  db_name =  ''DB'' comment= ''duplicate'' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area     422670336 bytes

Fixed Size                     1365068 bytes
Variable Size                318770100 bytes
Database Buffers              96468992 bytes
Redo Buffers                   6066176 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DB'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   sql clone "alter system set  db_unique_name =
 ''DB'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   shutdown clone immediate;
   startup clone force nomount
   backup as copy current controlfile auxiliary format  '/u03/app/oracle/oradata/db/control01.ctl';
   restore clone controlfile to  '/u03/app/oracle/fast_recovery_area/db/control02.ctl' from
 '/u03/app/oracle/oradata/db/control01.ctl';
   alter clone database mount;
}
executing Memory Script

sql statement: alter system set  db_name =  ''DB'' comment= ''Modified by RMAN duplicate'' scope=spfile

sql statement: alter system set  db_unique_name =  ''DB'' comment= ''Modified by RMAN duplicate'' scope=spfile

Oracle instance shut down

Oracle instance started

Total System Global Area     422670336 bytes

Fixed Size                     1365068 bytes
Variable Size                318770100 bytes
Database Buffers              96468992 bytes
Redo Buffers                   6066176 bytes

Starting backup at 2015-06-05 11:56:26
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying current control file
output file name=/u03/app/oracle/11.2.0/db/dbs/snapcf_db.f tag=TAG20150605T115626 RECID=5 STAMP=881582188
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 2015-06-05 11:56:29

Starting restore at 2015-06-05 11:56:29
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=19 device type=DISK

channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 2015-06-05 11:56:31

database mounted
RMAN-05538: WARNING: implicitly using DB_FILE_NAME_CONVERT

contents of Memory Script:
{
   set newname for datafile  1 to
 "/u03/app/oracle/oradata/db/system01.dbf";
   set newname for datafile  2 to
 "/u03/app/oracle/oradata/db/sysaux01.dbf";
   set newname for datafile  3 to
 "/u03/app/oracle/oradata/db/undotbs01.dbf";
   set newname for datafile  4 to
 "/u03/app/oracle/oradata/db/users01.dbf";
   set newname for datafile  5 to
 "/u03/app/oracle/oradata/db/test01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format
 "/u03/app/oracle/oradata/db/system01.dbf"   datafile
 2 auxiliary format
 "/u03/app/oracle/oradata/db/sysaux01.dbf"   datafile
 3 auxiliary format
 "/u03/app/oracle/oradata/db/undotbs01.dbf"   datafile
 4 auxiliary format
 "/u03/app/oracle/oradata/db/users01.dbf"   datafile
 5 auxiliary format
 "/u03/app/oracle/oradata/db/test01.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 2015-06-05 11:56:36
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
output file name=/u03/app/oracle/oradata/db/system01.dbf tag=TAG20150605T115636
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:55
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
output file name=/u03/app/oracle/oradata/db/sysaux01.dbf tag=TAG20150605T115636
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:55
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
output file name=/u03/app/oracle/oradata/db/undotbs01.dbf tag=TAG20150605T115636
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u03/app/oracle/oradata/db/test01.dbf
output file name=/u03/app/oracle/oradata/db/test01.dbf tag=TAG20150605T115636
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
output file name=/u03/app/oracle/oradata/db/users01.dbf tag=TAG20150605T115636
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 2015-06-05 11:58:45

sql statement: alter system archive log current

contents of Memory Script:
{
   backup as copy reuse
   archivelog like  "/u03/archivelog/1_20_881232587.dbf" auxiliary format
 "/u03/archivelog/1_20_881232587.dbf"   ;
   catalog clone archivelog  "/u03/archivelog/1_20_881232587.dbf";
   switch clone datafile all;
}
executing Memory Script

Starting backup at 2015-06-05 11:58:47
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log copy
input archived log thread=1 sequence=20 RECID=39 STAMP=881582326
output file name=/u03/archivelog/1_20_881232587.dbf RECID=0 STAMP=0
channel ORA_DISK_1: archived log copy complete, elapsed time: 00:00:01
Finished backup at 2015-06-05 11:58:49

cataloged archived log
archived log file name=/u03/archivelog/1_20_881232587.dbf RECID=39 STAMP=881582332

datafile 1 switched to datafile copy
input datafile copy RECID=5 STAMP=881582333 file name=/u03/app/oracle/oradata/db/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=6 STAMP=881582333 file name=/u03/app/oracle/oradata/db/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=7 STAMP=881582333 file name=/u03/app/oracle/oradata/db/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=8 STAMP=881582333 file name=/u03/app/oracle/oradata/db/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=9 STAMP=881582333 file name=/u03/app/oracle/oradata/db/test01.dbf

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

executing command: SET until clause

Starting recover at 2015-06-05 11:58:50
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 20 is already on disk as file /u03/archivelog/1_20_881232587.dbf
archived log file name=/u03/archivelog/1_20_881232587.dbf thread=1 sequence=20
media recovery complete, elapsed time: 00:00:01
Finished recover at 2015-06-05 11:58:53
Oracle instance started

Total System Global Area     422670336 bytes

Fixed Size                     1365068 bytes
Variable Size                318770100 bytes
Database Buffers              96468992 bytes
Redo Buffers                   6066176 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DB'' 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 =  ''DB'' 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     422670336 bytes

Fixed Size                     1365068 bytes
Variable Size                318770100 bytes
Database Buffers              96468992 bytes
Redo Buffers                   6066176 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "DB" 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
  '/u03/app/oracle/oradata/db/system01.dbf'
 CHARACTER SET ZHS16GBK


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

executing command: SET NEWNAME

renamed tempfile 1 to /u03/app/oracle/oradata/db/temp01.dbf in control file

cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/sysaux01.dbf RECID=1 STAMP=881582350
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/undotbs01.dbf RECID=2 STAMP=881582350
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/users01.dbf RECID=3 STAMP=881582350
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/test01.dbf RECID=4 STAMP=881582350

datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=881582350 file name=/u03/app/oracle/oradata/db/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=881582350 file name=/u03/app/oracle/oradata/db/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=881582350 file name=/u03/app/oracle/oradata/db/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=4 STAMP=881582350 file name=/u03/app/oracle/oradata/db/test01.dbf

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

database opened
Finished Duplicate Db at 2015-06-05 11:59:32

oracle 11g duplicate from active database 复制数据库(二)

下面介绍将数据库复制到远程主机相同目录结构的操作步骤
1.创建辅助实例的密码文件(这里辅助实例名为dup) ,在目的主机上为辅助实例创建密码文件可以有以下选项:
.手动创建密码文件,对于duplicate … from active database有额外的要求。必须使用SYS用户ID并且密码必须与原数据库的密码相匹配。当想要使用单独的密码来创建密码文件时因此可以启动辅助实例并使用它来连接原数据库。

.在执行duplicate … from active database命令时指定password file选项,在这种情况下,RMAN将原数据库的密码文件复制到目的主机上并且覆盖辅助实例已经存在的密码文件。如果原数据库密码文件有多个密码且你想让它们在副本数据库中使用时这种技术是非常有用的。

[oracle11@jingyong1 dbs]$ orapwd file=/u03/app/oracle/11.2.0/db/dbs/orapwdup password=system entries=10;

[oracle11@jingyong1 dbs]$ ls -lrt orapwdup
-rw-r----- 1 oracle11 oinstall 2560 Jun  5 09:40 orapwdup

2.创建辅助实例网络连接,修改监听文件,使用静态监听来监听辅助实例

[oracle11@jingyong1 admin]$ vi listener.ora


# listener.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = jingyong1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =

    (SID_DESC =
     (SID_NAME = dup)
      (ORACLE_HOME =/u03/app/oracle/11.2.0/db)
    )
  )

[oracle11@jingyong1 admin]$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 05-JUN-2015 11:25:35

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Starting /u03/app/oracle/11.2.0/db/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Log messages written to /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jingyong1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                05-JUN-2015 11:25:35
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Listener Log File         /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "cs" has 1 instance(s).
  Instance "cs", status UNKNOWN, has 1 handler(s) for this service...
Service "dup" has 1 instance(s).
  Instance "dup", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

给辅助实例增加网络服务名

[oracle11@jingyong1 admin]$ vi tnsnames.ora
# tnsnames.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

dup =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = dup)
      (UR = A)
    )
  )

3.创建辅助实例的参数文件,初始化参数文件的目录和内容依赖于给复制文件命名选择的策略。可以选择建议的技术来对原主机和目的主机使用相同的命名策略。因此对于Oracle RAC环境,对于原主机和目的主机可以使用相同的ORACLE_SID。在参数文件中将db_name设置为任意值,db_name是唯一必须设置的参数。创建参数文件有以下选项:
.为辅助实例来创建文本参数文件,初始化参数文件的存储目录是在主机上操作系统特定的缺省目录。例如,在Linux和UNIX中缺省参数文件名是ORACLE_HOME/dbs/initORACLE_SID.ora,在Windows平台上参数文件名是ORACLE_HOME\database\initORACLE_SID.ora

.在执行duplicate命令时指定spfile子句。duplicate … spfile技术最简单,因为在执行复制时RMAN自动将原数据库的spfile文件复制到辅助实例或从备份中还原spfile文件。如果在辅助实例上存在spfile文件,那么RMAN就会覆盖它。

[oracle11@jingyong1 dbs]$ vi initdup.ora
db_name=dup
db_unique_name=dup
control_files= /u03/app/oracle/oradata/db/control01.ctl
remote_login_passwordfile=exclusive
compatible = 11.2.0.4.0
db_block_size=8192
sga_target=300M
sga_max_size=300M
pga_aggregate_target=32M

4.启动辅助实例,启动SQL*Plus并使用sysdba权限连接到辅助实例。将辅助实例启动到nomount状态(如果参数文件在缺省目录中startup命令不需要pfile参数)。注意:确保辅助实例使用文本参数文件来启动而不是SPFILE参数文件。不要创建控制文件或试图mount或open辅助实例。

[oracle11@jingyong1 dbs]$ export ORACLE_SID=dup
[oracle11@jingyong1 dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 09:58:16 2015

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

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area  313860096 bytes
Fixed Size                  1364340 bytes
Variable Size             104861324 bytes
Database Buffers          201326592 bytes
Redo Buffers                6307840 bytes



5.在目标主机(运行被复制数据库的主机)配置辅助实例的网络服务名
[oracle11@oracle11g admin]$ vi tnsnames.ora
dup =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME =dup)
      (UR=A)
    )
  )

[oracle11@oracle11g admin]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 10:05:14 2015

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

SQL> conn sys/system@dup as sysdba
Connected.

6.加载或打开目标数据库,如果RMAN连接到原数据库作为目标数据库,那么原数据库为了执行复制必须设置为合适的状态。如果原数据库实例没有mount或open,那么将原数据库mount或open。如果执行active database复制,那么确保满足下面额外的条件:
-如果原数据库open,那么必须启用归档
-如果原数据库没有open,那么数据库不需要执行实例恢复
由于原数据库启用了归档所以可以将原数据库启动到open状态

[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 08:35:45 2015

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  422670336 bytes
Fixed Size                  1365068 bytes
Variable Size             310381492 bytes
Database Buffers          104857600 bytes
Redo Buffers                6066176 bytes
Database mounted.
Database opened.

7.使用duplicate命令来执行数据库复制。因为我们这里复制后数据库名为dup与原数据库名不相同,所以不用指定spfile子句从原数据库复制SPFILE参数文件。因为复制后的副本数据库与原数据库的文件拥有相同文件名,所以在执行duplicate命令时要使用nofilenamecheck选项。如果目录不同,在pfile里加这个2个参数进行转换:db_file_name_convert,log_file_name_convert。如果指定password file选项那么RMAN将会从原数据库复制密码文件到目的主机上。RMAN会自动复制SPFILE参数文件到目的主机,使用SPFILE文件来启动辅助实例,复制所有需要的数据库文件和归档重做日志文件到目的主机,并恢复数据库。最终,RMAN将使用resetlogs选项来创建联机重做日志。

[oracle11@oracle11g ~]$ rman target sys/system@db catalog rman/rman@cs auxiliary sys/system@dup

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Jun 5 10:54:38 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: DB (DBID=1644809111)
connected to recovery catalog database
connected to auxiliary database: DUP (not mounted)

[oracle11@oracle11g ~]$ rman target sys/system@db catalog rman/rman@cs auxiliary sys/system@dup

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Jun 5 11:09:58 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: DB (DBID=1644809111)
connected to recovery catalog database
connected to auxiliary database: DUP (not mounted)

RMAN> duplicate target database to dup from active database nofilenamecheck;

Starting Duplicate Db at 2015-06-05 11:10:13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=20 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     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DB'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   sql clone "alter system set  db_unique_name =
 ''DUP'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   shutdown clone immediate;
   startup clone force nomount
   backup as copy current controlfile auxiliary format  '/u03/app/oracle/oradata/db/control01.ctl';
   alter clone database mount;
}
executing Memory Script

sql statement: alter system set  db_name =  ''DB'' comment= ''Modified by RMAN duplicate'' scope=spfile

sql statement: alter system set  db_unique_name =  ''DUP'' comment= ''Modified by RMAN duplicate'' scope=spfile

Oracle instance shut down

Oracle instance started

Total System Global Area     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

Starting backup at 2015-06-05 11:10:32
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=32 device type=DISK
channel ORA_DISK_1: starting datafile copy
copying current control file
output file name=/u03/app/oracle/11.2.0/db/dbs/snapcf_db.f tag=TAG20150605T111033 RECID=4 STAMP=881579434
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 2015-06-05 11:10:36

database mounted

contents of Memory Script:
{
   set newname for datafile  1 to
 "/u03/app/oracle/oradata/db/system01.dbf";
   set newname for datafile  2 to
 "/u03/app/oracle/oradata/db/sysaux01.dbf";
   set newname for datafile  3 to
 "/u03/app/oracle/oradata/db/undotbs01.dbf";
   set newname for datafile  4 to
 "/u03/app/oracle/oradata/db/users01.dbf";
   set newname for datafile  5 to
 "/u03/app/oracle/oradata/db/test01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format
 "/u03/app/oracle/oradata/db/system01.dbf"   datafile
 2 auxiliary format
 "/u03/app/oracle/oradata/db/sysaux01.dbf"   datafile
 3 auxiliary format
 "/u03/app/oracle/oradata/db/undotbs01.dbf"   datafile
 4 auxiliary format
 "/u03/app/oracle/oradata/db/users01.dbf"   datafile
 5 auxiliary format
 "/u03/app/oracle/oradata/db/test01.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 2015-06-05 11:10:42
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
output file name=/u03/app/oracle/oradata/db/system01.dbf tag=TAG20150605T111042
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:56
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
output file name=/u03/app/oracle/oradata/db/sysaux01.dbf tag=TAG20150605T111042
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:05
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
output file name=/u03/app/oracle/oradata/db/undotbs01.dbf tag=TAG20150605T111042
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u03/app/oracle/oradata/db/test01.dbf
output file name=/u03/app/oracle/oradata/db/test01.dbf tag=TAG20150605T111042
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
output file name=/u03/app/oracle/oradata/db/users01.dbf tag=TAG20150605T111042
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-06-05 11:13:07

sql statement: alter system archive log current

contents of Memory Script:
{
   backup as copy reuse
   archivelog like  "/u03/archivelog/1_19_881232587.dbf" auxiliary format
 "/u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf"   ;
   catalog clone archivelog  "/u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf";
   switch clone datafile all;
}
executing Memory Script

Starting backup at 2015-06-05 11:13:11
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log copy
input archived log thread=1 sequence=19 RECID=38 STAMP=881579588
output file name=/u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf RECID=0 STAMP=0
channel ORA_DISK_1: archived log copy complete, elapsed time: 00:00:01
Finished backup at 2015-06-05 11:13:12

cataloged archived log
archived log file name=/u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf RECID=38 STAMP=881579595

datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=881579595 file name=/u03/app/oracle/oradata/db/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=5 STAMP=881579595 file name=/u03/app/oracle/oradata/db/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=6 STAMP=881579595 file name=/u03/app/oracle/oradata/db/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=7 STAMP=881579595 file name=/u03/app/oracle/oradata/db/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=8 STAMP=881579595 file name=/u03/app/oracle/oradata/db/test01.dbf

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

executing command: SET until clause

Starting recover at 2015-06-05 11:13:13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=18 device type=DISK

starting media recovery

archived log for thread 1 with sequence 19 is already on disk as file /u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf
archived log file name=/u03/app/oracle/11.2.0/db/dbs/arch1_19_881232587.dbf thread=1 sequence=19
media recovery complete, elapsed time: 00:00:00
Finished recover at 2015-06-05 11:13:17
Oracle instance started

Total System Global Area     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DUP'' 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 =  ''DUP'' 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     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "DUP" RESETLOGS ARCHIVELOG
  MAXLOGFILES     16
  MAXLOGMEMBERS      3
  MAXDATAFILES      100
  MAXINSTANCES     8
  MAXLOGHISTORY      292
 LOGFILE
  GROUP   1 ( '/u03/app/oracle/oradata/db/redo01.log' ) SIZE 50 M  REUSE,
  GROUP   2 ( '/u03/app/oracle/oradata/db/redo02.log' ) SIZE 50 M  REUSE,
  GROUP   3 ( '/u03/app/oracle/oradata/db/redo03.log' ) SIZE 50 M  REUSE
 DATAFILE
  '/u03/app/oracle/oradata/db/system01.dbf'
 CHARACTER SET ZHS16GBK


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

executing command: SET NEWNAME

renamed tempfile 1 to /u03/app/oracle/oradata/db/temp01.dbf in control file

cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/sysaux01.dbf RECID=1 STAMP=881579617
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/undotbs01.dbf RECID=2 STAMP=881579617
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/users01.dbf RECID=3 STAMP=881579617
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/db/test01.dbf RECID=4 STAMP=881579617

datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=881579617 file name=/u03/app/oracle/oradata/db/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=881579617 file name=/u03/app/oracle/oradata/db/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=881579617 file name=/u03/app/oracle/oradata/db/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=4 STAMP=881579617 file name=/u03/app/oracle/oradata/db/test01.dbf

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

database opened
Finished Duplicate Db at 2015-06-05 11:13:55

oracle 11g duplicate from active database 复制数据库(一)

使用duplicate from active database复制数据库
在oracle 11g中复制数据库有了两种方法,一是使用基于备份的复制,一种是active database方式来复制。执行active database复制,RMAN连接到原数据库实例作为target,连接到辅助实例作为auxiliary。RMAN将通过网络将active database复制到辅助实例,从而创建副本数据库并且不需要原数据库的备份。在复制数据时可以将数据库复制到与原数据库相同的主机,也可以将数据库复制到远程主机。

下面介绍将数据库复制到远程主机不同目录结构的操作步骤
1.创建辅助实例的密码文件(这里辅助实例名为dup) ,在目的主机上为辅助实例创建密码文件可以有以下选项:
.手动创建密码文件,对于duplicate … from active database有额外的要求。必须使用SYS用户ID并且密码必须与原数据库的密码相匹配。当想要使用单独的密码来创建密码文件时因此可以启动辅助实例并使用它来连接原数据库。

.在执行duplicate … from active database命令时指定password file选项,在这种情况下,RMAN将原数据库的密码文件复制到目的主机上并且覆盖辅助实例已经存在的密码文件。如果原数据库密码文件有多个密码且你想让它们在副本数据库中使用时这种技术是非常有用的。

[oracle11@jingyong1 dbs]$ orapwd file=/u03/app/oracle/11.2.0/db/dbs/orapwdup password=system entries=10;

[oracle11@jingyong1 dbs]$ ls -lrt orapwdup
-rw-r----- 1 oracle11 oinstall 2560 Jun  5 09:40 orapwdup

2.创建辅助实例网络连接,修改监听文件,使用静态监听来监听辅助实例

[oracle11@jingyong1 admin]$ vi listener.ora


# listener.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = jingyong1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =

    (SID_DESC =
     (SID_NAME = dup)
      (ORACLE_HOME =/u03/app/oracle/11.2.0/db)
    )
  )

[oracle11@jingyong1 admin]$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 05-JUN-2015 11:25:35

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Starting /u03/app/oracle/11.2.0/db/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Log messages written to /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jingyong1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                05-JUN-2015 11:25:35
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u03/app/oracle/11.2.0/db/network/admin/listener.ora
Listener Log File         /u03/app/oracle/diag/tnslsnr/jingyong1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jingyong1)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "cs" has 1 instance(s).
  Instance "cs", status UNKNOWN, has 1 handler(s) for this service...
Service "dup" has 1 instance(s).
  Instance "dup", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

给辅助实例增加网络服务名

[oracle11@jingyong1 admin]$ vi tnsnames.ora
# tnsnames.ora Network Configuration File: /u03/app/oracle/11.2.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

dup =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = dup)
      (UR = A)
    )
  )

3.创建辅助实例的参数文件,初始化参数文件的目录和内容依赖于给复制文件命名选择的策略。可以选择建议的技术来对原主机和目的主机使用相同的命名策略。因此对于Oracle RAC环境,对于原主机和目的主机可以使用相同的ORACLE_SID。在参数文件中将db_name设置为任意值,db_name是唯一必须设置的参数。创建参数文件有以下选项:
.为辅助实例来创建文本参数文件,初始化参数文件的存储目录是在主机上操作系统特定的缺省目录。例如,在Linux和UNIX中缺省参数文件名是ORACLE_HOME/dbs/initORACLE_SID.ora,在Windows平台上参数文件名是ORACLE_HOME\database\initORACLE_SID.ora

.在执行duplicate命令时指定spfile子句。duplicate … spfile技术最简单,因为在执行复制时RMAN自动将原数据库的spfile文件复制到辅助实例或从备份中还原spfile文件。如果在辅助实例上存在spfile文件,那么RMAN就会覆盖它。

[oracle11@jingyong1 dbs]$ vi initdup.ora
db_name=dup
db_unique_name=dup
control_files= /u03/app/oracle/oradata/dup/control01.ctl
remote_login_passwordfile=exclusive
compatible = 11.2.0.4.0
db_block_size=8192
sga_target=300M
sga_max_size=300M
pga_aggregate_target=32M
db_file_name_convert=('/u03/app/oracle/oradata/db/','/u03/app/oracle/oradata/dup/')
log_file_name_convert=('/u03/app/oracle/oradata/db/','/u03/app/oracle/oradata/dup/')

4.启动辅助实例,启动SQL*Plus并使用sysdba权限连接到辅助实例。将辅助实例启动到nomount状态(如果参数文件在缺省目录中startup命令不需要pfile参数)。注意:确保辅助实例使用文本参数文件来启动而不是SPFILE参数文件。不要创建控制文件或试图mount或open辅助实例。

[oracle11@jingyong1 dbs]$ export ORACLE_SID=dup
[oracle11@jingyong1 dbs]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 09:58:16 2015

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

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area  313860096 bytes
Fixed Size                  1364340 bytes
Variable Size             104861324 bytes
Database Buffers          201326592 bytes
Redo Buffers                6307840 bytes

5.在目标主机(运行被复制数据库的主机)配置辅助实例的网络服务名
[oracle11@oracle11g admin]$ vi tnsnames.ora
dup =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.56.11)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME =dup)
      (UR=A)
    )
  )

[oracle11@oracle11g admin]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 10:05:14 2015

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

SQL> conn sys/system@dup as sysdba
Connected.

6.加载或打开目标数据库,如果RMAN连接到原数据库作为目标数据库,那么原数据库为了执行复制必须设置为合适的状态。如果原数据库实例没有mount或open,那么将原数据库mount或open。如果执行active database复制,那么确保满足下面额外的条件:
-如果原数据库open,那么必须启用归档
-如果原数据库没有open,那么数据库不需要执行实例恢复
由于原数据库启用了归档所以可以将原数据库启动到open状态

[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Jun 5 08:35:45 2015

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

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  422670336 bytes
Fixed Size                  1365068 bytes
Variable Size             310381492 bytes
Database Buffers          104857600 bytes
Redo Buffers                6066176 bytes
Database mounted.
Database opened.

7.使用duplicate命令来执行数据库复制。因为我们这里复制后数据库名为dup与原数据库名不相同,所以不用指定spfile子句从原数据库复制SPFILE参数文件。因为复制后的副本数据库与原数据库的文件拥有相同文件名,所以在执行duplicate命令时要使用nofilenamecheck选项。如果目录不同,在pfile里加这个2个参数进行转换:db_file_name_convert,log_file_name_convert。如果指定password file选项那么RMAN将会从原数据库复制密码文件到目的主机上。RMAN会自动复制SPFILE参数文件到目的主机,使用SPFILE文件来启动辅助实例,复制所有需要的数据库文件和归档重做日志文件到目的主机,并恢复数据库。最终,RMAN将使用resetlogs选项来创建联机重做日志。

[oracle11@oracle11g ~]$ rman target sys/system@db catalog rman/rman@cs auxiliary sys/system@dup

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Jun 5 10:54:38 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: DB (DBID=1644809111)
connected to recovery catalog database
connected to auxiliary database: DUP (not mounted)

RMAN> duplicate target database to dup from active database nofilenamecheck;

Starting Duplicate Db at 2015-06-05 10:54:43
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=20 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     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DB'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   sql clone "alter system set  db_unique_name =
 ''DUP'' comment=
 ''Modified by RMAN duplicate'' scope=spfile";
   shutdown clone immediate;
   startup clone force nomount
   backup as copy current controlfile auxiliary format  '/u03/app/oracle/oradata/dup/control01.ctl';
   alter clone database mount;
}
executing Memory Script

sql statement: alter system set  db_name =  ''DB'' comment= ''Modified by RMAN duplicate'' scope=spfile

sql statement: alter system set  db_unique_name =  ''DUP'' comment= ''Modified by RMAN duplicate'' scope=spfile

Oracle instance shut down

Oracle instance started

Total System Global Area     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

Starting backup at 2015-06-05 10:55:02
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=38 device type=DISK
channel ORA_DISK_1: starting datafile copy
copying current control file
output file name=/u03/app/oracle/11.2.0/db/dbs/snapcf_db.f tag=TAG20150605T105502 RECID=3 STAMP=881578506
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
Finished backup at 2015-06-05 10:55:11

database mounted

contents of Memory Script:
{
   set newname for datafile  1 to
 "/u03/app/oracle/oradata/dup/system01.dbf";
   set newname for datafile  2 to
 "/u03/app/oracle/oradata/dup/sysaux01.dbf";
   set newname for datafile  3 to
 "/u03/app/oracle/oradata/dup/undotbs01.dbf";
   set newname for datafile  4 to
 "/u03/app/oracle/oradata/dup/users01.dbf";
   set newname for datafile  5 to
 "/u03/app/oracle/oradata/dup/test01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format
 "/u03/app/oracle/oradata/dup/system01.dbf"   datafile
 2 auxiliary format
 "/u03/app/oracle/oradata/dup/sysaux01.dbf"   datafile
 3 auxiliary format
 "/u03/app/oracle/oradata/dup/undotbs01.dbf"   datafile
 4 auxiliary format
 "/u03/app/oracle/oradata/dup/users01.dbf"   datafile
 5 auxiliary format
 "/u03/app/oracle/oradata/dup/test01.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 2015-06-05 10:55:16
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
output file name=/u03/app/oracle/oradata/dup/system01.dbf tag=TAG20150605T105516
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=/u03/app/oracle/oradata/db/sysaux01.dbf
output file name=/u03/app/oracle/oradata/dup/sysaux01.dbf tag=TAG20150605T105516
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:55
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
output file name=/u03/app/oracle/oradata/dup/undotbs01.dbf tag=TAG20150605T105516
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u03/app/oracle/oradata/db/test01.dbf
output file name=/u03/app/oracle/oradata/dup/test01.dbf tag=TAG20150605T105516
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
output file name=/u03/app/oracle/oradata/dup/users01.dbf tag=TAG20150605T105516
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-06-05 10:57:41

sql statement: alter system archive log current

contents of Memory Script:
{
   backup as copy reuse
   archivelog like  "/u03/archivelog/1_18_881232587.dbf" auxiliary format
 "/u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf"   ;
   catalog clone archivelog  "/u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf";
   switch clone datafile all;
}
executing Memory Script

Starting backup at 2015-06-05 10:57:44
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log copy
input archived log thread=1 sequence=18 RECID=37 STAMP=881578662
output file name=/u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf RECID=0 STAMP=0
channel ORA_DISK_1: archived log copy complete, elapsed time: 00:00:01
Finished backup at 2015-06-05 10:57:45

cataloged archived log
archived log file name=/u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf RECID=37 STAMP=881578668

datafile 1 switched to datafile copy
input datafile copy RECID=3 STAMP=881578668 file name=/u03/app/oracle/oradata/dup/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=4 STAMP=881578668 file name=/u03/app/oracle/oradata/dup/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=881578668 file name=/u03/app/oracle/oradata/dup/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=881578668 file name=/u03/app/oracle/oradata/dup/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=7 STAMP=881578668 file name=/u03/app/oracle/oradata/dup/test01.dbf

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

executing command: SET until clause

Starting recover at 2015-06-05 10:57:46
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=18 device type=DISK

starting media recovery

archived log for thread 1 with sequence 18 is already on disk as file /u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf
archived log file name=/u03/app/oracle/11.2.0/db/dbs/arch1_18_881232587.dbf thread=1 sequence=18
media recovery complete, elapsed time: 00:00:00
Finished recover at 2015-06-05 10:57:50
Oracle instance started

Total System Global Area     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes

contents of Memory Script:
{
   sql clone "alter system set  db_name =
 ''DUP'' 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 =  ''DUP'' 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     313860096 bytes

Fixed Size                     1364340 bytes
Variable Size                109055628 bytes
Database Buffers             197132288 bytes
Redo Buffers                   6307840 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "DUP" RESETLOGS ARCHIVELOG
  MAXLOGFILES     16
  MAXLOGMEMBERS      3
  MAXDATAFILES      100
  MAXINSTANCES     8
  MAXLOGHISTORY      292
 LOGFILE
  GROUP   1 ( '/u03/app/oracle/oradata/dup/redo01.log' ) SIZE 50 M  REUSE,
  GROUP   2 ( '/u03/app/oracle/oradata/dup/redo02.log' ) SIZE 50 M  REUSE,
  GROUP   3 ( '/u03/app/oracle/oradata/dup/redo03.log' ) SIZE 50 M  REUSE
 DATAFILE
  '/u03/app/oracle/oradata/dup/system01.dbf'
 CHARACTER SET ZHS16GBK


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

executing command: SET NEWNAME

renamed tempfile 1 to /u03/app/oracle/oradata/dup/temp01.dbf in control file

cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/dup/sysaux01.dbf RECID=1 STAMP=881578689
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/dup/undotbs01.dbf RECID=2 STAMP=881578689
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/dup/users01.dbf RECID=3 STAMP=881578689
cataloged datafile copy
datafile copy file name=/u03/app/oracle/oradata/dup/test01.dbf RECID=4 STAMP=881578689

datafile 2 switched to datafile copy
input datafile copy RECID=1 STAMP=881578689 file name=/u03/app/oracle/oradata/dup/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=2 STAMP=881578689 file name=/u03/app/oracle/oradata/dup/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=3 STAMP=881578689 file name=/u03/app/oracle/oradata/dup/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=4 STAMP=881578689 file name=/u03/app/oracle/oradata/dup/test01.dbf

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

database opened
Finished Duplicate Db at 2015-06-05 10:58:31

Oracle 11g创建虚拟私有目录RMAN-06004 ORA-00942错误的处理

一个网友在linux下的11.2.0.4 中创建RMAN的虚拟私有目录报错:

[oracle@jb ~]$ rman

Recovery Manager: Release 11.2.0.4.0 - Production on Sun May 10 17:23:25 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

RMAN> connect catalog vpc1/vpc1@cs

connected to recovery catalog database

RMAN> create virtual catalog;

found eligible base catalog owned by RMAN
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-06004: ORACLE error from recovery catalog database: ORA-00942: table or view does not exist

从上面信息可以看到found eligible base catalog owned by RMAN,说明已经找到了合格的基本恢复目录用户,由于使用的基本恢复目录用户rman已经注册了数据库,于是新建一个基本恢复目录用户cat1,虚拟私有目录用户 virtcat

1.创建基本恢复目录用户cat1,虚拟私有目录用户virtcat

SQL> create user cat1 identified by cat1
  2       default tablespace sysaux
  3       quota unlimited on sysaux;

User created.

SQL> grant recovery_catalog_owner to cat1;

Grant succeeded.

SQL> create user virtcat identified by virtcat
  2       default tablespace sysaux
  3       quota unlimited on sysaux;

User created.

SQL> grant recovery_catalog_owner to virtcat;

Grant succeeded.

2.创建基本恢复目录

[oracle@jb ~]$ rman catalog cat1/cat1@cs

Recovery Manager: Release 11.2.0.4.0 - Production on Sun May 10 17:24:50 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to recovery catalog database

RMAN> create catalog;

recovery catalog created

RMAN> grant register database to virtcat;

Grant succeeded.

3.创建虚拟私有恢复目录

[oracle@jb cs]$ rman catalog virtcat/virtcat@cs

Recovery Manager: Release 11.2.0.4.0 - Production on Sun May 10 17:25:40 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to recovery catalog database

RMAN> create virtual catalog;

found ineligible base catalog owned by RMAN
found eligible base catalog owned by CAT1
created virtual catalog against base catalog owned by CAT1

4.在虚拟私有目录中注册目标数据库

[oracle@jb cs]$ rman target sys/system@db catalog virtcat/virtcat@cs

Recovery Manager: Release 11.2.0.4.0 - Production on Sun May 10 17:46:51 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: DB (DBID=1640573015)
connected to recovery catalog database

RMAN> register database;

database registered in recovery catalog
starting full resync of recovery catalog
full resync complete

总结:在创建与使用虚拟私有恢复目录时,基本恢复目录中不能注册目标数据库,否则会出现RMAN-06004, ORA-00942错误信息。

oracle 还原点

正常还原点
创建正常还原点是给一个SCN或指定时间点指定一个还原点名称。因此,还原点实际上可以看作是SCN的标记或别名。在执行任何操作之前你可能想要撤消该操作,你可以创建一个正常还原点。控制文件会存储还原点名称与SCN,创建还原点可以消除提前记录SCN或在使用闪回时需要判断正确的SCN操作。

如果使用闪回或按时间点恢复,那么你可以使用还原点名称来代夫时间表达式或SCN。下面的命令支持使用还原点:
1.RMAN中的recover database与flashback database命令
2.SQL中的flashback table语句

正常的还原点是轻量级的。控制文件可以维护上千个正常还原点而不会影响数据库性能。正常还原点如果不手动删除,当执行恢复不再需要它们时会从控制文件中将其删除。
下面举例说明:
1.创建正常还原点

SQL> create restore point insert_point;

Restore point created.


SQL> set long 200
SQL> set linesize 200
SQL> SELECT NAME, SCN, TIME, DATABASE_INCARNATION#,
  2  GUARANTEE_FLASHBACK_DATABASE,STORAGE_SIZE
  3  FROM V$RESTORE_POINT;

NAME                        SCN TIME                                                                        DATABASE_INCARNATION# GUA STORAGE_SIZE
-------------------- ---------- --------------------------------------------------------------------------- --------------------- --- ------------
INSERT_POINT            1187948 23-APR-15 05.18.21.000000000 PM                                                                 3 NO             0

SQL> alter system checkpoint;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1187983 2015-04-23 17:18:59

2.新创建一个表test:

SQL> create table test as select * from dba_objects;

Table created.

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1188096 2015-04-23 17:20:20

3.将数据库闪回到创建正常还原点的时间点

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.
SQL> flashback database to scn 1187983;

Flashback complete.

SQL> alter database open read only;

Database altered.

4.检查表test是否存在,因为表test是在创建还原点之后所创建,当将数据库闪回到还原点后表test也就不存了:

SQL> select count(*) from test;
select count(*) from test
                     *
ERROR at line 1:
ORA-00942: table or view does not exist

受保护还原点
像正常还原点一样,受保护的还原点也是恢复操作中所指定SCN的一个别名。唯一的区别是受保护的还原点除了显式删除之外是不会从控制文件中删除的。通常来说,使用正常还原点执行的命令也可以使用受保护还原点来作为SCN的别名,除了另有说明之外,使用正常还原点与受保护还原点的使用方法是一样的

即使生成闪回日志被禁用,受保护还原点确保你能使用闪回数据库将数据文件恢复到还原点SCN所对应的状态。如果启用闪回日志,那么受保护的还原点会强制保留将数据库闪回到在最早创建的受保护还原点后的任意SCN所需要的闪回日志。因此,如果启用了闪回日志,可以将数据库闪回到受保护还原点后的任意SCN而不只是单个SCN。如果禁用了闪回日志,那么在执行flashback database命令时不能直接指定受保护还原点与当前时间之间的SCN。然后可以先闪回到受保护还原点再恢复到受保护还原点与当前时间之前的SCN。

闪回数据库的命令是flashback database to PIT,PIT可以是SCN、时间或还原点,顾名思义,就是将整个数据库回退到指定的一个时间点,实际上是数据库不完全恢复的另一种方式。真正的不完全恢复需要消耗的时间与数据库的大小有密切联系,数据库越庞大,需要的时间就越多。

闪回数据库技术改变了这个窘境,其处理问题方式可以着重于如何将相对较短的时间内发生的变更去除,而不是如何还原足够旧的备份(很花时间了),再利用归档日志前滚至指定时间。.

闪回数据库需要使用两种日志:闪回日志和重做日志。其中重做日志已为大家所知,而闪回日志就像是重做日志的反作用力,闪回日志的记载正好与重做日志的记载相反。比如,逻辑上重做日志记录insert命令的重做记录,闪回日志就记录delete命令的重做记录(当然实际上没那么简单)。在发起闪回操作时,只要根据写入闪回日志的相反顺序,即后写先读的顺序,将闪回记录从闪回日志读出并执行其记录的变更就能够将数据库在时间轴上倒推。

闪回日志的保存路径一定是快速恢复区的子目录,保存的期限则由参数db_flashback_ retention_target控制(单位为分钟),凡是超出保存期限的闪回日志将会在快速恢复区空间紧张的时候被自动删除。比如,指定数据库保留两天的闪回日志和归档日志:

SQL> alter system set db_flashback_retention_target=2880;

System altered.

使用v$database.FLASHBACK_ON可以查看闪回日志是否已启用,现在是“NO”,表示尚未启用:

SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
NO

在确保启用了归档模式之后,使用以下命令可以启用闪回日志,即闪回数据库功能:

SQL> alter database flashback on;

Database altered.

从此,在快速恢复区的flashback子目录下将会出现扩展名为.flb的文件,它们就是闪回日志。

SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
YES

闪回数据库命令的语法很简单:在进入MOUNT状态后执行flashback database to scn …或flashback database to timestamp …。

受保护还原点与存储快照
在实践中,受保护还原点可以替代存储快照。存储快照通常被用来在执行危险操作之前来保护数据库,比如大范围的数据库更新或应用打补丁或升级。相对于创建快照或复制一个数据库进行测试操作来说,你可以在主库或物理备库中创建一个受保护还原点。

闪回数据库与受保护还原点日志
闪回数据库日志与受保护还原点调用改变之前的数据文件块镜像,flashback database命令可以使用这些镜像来将数据文件还原到之前的状态。正常闪回日志与受保护还原点日志最主要的差别是当闪回区出现空间压力时是否这些日志会被删除。这种差异会影响日志的空间使用和数据库性能。你的恢复目录部分决定了是否启用闪回日志或者还原点,或都两者都使用。当单独或一起使用这些功能时对空间使用和性能的影响是需要考虑的。

受保护还原点与闪回区空间之关的系
当创建,保留,覆盖与删除闪回区中的闪回日志时存在以下规则:
1.如果闪回区有足够空间,那么当需要满足闪回保留目标时生成闪回日志。
2.如果闪回日志过旧不再满足闪回保留目标,那么可以重用这些闪回日志。
3.如果数据库必须创建闪回日志并且闪回区已经被使用完或者没有磁盘空间,那么旧的闪回日志会被重用。重用旧的闪回日志会缩短闪回数据库窗口。如果因为空间压力问题有过多的闪回日志被重用,那么闪回保留目标可能不会被满足。
4.如果闪回区填满,那么概括闪回区规则当闪回区要为其它文件提供空间时会自动删除归档重做日志来回收空间。在这种情况下,flashback database命令需要使用的重做日志的任何闪回日志也会被删除。概括闪回区规则,当出现以下情况下文件使用的空间会被回收。
a.文件被报告为过期并且闪回数据库不需要该文件。例如该文件保留的时间超过了db_flashback_retention_target参数设置
b.文件已经备份磁带
5.如果为了满足受保护还原点闪回区中没有文件满足删除条件。因为,闪回日志和其它为了满足受保护还原点的其它文件,额外的为了满足备份保留策略所需要的文件都可能会造成闪回区被填满。

当创建受保护还原点可以考虑不完全启动闪回日志,但需要监控闪回区的可用空间。如果为了满足你的保留策略和受保护还原点,闪回区中没有满足删除条件的文件,如果遇到磁盘空间被填满的情况时,数据库会执行删除。

禁用闪回日志与受保护还原点
假设在数据库禁用闪回日志的情况下创建了受保护还原点。在这种情况下,在受保护还原点创建时第一次被修改的数据文件块,数据库会将被修改前的数据块镜像存储在闪回日志中。因此闪回日志保留了每一个被修改的数据块在创建受保护还原点时的内容。后面对相同数据块的修改将不会被捕获除非在数据块最后被之前之前又创建了一个受保护还原点。

这种记录日志方法有以下重要的结果:
1.flashback database可以通过使用数据块镜像重新构造在创建受保护还原点时数据文件中的内容。
2.对于反复修改相同的数据,相比对正常的闪回日志受保护还原点所要使用的空间更少,因为每一个被修改的数据块只会被记录一次。应用程序有较少插入操作可能从节省磁盘空间中受益。当应用程序有大量插入或批量插入操作时这种优势就没有了。受保护还原点的日志开锁在禁用闪回日志时是很小的。

假如你的主要目标是要能将数据库恢复到受保护还原点所创建的时间点,那么在这种情况下,禁用闪回日志而只使用受保护还原点更有效。例如假设要对数据库执行升级,可以在启动升级之前创建一个受保护还原点,如果升级失败,那么可以执行flashback database来恢复数据库。

启用闪回日志与受保护还原点
如果启用闪回日志并定义一个或多个受保护还原点,那么数据库会执行正常的闪回日志操作。在这种情况下,闪回区将会保留将数据库恢复到受保护还原点与当前时间之间任何时间点所需要的所有闪回日志。闪回日志如果满足受保护还原点的要求在出现空间压力时是不会被删除的。启用闪回日志会占用一些性能开销。依赖于数据库的工作负载,可能会对闪回区造成极大的空间压力。因此应该要监控闪回区的空间使用情况。

使用闪回数据库与受保护还原点的条件
为了能成功执行闪回数据库和受保护还原点操作,必须首先设置一些关键的数据库选项。在启用闪回数据库之前进行以下数据库设置:
1.你的数据库必须运行在archivelog模式下,因为闪回数据库需要使用归档重做日志。
2.必须设置了闪回区,因为闪回日志只能存储在闪回区中。
3.对于RAC数据库,闪回区必须是集群文件系统或ASM

为了使用受保护还原点,数据库必须满足compatible参数必须设置为10.2.0或更高版本。但在使用正常还原点之前没有任何特定要求。

下面是举例来说明:
在启用闪回日志的情况下,创建受保护还原点
1.创建受保护还原点

SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
YES

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1187919 2015-04-23 17:17:26

SQL> create restore point  create_point guarantee flashback database;

Restore point created.

SQL> set long 200
SQL> set linesize 200
SQL> SELECT NAME, SCN, TIME, DATABASE_INCARNATION#,
  2  GUARANTEE_FLASHBACK_DATABASE,STORAGE_SIZE
  3  FROM V$RESTORE_POINT;

NAME                        SCN TIME                                                                        DATABASE_INCARNATION# GUA STORAGE_SIZE
-------------------- ---------- --------------------------------------------------------------------------- --------------------- --- ------------
CREATE_POINT            1187948 23-APR-15 05.18.21.000000000 PM                                                                 3 YES     52428800

SQL> alter system checkpoint;

System altered.

SQL> alter system switch logfile;

System altered.

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1187983 2015-04-23 17:18:59

2.新建表test

SQL> create table test as select * from dba_objects;

Table created.

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1188096 2015-04-23 17:20:20

因为我们启用了闪回日志,所以可以将数据库闪回到受保护还原点(create_point)SCN:1187948与当前时间2015-04-23 17:20:20 SCN:1188096之间的任意SCN所对应的状态。那么我们将数据库闪回到SCN为1187983,这时表test是还没有创建的。

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.
SQL> flashback database to scn 1187983;

Flashback complete.

SQL> alter database open read only;

Database altered.

SQL> select count(*) from test;
select count(*) from test
                     *
ERROR at line 1:
ORA-00942: table or view does not exist

在确认之前不要以resetlogs选项open数据库,这样可以多次执行闪回数据库操作,也可以撤消闪回数据库操作,下面是撤消闪回数据库操作是通过执行完全恢复来实现的:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.
SQL> recover database;
Media recovery complete.
SQL> alter database open;

Database altered.

SQL> select count(*) from test;

  COUNT(*)
----------
     86741

下面我们在禁用闪回日志的情况下执行闪回数据库操作:
1.禁用闪回日志并创建受保护还原点

SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
YES

SQL> alter database flashback off;

Database altered.

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.

SQL> create restore point delete_point guarantee flashback database;


Restore point created.

SQL> alter database open;

Database altered.


SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
RESTORE POINT ONLY



SQL> SELECT NAME, SCN, TIME, DATABASE_INCARNATION#,
  2  GUARANTEE_FLASHBACK_DATABASE,STORAGE_SIZE
  3  FROM V$RESTORE_POINT;

NAME                        SCN TIME                                                                        DATABASE_INCARNATION# GUA STORAGE_SIZE
-------------------- ---------- --------------------------------------------------------------------------- --------------------- --- ------------
DELETE_POINT            1192464 23-APR-15 07.36.16.000000000 PM                                                                 3 YES     52428800

SQL> alter system switch logfile;

System altered.

SQL> alter system checkpoint;

System altered.

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1192860 2015-04-23 19:37:51

2.删除表test

SQL> delete from test;

86743 rows deleted.

SQL> commit;

Commit complete.

SQL> select count(*) from test;

  COUNT(*)
----------
         0

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
    1193268 2015-04-23 19:38:57

现在我们来测试看在禁用闪回日志的情况下能不能将数据库闪回到受保护还原点(delete_point)scn:1192464与当前时间SCN:1193268之间的任意SCN,这里我们闪回到SCN:1192860,这里表test的记录还没有被删除

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.
SQL> flashback database to scn 1192860;
flashback database to scn 1192860
*
ERROR at line 1:
ORA-38726: Flashback database logging is not on.

错误提示已经说明了没有闪回日志,所以不能将数据库闪回到受保护还原点与当前时间之间的任意SCN。

下面将表test闪回到受保护还原点SCN:1192464

SQL> flashback database to restore point delete_point;

Flashback complete.

SQL> alter database open read only;

Database altered.

SQL> select count(*) from test;

  COUNT(*)
----------
     86743

从执行结果可知,已经恢复到受保护还原点所对应的状态,表test的数据恢复回来了。

从上面的执行结果可以看到在没有启用闪回日志的情况可以将数据库闪回到受保护还原点。如果要想在没有启用闪回日志的情况下将数据库闪回到受保护还原点与当前时间点之间的SCN:1192860,可以先将数据库闪加到受保护还原点,再执行恢复将数据库恢复到SCN:1192860。

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  188313600 bytes
Fixed Size                  1363496 bytes
Variable Size             150995416 bytes
Database Buffers           33554432 bytes
Redo Buffers                2400256 bytes
Database mounted.
SQL> flashback database to restore point delete_point;

Flashback complete.

SQL> alter  session  set  nls_date_format  =  'yyyy-mm-dd  hh24:mi:ss';

Session altered.

因为SCN:1192860,对应的时间就是2015-04-23 19:37:51,这里如果直接用SCN:1192860执行恢复会报以下错误 :

SQL> recover database until scn 1192860;
ORA-00277: illegal option to the UNTIL recovery flag SCN



SQL> recover database until time '2015-04-23 19:37:51';
Media recovery complete.
SQL> alter database open read only;

Database altered.

SQL> select count(*) from test;

  COUNT(*)
----------
     86743

从执行结果可知,已经恢复到受保护还原点所对应的状态,表test的数据恢复回来了。

使用Pipe来执行RMAN命令

RMAN Pipe接口是执行RMAN命令并接收命令输出的一种替代方法。使用这种接口,RMAN使用dbms_pipe包来获得命令并发送输出来代替操作系统shell。使用这种接口,可以编写可移值的RMAN程序接口。这个功能使用我们中心自己开发的数据库监控平台可以调用RMAN来执行备份,使用pipe接口编写RMAN备份脚本可以跨操作系统平台使用。
pipe接口是通过使用pipe命令行参数来进行调用的。RMAN使用两种私有管道:一个用来接收命令,一个用来发送输出。管道名是用pipe参数来决定的。例如,执行下面的命令 :

[oracle11@jingyong1 ~]$ rman PIPE abc TARGET /

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 10:38:53 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

通过管道连接执行多个RMAN命令
假设想要通过管道来连续执行多个命令。在每一个命令发送到管道执行并返回输出后,RMAN会暂停并等待下一个命令。

1.以pipe选项来启动RMAN并连接到目标数据库:

[oracle11@jingyong1 ~]$ rman PIPE abc TARGET /

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 10:38:53 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

也可以指定timeout选项,强制RMAN在指定的时间内没有从输入管道接收到命令就自动退出。

[oracle11@oracle11g ~]$ rman PIPE abc TARGET / TIMEOUT 60

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 14:51:12 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

[oracle11@oracle11g ~]$ date
Fri Apr 17 14:52:14 CST 2015

2.连接管目标数据库并通过dbms_pipe.pack_message与dbms_pipe.send_message在输入管道中输入要执行的命令。当RMAN使用管道来代替标准RMAN输入时会显示消息RMAN-00572,下面使用管道来显示当前的RMAN配置信息并对数据库执行备份。

[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Apr 17 11:49:25 2015

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set serverout on size 100000
SQL> declare
  2  i_v varchar2(2000):='show all;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /
0

PL/SQL procedure successfully completed.




[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Apr 17 11:49:30 2015

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set serverout on size 100000
SQL> declare
  2  i_v varchar2(2000):='backup as backupset database plus archivelog;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /
0

PL/SQL procedure successfully completed.

3.使用dbms_pipe.receive_message和dbms_pipe.unpack_message来读取RMAN的输出信息:

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',5);
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
connected to target database: DB (DBID=1640573015)
RMAN-00572: waiting for DBMS_PIPE input
using target database control file instead of recovery catalog
RMAN configuration parameters for database with db_unique_name DB are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR
LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO
'/u03/app/oracle/11.2.0/db/dbs/snapcf_db.f'; # default
RMAN-00572: waiting for DBMS_PIPE input

PL/SQL procedure successfully completed.

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',600);
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
Starting backup at 2015-04-17 12:21:43
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=18 RECID=11 STAMP=877175019
input archived log thread=1 sequence=19 RECID=12 STAMP=877175163
input archived log thread=1 sequence=20 RECID=13 STAMP=877212065
input archived log thread=1 sequence=21 RECID=14 STAMP=877253311
input archived log thread=1 sequence=22 RECID=15 STAMP=877262007
input archived log thread=1 sequence=23 RECID=16 STAMP=877262176
input archived log thread=1 sequence=24 RECID=17 STAMP=877262429
input archived log thread=1 sequence=25 RECID=18 STAMP=877262605
input archived log thread=1 sequence=26 RECID=19 STAMP=877262607
input archived log thread=1 sequence=27 RECID=20 STAMP=877262777
input archived log thread=1 sequence=28 RECID=21 STAMP=877263703
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:21:44
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:21:59
piece handle=/u03/app/oracle/11.2.0/db/dbs/1fq4juqn_1_1 tag=TAG20150417T122143
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 2015-04-17 12:21:59
Starting backup at 2015-04-17 12:21:59
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
input datafile file number=00005 name=/u03/app/oracle/oradata/db/example01.dbf
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:21:59
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:34
piece handle=/u03/app/oracle/11.2.0/db/dbs/1gq4jur7_1_1 tag=TAG20150417T122159
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:36
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:24:42
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:43
piece handle=/u03/app/oracle/11.2.0/db/dbs/1hq4jv03_1_1 tag=TAG20150417T122159
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:24:43
Starting backup at 2015-04-17 12:24:43
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=29 RECID=22 STAMP=877263883
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:24:44
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:45
piece handle=/u03/app/oracle/11.2.0/db/dbs/1iq4jv0c_1_1 tag=TAG20150417T122444
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:24:45
RMAN-00572: waiting for DBMS_PIPE input
Starting backup at 2015-04-17 12:26:09
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=18 RECID=11 STAMP=877175019
input archived log thread=1 sequence=19 RECID=12 STAMP=877175163
input archived log thread=1 sequence=20 RECID=13 STAMP=877212065
input archived log thread=1 sequence=21 RECID=14 STAMP=877253311
input archived log thread=1 sequence=22 RECID=15 STAMP=877262007
input archived log thread=1 sequence=23 RECID=16 STAMP=877262176
input archived log thread=1 sequence=24 RECID=17 STAMP=877262429
input archived log thread=1 sequence=25 RECID=18 STAMP=877262605
input archived log thread=1 sequence=26 RECID=19 STAMP=877262607
input archived log thread=1 sequence=27 RECID=20 STAMP=877262777
input archived log thread=1 sequence=28 RECID=21 STAMP=877263703
input archived log thread=1 sequence=29 RECID=22 STAMP=877263883
input archived log thread=1 sequence=30 RECID=23 STAMP=877263969
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:26:10
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:26:25
piece handle=/u03/app/oracle/11.2.0/db/dbs/1jq4jv32_1_1 tag=TAG20150417T122610
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 2015-04-17 12:26:25
Starting backup at 2015-04-17 12:26:25
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
input datafile file number=00005 name=/u03/app/oracle/oradata/db/example01.dbf
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:26:26
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:21
piece handle=/u03/app/oracle/11.2.0/db/dbs/1kq4jv3i_1_1 tag=TAG20150417T122625
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:55
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:29:24
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:27
piece handle=/u03/app/oracle/11.2.0/db/dbs/1lq4jv91_1_1 tag=TAG20150417T122625
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 2015-04-17 12:29:27
Starting backup at 2015-04-17 12:29:27
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=31 RECID=24 STAMP=877264168
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:29:28
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:29
piece handle=/u03/app/oracle/11.2.0/db/dbs/1mq4jv98_1_1 tag=TAG20150417T122928
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:29:29
RMAN-00572: waiting for DBMS_PIPE input

PL/SQL procedure successfully completed.

4.如果启动RMAN时使用了timeout选项,那么RMAN在指定时间间隔内没有接收到任何命令就会自动终止RMAN会话。为了强制RMAN立即终止RMAN会话可以发送exit命令。

SQL> declare
  2  i_v varchar2(2000):='exit;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',10);--10是超过时间为10秒
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
Recovery Manager complete.
PL/SQL procedure successfully completed.

对删除分区的分区表执行TSPITR

在有些情况下将分表区中的一些分区删除后需要恢复,但又不是对整个分区表恢复到某个时间点,这可以通过辅助数据库执行TSPITR来实现,下面的例子介绍了使用这种方法的具体操作过程。
1.对每个要执行恢复的分区在主数据库中创建一个表,分区表是sales,有28个分区,删除分区sales_1995,sales_1996之后对分区表sales执行TSPITR来恢复分区sales_1995,sales_1996

SQL> select a.owner,a.table_name,a.partitioning_type,a.subpartitioning_type,a.partition_count,a.def_tablespace_name from dba_part_tables a where a.owner='TEST' and a.table_name='SALES';

OWNER                          TABLE_NAME                     PARTITIONING_TYPE SUBPARTITIONING_TYPE PARTITION_COUNT DEF_TABLESPACE_NAME
------------------------------ ------------------------------ ----------------- -------------------- --------------- ------------------------------
TEST                           SALES                          RANGE             NONE                              28 TEST


SQL> select a.table_owner,a.table_name,a.partition_name from dba_tab_partitions a where a.table_owner='TEST' and a.table_name='SALES';

TABLE_OWNER                    TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------ ------------------------------
TEST                           SALES                          SALES_1995
TEST                           SALES                          SALES_1996
TEST                           SALES                          SALES_H1_1997
TEST                           SALES                          SALES_H2_1997
TEST                           SALES                          SALES_Q1_1998
TEST                           SALES                          SALES_Q2_1998
TEST                           SALES                          SALES_Q3_1998
TEST                           SALES                          SALES_Q4_1998
TEST                           SALES                          SALES_Q1_1999
TEST                           SALES                          SALES_Q2_1999
TEST                           SALES                          SALES_Q3_1999
TEST                           SALES                          SALES_Q4_1999
TEST                           SALES                          SALES_Q1_2000
TEST                           SALES                          SALES_Q2_2000
TEST                           SALES                          SALES_Q3_2000
TEST                           SALES                          SALES_Q4_2000
TEST                           SALES                          SALES_Q1_2001
TEST                           SALES                          SALES_Q2_2001
TEST                           SALES                          SALES_Q3_2001
TEST                           SALES                          SALES_Q4_2001
TEST                           SALES                          SALES_Q1_2002
TEST                           SALES                          SALES_Q2_2002
TEST                           SALES                          SALES_Q3_2002
TEST                           SALES                          SALES_Q4_2002
TEST                           SALES                          SALES_Q1_2003
TEST                           SALES                          SALES_Q2_2003
TEST                           SALES                          SALES_Q3_2003
TEST                           SALES                          SALES_Q4_2003

对主数据库进行备份

SQL> alter database begin backup;

Database altered.
[oracle@oracle11g backup]$ cp /u01/app/oracle/oradata/test/*.dbf /u02/backup/

SQL> alter database end backup;

Database altered.

SQL> alter database backup controlfile to '/u02/backup/control.ctl';

Database altered.

SQL> alter system switch logfile;

System altered.

2.删除分区表sales上的sales_1995,sales_1996

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
     430881 2015-04-10 20:56:59

在执行前记录当前时间,在对辅助数据库进行恢复时这就是恢复的目标时间点

SQL> alter table test.sales drop partition sales_1995;

Table altered.

SQL> alter table test.sales drop partition sales_1996;

Table altered.

SQL> select count(*) from test.sales partition(SALES_1995);
select count(*) from test.sales partition(SALES_1995)
                                          *
ERROR at line 1:
ORA-02149: Specified partition does not exist


SQL> select count(*) from test.sales partition(SALES_1996);
select count(*) from test.sales partition(SALES_1996)
                                          *
ERROR at line 1:
ORA-02149: Specified partition does not exist


SQL> select count(*) from test.sales  where time_id
3.创建辅助数据库
将辅助集和恢复集表空间的数据文件与备份的控制文件 还原到/u02/auxiliary目录中
[oracle@oracle11g backup]$ cp system01.dbf  /u02/auxiliary/
[oracle@oracle11g backup]$ cp sysaux01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp undotbs01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp test01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp temp01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control01.ctl
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control02.ctl
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control03.ctl
[oracle@oracle11g backup]$ cp /u02/archive/* /u02/backup/
[oracle@oracle11g backup]$ cp /u01/app/oracle/oradata/test/*.log /u02/backup/

创建初始化参数文件

[oracle@oracle11g auxiliary]$ vi initauxiliary.ora

db_name=test
db_unique_name=auxiliary
sga_max_size=160M
sga_target=160M
pga_aggregate_target=16M
db_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
log_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
control_files=('/u02/auxiliary/control01.ctl','/u02/auxiliary/control02.ctl','/u02/auxiliary/control03.ctl')
log_archive_dest_1='location=/u02/backup'
log_archive_format='%t_%s_%r.dbf'
compatible=10.2.0.5.0

还原和恢复辅助实例

[oracle@oracle11g ~]$ export ORACLE_SID=auxiliary
[oracle@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Fri Apr 10 21:05:42 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.

SQL> startup nomount pfile='/u02/auxiliary/initauxiliary.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272624 bytes
Variable Size              58721488 bytes
Database Buffers          104857600 bytes
Redo Buffers                2920448 bytes
SQL> alter database mount clone database;

Database altered.

SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE
  6  union all
  7  select name from v$tempfile;

NAME
--------------------------------------------------------------------------------
/u02/auxiliary/system01.dbf
/u02/auxiliary/undotbs01.dbf
/u02/auxiliary/sysaux01.dbf
/u02/auxiliary/users01.dbf
/u02/auxiliary/example01.dbf
/u02/auxiliary/test01.dbf
/u02/auxiliary/redo03.log
/u02/auxiliary/redo02.log
/u02/auxiliary/redo01.log
/u02/auxiliary/control01.ctl
/u02/auxiliary/control02.ctl

NAME
--------------------------------------------------------------------------------
/u02/auxiliary/control03.ctl
/u02/auxiliary/temp01.dbf

13 rows selected.

SQL> alter database datafile '/u02/auxiliary/system01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/undotbs01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/test01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/sysaux01.dbf' online;

Database altered.

SQL> alter database tempfile '/u02/auxiliary/temp01.dbf' online;

Database altered.

SQL> recover  database  until  time  '2015-04-10 20:56:59'  using  backup  controlfile;
ORA-00279: change 430764 generated at 04/10/2015 20:52:21 needed for thread 1
ORA-00289: suggestion : /u02/backup/1_7_876665479.dbf
ORA-00280: change 430764 for thread 1 is in sequence #7


Specify log: {=suggested | filename | AUTO | CANCEL}

ORA-00279: change 430825 generated at 04/10/2015 20:54:32 needed for thread 1
ORA-00289: suggestion : /u02/backup/1_8_876665479.dbf
ORA-00280: change 430825 for thread 1 is in sequence #8
ORA-00278: log file '/u02/backup/1_7_876665479.dbf' no longer needed for this
recovery


Specify log: {=suggested | filename | AUTO | CANCEL}
/u02/backup/redo02.log
Log applied.
Media recovery complete.
SQL> alter database open resetlogs;

Database altered.

SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
       999


从上面的结果可以看到我们将辅助数据库恢复到删除分区之前的时间点了。

4.在辅助数据库中创建与分区进行交换相关的表,而且交换表只能创建在SYSTEM表空间中,并且与分区表有完全相同的列名和数据类型。如果交换表没有创建在SYSTEM表空间中会出现ORA-01552错误。

SQL> create table test.tts_sales_1995 tablespace system  as select * from test.sales partition(SALES_1995) where 1=2;

Table created.

SQL> create table test.tts_sales_1996 tablespace system as select * from test.sales partition(SALES_1996) where 1=2;

Table created.

5.在辅助数据库上使用表tts_sales_1995,tts_sales_1996与分区sales_1995,sales_1996进行交换

SQL> alter table test.sales  exchange partition sales_1995 with table test.tts_sales_1995;

Table altered.

SQL> alter table test.sales exchange partition sales_1996 with table test.tts_sales_1996;

Table altered.

SQL> select count(*) from test.tts_sales_1995;

  COUNT(*)
----------
       999

SQL> select count(*) from test.tts_sales_1996;

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
         0

6.导出交换表tts_sales_1995,tts_sales_1996

[oracle@oracle11g auxiliary]$ export ORACLE_SID=auxiliary
[oracle@oracle11g auxiliary]$ exp \'test/test\' file=/u02/sales.dmp log=/u02/sales.log tables=\(tts_sales_1995,tts_sales_1996\)

Export: Release 10.2.0.5.0 - Production on Fri Apr 10 21:16:32 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                 TTS_SALES_1995        999 rows exported
. . exporting table                 TTS_SALES_1996        999 rows exported
Export terminated successfully without warnings.

7.将交换表 tts_sales_1995,tts_sales_1996导入到主数据库中

[oracle@oracle11g auxiliary]$ export ORACLE_SID=test
[oracle@oracle11g auxiliary]$ imp \'test/test\' file=/u02/sales.dmp log=/u02/sales_dr.log fromuser=test touser=test

Import: Release 10.2.0.5.0 - Production on Fri Apr 10 21:17:20 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V10.02.01 via conventional path
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing TEST's objects into TEST
. . importing table               "TTS_SALES_1995"        999 rows imported
. . importing table               "TTS_SALES_1996"        999 rows imported
Import terminated successfully without warnings.

8.对主数据库中的分区表sales增加两个分区sales_1995,sales_1996

SQL> alter table test.sales  add partition sales_1995 values less than(TO_DATE(' 1996-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'));
alter table test.sales  add partition sales_1995 values less than(TO_DATE(' 1996-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
                                      *
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition


SQL> alter table test.sales  split partition sales_h1_1997 at(TO_DATE(' 1996-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
  2  into  (partition sales_1995,partition sales_h1_1997);

Table altered.

SQL> alter table test.sales  split partition sales_h1_1997 at(TO_DATE(' 1997-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
  2  into  (partition sales_1996,partition sales_h1_1997);

Table altered.

9.使用表tts_sales_1995,tts_sales_1996与主数据库中的表sales的分区sales_1995,sales_1996进行交换

SQL> alter table test.sales  exchange partition sales_1995 with table test.tts_sales_1995;

Table altered.

SQL> alter table test.sales exchange partition sales_1996 with table test.tts_sales_1996;

Table altered.

SQL> select count(*) from test.tts_sales_1995;

  COUNT(*)
----------
         0

SQL> select count(*) from test.tts_sales_1996;

  COUNT(*)
----------
         0



SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
       999

从上面的结果可以看到,表sales的sales_1995,sales_1996两个分区的数据恢复回来,对主数据库进行备份这里不再赘述。

对分区表的部分分区执行TSPITR

分区表可以跨多个表空间,Oracle允许对部分分区执行TSPITR而不是所有分区。
1.对每个要执行恢复的分区在主数据库中创建一个表,分区表是sales,有28个分区,要执行TSPITR的分区是sales_1995,sales_1996,所以对这两个分区创建两个单独的表sales_1995,sales_1996

SQL> select a.owner,a.table_name,a.partitioning_type,a.subpartitioning_type,a.partition_count,a.def_tablespace_name from dba_part_tables a where a.owner='TEST' and a.table_name='SALES';

OWNER                          TABLE_NAME                     PARTITIONING_TYPE SUBPARTITIONING_TYPE PARTITION_COUNT DEF_TABLESPACE_NAME
------------------------------ ------------------------------ ----------------- -------------------- --------------- ------------------------------
TEST                           SALES                          RANGE             NONE                              28 TEST


SQL> select a.table_owner,a.table_name,a.partition_name from dba_tab_partitions a where a.table_owner='TEST' and a.table_name='SALES';

TABLE_OWNER                    TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------ ------------------------------
TEST                           SALES                          SALES_1995
TEST                           SALES                          SALES_1996
TEST                           SALES                          SALES_H1_1997
TEST                           SALES                          SALES_H2_1997
TEST                           SALES                          SALES_Q1_1998
TEST                           SALES                          SALES_Q2_1998
TEST                           SALES                          SALES_Q3_1998
TEST                           SALES                          SALES_Q4_1998
TEST                           SALES                          SALES_Q1_1999
TEST                           SALES                          SALES_Q2_1999
TEST                           SALES                          SALES_Q3_1999
TEST                           SALES                          SALES_Q4_1999
TEST                           SALES                          SALES_Q1_2000
TEST                           SALES                          SALES_Q2_2000
TEST                           SALES                          SALES_Q3_2000
TEST                           SALES                          SALES_Q4_2000
TEST                           SALES                          SALES_Q1_2001
TEST                           SALES                          SALES_Q2_2001
TEST                           SALES                          SALES_Q3_2001
TEST                           SALES                          SALES_Q4_2001
TEST                           SALES                          SALES_Q1_2002
TEST                           SALES                          SALES_Q2_2002
TEST                           SALES                          SALES_Q3_2002
TEST                           SALES                          SALES_Q4_2002
TEST                           SALES                          SALES_Q1_2003
TEST                           SALES                          SALES_Q2_2003
TEST                           SALES                          SALES_Q3_2003
TEST                           SALES                          SALES_Q4_2003


SQL> create table test.sales_1995 as select * from test.sales where 1=2;

Table created.

SQL> create table test.sales_1996 as select * from test.sales where 1=2;

Table created.

对主数据库进行备份

SQL> alter database begin backup;

Database altered.
[oracle@oracle11g backup]$ cp /u01/app/oracle/oradata/test/*.dbf /u02/backup/

SQL> alter database end backup;

Database altered.

SQL> alter database backup controlfile to '/u02/backup/control.ctl';

Database altered.

SQL> alter system switch logfile;

System altered.

2.删除要执行TSPITR的分区上的索引(这里是创建的本地索引)

SQL> select current_scn,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd hh24:mi:ss') from v$database;

CURRENT_SCN TO_CHAR(SCN_TO_TIME
----------- -------------------
     425540 2015-04-10 17:38:52



在执行前记录当前时间,在对辅助数据库进行恢复时这就是恢复的目标时间点

SQL> alter index  TEST.SALES_CUST_BIX modify partition SALES_1995 unusable;

Index altered.

SQL> alter index  TEST.SALES_CUST_BIX modify partition SALES_1996 unusable;

Index altered.

3.交换分区表
每一个要执行TSPITR的分区与其相关的表进行分区交换

SQL> alter table test.sales exchange partition sales_1995 with table test.sales_1995;

Table altered.

SQL> alter table test.sales exchange partition sales_1996 with table test.sales_1996;

Table altered.

SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales_1995;

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales_1996;

  COUNT(*)
----------
       999

5.创建辅助数据库
将辅助集和恢复集表空间的数据文件与备份的控制文件 还原到/u02/auxiliary目录中

[oracle@oracle11g backup]$ cp system01.dbf  /u02/auxiliary/
[oracle@oracle11g backup]$ cp sysaux01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp undotbs01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp test01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp temp01.dbf /u02/auxiliary/
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control01.ctl
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control02.ctl
[oracle@oracle11g backup]$ cp control.ctl /u02/auxiliary/control03.ctl
[oracle@oracle11g backup]$ cp /u02/archive/* /u02/backup/
[oracle@oracle11g backup]$ cp /u01/app/oracle/oradata/test/*.log /u02/backup/

创建初始化参数文件

[oracle@oracle11g auxiliary]$ vi initauxiliary.ora

db_name=test
db_unique_name=auxiliary
sga_max_size=160M
sga_target=160M
pga_aggregate_target=16M
db_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
log_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
control_files=('/u02/auxiliary/control01.ctl','/u02/auxiliary/control02.ctl','/u02/auxiliary/control03.ctl')
log_archive_dest_1='location=/u02/backup'
log_archive_format='%t_%s_%r.dbf'
compatible=10.2.0.5.0

还原与恢复辅助实例

[oracle@oracle11g ~]$ export ORACLE_SID=auxiliary
[oracle@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Fri Apr 10 15:40:51 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.

SQL> startup nomount pfile='/u02/auxiliary/initauxiliary.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272624 bytes
Variable Size              58721488 bytes
Database Buffers          104857600 bytes
Redo Buffers                2920448 bytes


SQL> alter database mount clone database;

Database altered.
SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u02/auxiliary/system01.dbf
/u02/auxiliary/undotbs01.dbf
/u02/auxiliary/sysaux01.dbf
/u02/auxiliary/users01.dbf
/u02/auxiliary/example01.dbf
/u02/auxiliary/test01.dbf
/u02/auxiliary/redo03.log
/u02/auxiliary/redo02.log
/u02/auxiliary/redo01.log
/u02/auxiliary/control01.ctl
/u02/auxiliary/control02.ctl
/u02/auxiliary/control03.ctl

12 rows selected.

SQL> alter database datafile '/u02/auxiliary/system01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/undotbs01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/test01.dbf' online;

Database altered.


SQL> alter database datafile '/u02/auxiliary/sysaux01.dbf' online;

Database altered.


SQL>

Database altered.



SQL> recover  database  until  time  '2015-04-10 17:38:52'  using  backup  controlfile;
ORA-00279: change 425356 generated at 04/10/2015 17:31:13 needed for thread 1
ORA-00289: suggestion : /u02/backup/1_6_876665479.dbf
ORA-00280: change 425356 for thread 1 is in sequence #6


Specify log: {=suggested | filename | AUTO | CANCEL}

ORA-00279: change 425511 generated at 04/10/2015 17:37:22 needed for thread 1
ORA-00289: suggestion : /u02/backup/1_7_876665479.dbf
ORA-00280: change 425511 for thread 1 is in sequence #7
ORA-00278: log file '/u02/backup/1_6_876665479.dbf' no longer needed for this recovery


Specify log: {=suggested | filename | AUTO | CANCEL}
/u02/backup/redo01.log
Log applied.
Media recovery complete.
SQL> alter database  open resetlogs;

Database altered.

SQL> select count(*) from test.sales_1995;

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales_1996;

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
       999

从上面的结果可以看到我们将辅助数据库恢复到执行交换分区之前了。

6.在辅助数据库中创建与分区进行交换相关的表,而且交换表只能创建在SYSTEM表空间中,并且与分区表有完全相同的列名和数据类型。如果交换表没有创建在SYSTEM表空间中会出现ORA-01552错误

SQL> create table test.tts_sales_1995 tablespace system  as select * from test.sales partition(SALES_1995) where 1=2;

Table created.

SQL> create table test.tts_sales_1996 tablespace system as select * from test.sales partition(SALES_1996) where 1=2;

Table created.

SQL> select count(*) from test.tts_sales_1995;

  COUNT(*)
----------
         0

SQL> select count(*) from test.tts_sales_1996;

  COUNT(*)
----------
         0

7.将辅助数据库中的分区表的sales_1995,sales_1996分区上的索引删除(这里是创建的本地索引)

SQL> alter index  SALES_CUST_BIX modify partition SALES_1995 unusable;

Index altered.

SQL> alter index  SALES_CUST_BIX modify partition SALES_1996 unusable;

Index altered.

8.在辅助数据库上使用表tts_sales_1995,tts_sales_1996与分区sales_1995,sales_1996进行交换

SQL> alter table test.sales  exchange partition sales_1995 with table test.tts_sales_1995;

Table altered.

SQL> alter table test.sales exchange partition sales_1996 with table test.tts_sales_1996;

Table altered.

SQL> select count(*) from test.tts_sales_1995;

  COUNT(*)
----------
       999

SQL> select count(*) from test.tts_sales_1996;

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
         0

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
         0

9.导出交换表tts_sales_1995,tts_sales_1996

SQL> create temporary tablespace test_temp tempfile '/u02/auxiliary/test_temp01.dbf' size 50M;

Tablespace created.

SQL>  alter database default temporary tablespace test_temp;

Database altered.




SQL> alter tablespace test read only;

Tablespace altered.


[oracle@oracle11g ~]$ export ORACLE_SID=auxiliary
[oracle@oracle11g auxiliary]$ exp \'test/test\' file=/u02/sales.dmp log=/u02/sales.log tables=\(tts_sales_1995,tts_sales_1996\)

Export: Release 10.2.0.5.0 - Production on Fri Apr 10 19:10:09 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                 TTS_SALES_1995        999 rows exported
. . exporting table                 TTS_SALES_1996        999 rows exported
Export terminated successfully without warnings.

10.将交换表 tts_sales_1995,tts_sales_1996导入到主数据库中

[oracle@oracle11g auxiliary]$ export ORACLE_SID=test
[oracle@oracle11g auxiliary]$ imp \'test/test\' file=/u02/sales.dmp log=/u02/sales_dr.log fromuser=test touser=test

Import: Release 10.2.0.5.0 - Production on Fri Apr 10 19:12:01 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V10.02.01 via conventional path
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing TEST's objects into TEST
. . importing table               "TTS_SALES_1995"        999 rows imported
. . importing table               "TTS_SALES_1996"        999 rows imported
Import terminated successfully without warnings.

11.使用表tts_sales_1995,tts_sales_1996与主数据库中的表sales的分区sales_1995,sales_1996进行交换

SQL> alter table test.sales  exchange partition sales_1995 with table test.tts_sales_1995;

Table altered.

SQL> alter table test.sales exchange partition sales_1996 with table test.tts_sales_1996;

Table altered.

SQL> select count(*) from test.tts_sales_1995;

  COUNT(*)
----------
         0

SQL> select count(*) from test.tts_sales_1996;

  COUNT(*)
----------
         0



SQL> select count(*) from test.sales partition(SALES_1995);

  COUNT(*)
----------
       999

SQL> select count(*) from test.sales partition(SALES_1996);

  COUNT(*)
----------
       999

从上面的结果可以看到,表sales的sales_1995,sales_1996两个分区的数据恢复回来,最后需要对对主数据库进行备份这里不再赘述。

使用传输表空间执行用户管理的TSPITR

用户管理的表空间按时间点恢复(TSPITR)是使用传输表空间来快速恢复一个或多个表空间到之前某个时间点。在以下情况下使用用户管理的表空间按时间点恢复很有用:
1.当错误执行drop tablespace操作
2.错误的作业或DML语句只对数据库的一部分产生影响
3.在一个物理数据库中有多个方案使用不同的表空间当一个方案要恢复到与其它方案不相同的时间点
4.当表空间在一个大数据库中(VLDB)表空间按时间点恢复要比使用备份还原和恢复整个数据库更有效

一种快速跨数据库传输表空间的方法是从源数据库卸载并且目标数据库中附加表空间。数据库可以跨平台,例如,Solaris和Windows 2000。卸载和附加是通过导出和导入工具来完成的。但并不是实际导出和导入表数据,只是简单的导出和导入内部元数据。

表空间按时间点恢复(TSPITR)的方法
在Oracle9i之前,可以有两种方法来执行用户管理的表空间按时间点恢复:
.传统用户管理的表空间按时间点恢复要求你创建一个克隆数据库。
.使用传输表空间执行用户管理的表空间按时间点恢复

在Oracle10g中,表空间按时间点恢复应该使用传输表空间来完成。传输表空间与传统的方法相比更简单易用。

TSPITR(表空间按时间点恢复)是通过从主数据库中删除要被恢复的表空间,还原一个副本数据库叫作辅助数据库并将其恢复到目标时间点,然后从辅助数据库中将相关的表空间传输到主数据库中.为了便于使用,强烈建议你将辅助数据库和主数据库放在不同的主机上。也还可以在相同主机上执行TSPITR

执行用户管理的表空间按时间点恢复的基本过程如下:
1.将需要执行表空间按时间点恢复的表空间脱机
2.对辅助数据库进行前期设置
3.创建辅助数据库并将其恢复到目标时间点
4.从主数据库中删除要执行表空间按时间点恢复的表空间
5.使用传输表侬间将传输表空间集从辅助数据库传输到主数据库

用户管理的表空间按时间点恢复的准备工作的基本步骤
1.回顾表空间按时间点恢复的要求
在执行TSPITR之前要满足以下条件:
.确保对恢复和辅助集表空间的所有数据文件创建了备份。数据文件备份要在恢复目标时间之前创建
.确保有控制文件备份辅助数据库可以使用。控制文件备份必须满足这些条件:
–控制文件必须是在恢复目标时间之间进行备份的
–控制文件必须使用下面的语句进行备份
alter database backup controlfile to ‘cf_name’;
.确保组成恢复集的所有文件都在辅助数据库的恢复集中,否则在传输表空间的导出步骤阶段会失败。
.在辅助主机上给辅助数据库分配足够的磁盘空间
.提供足够的物理内存启动辅助实例
.如果要被恢复的表空间已经被重命名,确保TSPITR的目标SCN在表空间重命名之后。如果恢复目标时间在重命名之前可以执行数据库按时间点恢复。

2.识别恢复和辅助集表空间的所有数据文件
在你创建辅助数据库之前,连接到主数据库并获得关于主数据库的以下所有信息:
.恢复集表空间所有数据文件的文件名
.system表空间的数据文件名
.undo表空间或包含回滚段表空间的数据文件名
.控制文件名
执行下面的语句可以查询所有数据文件和控制文件名

SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/test/system01.dbf
/u01/app/oracle/oradata/test/undotbs01.dbf
/u01/app/oracle/oradata/test/sysaux01.dbf
/u01/app/oracle/oradata/test/users01.dbf
/u01/app/oracle/oradata/test/example01.dbf
/u01/app/oracle/oradata/test/tspitr01.dbf
/u01/app/oracle/oradata/test/test01.dbf
/u01/app/oracle/oradata/test/jy01.dbf
/u01/app/oracle/oradata/test/control01.ctl
/u01/app/oracle/oradata/test/control02.ctl
/u01/app/oracle/oradata/test/control03.ctl

为了判断system和恢复集表空间的数据文件名可以执行类似于下面的语句:

SQL> SELECT t.NAME AS "reco_tbs", d.NAME AS "dbf_name"
  2  FROM V$DATAFILE d, V$TABLESPACE t
  3  WHERE t.TS# = d.TS#
  4  AND t.NAME IN ('SYSTEM', 'TSPITR', 'TEST');

reco_tbs                           dbf_name
------------------------------     --------------------------------------------------------------------------------
SYSTEM                             /u01/app/oracle/oradata/test/system01.dbf

TSPITR                             /u01/app/oracle/oradata/test/tspitr01.dbf

TEST                               /u01/app/oracle/oradata/test/test01.dbf

如果数据库运行在手动管理undo模式下,那么执行下面的查询就可显示包含回滚段的表空间的数据文件名

SQL> SELECT DISTINCT r.TABLESPACE_NAME AS "rbs_tbs", d.FILE_NAME AS "dbf_name"
  2  FROM DBA_ROLLBACK_SEGS r, DBA_DATA_FILES d
  3  WHERE r.TABLESPACE_NAME=d.TABLESPACE_NAME;

rbs_tbs                          dbf_name
------------------------------   --------------------------------------------------------------------------------
SYSTEM                           /u01/app/oracle/oradata/test/system01.dbf

UNDOTBS1                         /u01/app/oracle/oradata/test/undotbs01.dbf

如果数据库运行在自动管理undo模式下,那么执行下面的查询就可以显示undo表空间的数据文件名

SQL> SELECT DISTINCT u.TABLESPACE_NAME AS "undo_tbs", d.FILE_NAME AS "dbf_name"
  2  FROM DBA_UNDO_EXTENTS u, DBA_DATA_FILES d
  3  WHERE u.TABLESPACE_NAME=d.TABLESPACE_NAME;

undo_tbs                         dbf_name
------------------------------   --------------------------------------------------------------------------------
UNDOTBS1                         /u01/app/oracle/oradata/test/undotbs01.dbf

3.判断那些对象将会丢失
当对表空间执行TSPITR时,任何在恢复目标时间之后创建的对象都将会丢失。为了判断那些对象将会丢失,查询主数据库中的ts_pitr_objects_to_be_dropped视图。该视图中的owner列,显示的了将会删除的对象的所有者。name列,显示的是当执行TSPITR后丢失对象的对象名。create_time列,显示的是对象创建的时间戳。tablespace_name列,显示的是对象所存储的表空间。

SQL> SELECT OWNER, NAME, TABLESPACE_NAME,
  2  TO_CHAR(CREATION_TIME, 'YYYY-MM-DD:HH24:MI:SS')
  3  FROM SYS.TS_PITR_OBJECTS_TO_BE_DROPPED
  4  WHERE TABLESPACE_NAME IN ('TSPITR','TEST')
  5  AND CREATION_TIME > TO_DATE('2015-04-07 12:00:00','YYYY-MM-DD:HH24:MI:SS')
  6  ORDER BY TABLESPACE_NAME, CREATION_TIME;

no rows selected

4.选择连接辅助实例的方法
要必须能够连接到辅助实例。可以使用Oracle Net或操作系统审计方法

5.为辅助实例创建密码文件
创建密码文件使用orapwd命令,如果不使用密码文件可以跳过这一步

6.为辅助实例创建初始化参数文件
创建一个新的初始化参数文件而不是复制一个再修改。通过对以下参数设置较低的内存来节省内存开销:
.db_cache_size
.shared_pool_size
.large_pool_size

辅助数据库可以与主数据库在相同主机上或者在不同的主机上。因为辅助数据库的数据文件名与主数据库的数据文件名相同,你必须更新辅助控制文件来指定辅助数据库的数据文件。如果辅助数据库与主数据库在同一台主机上,或者辅助数据库与主数据库在不同的主机上但使用不同的路径名,那么必须重命名控制文件,数据文件和联机重做日志文件。如果辅助数据库与主数据库在不同的主机上且使用相同的目录路径,那么只需要重命名联机重做日志文件。通过查看主数据库的联机重做日志文件名所以可以确保在创建辅助数据库时使用唯一的联机重做日志文件名。

SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/test/redo02.log
/u01/app/oracle/oradata/test/redo03.log
/u01/app/oracle/oradata/test/redo01.log

辅助实例初始化参数
db_name:辅助数据库名。该值与主数据库相同
control_files:辅助数据库控制文件。如果辅助数据库与主数据库在相同主机上,确保控制文件名与主数据库的控制文件名不一样。
db_unique_name:就算辅助数据库的值与主数据库相同也允许启动辅助数据库。将它设置成唯一值,例如db_unique_name=aux。这个参数只有辅助数据库与主数据库在相同主机上才需要设置。
db_file_name_convert:使用模式来转换辅助数据库的数据文件名。这个参数只有在与主数据库相同的主要上还原辅助数据库时或者在不同的主机上使用不同的目录路径时才需要。
log_file_name_convert:使用模式来转换辅助数据库的联机重做日志文件名。这个参数是强制性的。
log_archive_dest_1:指定恢复所需要的归档重做日志的缺省目录。这个参数指定辅助主机上的目录。
log_archive_format:指定归档重做日志文件格式。应该使用与主数据库相同的参数值。

在相同主机上通过还原和恢复辅助数据库执行用户管理的TSPITR
下面的例子假设在主数据库test所在主机oracle11g上还原辅助数据库。在这种情况下,主数据库的所有文件都存储在目录/u01/app/oracle/oradata/test目录中,并且想还原辅助数据库到/u02/auxiliary目录中。所以需要设置db_file_name_convert和log_file_name_convert来转换文件名。下面的例子将对数据库test中的表空间tspitr,test执行按时间点恢复,其执行步骤如下:

1.先对主数据库test使用操作系统命令进行备份

[oracle@oracle11g test]$ ls -lrt
total 1450748
-rw-r----- 1 oracle oinstall  52436992 Apr  7 15:43 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52436992 Apr  8 09:39 temp01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 08:57 users01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 08:57 tspitr01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 08:57 test01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo03.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo02.log
-rw-r----- 1 oracle oinstall  52436992 Apr  9 08:57 jy01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 08:57 example01.dbf
-rw-r----- 1 oracle oinstall 293609472 Apr  9 16:02 sysaux01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 16:12 undotbs01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 16:12 system01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:20 redo01.log
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control03.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control01.ctl

[oracle@oracle11g test]$ cp *.dbf /u02/backup/
SQL> alter database backup controlfile to '/u02/control.ctl';

Database altered

要注意的是控制文件不能使用操作系统命令cp来创建否则会出现如下错误:

SQL> alter database mount clone database;
alter database mount clone database
*
ERROR at line 1:
ORA-01696: control file is not a clone control file


[oracle@oracle11g test]$ ls -lrt /u02/backup/
total 1450744
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:22 control01.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:22 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:22 control03.ctl
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:22 example01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:22 jy01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:22 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo03.log
-rw-r----- 1 oracle oinstall 293609472 Apr  9 16:23 sysaux01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:23 test01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:23 temp01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 16:23 system01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:23 tspitr01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 16:24 undotbs01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:24 users01.dbf

2.将辅助集和恢复集文件的备份还原到新目录中/u02/auxiliary

[oracle@oracle11g test]$ cp /u02/control.ctl /u02/auxiliary/control01.ctl
[oracle@oracle11g test]$ cp /u02/control.ctl /u02/auxiliary/control02.ctl
[oracle@oracle11g test]$ cp /u02/control.ctl /u02/auxiliary/control03.ctl
[oracle@oracle11g test]$ cp /u02/backup/system01.dbf /u02/auxiliary/
[oracle@oracle11g test]$ cp /u02/backup/sysaux01.dbf /u02/auxiliary/
[oracle@oracle11g test]$ cp /u02/backup/undotbs01.dbf /u02/auxiliary/
[oracle@oracle11g test]$ cp /u02/backup/tspitr01.dbf /u02/auxiliary/
[oracle@oracle11g test]$ cp /u02/backup/test01.dbf /u02/auxiliary/
[oracle@oracle11g test]$ ls -lrt /u02/auxiliary/
total 1077500
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:29 control03.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:29 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:29 control01.ctl
-rw-r----- 1 oracle oinstall 492838912 Apr  9 16:30 system01.dbf
-rw-r----- 1 oracle oinstall 293609472 Apr  9 16:31 sysaux01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 16:33 undotbs01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:33 tspitr01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:34 test01.dbf

3.创建参数文件

[oracle@oracle11g auxiliary]$ vi initauxiliary.ora

db_name=test
db_unique_name=auxiliary
sga_max_size=160M
sga_target=160M
pga_aggregate_target=16M
db_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
log_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/auxiliary/')
control_files=('/u02/auxiliary/control01.ctl','/u02/auxiliary/control02.ctl','/u02/auxiliary/control03.ctl')
log_archive_dest_1='location=/u02'
log_archive_format='%t_%s_%r.dbf'
compatible=10.2.0.5.0

4.使用创建的参数文件 将辅助数据库启动到nomount状态

[oracle@oracle11g auxiliary]$ export ORACLE_SID=auxiliary
[oracle@oracle11g auxiliary]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 16:46:51 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.


SQL> startup nomount pfile='/u02/auxiliary/initauxiliary.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272624 bytes
Variable Size              58721488 bytes
Database Buffers          104857600 bytes
Redo Buffers                2920448 bytes

5.指定clone关键字来mount辅助数据库

SQL> alter database mount clone database;

Database altered.

6.手动重命名所有辅助数据文件只有没有使用db_file_name_convert和log_file_name_convert参数时才需要执行。在这里使用了这两个参数来重命名所有的数据文件和联机重做日志文件。

7.执行以下脚本来确辅助数据库的所有数据文件名命名是否正确

SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u02/auxiliary/system01.dbf
/u02/auxiliary/undotbs01.dbf
/u02/auxiliary/sysaux01.dbf
/u02/auxiliary/users01.dbf
/u02/auxiliary/example01.dbf
/u02/auxiliary/tspitr01.dbf
/u02/auxiliary/test01.dbf
/u02/auxiliary/jy01.dbf
/u02/auxiliary/redo02.log
/u02/auxiliary/redo03.log
/u02/auxiliary/redo01.log
/u02/auxiliary/control01.ctl
/u02/auxiliary/control02.ctl
/u02/auxiliary/control03.ctl

14 rows selected.

如果不正确在步骤6中手动重命名数据文件

8.将辅助集和恢复集表空间的数据文件设置为联机状态。

SQL> alter database datafile '/u02/auxiliary/system01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/undotbs01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/sysaux01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/tspitr01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/auxiliary/test01.dbf' online;

Database altered.

9.使用using backup controlfile选项将辅助数据库恢复到指定的恢复目标时间点。

SQL> recover database until cancel using backup controlfile;
ORA-00279: change 1718118 generated at 04/09/2015 08:57:02 needed for thread 1
ORA-00289: suggestion : /u02/1_13_876413942.dbf
ORA-00280: change 1718118 for thread 1 is in sequence #13


Specify log: {=suggested | filename | AUTO | CANCEL}
/u01/app/oracle/oradata/test/redo01.log
Log applied.
Media recovery complete.

10.以resetlogs选项来打开辅助数据库

SQL> alter database open resetlogs;

Database altered.

下面就是执行传输表空间的操作
11.使用管理权限连接辅助数据库

[oracle@oracle11g auxiliary]$ export ORACLE_SID=auxiliary
[oracle@oracle11g auxiliary]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 20:12:27 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

12.执行alter tablespace … read only语句将执行完按时间点恢复的表空间tspitr,test设置为只读状态

SQL> alter tablespace tspitr read only;

Tablespace altered.

SQL> alter tablespace test read only;

Tablespace altered.

13.对表空间tspitr,test执行自包含检查

SQL> exec sys.dbms_tts.transport_set_check('tspitr,test',true,true);

PL/SQL procedure successfully completed.

SQL> select * from sys.transport_set_violations;

no rows selected

14.使用Oracle导出工具生成传输表空间集的元数据

[oracle@oracle11g ~]$ export ORACLE_SID=auxiliary
[oracle@oracle11g u02]$ exp \'sys/jy as sysdba\' transport_tablespace=y tablespaces=tspitr,test tts_full_check=y file=/u02/transport_tablespace.dmp log=/u02/transport_tablespace.log

Export: Release 10.2.0.5.0 - Production on Thu Apr 9 20:29:58 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace TSPITR ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                         TSPITR
For tablespace TEST ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

下面将表空间tspitr,test附加到主数据库中
15.连接到主数据库

[oracle@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 21:07:39 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

16.从主数据库中将表空间tspitr,test删除

SQL> drop tablespace tspitr including contents and datafiles;

Tablespace dropped.

SQL> drop tablespace test including contents and datafiles;

Tablespace dropped.

17.将辅助数据库中的恢复集表空间的数据文件复制到主数据库目录/u01/app/oracle/oradata/test中

[oracle@oracle11g auxiliary]$ cp test01.dbf /u01/app/oracle/oradata/test/
[oracle@oracle11g auxiliary]$ cp tspitr01.dbf /u01/app/oracle/oradata/test/
[oracle@oracle11g auxiliary]$ ls -lrt /u01/app/oracle/oradata/test/
total 1450748
-rw-r----- 1 oracle oinstall  52436992 Apr  7 15:43 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo03.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo02.log
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:59 users01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:59 jy01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:59 example01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 17:18 temp01.dbf
-rw-r----- 1 oracle oinstall 293609472 Apr  9 21:02 sysaux01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 21:11 undotbs01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 21:11 system01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 21:11 redo01.log
-rw-r----- 1 oracle oinstall  10493952 Apr  9 21:11 test01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 21:12 tspitr01.dbf
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:12 control03.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:12 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:12 control01.ctl

18.将传输表空间集中的表空间test,tspitr附加到主数据库test中

[oracle@oracle11g auxiliary]$ export ORACLE_SID=test
[oracle@oracle11g auxiliary]$ imp \'sys/zzh_2046 as sysdba\' transport_tablespace=y file=/u02/transport_tablespace.dmp log=/u02/transport_tablespace_dr.log datafiles='/u01/app/oracle/oradata/test/tspitr01.dbf','/u01/app/oracle/oradata/test/test01.dbf'

Import: Release 10.2.0.5.0 - Production on Thu Apr 9 21:17:04 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing TSPITR's objects into TSPITR
. . importing table                       "TSPITR"
. importing TEST's objects into TEST
. . importing table                         "TEST"
. importing SYS's objects into SYS
Import terminated successfully without warnings.


SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/test/system01.dbf
/u01/app/oracle/oradata/test/undotbs01.dbf
/u01/app/oracle/oradata/test/sysaux01.dbf
/u01/app/oracle/oradata/test/users01.dbf
/u01/app/oracle/oradata/test/example01.dbf
/u01/app/oracle/oradata/test/tspitr01.dbf
/u01/app/oracle/oradata/test/test01.dbf
/u01/app/oracle/oradata/test/jy01.dbf
/u01/app/oracle/oradata/test/redo02.log
/u01/app/oracle/oradata/test/redo03.log
/u01/app/oracle/oradata/test/redo01.log

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/test/control01.ctl
/u01/app/oracle/oradata/test/control02.ctl
/u01/app/oracle/oradata/test/control03.ctl

14 rows selected.

SQL> select count(*) from tspitr.tspitr;

  COUNT(*)
----------
     50707

SQL> select count(*) from test.test;

  COUNT(*)
----------
     50683

19.将辅助数据库中的表空间tspitr,test设置为读写模式

SQL> alter tablespace tspitr read write;

Tablespace altered.

SQL> alter tablespace test read write;

Tablespace altered.

20.使用操作系统工具对恢复的表空间进行备份或者RMAN对整个主数据库进行备份这里不再赘述

在不同的主机使用不同的目录路径还原辅助数据执行用户管理的TSPITR
下面的例子假设在主机jingyong1上创建辅助数据库aux,主数据库test在主机oracle11g上,这里仍然使用db_file_name_covert和log_file_name_convert参数进行数据文件和联机重做日志文件的转换.
1.先对主数据库test使用操作系统命令进行备份

[oracle@oracle11g test]$ ls -lrt
total 1450748
-rw-r----- 1 oracle oinstall  52436992 Apr  7 15:43 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52436992 Apr  8 09:39 temp01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 08:57 users01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 08:57 tspitr01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 08:57 test01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo03.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo02.log
-rw-r----- 1 oracle oinstall  52436992 Apr  9 08:57 jy01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 08:57 example01.dbf
-rw-r----- 1 oracle oinstall 293609472 Apr  9 16:02 sysaux01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 16:12 undotbs01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 16:12 system01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:20 redo01.log
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control03.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 16:21 control01.ctl

[oracle@oracle11g test]$ cp *.dbf /u02/backup/
SQL> alter database backup controlfile to '/u02/control.ctl';

Database altered

要注意的是控制文件不能使用操作系统命令cp来创建否则会出现如下错误:

SQL> alter database mount clone database;
alter database mount clone database
*
ERROR at line 1:
ORA-01696: control file is not a clone control file


[oracle@oracle11g test]$ ls -lrt /u02/backup/
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:22 example01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:22 jy01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:22 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 16:22 redo03.log
-rw-r----- 1 oracle oinstall 293609472 Apr  9 16:23 sysaux01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:23 test01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:23 temp01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 16:23 system01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:23 tspitr01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 16:24 undotbs01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:24 users01.dbf

2.将辅助集和恢复集文件的备份和需要的归档重做日志和联机重做日志文件还原到新目录/u02/aux,/u02/archive中

[oracle@jingyong1 u02]$ scp -r oracle@192.168.56.2:/u02/backup/*.dbf /u02/aux
oracle@192.168.56.2's password:
sysaux01.dbf                                                                                                                         100%  280MB   7.4MB/s   00:38
system01.dbf                                                                                                                         100%  470MB   7.2MB/s   01:05
test01.dbf                                                                                                                           100%   10MB   5.0MB/s   00:02
tspitr01.dbf                                                                                                                         100%  100MB   7.7MB/s   00:13
undotbs01.dbf                                                                                                                        100%  170MB   5.0MB/s   00:34
[oracle@jingyong1 u02]$ scp -r oracle@192.168.56.2:/u02/control.ctl /u02/aux
oracle@192.168.56.2's password:
control.ctl                                                                                                                          100% 7216KB   7.1MB/s   00:00
[oracle@jingyong1 u02]$ scp -r oracle@192.168.56.2:/u02/1_11_876413942.dbf /u02/archive
oracle@192.168.56.2's password:
1_11_876413942.dbf                                                                                                                   100% 5751KB   5.6MB/s   00:01
[oracle@jingyong1 u02]$ scp -r oracle@192.168.56.2:/u02/1_12_876413942.dbf /u02/archive
oracle@192.168.56.2's password:
1_12_876413942.dbf                                                                                                                   100%   15MB  14.9MB/s   00:01
[oracle@jingyong1 u02]$ scp -r oracle@192.168.56.2:/u01/app/oracle/oradata/test/*.log /u02/aux
oracle@192.168.56.2's password:
redo01.log                                                                                                                           100%   50MB   8.3MB/s   00:06
redo02.log                                                                                                                           100%   50MB  12.5MB/s   00:04
redo03.log                                                                                                                           100%   50MB  10.0MB/s   00:05


[oracle@jingyong1 aux]$ ls -lrt
total 1216824
-rw-r----- 1 oracle oinstall 293609472 Apr  9 20:43 sysaux01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 20:44 system01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 20:44 test01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 20:44 tspitr01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 20:45 undotbs01.dbf
-rw-r----- 1 oracle oinstall   7389184 Apr  9 20:46 control.ctl
-rw-r----- 1 oracle oinstall  52429312 Apr  9 20:47 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 20:47 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 20:47 redo03.log
[oracle@jingyong1 aux]$ ls -lrt /u02/archive/
total 21076
-rw-r----- 1 oracle oinstall  5888512 Apr  9 20:46 1_11_876413942.dbf
-rw-r----- 1 oracle oinstall 15657472 Apr  9 20:47 1_12_876413942.dbf

3.创建参数文件

[oracle@jingyong1 aux]$ vi initaux.ora
db_name=test
db_unique_name=aux
sga_max_size=160M
sga_target=160M
pga_aggregate_target=16M
db_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/aux/')
log_file_name_convert=('/u01/app/oracle/oradata/test/','/u02/aux/')
control_files='/u02/aux/control.ctl'
log_archive_dest_1='location=/u02/archive'
log_archive_format='%t_%s_%r.dbf'
compatible=10.2.0.5.0

4.使用创建的参数文件 将辅助数据库启动到nomount状态

[oracle@jingyong1 ~]$ export ORACLE_SID=aux
[oracle@jingyong1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 20:56:16 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.

SQL> startup nomount pfile='/u02/aux/initaux.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272624 bytes
Variable Size              58721488 bytes
Database Buffers          104857600 bytes
Redo Buffers                2920448 bytes

5.指定clone关键字来mount辅助数据库

SQL> alter database mount clone database;

Database altered.

6.手动重命名所有辅助数据文件只有没有使用db_file_name_convert和log_file_name_convert参数时才需要执行。在这里使用了这两个参数来重命名所有的数据文件和联机重做日志文件。

7.执行以下脚本来确辅助数据库的所有数据文件名命名是否正确

SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u02/aux/system01.dbf
/u02/aux/undotbs01.dbf
/u02/aux/sysaux01.dbf
/u02/aux/users01.dbf
/u02/aux/example01.dbf
/u02/aux/tspitr01.dbf
/u02/aux/test01.dbf
/u02/aux/jy01.dbf
/u02/aux/redo02.log
/u02/aux/redo03.log
/u02/aux/redo01.log
/u02/aux/control.ctl

12 rows selected.

如果不正确在步骤6中手动重命名数据文件

8.将辅助集和恢复集表空间的数据文件设置为联机状态。

SQL> alter database datafile '/u02/aux/system01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/aux/undotbs01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/aux/sysaux01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/aux/tspitr01.dbf' online;

Database altered.

SQL> alter database datafile '/u02/aux/test01.dbf' online;

Database altered.

9.使用using backup controlfile选项将辅助数据库恢复到指定的恢复目标时间点

SQL> recover database until cancel using backup controlfile;
ORA-00279: change 1718118 generated at 04/09/2015 08:57:02 needed for thread 1
ORA-00289: suggestion : /u02/archive/1_13_876413942.dbf
ORA-00280: change 1718118 for thread 1 is in sequence #13


Specify log: {=suggested | filename | AUTO | CANCEL}
/u02/aux/redo01.log
Log applied.
Media recovery complete.

10.以resetlogs选项来打开辅助数据库

SQL> alter database open resetlogs;

Database altered.

下面就是执行传输表空间的操作
11.使用管理权限连接辅助数据库

[oracle@jingyong1 ~]$ export ORACLE_SID=aux
[oracle@jingyong1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 21:03:23 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

12.执行alter tablespace … read only语句将执行完按时间点恢复的表空间tspitr,test设置为只读状态

SQL> alter tablespace tspitr read only;

Tablespace altered.

SQL> alter tablespace test read only;

Tablespace altered.

13.对表空间tspitr,test执行自包含检查

SQL> exec sys.dbms_tts.transport_set_check('tspitr,test',true,true);

PL/SQL procedure successfully completed.

SQL> select * from sys.transport_set_violations;

no rows selected

14.使用Oracle导出工具生成传输表空间集的元数据

[oracle@jingyong1 u02]$ export ORACLE_SID=aux
[oracle@jingyong1 u02]$ exp \'sys/jy as sysdba\' transport_tablespace=y tablespaces=tspitr,test tts_full_check=y file=/u02/tts.dmp log=/u02/tts.log

Export: Release 10.2.0.5.0 - Production on Thu Apr 9 21:05:53 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace TSPITR ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                         TSPITR
For tablespace TEST ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                           TEST
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

下面将表空间tspitr,test附加到主数据库中
15.连接到主数据库

[oracle@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Thu Apr 9 21:22:32 2015

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

16.从主数据库中将表空间tspitr,test删除

SQL> drop tablespace tspitr including contents and datafiles;

Tablespace dropped.

SQL> drop tablespace test including contents and datafiles;

Tablespace dropped.

17.将辅助数据库中的恢复集表空间的数据文件复制到主数据库目录/u01/app/oracle/oradata/test中

[oracle@oracle11g test]$ scp -r oracle@192.168.56.11:/u02/tts.dmp /u02/
oracle@192.168.56.11's password:
tts.dmp                                                                                                                              100%   24KB  24.0KB/s   00:00
[oracle@oracle11g test]$ scp -r oracle@192.168.56.11:/u02/aux/tspitr01.dbf /u01/app/oracle/oradata/test/
oracle@192.168.56.11's password:
tspitr01.dbf                                                                                                                         100%  100MB   6.3MB/s   00:16
[oracle@oracle11g test]$ scp -r oracle@192.168.56.11:/u02/aux/test01.dbf /u01/app/oracle/oradata/test/
oracle@192.168.56.11's password:
test01.dbf                                                                                                                           100%   10MB   5.0MB/s   00:02
[oracle@oracle11g test]$ ls -lrt
total 1450748
-rw-r----- 1 oracle oinstall  52436992 Apr  7 15:43 jy01.dbf.bak
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo03.log
-rw-r----- 1 oracle oinstall  52429312 Apr  9 08:57 redo02.log
-rw-r----- 1 oracle oinstall  10493952 Apr  9 16:59 users01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 16:59 jy01.dbf
-rw-r----- 1 oracle oinstall 104865792 Apr  9 16:59 example01.dbf
-rw-r----- 1 oracle oinstall  52436992 Apr  9 17:18 temp01.dbf
-rw-r----- 1 oracle oinstall 293609472 Apr  9 21:19 sysaux01.dbf
-rw-r----- 1 oracle oinstall 492838912 Apr  9 21:24 system01.dbf
-rw-r----- 1 oracle oinstall 178266112 Apr  9 21:25 undotbs01.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  9 21:25 redo01.log
-rw-r----- 1 oracle oinstall 104865792 Apr  9 21:25 tspitr01.dbf
-rw-r----- 1 oracle oinstall  10493952 Apr  9 21:26 test01.dbf
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:26 control03.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:26 control02.ctl
-rw-r----- 1 oracle oinstall   7389184 Apr  9 21:26 control01.ctl
[oracle@oracle11g test]$ ls -lrt /u02/
-rw-r--r-- 1 oracle oinstall      24576 Apr  9 21:25 tts.dmp

18.将传输表空间集中的表空间test,tspitr附加到主数据库test中

[oracle@oracle11g auxiliary]$ export ORACLE_SID=test
[oracle@oracle11g auxiliary]$ imp \'sys/zzh_2046 as sysdba\' transport_tablespace=y file=/u02/tts.dmp log=/u02/tts_dr.log datafiles='/u01/app/oracle/oradata/test/tspitr01.dbf','/u01/app/oracle/oradata/test/test01.dbf'

Import: Release 10.2.0.5.0 - Production on Thu Apr 9 21:27:47 2015

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V10.02.01 via conventional path
About to import transportable tablespace(s) metadata...
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing TSPITR's objects into TSPITR
. . importing table                       "TSPITR"
. importing TEST's objects into TEST
. . importing table                         "TEST"
. importing SYS's objects into SYS
Import terminated successfully without warnings.


SQL> SELECT NAME FROM V$DATAFILE
  2  UNION ALL
  3  SELECT MEMBER FROM V$LOGFILE
  4  UNION ALL
  5  SELECT NAME FROM V$CONTROLFILE;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/test/system01.dbf
/u01/app/oracle/oradata/test/undotbs01.dbf
/u01/app/oracle/oradata/test/sysaux01.dbf
/u01/app/oracle/oradata/test/users01.dbf
/u01/app/oracle/oradata/test/example01.dbf
/u01/app/oracle/oradata/test/tspitr01.dbf
/u01/app/oracle/oradata/test/test01.dbf
/u01/app/oracle/oradata/test/jy01.dbf
/u01/app/oracle/oradata/test/redo02.log
/u01/app/oracle/oradata/test/redo03.log
/u01/app/oracle/oradata/test/redo01.log
/u01/app/oracle/oradata/test/control01.ctl
/u01/app/oracle/oradata/test/control02.ctl
/u01/app/oracle/oradata/test/control03.ctl

14 rows selected.

SQL> select count(*) from tspitr.tspitr;

  COUNT(*)
----------
     50707

SQL> select count(*) from test.test;

  COUNT(*)
----------
     50683

19.将辅助数据库中的表空间tspitr,test设置为读写模式

SQL> alter tablespace tspitr read write;

Tablespace altered.

SQL> alter tablespace test read write;

Tablespace altered.

20.使用操作系统工具对恢复的表空间进行备份或者RMAN对整个主数据库进行备份这里不再赘述

利用RMAN在文件系统与ASM之间迁移数据库

在Oracle10g中在操作系统与ASM之间不能使用复制命令(11g有相关复制命令)比如cp操作来复制文件,为了使用ASM来管理已经存在的数据库必须将数据库迁移到ASM中。可以通过RMAN来完成这项操作。假设在文件系统和ASM磁盘组中有足够的空间来存储整个数据库可以将数据库从文件系统中直接迁移到ASM中。如果在ASM和文件系统同时没有足够的空间来存储整个数据库,可以将数据库备份到磁带,使用旧的磁盘来创建ASM磁盘组并使用磁带备份将数据库还原到ASM中。

如果文件系统和ASM同时有足够的空间来存储整个数据库,可以不使用磁带过渡直接将数据库迁移到ASM中。在这个例子中目标ASM磁盘组为+DISK1。在迁移的过程中所有的闪回日志将会被丢弃,因此,数据库中的任何受保护还原点都将变为无效。在执行迁移之前需要将其删除。

将数据库从文件系统迁移到ASM的操作步骤如下:
1.记录数据库迁移之前的数据文件,控制文件,联机重做日志文件的文件名

SQL> select name,status from v$datafile;

NAME                                                                             STATUS
-------------------------------------------------------------------------------- -------
/u01/app/oracle/oradata/test/system01.dbf                                        SYSTEM
/u01/app/oracle/oradata/test/undotbs01.dbf                                       ONLINE
/u01/app/oracle/oradata/test/sysaux01.dbf                                        ONLINE
/u01/app/oracle/oradata/test/users01.dbf                                         ONLINE
/u01/app/oracle/oradata/test/example01.dbf                                       ONLINE
/u01/app/oracle/oradata/test/tspitr01.dbf                                        ONLINE
/u01/app/oracle/oradata/test/test01.dbf                                          ONLINE

7 rows selected

SQL> select member,status from v$logfile;

MEMBER                                                                           STATUS
-------------------------------------------------------------------------------- -------
/u01/app/oracle/oradata/test/redo03.log
/u01/app/oracle/oradata/test/redo02.log
/u01/app/oracle/oradata/test/redo01.log

SQL> show parameter control_files

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      /u01/app/oracle/oradata/test/control01.ctl,
                                                 /u01/app/oracle/oradata/test/control02.ctl,
                                                 /u01/app/oracle/oradata/test/control03.ctl

2.将数据库文件作为副本备份到ASM磁盘组

[oracle@oracle11g ~]$ export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';
[oracle@oracle11g ~]$ rman target sys/zzh_2046@test  catalog rman/rman@jy

Recovery Manager: Release 10.2.0.5.0 - Production on Wed Apr 1 16:06:19 2015

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

connected to target database: TEST (DBID=2168949517)
connected to recovery catalog database

RMAN> backup as copy incremental level 0 database format '+DISK1' tag 'DB_ASM_MIGRATION';

Starting backup at 2015-04-01 16:06:27
starting full resync of recovery catalog
full resync complete
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=151 devtype=DISK
channel ORA_DISK_1: starting datafile copy
input datafile fno=00001 name=/u01/app/oracle/oradata/test/system01.dbf
output filename=+DISK1/test/datafile/system.266.875894803 tag=DB_ASM_MIGRATION recid=17 stamp=875894833
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile copy
input datafile fno=00003 name=/u01/app/oracle/oradata/test/sysaux01.dbf
output filename=+DISK1/test/datafile/sysaux.267.875894845 tag=DB_ASM_MIGRATION recid=18 stamp=875894865
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting datafile copy
input datafile fno=00005 name=/u01/app/oracle/oradata/test/example01.dbf
output filename=+DISK1/test/datafile/example.265.875894871 tag=DB_ASM_MIGRATION recid=19 stamp=875894883
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile fno=00006 name=/u01/app/oracle/oradata/test/tspitr01.dbf
output filename=+DISK1/test/datafile/tspitr.264.875894885 tag=DB_ASM_MIGRATION recid=20 stamp=875894892
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:16
channel ORA_DISK_1: starting datafile copy
input datafile fno=00002 name=/u01/app/oracle/oradata/test/undotbs01.dbf
output filename=+DISK1/test/datafile/undotbs1.263.875894899 tag=DB_ASM_MIGRATION recid=21 stamp=875894903
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile fno=00004 name=/u01/app/oracle/oradata/test/users01.dbf
output filename=+DISK1/test/datafile/users.262.875894907 tag=DB_ASM_MIGRATION recid=22 stamp=875894907
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
channel ORA_DISK_1: starting datafile copy
input datafile fno=00007 name=/u01/app/oracle/oradata/test/test01.dbf
output filename=+DISK1/test/datafile/test.261.875894909 tag=DB_ASM_MIGRATION recid=23 stamp=875894910
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-04-01 16:08:30

Starting Control File and SPFILE Autobackup at 2015-04-01 16:08:30
piece handle=/u02/c-2168949517-20150401-08 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 16:08:35


ASMCMD> ls -lrt
Type      Redund  Striped  Time             Sys  Name
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    EXAMPLE.265.875894871
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    SYSAUX.267.875894845
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    SYSTEM.266.875894803
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    TEST.261.875894909
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    TSPITR.264.875894885
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    UNDOTBS1.263.875894899
DATAFILE  UNPROT  COARSE   APR 01 16:00:00  Y    USERS.262.875894907

为了确保数据库能执行一致性恢复,归档当前联机重做日

RMAN> sql 'alter system archive log current';

sql statement: alter system archive log current

如果数据库很大在执行上面的备份时可能会要很长时间。如果在执行备份时有大量的数据库活动,可以对数据库生成一个增量备份在迁移之前将0级备份之后的改变刷新到0级备份所创建的副本中。

RMAN> backup incremental level 1 for recover of copy with tag 'DB_ASM_MIGRATION' database;

Starting backup at 2015-04-01 16:19:52
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental level 1 datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/u01/app/oracle/oradata/test/system01.dbf
input datafile fno=00003 name=/u01/app/oracle/oradata/test/sysaux01.dbf
input datafile fno=00005 name=/u01/app/oracle/oradata/test/example01.dbf
input datafile fno=00006 name=/u01/app/oracle/oradata/test/tspitr01.dbf
input datafile fno=00002 name=/u01/app/oracle/oradata/test/undotbs01.dbf
input datafile fno=00004 name=/u01/app/oracle/oradata/test/users01.dbf
input datafile fno=00007 name=/u01/app/oracle/oradata/test/test01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-04-01 16:19:53
channel ORA_DISK_1: finished piece 1 at 2015-04-01 16:20:38
piece handle=/u02/ora_test875895593_1301 tag=DB_ASM_MIGRATION comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:45
Finished backup at 2015-04-01 16:20:39

Starting Control File and SPFILE Autobackup at 2015-04-01 16:20:39
piece handle=/u02/c-2168949517-20150401-09 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 16:20:42

RMAN> recover copy of database with tag 'DB_ASM_MIGRATION';

Starting recover at 2015-04-01 16:21:09
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backupset restore
channel ORA_DISK_1: specifying datafile copies to recover
recovering datafile copy fno=00001 name=+DISK1/test/datafile/system.266.875894803
recovering datafile copy fno=00002 name=+DISK1/test/datafile/undotbs1.263.875894899
recovering datafile copy fno=00003 name=+DISK1/test/datafile/sysaux.267.875894845
recovering datafile copy fno=00004 name=+DISK1/test/datafile/users.262.875894907
recovering datafile copy fno=00005 name=+DISK1/test/datafile/example.265.875894871
recovering datafile copy fno=00006 name=+DISK1/test/datafile/tspitr.264.875894885
recovering datafile copy fno=00007 name=+DISK1/test/datafile/test.261.875894909
channel ORA_DISK_1: reading from backup piece /u02/ora_test875895593_1301
channel ORA_DISK_1: restored backup piece 1
piece handle=/u02/ora_test875895593_1301 tag=DB_ASM_MIGRATION
channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
Finished recover at 2015-04-01 16:21:19

Starting Control File and SPFILE Autobackup at 2015-04-01 16:21:19
piece handle=/u02/c-2168949517-20150401-0a comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 16:21:23

3.在ASM磁盘组中创建一个SPFILE文件副本。将要迁移的数据库SPFILE文件存储为+DISK1/spfile如果数据库是使用的SPFILE启动,就执行以下命令:

RMAN> run
2> {
3> backup as backupset spfile;
4> restore spfile to '+DISK1/spfile';
5> }

Starting backup at 2015-04-01 16:24:46
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 2015-04-01 16:24:48
channel ORA_DISK_1: finished piece 1 at 2015-04-01 16:24:49
piece handle=/u02/ora_test875895887_1331 tag=TAG20150401T162446 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 2015-04-01 16:24:49

Starting Control File and SPFILE Autobackup at 2015-04-01 16:24:49
piece handle=/u02/c-2168949517-20150401-0b comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 16:24:51

Starting restore at 2015-04-01 16:24:52
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: restoring SPFILE
output filename=+DISK1/spfile
channel ORA_DISK_1: reading from backup piece /u02/c-2168949517-20150401-0b
channel ORA_DISK_1: restored backup piece 1
piece handle=/u02/c-2168949517-20150401-0b tag=TAG20150401T162450
channel ORA_DISK_1: restore complete, elapsed time: 00:00:04
Finished restore at 2015-04-01 16:24:56


ASMCMD> ls -lrt
Type           Redund  Striped  Time             Sys  Name
                                                 Y    DB_UNKNOWN/
                                                 Y    TEST/
                                                 N    spfile => +DISK1/DB_UNKNOWN/PARAMETERFILE/SPFILE.260.875895895

如果数据库是使用PFILE启动,可以在SQL*Plus中执行create spfile命令来创建SPFILE

SQL>create spfile='+DISK1/spfile' from pfile='$ORACLE_HOME/dbs/inittest.ora'

File created.

4.如果被迁移的数据库是备库,需要停止管理恢复模式

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

执行一致性关闭

RMAN> SHUTDOWN IMMEDIATE;


5.在目录/u02/backup中创建一个 pfile参数文件来引用新的SPFILE文件,并重新启动实例到nomount状态

[oracle@oracle11g backup]$ vi inittest_temp.ora
spfile='+DISK1/spfile'

RMAN> shutdown immediate

database closed
database dismounted
Oracle instance shut down

SQL> startup nomount pfile='/u02/backup/inittest_temp.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272600 bytes
Variable Size             109053160 bytes
Database Buffers           54525952 bytes
Redo Buffers                2920448 bytes

6.为了下一步将控制文件迁移到ASM中,修改control_files参数:

SQL> alter system set control_files='+DISK1/control01.ctl' scope=spfile;

System altered.

7.通过设置db_recovery_file_dest和db_recovery_file_dest_size来指定闪回区,这里闪回区大小设置为2G(因为这里只是进行迁移测试实验)

SQL> alter system set db_recovery_file_dest_size=2G;

System altered.

SQL> alter system set db_recovery_file_dest='+DISK1';

System altered.

8.将实例重新启动到nomount状态,使用RMAN在ASM中创建新的控制文件(使用原来控制文件来创建),切换数据文件,并执执恢复

SQL> shutdown immediate
ORA-01507: database not mounted


ORACLE instance shut down.
SQL> startup nomount pfile='/u02/backup/inittest_temp.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272600 bytes
Variable Size             109053160 bytes
Database Buffers           54525952 bytes
Redo Buffers                2920448 bytes

RMAN> restore controlfile from '/u01/app/oracle/oradata/test/control01.ctl';

Starting restore at 2015-04-01 17:11:20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK

channel ORA_DISK_1: copied control file copy
output filename=+DISK1/control01.ctl
Finished restore at 2015-04-01 17:11:28


RMAN> alter database mount;

database mounted
released channel: ORA_DISK_1

RMAN> switch database to copy;

datafile 1 switched to datafile copy "+DISK1/test/datafile/system.266.875894803"
datafile 2 switched to datafile copy "+DISK1/test/datafile/undotbs1.263.875894899"
datafile 3 switched to datafile copy "+DISK1/test/datafile/sysaux.267.875894845"
datafile 4 switched to datafile copy "+DISK1/test/datafile/users.262.875894907"
datafile 5 switched to datafile copy "+DISK1/test/datafile/example.265.875894871"
datafile 6 switched to datafile copy "+DISK1/test/datafile/tspitr.264.875894885"
datafile 7 switched to datafile copy "+DISK1/test/datafile/test.261.875894909"
starting full resync of recovery catalog
full resync complete

RMAN> recover database;

Starting recover at 2015-04-01 17:12:29
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK

starting media recovery
media recovery complete, elapsed time: 00:00:03

Finished recover at 2015-04-01 17:12:33

9.将临时文件迁移到ASM,必须为每个临时文件执行set newname命令,然后执行switch命令

RMAN> run
2> {
3> set newname for tempfile '/u01/app/oracle/oradata/test/temp01.dbf' to '+DISK1';
4>switch tempfile all;
5> }

executing command: SET NEWNAME

renamed temporary file 1 to +DISK1 in control file
starting full resync of recovery catalog
full resync complete

10.禁用闪回数据库并重新启动闪回数据库让闪回日志创建在ASM中

SQL> alter database flashback off;

Database altered.

SQL> alter database flashback on;

Database altered.

11.改变跟踪文件不会被迁移。只能禁用改变跟踪再重新启用改变跟踪,并将改变跟踪文件存储在ASM中

SQL> alter database disable block change tracking;

Database altered.

SQL> alter database enable block change tracking using file '+DISK1';

Database altered.

12.如果迁移的是主库,打开数据库

SQL> alter database open;

Database altered.

如果是备库,恢复管理恢复模式

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE;

13.对于主库,要迁移联机重做日志文件是通过增加新的重做日志文件并存储在ASM中并删除旧的重做日志文件。对于备库,就是删除旧的备重做日志并增加新的备重做日志并存储在ASM中


SQL> select * from v$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS            FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ----------------  ------------- ------------
         1          1         97   52428800          1 NO  CURRENT            1288360      01-APR-15
         2          1         95   52428800          1 YES INACTIVE           1226685      30-MAR-15
         3          1         96   52428800          1 YES INACTIVE           1268619      31-MAR-15

SQL> alter database drop logfile group 2;

Database altered.

SQL> alter database add logfile group 2 '+DISK1/redo02.log' size 50M;

Database altered.

SQL> alter database drop logfile group 3;

Database altered.

SQL> alter database add logfile group 3 '+DISK1/redo03.log' size 50M;

Database altered.

SQL> alter system switch logfile;

System altered.

SQL> alter database drop logfile group 1;
alter database drop logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of instance test (thread 1)
ORA-00312: online log 1 thread 1: '/u01/app/oracle/oradata/test/redo01.log'


SQL> alter database clear logfile group 1;

Database altered.

SQL> alter database drop logfile group 1;

Database altered.

SQL> alter database add logfile group 1 '+DISK1/redo01.log' size 50M;

Database altered.

将数据库从ASM中迁移到文件系统
1.记录数据库迁移之前的数据文件,控制文件,联机重做日志文件的文件名

SQL> select member,status from v$logfile;

MEMBER                                                                           STATUS
-------------------------------------------------------------------------------- -------
+DISK1/redo03.log
+DISK1/redo02.log
+DISK1/redo01.log

SQL> show parameter control_files

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      +DISK1/control01.ctl

SQL> select name,status from v$datafile;

NAME                                                                             STATUS
-------------------------------------------------------------------------------- -------
+DISK1/test/datafile/system.266.875894803                                        SYSTEM
+DISK1/test/datafile/undotbs1.263.875894899                                      ONLINE
+DISK1/test/datafile/sysaux.267.875894845                                        ONLINE
+DISK1/test/datafile/users.262.875894907                                         ONLINE
+DISK1/test/datafile/example.265.875894871                                       ONLINE
+DISK1/test/datafile/tspitr.264.875894885                                        ONLINE
+DISK1/test/datafile/test.261.875894909                                          ONLINE

7 rows selected

2.将数据库文件作为副本备份到ASM磁盘组

RMAN> backup as copy incremental level 0 database format '/u02/asm_backup/%U' tag 'NO_ASM_MIGRATION';

Starting backup at 2015-04-01 20:47:32
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=140 devtype=DISK
channel ORA_DISK_1: starting datafile copy
input datafile fno=00001 name=+DISK1/test/datafile/system.266.875894803
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSTEM_FNO-1_4rq3amf5 tag=NO_ASM_MIGRATION recid=46 stamp=875911696
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile copy
input datafile fno=00003 name=+DISK1/test/datafile/sysaux.267.875894845
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSAUX_FNO-3_4sq3amgj tag=NO_ASM_MIGRATION recid=47 stamp=875911724
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile fno=00005 name=+DISK1/test/datafile/example.265.875894871
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-EXAMPLE_FNO-5_4tq3amhm tag=NO_ASM_MIGRATION recid=48 stamp=875911742
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile fno=00006 name=+DISK1/test/datafile/tspitr.264.875894885
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-TSPITR_FNO-6_4uq3ami5 tag=NO_ASM_MIGRATION recid=49 stamp=875911756
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile fno=00002 name=+DISK1/test/datafile/undotbs1.263.875894899
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-UNDOTBS1_FNO-2_4vq3amik tag=NO_ASM_MIGRATION recid=50 stamp=875911766
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
channel ORA_DISK_1: starting datafile copy
input datafile fno=00004 name=+DISK1/test/datafile/users.262.875894907
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-USERS_FNO-4_50q3amin tag=NO_ASM_MIGRATION recid=51 stamp=875911768
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile copy
input datafile fno=00007 name=+DISK1/test/datafile/test.261.875894909
output filename=/u02/asm_backup/data_D-TEST_I-2168949517_TS-TEST_FNO-7_51q3amio tag=NO_ASM_MIGRATION recid=52 stamp=875911769
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 2015-04-01 20:49:29

Starting Control File and SPFILE Autobackup at 2015-04-01 20:49:30
piece handle=/u02/c-2168949517-20150401-16 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 20:49:34


[oracle@oracle11g asm_backup]$ ls -lrt
total 1014840
-rw-r----- 1 oracle oinstall 482353152 Apr  1 20:48 data_D-TEST_I-2168949517_TS-SYSTEM_FNO-1_4rq3amf5
-rw-r----- 1 oracle oinstall 293609472 Apr  1 20:48 data_D-TEST_I-2168949517_TS-SYSAUX_FNO-3_4sq3amgj
-rw-r----- 1 oracle oinstall 104865792 Apr  1 20:49 data_D-TEST_I-2168949517_TS-EXAMPLE_FNO-5_4tq3amhm
-rw-r----- 1 oracle oinstall 104865792 Apr  1 20:49 data_D-TEST_I-2168949517_TS-TSPITR_FNO-6_4uq3ami5
-rw-r----- 1 oracle oinstall  31465472 Apr  1 20:49 data_D-TEST_I-2168949517_TS-UNDOTBS1_FNO-2_4vq3amik
-rw-r----- 1 oracle oinstall  10493952 Apr  1 20:49 data_D-TEST_I-2168949517_TS-USERS_FNO-4_50q3amin
-rw-r----- 1 oracle oinstall  10493952 Apr  1 20:49 data_D-TEST_I-2168949517_TS-TEST_FNO-7_51q3amio

为了确保数据库能执行一致性恢复,归档当前联机重做日

RMAN> sql 'alter system archive log current';

sql statement: alter system archive log current

如果数据库很大在执行上面的备份时可能会要很长时间。如果在执行备份时有大量的数据库活动,可以对数据库生成一个增量备份在迁移之前将0级备份之后的改变刷新到0级备份所创建的副本中。

RMAN> backup incremental level 1 for recover of copy with tag 'NO_ASM_MIGRATION' database;

Starting backup at 2015-04-01 20:51:43
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental level 1 datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=+DISK1/test/datafile/system.266.875894803
input datafile fno=00003 name=+DISK1/test/datafile/sysaux.267.875894845
input datafile fno=00005 name=+DISK1/test/datafile/example.265.875894871
input datafile fno=00006 name=+DISK1/test/datafile/tspitr.264.875894885
input datafile fno=00002 name=+DISK1/test/datafile/undotbs1.263.875894899
input datafile fno=00004 name=+DISK1/test/datafile/users.262.875894907
input datafile fno=00007 name=+DISK1/test/datafile/test.261.875894909
channel ORA_DISK_1: starting piece 1 at 2015-04-01 20:51:44
channel ORA_DISK_1: finished piece 1 at 2015-04-01 20:52:19
piece handle=/u02/ora_test875911904_1631 tag=NO_ASM_MIGRATION comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:35
Finished backup at 2015-04-01 20:52:19

Starting Control File and SPFILE Autobackup at 2015-04-01 20:52:19
piece handle=/u02/c-2168949517-20150401-17 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 20:52:23


RMAN> recover copy of database with tag 'NO_ASM_MIGRATION';

Starting recover at 2015-04-01 20:52:57
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backupset restore
channel ORA_DISK_1: specifying datafile copies to recover
recovering datafile copy fno=00001 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSTEM_FNO-1_4rq3amf5
recovering datafile copy fno=00002 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-UNDOTBS1_FNO-2_4vq3amik
recovering datafile copy fno=00003 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSAUX_FNO-3_4sq3amgj
recovering datafile copy fno=00004 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-USERS_FNO-4_50q3amin
recovering datafile copy fno=00005 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-EXAMPLE_FNO-5_4tq3amhm
recovering datafile copy fno=00006 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-TSPITR_FNO-6_4uq3ami5
recovering datafile copy fno=00007 name=/u02/asm_backup/data_D-TEST_I-2168949517_TS-TEST_FNO-7_51q3amio
channel ORA_DISK_1: reading from backup piece /u02/ora_test875911904_1631
channel ORA_DISK_1: restored backup piece 1
piece handle=/u02/ora_test875911904_1631 tag=NO_ASM_MIGRATION
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished recover at 2015-04-01 20:53:00

Starting Control File and SPFILE Autobackup at 2015-04-01 20:53:00
piece handle=/u02/c-2168949517-20150401-18 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 20:53:04

3.在/u02/asm_backup目录中创建一个SPFILE文件副本。将要迁移的数据库SPFILE文件存储为
/u02/asm_backup/spfiletest.ora如果数据库是使用的SPFILE启动,就执行以下命令:

RMAN> run
2> {
3> backup as backupset spfile;
4> restore spfile to '/u02/asm_backup/spfiletest.ora';
5> }

Starting backup at 2015-04-01 20:55:31
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 2015-04-01 20:55:32
channel ORA_DISK_1: finished piece 1 at 2015-04-01 20:55:33
piece handle=/u02/ora_test875912132_1661 tag=TAG20150401T205531 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-01 20:55:33

Starting Control File and SPFILE Autobackup at 2015-04-01 20:55:33
piece handle=/u02/c-2168949517-20150401-19 comment=NONE
Finished Control File and SPFILE Autobackup at 2015-04-01 20:55:36

Starting restore at 2015-04-01 20:55:38
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: restoring SPFILE
output filename=/u02/asm_backup/spfiletest.ora
channel ORA_DISK_1: reading from backup piece /u02/c-2168949517-20150401-19
channel ORA_DISK_1: restored backup piece 1
piece handle=/u02/c-2168949517-20150401-19 tag=TAG20150401T205533
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 2015-04-01 20:55:39

[oracle@oracle11g asm_backup]$ ls -lrt
total 1014844
-rw-r----- 1 oracle oinstall  10493952 Apr  1 20:52 data_D-TEST_I-2168949517_TS-USERS_FNO-4_50q3amin
-rw-r----- 1 oracle oinstall  31465472 Apr  1 20:52 data_D-TEST_I-2168949517_TS-UNDOTBS1_FNO-2_4vq3amik
-rw-r----- 1 oracle oinstall 104865792 Apr  1 20:52 data_D-TEST_I-2168949517_TS-TSPITR_FNO-6_4uq3ami5
-rw-r----- 1 oracle oinstall  10493952 Apr  1 20:52 data_D-TEST_I-2168949517_TS-TEST_FNO-7_51q3amio
-rw-r----- 1 oracle oinstall 482353152 Apr  1 20:52 data_D-TEST_I-2168949517_TS-SYSTEM_FNO-1_4rq3amf5
-rw-r----- 1 oracle oinstall 293609472 Apr  1 20:52 data_D-TEST_I-2168949517_TS-SYSAUX_FNO-3_4sq3amgj
-rw-r----- 1 oracle oinstall 104865792 Apr  1 20:52 data_D-TEST_I-2168949517_TS-EXAMPLE_FNO-5_4tq3amhm
-rw-r----- 1 oracle oinstall      2560 Apr  1 20:55 spfiletest.ora

如果数据库是使用PFILE启动,可以在SQL*Plus中执行create spfile命令来创建SPFILE

SQL>create spfile='/u02/asm_backup/spfiletest.ora' from pfile='+DISK1/inittest.ora'

File created.

4.如果被迁移的数据库是备库,需要停止管理恢复模式

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

执行一致性关闭

RMAN> SHUTDOWN IMMEDIATE;

5.在目录/u02/asm_backup中创建一个 pfile参数文件来引用新的SPFILE文件,并重新启动实例到nomount状态。

[oracle@oracle11g asm_backup]$ vi inittest_temp.ora
spfile='/u02/asm_backup/spfiletest.ora'


SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.



SQL> startup nomount pfile='/u02/asm_backup/inittest_temp.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272600 bytes
Variable Size             109053160 bytes
Database Buffers           54525952 bytes
Redo Buffers                2920448 bytes

6.为了下一步将控制文件迁移到文件系统中,修改control_files参数:

SQL> alter system set control_files='/u02/asm_backup/control01.ctl' scope=spfile;

System altered.

7.通过设置db_recovery_file_dest和db_recovery_file_dest_size来指定闪回区,这里闪回区大小设置为2G(因为这里只是进行迁移测试实验)

SQL> alter system set db_recovery_file_dest_size=2G;

System altered.

SQL> alter system set db_recovery_file_dest='/u01/app/oracle/oradata/flash_recovery_area' scope=spfile;

System altered.

8.将实例重新启动到nomount状态,使用RMAN在文件系统中创建新的控制文件(使用原来控制文件来创建),切换数据文件,并执执恢复

SQL> shutdown immediate
ORA-01507: database not mounted


ORACLE instance shut down.
SSQL> startup nomount pfile='/u02/asm_backup/inittest_temp.ora'
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1272600 bytes
Variable Size             109053160 bytes
Database Buffers           54525952 bytes
Redo Buffers                2920448 bytes

RMAN> restore controlfile from '+DISK1/control01.ctl';

Starting restore at 2015-04-01 21:17:54
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK

channel ORA_DISK_1: copied control file copy
output filename=/u02/asm_backup/control01.ctl
Finished restore at 2015-04-01 21:18:04




RMAN> alter database mount;

database mounted
released channel: ORA_DISK_1

RMAN> switch database to copy;

datafile 1 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSTEM_FNO-1_4rq3amf5"
datafile 2 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-UNDOTBS1_FNO-2_4vq3amik"
datafile 3 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-SYSAUX_FNO-3_4sq3amgj"
datafile 4 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-USERS_FNO-4_50q3amin"
datafile 5 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-EXAMPLE_FNO-5_4tq3amhm"
datafile 6 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-TSPITR_FNO-6_4uq3ami5"
datafile 7 switched to datafile copy "/u02/asm_backup/data_D-TEST_I-2168949517_TS-TEST_FNO-7_51q3amio"
starting full resync of recovery catalog
full resync complete

RMAN> recover database;

Starting recover at 2015-04-01 21:18:38
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK

starting media recovery
media recovery complete, elapsed time: 00:00:02

Finished recover at 2015-04-01 21:18:41

8.将临时文件迁移到ASM,必须为每个临时文件执行set newname命令,然后执行switch命令

RMAN> run
2> {
3> set newname for tempfile '+DISK1/TEST/TEMPFILE/TEMP.256.875908789' to '/u02/asm_backup/temp01.dbf';
4> switch tempfile all;
5> }

executing command: SET NEWNAME

renamed temporary file 1 to /u02/asm_backup/temp01.dbf in control file
starting full resync of recovery catalog
full resync complete

9.禁用闪回数据库并重新启动闪回数据库让闪回日志创建在ASM中

SQL> alter database flashback off;

Database altered.

SQL> alter database flashback on;

Database altered.


10.如果迁移的是主库,打开数据库

SQL> alter database open;

Database altered.

如果是备库,恢复管理恢复模式

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE;

11.对于主库,要迁移联机重做日志文件是通过增加新的重做日志文件并存储在文件系统中并删除旧的重做日志文件。对于备库,就是删除旧的备重做日志并增加新的备重做日志并存储在文件系统中。

SQL> select * from v$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARCHIVED STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
         1          1         99   52428800          1 NO       CURRENT                1292113 2015/4/1 20
         2          1         98   52428800          1 YES      INACTIVE               1290567 2015/4/1 20
         3          1          0   52428800          1 YES      UNUSED                       0


SQL> alter database drop logfile group 2;

Database altered.

SQL> alter database add logfile group 2 '/u02/asm_backup/redo02.log' size 50M;

Database altered.

SQL> alter database drop logfile group 3;

Database altered.

SQL> alter database add logfile group 3 '/u02/asm_backup/redo03.log' size 50M;

Database altered.

SQL> alter system switch logfile;

System altered.


SQL> alter database drop logfile group 1;
alter database drop logfile group 1
*
ERROR at line 1:
ORA-01623: log 1 is current log for instance test (thread 1) - cannot drop
ORA-00312: online log 1 thread 1: '+DISK1/redo01.log'

SQL> alter database clear logfile group 1;

Database altered.

SQL> alter database drop logfile group 1;

Database altered.

SQL> alter database add logfile group 1 '/u02/asm_backup/redo01.log' size 50M;

Database altered.