聊聊Oracle可传输表空间(Transportable Tablespace)

转载:http://space.itpub.net/17203031/viewspace-749513


我们在Oracle环境中,有很多进行数据备份和移植手段,如exp/impexpdp/impdprman等。在这些方法中,可传输表空间(Transportable Tablespace)一直是传统意义上最快数据移植的技术手段。理想情况下,Transportable Tablespace可以实现近似网络直传的速率特点。本篇中,我们来介绍一下传输表空间技术的一些使用细节。

 

1Transportable Tablespace概述

 

其他传统意义上的备份迁移手段,大都是遵循“抽取-传输-还原”的模式。以expdp为例,Oracle使用专门的内部调度作业,将需要导出的数据(Used Block)转化为dmp格式文件进行存储保存。之后,通过网络进行传输到Target Envionment,最后再还原到新环境上。这种模式的加速优化,主要体现在抽取和还原上,如使用并行等手段。

 

Transportable Tablespace完全不是这样的概念。如果比喻的话,它类似一种Portable/Plugin的理念。相同平台、字符集的表空间,完全可以将数据文件直接拷贝到Target EnvironmentOracle层面只需要让数据字典知道这些数据的metadata就可以了。

 

相对于其他手段,Transportable Tablespace最大的好处就在于不需要进行繁复的抽取和还原过程,而且对中间环境的空间要求很小。下面通过一系列的实验来进行演示。

 

2、环境准备和前提条件

 

Oracle Transportable Tablespace(以下简称为TTS)出现的很早。传统的TTS有三个层面基础限制,分别为:

 

ü 表空间内容self contained。我们一次性导出的表空间(一个或者多个),不能在其他表空间中存在依赖对象。比如,我们常常将一个数据表数据和索引分布在不同的表空间上,这样如果我们使用Transportable Tablespace,就要求必须将这些表空间一次性全部导出;

ü SourceTarget DBCharacter SetNational Character Set必须完全相同;

ü 操作系统Source Target DB要求兼容。注意:在10g以上版本,这个限制已经取消;

 

我们会在下面更加直观的介绍这些约束和检查方法。由于笔者环境的限制,一些实验只能在一台服务器上进行。具体实验选择Oracle 11gR2

 

 

SQL> select * from v$version;

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

PL/SQL Release 11.2.0.1.0 - Production

CORE       11.2.0.1.0        Production

 

--构造两个实验表空间

SQL> create tablespace ttstbl datafile size 10m autoextend on

 2 extent management local uniform. size 1m

 3 segment space management auto;

Tablespace created

 

SQL> create tablespace ttsind datafile size 10m autoextend on

 2 extent management local uniform. size 1m

 3 segment space management auto;

Tablespace created

 

--测试用户

SQL> create user test identified by test default tablespace ttstbl;

User created

 

SQL> grant resource, connect to test;

Grant succeeded

 

SQL> grant select_catalog_role to test;

Grant succeeded

 

 

使用test用户在表空间中创建一些对象。

 

 

SQL> conn test/test@ora11gp;

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as test

 

SQL> create table ttablespace ttstblas select * from dba_objects;

Table created

 

SQL> create index idx_t_id on t(object_id)tablespace ttsind;

Index created

 

 

3Pre-Condition Check

 

作为先决条件,首选确定Source数据库的字符集信息。

 

 

SQL> select value from nls_database_parameters where parameter='NLS_CHARACTERSET';

VALUE

--------------------

AL32UTF8

 

SQL> select value from nls_database_parameters where parameter='NLS_NCHAR_CHARACTERSET';

VALUE

--------------------

AL16UTF16

 

 

希望导出新创建的表空间ttsindttstbl

 

 

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name like 'TTS%';

 

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf                     TTSTBL

/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf                     TTSIND

 

 

[oracle@bsplinux datafile]$ ls -l

total 2095500

(篇幅原因,省略部分内容)

-rw-r----- 1 oracle oinstall 10493952 Nov 19 18:04 o1_mf_ttsind_8bmyjz69_.dbf

-rw-r----- 1 oracle oinstall 20979712 Nov 19 17:59 o1_mf_ttstbl_8bmyjf3w_.dbf

-rw-r----- 1 oracle oinstall 267395072 Nov 19 18:13 o1_mf_undotbs1_7vpyc2py_.dbf

-rw-r----- 1 oracle oinstall 11804672 Nov 19 17:29 o1_mf_users_7vpyc2xd_.dbf

 

 

传统的约束条件中,操作系统平台OS是使用TTS不能回避的因素。Oracle将支持平台划分为两个大类型BigLittle,同平台之间可以进行自由表空间移植,异平台之间不允许进行移植。注意:这个限制在Oracle 10g之后被打破。具体我们后面详细介绍。

 

我们可以通过查询v$transportable_platform来确定系统之间是否兼容。

 

 

SQL> col platform_name for a30;

SQL> select platform_name, ENDIAN_FORMAT from v$transportable_platform;

 

PLATFORM_NAME                 ENDIAN_FORMAT

------------------------------ --------------

Solaris[tm] OE (32-bit)       Big

