转载自:
http://www.csdn.net/article/2015-02-02/2823788
Oracle12c的datapump功能跟以前差不多,在多租户的环境中执行导入\导出以及使用一些更细化的参数的时候,几乎没有区别,依然很好用,效率很高。目前有很多的用户仍然在使用exp/imp工具在执行一些迁移、备份、过滤和转移数据的工作,相比起来,数据泵的效率更高、更易用并且更方便管理,但exp/imp在有些时候可以完成datapump不适用的情况。两者的操作风格和格式很相似,相信在您掌握了其中任一种工具之后,另一种会很容易上手。
数据泵可以高效备份、复制、保护和传输大量的数据和源数据。在导入和导出过程中可以做到过滤数据和对象,并且能够在全数据库级、方案级、表级和表空间级实现导入导出。
在此,我们简单体验一下数据泵在可插拔数据库中的基本操作过程,你将会发现,跟以前传统的非插拔数据库几乎没有太大区别。
在Oracle12c的可插拔数据库环境中,首先启动CDB:
[oracle@cafe ~]$ export ORACLE_SID=cup -----CDB的名字自定义为cup,顾名思义cup作为容器储存数据库,我们的PDB自定义为tea。
- [oracle@cafe ~]$ sqlplus / as sysdba
- SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 23 15:06:41 2015
- Copyright (c) 1982, 2014, Oracle. All rights reserved.
- Connected to an idle instance.
- SQL> startup -----启动CDB
- ORACLE instance started.
- Total System Global Area 767557632 bytes
- Fixed Size 2929112 bytes
- Variable Size 574623272 bytes
- Database Buffers 184549376 bytes
- Redo Buffers 5455872 bytes
- Database mounted.
- Database opened.
- SQL> alter pluggable database tea open; -----在CDB中启动可插拔数据库(PDB)tea。
- Pluggable database altered.
- SQL> alter session set container = tea; -----切换到PDB中(tea)。
- Session altered.
- SQL> show con_name
- CON_NAME
- ------------------------------
- TEA -----经验证,已切换到tea数据库容器。
- SQL> show parameter service;
做数据泵的导出需要首先创建目录对象,主要用于作为导出目的地存放导出文件用,应该事检查操作系统中有充足的空间可用并且该空间在数据库服务器本地。这样做的好处之一是可以对目录结构等信息达到保密的安全效果,不赞成使用默认的路径,因为可能会导致空间不足问题,以至于影响数据库正常功能。
在数据库系统中创建相关目录:
- [root@cafe /]# mkdir /oradump
- [root@cafe /]# chmod -R 777 /oradump
- [root@cafe /]# chown -R oracle.oinstall /oradump
- SQL> create directory dump_dir as '/oradump'; -----在数据库中创建该目录的对象信息。
- Directory created.
- SQL> select directory_name,directory_path from dba_directories;
- DIRECTORY_NAME DIRECTORY_PATH
- -------------- --------------
- DUMP_DIR /oradump
插入测试数据,以备验证后期验证导出、导入的效果:
- SQL> create table test(id number);
- Table created.
- SQL> insert into test values(1);
- 1 row created.
- SQL> CREATE USER eric IDENTIFIED BY gao ACCOUNT UNLOCK;
- User created.
- SQL> grant dba to eric;
- Grant succeeded.
expdp eric/gao directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
参数解释:
- directory:指定的是咱们刚创建的目录对象中的路径别名,在dba_directories中对应着directory_name字段。
- dumpfile:自定义导出文件的filename。
- logfile:可以把整个导入、导出过程中的操作信息输出到文件中,以便后期验证备份效果和排错用。
- full:等于y的时候为全库导出模式。
注意:导出所用的用户不要用sys,一般用具有dba角色的用户即可,system也可用。
导出过程:
[oracle@cafe ~]$ expdp eric/gao@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
Export: Release 12.1.0.2.0 - Production on Fri Jan 23 18:34:12 2015 -----此行包含版本、导出操作开始的时间,该时间可用于计算导出总时间和记录操作时间点。
- Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
- Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
- With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
- Starting "ERIC"."SYS_EXPORT_FULL_01": eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
- Estimate in progress using BLOCKS method...
- Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
- Total estimation using BLOCKS method: 4.265 MB
- ...省略多行...
- Processing object type DATABASE_EXPORT/PRE_SYSTEM_IMPCALLOUT/MARKER
- Processing object type DATABASE_EXPORT/PRE_INSTANCE_IMPCALLOUT/MARKER
- Processing object type DATABASE_EXPORT/TABLESPACE
- . . exported "SYS"."KU$_USER_MAPPING_VIEW" 6.109 KB 38 rows
- ...省略多行...
- . . exported "SYS"."NACL$_WALLET_EXP" 0 KB 0 rows
- . . exported "WMSYS"."WM$EXP_MAP" 7.718 KB 3 rows
- Master table "ERIC"."SYS_EXPORT_FULL_01" successfully loaded/unloaded
- ******************************************************************************
- Dump file set for ERIC.SYS_EXPORT_FULL_01 is:
- /oradump/fullbak.dmp
- Job "ERIC"."SYS_EXPORT_FULL_01" successfully completed at Fri Jan 23 18:49:29 2015 elapsed 0 00:12:55
查看一下导出的文件:
[root@cafe oradump]# ls -lh
总用量 2.8M
-rw-r----- 1 oracle oinstall 2.8M 1月 23 18:49 fullbak.dmp
导出的实际容量和其估算的容量还是有一定出入的,因此建议在准备存储空间的时候多预留一些。
-rw-r--r-- 1 oracle oinstall 9.4K 1月 23 18:49 fullbak.log
我们也可以只估算导出文件的大小而不进行实际的导出操作:
expdp eric/gao@tea estimate_only=y directory=dump_dir logfile=n full=y
此处我们使用了estimate_only参数,将该参数置于y状态即可开启估算功能,此处不需要写dumpfile参数,否则可能会报错。
输出结果:
- [oracle@cafe ~]$ expdp eric/gao@tea estimate_only=y directory=dump_dir logfile=n full=y
- Export: Release 12.1.0.2.0 - Production on Fri Jan 23 22:08:30 2015
- Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
- Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
- With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
- Starting "ERIC"."SYS_EXPORT_FULL_01": eric/********@tea estimate_only=y directory=dump_dir logfile=n full=y
- Estimate in progress using BLOCKS method...
- Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
- . estimated "SYS"."KU$_USER_MAPPING_VIEW" 16 KB
- . estimated "ORDDATA"."ORDDCM_DOCS" 1.25 MB
- . estimated "WMSYS"."WM$CONSTRAINTS_TABLE 320 KB
- . estimated "WMSYS"."WM$LOCKROWS_INFO 192 KB
- . estimated "WMSYS"."WM$UDTRIG_INFO 192 KB<br>. estimated "LBACSYS"."OLS$AUDIT_ACTIONS" 64 KB
- . estimated "LBACSYS"."OLS$DIP_EVENTS" 64 KB
- . estimated "LBACSYS"."OLS$INSTALLATIONS" 64 KB
- . estimated "LBACSYS"."OLS$PROPS" 64 KB
- ...省略多行...
- Total estimation using BLOCKS method: 4.265 MB
此处我们看到,该值跟咱们上一次做实际全库导出过程中算的值一样。
Job "ERIC"."SYS_EXPORT_FULL_01" successfully completed at Fri Jan 23 22:10:18 2015 elapsed 0 00:01:32
数据泵有一个好处,那就是交互操作模式,我们可以在作业运行中去监控运行状态,并且可以暂停、启动作业,也可以终止作业。
查看正在执行的导入导出状态:
- Export> status -----查看当前作业状态
- Job: SYS_EXPORT_FULL_02 -----作业名,上面的全库导出我们也看到过有对应的名字
- Operation: EXPORT -----操作类型:
- Mode: FULL -----模式:全库导出
- State: EXECUTING -----状态:执行中
- Bytes Processed: 0
- Current Parallelism: 1 -----当前并行数,如果指定了parallel参数,便会影响该值,并且下面会对应的有worker1~workerN
- Job Error Count: 0
- Dump File: /oradump/fullbak.dmp
- bytes written: 4,096 -----已写入的字节
- Worker 1 Status:
- Instance ID: 1
- Instance name: cup -----实例名为cup,容器数据库CDB的名称
- Host name: cafe -----操作系统主机名
- Process Name: DW00 -----相关写进程,如果设置了parallel的话,会有更多DWNN。
- State: EXECUTING
返回导入/导出命令行输出状态:
- Export> continue_client
- Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
- Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
- Total estimation using BLOCKS method: 4.578 MB
- Processing object type DATABASE_EXPORT/PRE_SYSTEM_IMPCALLOUT/MARKER
- Processing object type DATABASE_EXPORT/PRE_INSTANCE_IMPCALLOUT/MARKER
- Processing object type DATABASE_EXPORT/TABLESPACE
- Processing object type DATABASE_EXPORT/PROFILE
- Export> stop_job
- Are you sure you wish to stop this job ([yes]/no): yes
附加正在运行的作业,在这里就用到了咱们一再提到的job名称了,使用attach参数指定作业名称就可附加到正在执行或者是暂停的作业中去:
- [oracle@cafe oradump]$ expdp eric/gao@tea attach=eric.SYS_EXPORT_FULL_02
- Export: Release 12.1.0.2.0 - Production on Fri Jan 23 22:24:04 2015
- Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
- Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
- With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
- Job: SYS_EXPORT_FULL_02
- Owner: ERIC
- Operation: EXPORT
- Creator Privs: TRUE
- GUID: 0D537C91F12C103FE0537EE0A8C01C87
- Start Time: Friday, 23 January, 2015 22:24:16
- Mode: FULL
- Instance: cup
- Max Parallelism: 1
- Timezone: -07:00
- Timezone version: 18
- Endianness: LITTLE
- NLS character set: ZHS16GBK
- NLS NCHAR character set: AL16UTF16
- EXPORT Job Parameters:
- Parameter Name Parameter Value:
- CLIENT_COMMAND eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
- State: IDLING -----在这里我们看到job是处于暂停状态的
- Bytes Processed: 0
- Current Parallelism: 1
- Job Error Count: 0
- Dump File: /oradump/fullbak.dmp
- bytes written: 4,096
- Worker 1 Status:
- Instance ID: 1
- Instance name: cup
- Host name: cafe
- Process Name: DW00
- State: UNDEFINED
启动暂停的作业:
- Export> start_job
- Export> status
- Job: SYS_EXPORT_FULL_02
- Operation: EXPORT
- Mode: FULL
- State: EXECUTING
- Bytes Processed: 0
- Current Parallelism: 1
- Job Error Count: 0
- Dump File: /oradump/fullbak.dmp
- bytes written: 69,632
- Worker 1 Status:
- Instance ID: 1
- Instance name: cup
- Host name: cafe
- Process Name: DW00
- State: EXECUTING -----作业继续执行了
- Object Schema: SYS
- Object Name: KU$_USER_MAPPING_VIEW
- Object Type: DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE
- Completed Objects: 1
- Worker Parallelism: 1
终止数据泵作业,在作业执行中途遇到问题不得不停止的时候,可以用kill_job命令终止操作:
- Export> kill_job
- Are you sure you wish to stop this job ([yes]/no): yes
终止之后系统中不会有实际的导出文件生成:
[oracle@cafe oradump]$ ls -lt
总用量 4 -rw-r--r-- 1 oracle oinstall 1398 1月 23 22:31 fullbak.log
查看数据泵输出的日志,我们看一下终止操作在日志中的信息:
- Job "ERIC"."SYS_EXPORT_FULL_02" stopped due to fatal error at Fri Jan 23 22:31:01 2015 elapsed 0 00:06:48
我们删除数据库tea中的表,然后在数据库全备的文件中恢复该表,以验证导出的效果:
- SQL> drop table test;
- Table dropped.
- SQL> select * from test;
- select * from test
- *
- ERROR at line 1:
- ORA-00942: table or view does not exist -----该表已不存在。
然后从全备份中单独导入test表,在这里我们用全备的备份集,使用table参数保证单独导入该表,保证减小系统开销:
- [oracle@cafe oradump]$ impdp eric/gao@tea directory=dump_dir dumpfile=fullbak.dmp logfile=imptab.log tables=test
- Import: Release 12.1.0.2.0 - Production on Fri Jan 23 23:20:31 2015
- Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
- Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
- With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
- Master table "ERIC"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
- Starting "ERIC"."SYS_IMPORT_TABLE_01": eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=imptab.log tables=test
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
- . . imported "ERIC"."TEST" 5.046 KB 1 rows -----已导入1行数据,5K的数据量。
- Processing object type DATABASE_EXPORT/SCHEMA/TABLE/STATISTICS/TABLE_STATISTICS
- Processing object type DATABASE_EXPORT/STATISTICS/MARKER
- Job "ERIC"."SYS_IMPORT_TABLE_01" successfully completed at Fri Jan 23 23:21:37 2015 elapsed 0 00:00:59
验证表test已经恢复到了删除之前的状态:
- [oracle@cafe oradump]$ sqlplus eric/gao@tea
- SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 23 23:26:53 2015
- Copyright (c) 1982, 2014, Oracle. All rights reserved.
- Last Successful login time: Fri Jan 23 2015 23:23:49 +08:00
- Connected to:
- Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
- With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
- SQL> select * from test;
OK,数据已回来。在实际项目中,可能数据量、环境和客户要求都比此次试验规模庞大、情况复杂,但是万变不离其宗,切记一定实现规划好备份空间,密切的关注备份状态。
作者:高强,山东华鲁科技发展股份有限公司,负责Oracle、Linux、AIX和DB2运维,擅长Oracle、Linux、AIX、存储和备份,ChinaUnix论坛Oracle板块版主,ChinaUnix博客专家,ITPUB名人堂嘉宾,优酷网认证音乐牛人:EricGuitar。