初探data pump export (二)

初探data pump export (二)
data pump export 提供的接口
通过expdp 命令调用data pump export 实用工具,具体的操作特权取决于你提供的参数。另外不建议使用sysdba调用 export,因为sysdba 是内部的和特殊功能相关的,sysdba 的表现形式和普遍用户不同,除非oracle 技术支持人员要求你使用sysdba。
一、命令行接口。允许你通过命令行直接指定大部分的export 参数。当你指定export 操作的时候指定的参数很少,另外不考虑参数重用的话建议使用,命令行调用方式。
[oracle@zeng ~]$ expdp hr tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes
二、参数文件接口。允许你通过命令行界面指定一个参数文件(使用 parfile)。如果需要提供的export 参数较多,或者考虑到操作的重复性参数的重用性,提供给export 的参数中使用了引号,建议使用参数文件调用的方式。这样如果下次需要执行相同的或者类似的export 操作,改改参数就好了。可以提高效率,减少错误。
先编辑一个参数文件写入下面的内容。
[oracle@zeng ~]$ vi expfile.par
-- INSERT --                                                            
tables=employees,jobs
dumpfile=exp_hr.dmp
reuse_dumpfiles=yes
logfile=export_hr.log
使用参数文件调用data pump export
[oracle@zeng ~]$ expdp hr parfile=/home/oracle/expfile.par
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:05:47 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
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 "HR"."SYS_EXPORT_TABLE_01":  hr/******** parfile=/home/oracle/expfile.par 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
. . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows
. . exported "HR"."JOBS"                                 6.984 KB      19 rows
Master table "HR"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_TABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/exp_hr.dmp
Job "HR"."SYS_EXPORT_TABLE_01" successfully completed at 16:06:12
三、交互式命令行界面。在作业正在执行中或者出现异常的时候,你需要对作用进行监控和干预,可以在工作执行的时候按 Ctrl+C 进入交互式命令行。或者出现异常的时候使用expdp attach job_name 进入。也可以在另外一个地方执行expdp attach job_name 对作业进行监控和干预。
[oracle@zeng ~]$ expdp hr tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 15:58:07 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
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 "HR"."SYS_EXPORT_TABLE_01":  hr/******** tables=employees,jobs dumpfile=exp_hr.dmp nologfile=yes 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 128 KB
^C
Export> stop_job
Are you sure you wish to stop this job ([yes]/no): yes
data pump export 提供的导出模式
data pump export 提供了5种不同的模式供导出不同的数据库部分。通过在命令行或者参数文件中
指定导出的模式。
一、完全导出模式(full export mode)
通过指定full 参数执行该模式。在完全导出模式下,整个数据库都会被导出。使用该模式需要datapump_exp_full_database 角色的支持。
关于full 参数
默认是不启用的(full=no)。如果你需要执行整个数据库的导出,指定full=yes。在该模式下面整个数据库中的数据(包括元数据)都会被导出。可以通过过滤来限制导出。在full mode 下不会导出system 模式,其中存放着oracle 管理的数据,和元数据.比如不导出的系统模式包括,sys,ordsys,mdsys.授权给sys 拥有的对象也不会导出.
二、schema 模式(schema mode)
通过指定schemas 参数来执行该模式。这是默认的导出模式。如果执行导出的用户拥有datapump_exp_full_database 角色。你可以指定一个schemas 列表.如果用户没有datapump_exp_full_database只能导出自己的schema。(sys schema 不能作为导出源。)交叉参照的模式中的对象不会被导出,除非把该表空间也列在schemas 中.比如一个表中定义的一个触发器位于另外一个表空间,则这个触发器不会被导出。
schemas 参数的语法格式:schemas=schema_name,schema_name... 如果不指定schemas 默认就是当前执行expdp 的用户的schemas。
先把两项和data pump 相关的强大特权都回收回来。
SQL> revoke datapump_imp_full_database from hr;
Revoke succeeded
SQL> revoke datapump_exp_full_database from hr;
Revoke succeeded.
想导出别人的schemas 不行了吧。
[oracle@zeng ~]$ expdp schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:27:15 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Username: hr
Password: 
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
ORA-31631: privileges are required
ORA-39109: Unprivileged users may not operate upon other users' schemas
只给hr 授予datapump_imp_full_database .
SQL> grant datapump_imp_full_database to hr;
Grant succeeded.
[oracle@zeng ~]$ expdp hr/hr schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes   
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 16:28:48 2012
.
.
.
Master table "HR"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:
  /opt/oracle11g/admin/oracl/exp_zeng.dump
Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at 16:30:00
只有datapump_imp_full_database 依然可以导出其他用户的schema。
只给hr 授予datapump_exp_full_database .
SQL> revoke datapump_imp_full_database from hr;
Revoke succeeded.
SQL> grant datapump_exp_full_database to hr;
Grant succeeded
[oracle@zeng ~]$ expdp hr/hr schemas=zeng dumpfile=dump_test_dir:exp_zeng.dump logfile=dump_test_dir:exp_zeng.log reuse_dumpfiles=yes
.
.
.
Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:
  /opt/oracle11g/admin/oracl/exp_zeng.dump
Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at 16:34:22
只有datapump_exp_full_database 依然可以导出其他用户的schema。从上面的实验可以看出
这两个角色是很强大的,回头找些资料来认真学习下这两个角色。另外不建议hr/hr 这种提供密码的方式,因为这很容易就泄露了用户的密码。
三、表模式(table mode)
通过指定tables 参数来指定该模式。在表模式下,仅仅指定的表,表分区,和依赖他们的对象会被导出。如果tables 参数和transporttable=always 一起指定。那么仅有元数据会被导出。实际要导出的数据你应该复制相应的数据文件到目标数据库。这将会是export 操作非常的快。如果你跨平台或者发行版本复制数据文件,可能需要经过RMAN处理.如果拥有datapump_exp_full_database角色,用户可以导出其他schema 中的表.同样的交叉参照的模式中的对象不会被导出。
在得出下面的输出的前,我遇到了两个错误:
ORA-29335: tablespace 'USERS' is not read only
使用transportable=always 参数需要把对象所在的表空间alter 为read_only。
SQL>  alter tablespace users read only;
Tablespace altered.
另外一个是:ORA-01647: tablespace 'USERS' is read-only, cannot allocate space in it
因为我是使用hr 来执行操作的,既然hr 所在的表空间已经设置为只读了,所有分配不到data pump export job 所需要的工作空间。把执行导出操作的用户改为system 再测试就ok了。或者改为其他默认表空间不是users 并且有权限的用户。
[oracle@zeng ~]$ expdp system tables=hr.employees transportable=always dumpfile=exp_hr.dmp reuse_dumpfiles=yes logfile=exp_hr.log
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 17:18:45 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
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 "SYSTEM"."SYS_EXPORT_TABLE_01":  system/******** tables=hr.employees transportable=always dumpfile=exp_hr.dmp reuse_dumpfiles=yes logfile=exp_hr.log 
Processing object type TABLE_EXPORT/TABLE/PLUGTS_BLK
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/END_PLUGTS_BLK
Master table "SYSTEM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/exp_hr.dmp
******************************************************************************
Datafiles required for transportable tablespace USERS:
  /opt/oracle11g/oradata/oracl/users01.dbf
Job "SYSTEM"."SYS_EXPORT_TABLE_01" successfully completed at 17:19:14
四、表空间模式(tablespace mode )
通过指定tablespaces 参数来执行该模式,在表空间模式下,只有在指定表空间中的表及其依赖对象会被导出(包括对象的数据与元数据)。在表空间模式下,有特权的用户可以导出所有的表及其依赖对象,没有特权的用户只能导出在自己schemas中的数据。
先把上面alter 为只读的表空间,alter 为读写状态。
SQL> alter tablespace users read write;
Tablespace altered.
[oracle@zeng ~]$ expdp zeng tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log  job_name=exp_zeng 
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 18:38:34 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
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 "ZENG"."EXP_ZENG":  zeng/******** tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log job_name=exp_zeng 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
Master table "ZENG"."EXP_ZENG" successfully loaded/unloaded
******************************************************************************
Dump file set for ZENG.EXP_ZENG is:
  /opt/oracle11g/admin/oracl/exp_zeng.dmp
Job "ZENG"."EXP_ZENG" successfully completed at 18:39:18
从上面长长的expdp 参数可以想到,使用parfile是更好的选择,因为这样可以用漂亮的方式给expdp提供导出参数。
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
可得zeng 只导出了属于他自己的表account_manager,并且表中只有一条记录。因为zeng 没有特权。
不信的话可以查看user_objects 确定一下。
SQL> show user
USER is "ZENG"
SQL> select object_name from user_objects;
OBJECT_NAME
--------------------------------------------------------------------------------
CONSTRAINTS_ID#_PK
ACCOUNT_MANAGER
改由hr 来执行上面的命令。
[oracle@zeng ~]$ expdp hr/hr tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log  job_name=exp_zeng     
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 18:44:36 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 "HR"."EXP_ZENG":  hr/******** tablespaces=users dumpfile=dump_test_dir:exp_zeng.dmp reuse_dumpfiles=yes logfile=dump_test_dir:exp_zeng.log job_name=exp_zeng 
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 512 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "HR"."COUNTRIES"                            6.367 KB      25 rows
. . exported "HR"."DEPARTMENTS"                          7.007 KB      27 rows
. . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows
. . exported "HR"."JOBS"                                 6.984 KB      19 rows
. . exported "HR"."JOB_HISTORY"                          7.054 KB      10 rows
. . exported "HR"."LOCATIONS"                            8.273 KB      23 rows
. . exported "HR"."REGIONS"                              5.476 KB       4 rows
. . exported "ZENG"."ACCOUNT_MANAGER"                    7.125 KB       1 rows
Master table "HR"."EXP_ZENG" successfully loaded/unloaded
******************************************************************************
Dump file set for HR.EXP_ZENG is:
  /opt/oracle11g/admin/oracl/exp_zeng.dmp
Job "HR"."EXP_ZENG" successfully completed at 18:45:34
结果就完全不同了hr 把他自己模式中的表全面都导出了,还把zeng 模式中的表也给导出了。
因为hr 是有DATAPUMP_EXP_FULL_DATABASE 角色的大佬。
SQL> select * from dba_role_privs
  2  where grantee='HR';
GRANTEE                        GRANTED_ROLE                   ADM DEF
------------------------------ ------------------------------ --- ---
HR                             RESOURCE                       NO  YES
HR                             DATAPUMP_EXP_FULL_DATABASE     NO  YES
五、传输表空间模式(transportable tablespace mode)
通过指定transport_tablespaces 参数来执行该模式。在传输表空间模式,只有在指定表空间的表及其依赖的对象会被导出。数据文件的复制是一项单独的操作,也就是说dumpfile 中只有相关对象的元数据。另外一个传输表空模式的作用停止以后不能重新开始。加密的列不支持传输表空间模式。传输表空间模式导出的dumpfile 不能导入到比源数据库版本更低的目标数据库中。
在执行表空间模式前需要把要执行表空间模式的表空间alter 为只读。另外不能使用schema 在该表空间的用户来执行导出,否则会报ORA-01647 错误。
[oracle@zeng ~]$ expdp system transport_tablespaces=users dumpfile=tablespace_exp.dmp logfile=tablespace.log      
Export: Release 11.2.0.1.0 - Production on Mon Mar 19 23:13:14 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Password: 
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 "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01":  system/******** transport_tablespaces=users dumpfile=tablespace_exp.dmp logfile=tablespace.log 
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/CONSTRAINT/REF_CONSTRAINT
Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS
Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
Master table "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TRANSPORTABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/tablespace_exp.dmp
******************************************************************************
Datafiles required for transportable tablespace USERS:
  /opt/oracle11g/oradata/oracl/users01.dbf
Job "SYSTEM"."SYS_EXPORT_TRANSPORTABLE_01" successfully completed at 23:15:14
上面我执行了一个传输表空间模式的导出,data pump export 还很聪明,会告诉你需要传输的表空间文件:
Dump file set for SYSTEM.SYS_EXPORT_TRANSPORTABLE_01 is:
  /opt/oracle11g/admin/oracl/dpdump/tablespace_exp.dmp
注:之所有需要把表空间alter 为只读,是因为要保证导出数据与现存数据的一致性。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26110315/viewspace-719071/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26110315/viewspace-719071/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值