Solaris[tm] OE (64-bit)       Big

Microsoft Windows IA (32-bit) Little

Linux IA (32-bit)             Little

AIX-Based Systems (64-bit)    Big

HP-UX (64-bit)                Big

HP Tru64 UNIX                 Little

HP-UX IA (64-bit)             Big

Linux IA (64-bit)             Little

HP Open VMS                   Little

Microsoft Windows IA (64-bit) Little

IBM zSeries Based Linux       Big

Linux x86 64-bit              Little

Apple Mac OS                  Big

Microsoft Windows x86 64-bit  Little

Solaris Operating System (x86) Little

IBM Power Based Linux         Big

HP IA Open VMS                Little

Solaris Operating System (x86- Little

64)                           

Apple Mac OS (x86-64)         Little

 

20 rows selected

 

 

对于自己的数据库,v$database视图中可以查询自己的平台情况信息。

 

 

SQL> select PLATFORM_ID , PLATFORM_NAME from v$database;

 

PLATFORM_ID PLATFORM_NAME

----------- ------------------------------

        10 Linux IA (32-bit)

 

 

确定平台兼容之后,我们需要确定一次移植的表空间之间是否“Self Contained”。Oracle提供的dbms_tts包方法来进行验证。

 

 

SQL> exec dbms_tts.transport_set_check('ttstbl, ttsind',true);

PL/SQL procedure successfully completed

 

SQL> select * from transport_set_violations;

 

VIOLATIONS

--------------------------------------------------------------------------------

 

 

如果验证出现错误,我们可以在transport_set_violations中查询到提示信息。只有解决了self contained问题,才能继续下面的步骤。

 

4、环境数据导出

 

为了控制变化,我们需要将表空间设置为只读。具体语句为:alter tablespace xxx read only

 

 

SQL> select tablespace_name, status from dba_tablespaces where tablespace_name like 'TTS%';

 

TABLESPACE_NAME               STATUS

------------------------------ ---------

TTSIND                        READ ONLY

TTSTBL                        READ ONLY

 

 

Oracle TTS需要使用exp/expdp将表空间的元数据Metadata信息导出为dmp文件,用于描述表空间信息。注意,这个过程时间很短,而且生成的dmp文件通常很小。

 

 

[root@bsplinux ~]# cd /

[root@bsplinux /]# mkdir transtts

[root@bsplinux /]# chown -R oracle:oinstall transtts/

[root@bsplinux /]# ls -l | grep transtts

drwxr-xr-x  2 oracle  oinstall 4096 Nov 19 18:19 transtts

[root@bsplinux /]#

 

 

Exp支持了TTS工作方法。

 

 

[oracle@bsplinux transtts]$exp userid=\"/ as sysdba\" transport_tablespace=y tablespaces=ttstbl,ttsind file=ttsdmp.dmp log=res.log statistics=none

 

Export: Release 11.2.0.1.0 - Production on Mon Nov 19 19:32:14 2012

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set

Note: table data (rows) will not be exported

About to export transportable tablespace metadata...

For tablespace TTSTBL ...

. exporting cluster definitions

. exporting table definitions

. . exporting table                             T

For tablespace TTSIND ...

. exporting cluster definitions

. exporting table definitions

. exporting referential integrity constraints

. exporting triggers

. end transportable tablespace metadata export

Export terminated successfully without warnings.

 

 

之后,我们只需要直接将数据文件拷贝出来。

 

 

SQL> select 'cp '||file_name ||' /transtts' from dba_data_files where tablespace_name like 'TTS%';

 

'CP'||FILE_NAME||'/TRANSTTS'

--------------------------------------------------------------------------------

cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf /transtts

cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts

 

 

[oracle@bsplinux transtts]$ cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf /transtts

cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts

[oracle@bsplinux transtts]$ cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts

[oracle@bsplinux transtts]$

 

 

 

[oracle@bsplinux transtts]$ ls -l

total 30796

-rw-r----- 1 oracle oinstall 10493952 Nov 19 19:35 o1_mf_ttsind_8bmyjz69_.dbf

-rw-r----- 1 oracle oinstall 20979712 Nov 19 19:35 o1_mf_ttstbl_8bmyjf3w_.dbf

-rw-r--r-- 1 oracle oinstall     724 Nov 19 19:33 res.log

-rw-r--r-- 1 oracle oinstall   16384 Nov 19 19:33 ttsdmp.dmp

 

 

/transtts目录中,保存了所有需要还原数据库的信息。

 

5、数据环境恢复

 

我们需要将数据文件通过FTP/SFTP传输到目标数据库服务器上。笔者先将数据环境还原,因为使用的是相同的数据库。

 

 

SQL> drop tablespace ttstbl including contents and datafiles;

Tablespace dropped

 

SQL> drop tablespace ttsind including contents and datafiles;

Tablespace dropped

 

SQL> select file_name from dba_data_files where tablespace_name like 'TTS%';

FILE_NAME

--------------------------------------------------------------------------------

 

 

Source数据库已经没有TTS表空间了。下面,我们将数据文件拷贝到新位置,并且导入Metadata信息。

 

 

[oracle@bsplinux transtts]$ cp o1_mf_ttstbl_8bmyjf3w_.dbf /u01/app/oradata/ORA11G/datafile/

[oracle@bsplinux transtts]$ cp o1_mf_ttsind_8bmyjz69_.dbf /u01/app/oradata/ORA11G/datafile/

 

[oracle@bsplinux datafile]$ ls -l

total 2095500

-rw-r----- 1 oracle oinstall 10493952 Jul 3 03:48 mytesttbl01.dbf

-rw-r----- 1 oracle oinstall 10493952 Jul 3 03:48 mytesttbl02.dbf

-rw-r----- 1 oracle oinstall 104865792 Nov 19 17:29 o1_mf_rman_ts_87bx5kcg_.dbf

-rw-r----- 1 oracle oinstall 838868992 Nov 19 19:46 o1_mf_sysaux_7vpyc2hb_.dbf

-rw-r----- 1 oracle oinstall 807411712 Nov 19 19:43 o1_mf_system_7vpyc1x7_.dbf

-rw-r----- 1 oracle oinstall 60825600 Nov 19 18:02 o1_mf_temp_7vpz05do_.tmp

-rw-r----- 1 oracle oinstall 10493952 Nov 19 19:47 o1_mf_ttsind_8bmyjz69_.dbf

-rw-r----- 1 oracle oinstall 20979712 Nov 19 19:46 o1_mf_ttstbl_8bmyjf3w_.dbf

-rw-r----- 1 oracle oinstall 267395072 Nov 19 19:47 o1_mf_undotbs1_7vpyc2py_.dbf

-rw-r----- 1 oracle oinstall 11804672 Nov 19 17:29 o1_mf_users_7vpyc2xd_.dbf

 

 

Imp命令导入。

 

 

[oracle@bsplinux transtts]$ imp userid=\'/ as sysdba\' file=ttsdmp.dmp transport_tablespace=y tablespaces=ttsind,ttstbl datafiles=/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf,/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf

 

Import: Release 11.2.0.1.0 - Production on Mon Nov 19 19:51:25 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

 

Export file created by EXPORT:V11.02.00 via conventional path

About to import transportable tablespace(s) metadata...

import done in AL32UTF8 character set and AL16UTF16 NCHAR character set

. importing SYS's objects into SYS

. importing SYS's objects into SYS

. importing TEST's objects into TEST

. . importing table                           "T"

. importing SYS's objects into SYS

Import terminated successfully without warnings.

[oracle@bsplinux transtts]$

 

 

导入元数据后,Oracle中可以确定导入的表空间了。

 

 

SQL> select file_name,tablespace_name from dba_data_files where tablespace_name like 'TTS%';

 

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf                     TTSIND

/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf                     TTSTBL

 

SQL> conn test/test@ora11gp

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as test

 

SQL> select count(*) from t;

 

 COUNT(*)

----------

    72348

 

 

注意,导入后的表空间还是read only状态,需要开启。

 

 

SQL> alter tablespace ttsind read write;

Tablespace altered

 

SQL> alter tablespace ttstbl read write;

Tablespace altered

 

 

上面我们给出了一个简单的TTS例子。对TTS来说,还有很多细节和复杂应用场景,留待后面继续。



6Oracle数据泵Data Pump导出导入

 

Exp/ImpOracle早期推出的数据逻辑备份还原工具,使用简单、功能强大。但是Exp/Imp对一些Oracle新特性支持不是很好,而且对于海量数据备份还原速度还是不能满足要求。于是从10g开始,Oracle推出了数据泵(Data Pump)作为Exp/Imp的升级替代版本。

 

使用Data Pump也是可以进行TTS元数据的导出。下面我们将实验使用Data Pump重新做一次。注意:表空间Read Only配置和相关的检查步骤略过。

 

Data Pump是一个服务端工具,使用的话需要定义directory对象。

 

 

SQL> create or replace directory TTSDMP

 2   as '/transtts';

Directory created

 

SQL> grant read, write on directory ttsdmp to public;

Grant succeeded

 

 

导出元数据dmp文件。

 

 

[oracle@bsplinux transtts]$ expdp userid=\'/ as sysdba\' transport_tablespaces=ttsind,ttstbl dumpfile=ttsdmp.dmp directory=ttsdmp transport_full_check=y

 

Export: Release 11.2.0.1.0 - Production on Mon Nov 19 20:06:33 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Starting "SYS"."SYS_EXPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" transport_tablespaces=ttsind,ttstbl dumpfile=ttsdmp.dmp directory=ttsdmp transport_full_check=y

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/INDEX

Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Master table "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded

******************************************************************************

Dump file set for SYS.SYS_EXPORT_TRANSPORTABLE_01 is:

 /transtts/ttsdmp.dmp

******************************************************************************

Datafiles required for transportable tablespace TTSIND:

 /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf

Datafiles required for transportable tablespace TTSTBL:

 /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf

Job "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 20:08:02

 

 

拷贝数据文件。

 

 

[oracle@bsplinux datafile]$ cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf /transtts

cp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf /transtts

[oracle@yjz69_.dbf /transttscp /u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bm

 

[oracle@bsplinux transtts]$ pwd

/transtts

[oracle@bsplinux transtts]$ ls -l

total 30888

-rw-r--r-- 1 oracle oinstall    1402 Nov 19 20:08 export.log

-rw-r----- 1 oracle oinstall 10493952 Nov 19 20:09 o1_mf_ttsind_8bmyjz69_.dbf

-rw-r----- 1 oracle oinstall 20979712 Nov 19 20:09 o1_mf_ttstbl_8bmyjf3w_.dbf

-rw-r----- 1 oracle oinstall  106496 Nov 19 20:08 ttsdmp.dmp

 

 

标红部分的文件为进行TTS的必要内容。

 

说明:由于我们在相同的环境下进行测试,所以将表空间删除。

 

 

SQL> drop tablespace ttstbl including contents and datafiles;

Tablespace dropped

 

SQL> drop tablespace ttsind including contents and datafiles;

Tablespace dropped

 

SQL> select file_name,tablespace_name from dba_data_files where tablespace_name like 'TTS%';

 

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

 

 

表空间删除成功。下面进行实际还原过程。

 

 

[oracle@bsplinux transtts]$ cp o1_mf_ttsind_8bmyjz69_.dbf /u01/app/oradata/ORA11G/datafile/

[oracle@bsplinux transtts]$ cp o1_mf_ttstbl_8bmyjf3w_.dbf /u01/app/oradata/ORA11G/datafile/

 

[oracle@bsplinux datafile]$ ls -l | grep tts

-rw-r----- 1 oracle oinstall 10493952 Nov 19 20:15 o1_mf_ttsind_8bmyjz69_.dbf

-rw-r----- 1 oracle oinstall 20979712 Nov 19 20:15 o1_mf_ttstbl_8bmyjf3w_.dbf

 

 

使用impdp工具导入元数据信息。

 

 

[oracle@bsplinux transtts]$ impdp userid=\'/ as sysdba\' directory=ttsdmp dumpfile=ttsdmp.dmp transport_datafiles='/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf','/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf'

 

Import: Release 11.2.0.1.0 - Production on Mon Nov 19 20:18:41 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully loaded/unloaded

Starting "SYS"."SYS_IMPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" directory=ttsdmp dumpfile=ttsdmp.dmp transport_datafiles=/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf,/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/INDEX

Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Job "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed at 20:18:56

 

 

 

此时,表空间已经导回,对应的数据表也能够还原回去。

 

 

SQL> select file_name,tablespace_name from dba_data_files where tablespace_name like 'TTS%';

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/u01/app/oradata/ORA11G/datafile/o1_mf_ttsind_8bmyjz69_.dbf                     TTSIND

/u01/app/oradata/ORA11G/datafile/o1_mf_ttstbl_8bmyjf3w_.dbf                     TTSTBL

 

--切换回test用户

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

 COUNT(*)

----------

    72348

 

 

注意:无论是使用exp/imp还是Data Pump,导入对象对应的owner必须存在并且名称一致。如果不存在owner对象,即使使用Data Pump也不会自动将用户重新创建。

 

7、异平台TTS移植

 

相同平台类型移植,一直是TTS使用的一个前提条件,也是一个很难逾越的屏蔽。这给TTS的使用带来一些限制。比如,我们不能实现从AIX平台到开放Linux平台的迁移。于是,在10g以上的版本中,我们可以突破这个限制。

 

下面我们尝试将一个表空间从AIX平台迁移到Linux平台。

 

AIX平台上,我们将其作为source database

 

 

SQL> select * from v$version;

 

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

PL/SQL Release 11.2.0.1.0 - Production

CORE       11.2.0.1.0        Production

 

TNS for IBM/AIX RISC System/6000: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 - Production

 

SQL> select count(*),sum(bytes)/1024/1024 from dba_segments where tablespace_name='NBSPOC';

 

 COUNT(*) SUM(BYTES)/1024/1024

---------- --------------------

      174                 286

 

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name='NBSPOC';

 

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/nbstdata01/oradata/NBSTEST/NBSTEST/NBSPOCTBL01.dbf                             NBSPOC

 

 

NBSPOC表空间中包括174个对象,约286M数据。下面设置表空间Read Only和平台检查。

 

 

SQL> alter tablespace nbspoc read only;

Tablespace altered

 

SQL> select PLATFORM_NAME from v$database;

PLATFORM_NAME

------------------------------

AIX-Based Systems (64-bit)

 

 

我们从v$transportable_platform视图中,可以看出AIX-Based Systems(64-bit)属于Big类型。而Target目标LinuxLittle类型。所以,我们在进行跨平台迁移。

 

注意:跨平台迁移也需要满足字符集相同的要求。

 

元数据导出我们计划使用expdp,所以配置directory对象。

 

 

oracle:/ftptemp>cd trans

oracle:/ftptemp/trans>ls -l

total 0

oracle:/ftptemp/trans>pwd

/ftptemp/trans

oracle:/ftptemp/trans>

 

SQL> create directory dmpdir as '/ftptemp/trans';

Directory created

 

 

检查表空间NBSPOC的依赖性。

 

 

SQL> exec dbms_tts.transport_set_check('NBSPOC',true);

PL/SQL procedure successfully completed

 

SQL> select * from transport_set_violations;

VIOLATIONS

--------------------------------------------------------------------------------

 

 

Expdp导出表空间对象元数据信息。

 

 

oracle:/ftptemp/trans>=ttspoc.dmp directory=dmpdir transport_full_check=y              <

 

Export: Release 11.2.0.1.0 - Production on Mon Nov 19 14:26:51 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Starting "SYS"."SYS_EXPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" transport_tablespaces=nbspoc dumpfile=ttspoc.dmp directory=dmpdir transport_full_check=y

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/INDEX

Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT

Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/COMMENT

Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/REF_CONSTRAINT

Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Master table "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded

******************************************************************************

Dump file set for SYS.SYS_EXPORT_TRANSPORTABLE_01 is:

 /ftptemp/trans/ttspoc.dmp

******************************************************************************

Datafiles required for transportable tablespace NBSPOC:

 /nbstdata01/oradata/NBSTEST/NBSTEST/NBSPOCTBL01.dbf

Job "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 14:28:43

 

oracle:/ftptemp/trans>ls -l

total 19224

-rw-r--r--   1 oracle  dba           1529 Nov 19 14:28 export.log

-rw-r-----   1 oracle  dba        9838592 Nov 19 14:28 ttspoc.dmp大约9M

 

 

注意,下面是迁移的关键步骤,我们需要将对应的迁移数据文件Convert到目标平台格式。Oracle推荐使用Rman进行这个工作。

 

 

oracle:/ftptemp/trans>rman nocatalog

 

Recovery Manager: Release 11.2.0.1.0 - Production on Mon Nov 19 14:36:47 2012

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

 

RMAN> connect target /

connected to target database: NBSTEST (DBID=2351142467)

using target database control file instead of recovery catalog

 

RMAN> convert tablespace 'NBSPOC'

2> to platform="Linux IA (32-bit)" –目标平台格式(可以查询目标数据库的v$database视图)

3> db_file_name_convert='/nbstdata01/oradata/NBSTEST/NBSTEST/NBSPOCTBL01.dbf','/ftptemp/trans/NBSPOCTBL01.dbf';

 

Starting conversion at source at 19-NOV-12

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=355 device type=DISK

channel ORA_DISK_1: starting datafile conversion

input datafile file number=00014 name=/nbstdata01/oradata/NBSTEST/NBSTEST/NBSPOCTBL01.dbf

converted datafile=/ftptemp/trans/NBSPOCTBL01.dbf

channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:55

Finished conversion at source at 19-NOV-12

 

 

从命令行情况看,Rman相当于将指定的数据文件进行转换,拷贝转换过的版本到一个Stage目录上。这样,目录/ftptemp/trans中就已经包括了所有的迁移内容了。

 

 

oracle:/ftptemp/trans>ls -l

total 2067240

-rw-r-----   1 oracle  dba     1048584192 Nov 19 14:41 NBSPOCTBL01.dbf

-rw-r--r--   1 oracle  dba           1529 Nov 19 14:28 export.log

-rw-r-----   1 oracle  dba        9838592 Nov 19 14:28 ttspoc.dmp

 

 

 

转换到Target环境,是一台Linux服务器。

 

 

SQL> conn sys/oracle@ora11gp as sysdba

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as SYS

 

SQL> select PLATFORM_NAME from v$database;

PLATFORM_NAME

------------------------------

Linux IA (32-bit)

 

 

下面就是用FTP将传输文件传输到目标平台,我们使用Linux自带的FTP命令行客户端完成。

 

[oracle@bsplinux transtts]$ ls -l

total 0

[oracle@bsplinux transtts]$ pwd

/transtts

[oracle@bsplinux transtts]$

 

--调用FTP客户端

[oracle@bsplinux transtts]$ ftp

ftp> open 10.1.15.66

Connected to 10.1.15.66.

220 P550_05_LC FTP server (Version 4.2 Wed Dec 23 11:06:15 CST 2009) ready.

502 authentication type cannot be set to GSSAPI

502 authentication type cannot be set to KERBEROS_V4

KERBEROS_V4 rejected as an authentication type

Name (10.1.15.66:oracle): oracle

331 Password required for oracle.

Password:

230-Last unsuccessful login: Wed Nov 14 15:35:34 GMT+08:00 2012 on /dev/pts/1 from 10.1.39.109

230-Last login: Mon Nov 19 14:24:58 GMT+08:00 2012 on /dev/pts/2 from 10.1.39.62

230 User oracle logged in.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp>

 

--切换目录

ftp> cd /ftptemp/trans

250 CWD command successful.

ftp> dir

227 Entering Passive Mode (10,1,15,66,134,182)

150 Opening data connection for /bin/ls.

total 2067240

-rw-r-----   1 oracle  dba     1048584192 Nov 19 14:41 NBSPOCTBL01.dbf

-rw-r--r--   1 oracle  dba           1529 Nov 19 14:28 export.log

-rw-r-----   1 oracle  dba        9838592 Nov 19 14:28 ttspoc.dmp

226 Transfer complete.

 

ftp> get ttspoc.dmp

local: ttspoc.dmp remote: ttspoc.dmp

227 Entering Passive Mode (10,1,15,66,134,206)

150 Opening data connection for ttspoc.dmp (9838592 bytes).

226 Transfer complete.

9838592 bytes received in 0.85 seconds (1.1e+04 Kbytes/s)

ftp> get export.log

local: export.log remote: export.log

227 Entering Passive Mode (10,1,15,66,134,211)

150 Opening data connection for export.log (1529 bytes).

226 Transfer complete.

1529 bytes received in 0.17 seconds (9 Kbytes/s)

ftp> get NBSPOCTBL01.dbf

local: NBSPOCTBL01.dbf remote: NBSPOCTBL01.dbf

227 Entering Passive Mode (10,1,15,66,134,228)

150 Opening data connection for NBSPOCTBL01.dbf (1048584192 bytes).

226 Transfer complete.

1048584192 bytes received in 3.6e+02 seconds (2.9e+03 Kbytes/s)

ftp>

 

--传输完毕

[oracle@bsplinux transtts]$ ls -l

total 1034640

-rw-r--r-- 1 oracle oinstall      1529 Nov 19 21:51 export.log

-rw-r--r-- 1 oracle oinstall 1048584192 Nov 19 21:58 NBSPOCTBL01.dbf

-rw-r--r-- 1 oracle oinstall   9838592 Nov 19 21:51 ttspoc.dmp

[oracle@bsplinux transtts]$

 

 

将拷贝的数据复制到数据文件目录,进行导入元数据步骤。注意,在Target环境中,要首先创建好用户和权限信息。

 

 

SQL> create user nbspoc identified by nbspoc;

User created

 

SQL> grant resource, connect to nbspoc;

Grant succeeded

 

 

导入表空间信息。

 

 

[oracle@bsplinux transtts]$ impdp userid=\'/ as sysdba\' directory=ttsdmp dumpfile=ttspoc.dmp transport_datafiles='/u01/app/oradata/ORA11G/datafile/NBSPOCTBL01.dbf'

 

Import: Release 11.2.0.1.0 - Production on Mon Nov 19 22:13:42 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully loaded/unloaded

Starting "SYS"."SYS_IMPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" directory=ttsdmp dumpfile=ttspoc.dmp transport_datafiles=/u01/app/oradata/ORA11G/datafile/NBSPOCTBL01.dbf

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/INDEX

Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT

Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/COMMENT

Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/REF_CONSTRAINT

Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Job "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed at 22:16:29

 

 

 

设置收尾信息和验证。

 

 

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name like 'NBS%';

FILE_NAME                                                                       TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/u01/app/oradata/ORA11G/datafile/NBSPOCTBL01.dbf                                NBSPOC

 

SQL> select count(*), sum(bytes)/1024/1024 from dba_segments where tablespace_name='NBSPOC';

 

 COUNT(*) SUM(BYTES)/1024/1024

---------- --------------------

      174                 286

 

SQL> select tablespace_name, status from dba_tablespaces where tablespace_name like 'NBS%';

TABLESPACE_NAME               STATUS

------------------------------ ---------

NBSPOC                        READ ONLY

 

SQL> alter tablespace nbspoc read write;

Tablespace altered

 

 

迁移成功。那么,还有很多更复杂的情况,比如ASM的引入。在ASM环境下,我们是不能够直接访问到数据文件,拷贝数据文件的(Oracle 11gR2之前)。如何迁移ASM下的数据文件,我们继续探讨。



最后我们聊聊关于ASM下的TTS使用。应该说,ASMOracle在存储层面的重要组件,也是在软件层面实现冗余和平衡IO的关键解决方案。对11gR2 RAC而言,ASM是取代裸设备的重要组件。

 

应用ASM的一个重要特点是:文件不再归属OS文件系统管理范畴,而是归属Oracle ASM Instance管理。数据文件的冗余、IO分散、负载均衡乃至操作都是通过Database Server InstanceASM Intance协调完成。我们从操作系统中是不能涉及到的。如果要使用TTS,我们需要怎么处理呢?

 

8ASM端配置使用

 

我们本次迁移的目标是进行Linux平台之间的迁移,一端SourceASM存储,而另一端是使用传统的文件系统存储。

 

首先是Source环境配置信息。

 

 

 

SQL> select * from v$version;

 

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production

PL/SQL Release 11.2.0.3.0 - Production

CORE       11.2.0.3.0        Production

 

TNS for Linux: Version 11.2.0.3.0 - Production

NLSRTL Version 11.2.0.3.0 - Production

 

 

SQL> select name, platform_name from v$database;

 

NAME     PLATFORM_NAME

--------- --------------------------------------------------------------------------------

ORA11GAS Linux IA (32-bit)

 

 

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name='TTS_SIMPLE';

 

FILE_NAME                                                  TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

+DATA/ora11gasm/datafile/tts_simple.267.799910245               TTS_SIMPLE

 

 

数据文件保存在ASM存储的+DATA Disk Group中。为了进行试验,在表空间中创建一些对象。

 

 

SQL> create user tts_simple identified by tts_simple default tablespace tts_simple;

User created

 

SQL> grant resource, connect to tts_simple;

Grant succeeded

 

SQL> conn tts_simple/tts_simple@ora11gasm

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.3.0

Connected as tts_simple

 

SQL> create table t as select * from all_objects;

Table created

 

SQL> select count(*) from t;

 COUNT(*)

----------

    56289

 

 

Self Contained检查。

 

 

 

SQL> exec dbms_tts.transport_set_check('tts_simple',true);

PL/SQL procedure successfully completed

 

SQL> select * from transport_set_violations;

 

VIOLATIONS

---------------------------------------------------------------------------

 

SQL> alter tablespace tts_simpleread only;

Tablespace altered

 

 

使用expdp导出元数据,首先需要创建directory对象。

 

 

[root@bspdev /]# ls -l | grep ttstest

drwxr-xr-x  2 oracle oinstall 4096 Nov 21 05:24 ttstest

[root@bspdev /]#

 

SQL> create directory ttsdmp as '/ttstest';

Directory created

 

 

导出元数据dmp包。

 

 

[oracle@bspdev ttstest]$ expdp userid=\'/ as sysdba\' transport_tablespaces=tts_simple dumpfile=tts_simple.dmp directory=ttsdmp transport_full_check=y

 

Export: Release 11.2.0.3.0 - Production on Wed Nov 21 05:31:04 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production

With the Partitioning, Automatic Storage Management, OLAP, Data Mining

and Real Application Testing options

Starting "SYS"."SYS_EXPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" transport_tablespaces=tts_simple dumpfile=tts_simple.dmp directory=ttsdmp transport_full_check=y

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Master table "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded

******************************************************************************

Dump file set for SYS.SYS_EXPORT_TRANSPORTABLE_01 is:

 /ttstest/tts_simple.dmp

******************************************************************************

Datafiles required for transportable tablespace TTS_SIMPLE:

 +DATA/ora11gasm/datafile/tts_simple.267.799910245

Job "SYS"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 05:31:49

 

 

下面是一个实验的关键步骤,就是拷贝出数据文件。在传统的文件系统中,我们可以直接使用FTP/SFTP将数据文件拷贝到目标Target端。但是在ASM情况下,我们操作系统的命令是不管用的。

 

问题的核心问题就是,如何从ASM Diskgroup中将数据文件拷贝到文件系统中。目前我们有两种方法:

 

ü 一种是传统的做法,就是利用RMANbackup as copy方式,将数据文件原封不动的拷贝到文件系统中;

ü 11g中,ASM命令行工具asmcmd提供了cp命令,支持ASM和文件系统之间的拷贝操作;

 

我们实验选择RMAN方法。

 

 

[oracle@bspdev ttstest]$ rman nocatalog

 

Recovery Manager: Release 11.2.0.3.0 - Production on Wed Nov 21 05:32:23 2012

 

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

 

RMAN> connect target /

 

connected to target database: ORA11GAS (DBID=2390045008)

using target database control file instead of recovery catalog

 

 

RMAN>backup as copy tablespace tts_simple format '/ttstest/%U';   

 

Starting backup at 21-NOV-12

using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile copy

input datafile file number=00006 name=+DATA/ora11gasm/datafile/tts_simple.267.799910245

output file name=/ttstest/data_D-ORA11GAS_I-2390045008_TS-TTS_SIMPLE_FNO-6_0bnqrccg tag=TAG20121121T055216 RECID=3 STAMP=799912337

channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03

Finished backup at 21-NOV-12

 

Starting Control File and SPFILE Autobackup at 21-NOV-12

piece handle=+RECO/ora11gasm/autobackup/2012_11_21/s_799912339.273.799912341 comment=NONE

Finished Control File and SPFILE Autobackup at 21-NOV-12

 

RMAN>

 

 

我们就可以在文件系统中找到备份文件。

 

 

[oracle@bspdev ttstest]$ ls -l

total 10376

-rw-r----- 1 oracle asmadmin 10493952 Nov 21 05:52 data_D-ORA11GAS_I-2390045008_TS-TTS_SIMPLE_FNO-6_0bnqrccg

-rw-r--r-- 1 oracle asmadmin    1201 Nov 21 05:31 export.log

-rw-r----- 1 oracle asmadmin  106496 Nov 21 05:31 tts_simple.dmp

[oracle@bspdev ttstest]$

 

 

使用mv命令进行重命名。

 

 

[oracle@bspdev ttstest]$ mv data_D-ORA11GAS_I-2390045008_TS-TTS_SIMPLE_FNO-6_0bnqrccg tts_simple01.dbf

[oracle@bspdev ttstest]$ ls -l

total 10376

-rw-r--r-- 1 oracle asmadmin    1201 Nov 21 05:31 export.log

-rw-r----- 1 oracle asmadmin 10493952 Nov 21 05:52 tts_simple01.dbf

-rw-r----- 1 oracle asmadmin  106496 Nov 21 05:31 tts_simple.dmp

[oracle@bspdev ttstest]$

 

 

9、目标环境还原

 

我们选择将TTS还原到非ASMLinux平台上。首选需要使用FTP/SFTPdmp元数据和数据文件拷贝到Target环境上。

 

 

 

[root@bspdev ~]# cd /

[root@bspdev /]# mkdir ttstest

[root@bspdev /]# chown oracle:oinstall ttstest/

[root@bspdev /]# su - oracle

[oracle@bspdev ~]$ cd /ttstest/

[oracle@bspdev ttstest]$ ls -l

total 0

[oracle@bspdev ttstest]$ pwd

/ttstest

[oracle@bspdev ttstest]$

 

--拷贝结束

[oracle@bspdev ttstest]$ ls -l

total 10376

-rw-r--r-- 1 oracle oinstall    1201 Nov 21 06:14 export.log

-rw-r--r-- 1 oracle oinstall 10493952 Nov 21 06:14 tts_simple01.dbf

-rw-r--r-- 1 oracle oinstall  106496 Nov 21 06:14 tts_simple.dmp

 

 

将数据文件归位,并且创建directory对象,用于进行Metadata还原。

 

 

[oracle@bspdev ttstest]$ cp tts_simple01.dbf /u01/oradata/WILSON/datafile/

[oracle@bspdev ttstest]$ cd /u01/oradata/WILSON/datafile/

[oracle@bspdev datafile]$ ls -l

total 1930836

-rw-r----- 1 oracle oinstall 104865792 Nov 21 06:11 o1_mf_example_7xt46m9x_.dbf

(篇幅原因,省略部分……)

-rw-r----- 1 oracle oinstall 110108672 Nov 21 06:17 o1_mf_undotbs1_7xt3yzl5_.dbf

-rw-r----- 1 oracle oinstall 15736832 Nov 21 06:11 o1_mf_users_805nxydh_.dbf

-rw-r--r-- 1 oracle oinstall 10493952 Nov 21 06:17 tts_simple01.dbf

 

[oracle@bspdev datafile]$ pwd

/u01/oradata/WILSON/datafile

 

--Sqlplus命令

SQL> create directory ttsdmp as '/ttstest';

Directory created

 

SQL> create user tts_simple identified by tts_simple;

User created

 

SQL> grant connect, resource to tts_simple;

Grant succeeded

 

 

还原元数据信息。

 

 

[oracle@bspdev ttstest]$ impdp userid=\'/ as sysdba\' directory=ttsdmp dumpfile=tts_simple.dmp transport_datafiles='/u01/oradata/WILSON/datafile/tts_simple01.dbf'

 

Import: Release 11.2.0.1.0 - Production on Wed Nov 21 06:22:43 2012

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully loaded/unloaded

Starting "SYS"."SYS_IMPORT_TRANSPORTABLE_01": userid="/******** AS SYSDBA" directory=ttsdmp dumpfile=tts_simple.dmp transport_datafiles=/u01/oradata/WILSON/datafile/tts_simple01.dbf

Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK

Processing object type TRANSPORTABLE_EXPORT/TABLE

Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK

Job "SYS"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed at 06:23:01

 

 

SQL> select file_name, tablespace_name from dba_data_files where tablespace_name like 'TTS%';

 

FILE_NAME                                                     TABLESPACE_NAME

-------------------------------------------------------------------------------- ------------------------------

/u01/oradata/WILSON/datafile/tts_simple01.dbf                    TTS_SIMPLE

 

SQL> alter tablespace tts_simple read write;

Tablespace altered

 

SQL> select count(*) from tts_simple.t;

 COUNT(*)

----------

    56289

 

 

10More Complex Cases

 

上面的系列,我们已经介绍了使用TTS的一些基本场景和处理方法。当然,在实际工作中可以会遇到更加复杂的场景。比如,从RAC环境下将一个ASM保存的表空间迁移到异平台中一个ASM服务器上。

 

在官方的解决方案中,有几个需要注意的细节。

 

首先就是元数据导出,使用expdp导出的时候,我们创建的directory可以是ASM磁盘组目录。但是,Expdp的日志却只能存放在文件系统中,所以需要额外的directory对象。

 

其次就是传输。Oracle推荐使用dbms_file_transfer.put_file方法将文件从一个ASM传出到另一个ASM或者文件系统。

 

如果是进行平台转换,要注意不要使用dbms_file_transfer进行传输。因为目前的Oracle版本中,这个包方法还不能支持平台间文件的传输。

 

11、结论

 

TTS为称为最快的数据迁移方法,因为我们不需要对数据进行转换,只需要消耗网络传输文件的时间。但是,TTS针对那些很空的表空间来说,不是一件好方法。例如,一个表空间包括两个文件,占到2G空间,但是内部的有效数据只有10M。消耗2G网络流量显然不是好方法。



猜你喜欢

转载自blog.csdn.net/yrg5101/article/details/8479484
今日推荐