1. 为什么选择数据泵
数据泵是Oracle 10g开始引入的新技术,对现在来说已经不是新技术了,11g都已经结束标准支持期了。 技术更新
从10g版本开始,数据泵技术跟随新版本进行不断的完善,对新版本的Oracle数据库具有更强的适应性。 BUG更少
数据泵可以使用直接路径、并行等特性,对大数据量的迁移来说,具有更好的性能,可以减少大量导入导出时间。 性能更好
exp/imp 不能导出空表,而且由于从10g开始停止更新,后面版本引入的新对象都不支持导出,如自定义对象。 功能更强
1.1. 与exp/imp的主要区别
Ø exp/imp 是客户端工具,可以在客户端使用;数据泵是服务端工具,只能在服务端使用。
Ø exp/imp 不能导出空表;数据泵可以。
Ø exp/imp 跟数据泵导出的文件不能通用。
2. 基本概念
数据泵的命令行命令是expdp/impdp,是一种数据库之间 移动数据的工具。
目录(directory)是Oracle的一种对象类型,可以认为是一个指向物理存储路径的指针,用来指定数据泵导出导入文件使用的路径。
模式(schema)是一组数据库对象的集合,一般一个用户对应一个schema。
3. 基本步骤
3.1 创建目录
|
SQL> create directory dump_dir as ' /home/oracle/dump'; |
dump_dir 是目录名称;可以根据实际情况自己定义, 不重复即可,如果系统中已经存在,可以直接使用;
/home/oracle/dump 是操作系统物理路径;必须保证该路径 真实存在,并具有 足够空间存放备份文件;
3.2 授权
|
SQL> grant read,write on directory dump_dir to public; |
dump_dir 是上面创建得目录名称;
public 表示这个目录是公开的;也可以针对某个用户授权;
3.3 执行导入导出
|
$ expdp system/dbmanager directory= dump_dir full=Y dumpfile=full_20190101.dmp logfile= full_expdp_20190101.log |
|
$ impdp system/dbmanager directory= dump_dir dumpfile=full_20190101.dmp logfile= full_impdp_20190101.log |
system 是导出命令所用的用户;
dbmanager 是system用户的密码;
dump_dir 是上面创建得目录名称;
full=Y 表示全库导出;
dumpfile/logfile 根据实际情况命名即可;该命名需要能够描述备份的内容及时间等基本信息;该命名重复的话会 覆盖;
4. 常用方式
4.1 expdp 导出
|
## 导出一张表 ,例: expdp system/dbmanager directory= dump_dir dumpfile= emp_20190101.dmp logfile= emp_expdp_20190101.log tables= scott.emp
## 导出多张表 ,例: expdp system/dbmanager directory= dump_dir dumpfile= empdept_20190101.dmp logfile= empdept_expdp_20190101.log tables = \(scott.emp,scott.dept\ )
## 导出一个用户(导出这个用户的所有对象) ,例: expdp system/dbmanager directory= dump_dir dumpfile= scott_20190101.dmp logfile= scott_expdp_20190101.log schemas = scott
## 导出多个用户 ,例: expdp system/dbmanager directory= dump_dir dumpfile= scotthr_20190101.dmp logfile= scotthr_expdp_20190101.log schemas = \(scott,hr\)
## 导出整个数据库(sys的用户数据不会被导出) ,例: expdp system/dbmanager directory= dump_dir dumpfile= full_20190101.dmp logfile =full_expdp_20190101.log full = y
## 并行导出 ,例: expdp system/dbmanager directory=dump_dir dumpfile=scott_20190101 _%U .dmp logfile=scott_expdp_20190101.log schemas=scott parallel=8
## 导出用户元数据(包含表定义、存储过程、函数等等) ,例: expdp system/dbmanager directory=dump_dir dumpfile=scott_20190101.dmp logfile=scott_expdp_20190101.log schemas=scott content=metadata_only
## 导出用户存储过程 ,例: expdp system/dbmanager directory=dump_dir dumpfile=scott_20190101.dmp logfile=scott_expdp_20190101.log schemas=scott include=procedure
## 导出用户函数和视图 ,例: expdp system/dbmanager directory=dump_dir dumpfile=scott_20190101.dmp logfile=scott_expdp_20190101.log schemas=scott include=\(function,view\)
## 导出一个用户,但不包括索引 ,例: expdp system/dbmanager directory=dump_dir dumpfile=scott_20190101.dmp logfile=scott_expdp_20190101.log schemas=scott exclude=index |
4.2 impdp 导入
|
## 导入dmp文件中的所有数据 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log full=y
## 导入一张表 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log tables=scott.emp
## 导入多张表 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log tables=\(scott.emp,scott.dept\)
## 导入一个用户 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log schemas=scott
## 导入多个用户 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log schemas=\(scott,hr\)
## 并行导入 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101 _%U .dmp logfile=full_impdp_20190101.log parallel=5
## 导入元数据(包含表定义、存储过程、函数等等) ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log content=metadata_only
## 导入存储过程 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log include=procedure
## 导入函数和视图 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log include=\(function,view\)
## 导入数据,但不包括索引 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log exclude=index
## 重命名表名导入 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log remap_table=scott.emp:emp1
## 重命名schema名导入 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log remap_schema=scott:tim
## 重命名表空间名导入 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log remap_tablespace=users:pams
## 将dmp文件的ddl语句导入到一个文件,不导入数据库 ,例: impdp system/dbmanager directory=dump_dir dumpfile=full_20190101.dmp logfile=full_impdp_20190101.log sqlfile=import.sql |
5. 常用参数
5.1 expdp 参数说明
|
attach=[schema_name.]job_name 说明:nodefault。连接到作业,进入交互模式。
## 导出模式,以下五个参数互斥: full=[ Y | N ] 说明:导出所有数据和元数据。要执行完全导出,需要具有datapump_exp_full_database角色。
schemas=schema_name[,...] 说明:导出用户。
tables=[schema_name.]table_name[:partition_name][,...] 说明:导出表。
tablespaces=tablespace_name[,...] 说明:导出表空间。
transport_tablespaces=tablespace_name[,...] 说明:导出可移动表空间。
## 过滤条件,以下三个参数互斥: query=[schema.][table_name:] query_clause 说明:按查询条件导出。
exclude=object_type[:name_clause][,...] 说明:排除特定的对象类型。
include=object_type[:name_clause][,...] 说明:包括特定的对象类型。
## 其他参数: directory=directory_object 说明:导出路径。
dumpfile=file_name[,...] 说明:导出的文件名。
logfile=file_name 说明:导出的日志文件名。
content=[ all | data_only | metadata_only] 说明:指定要导出的数据。
parallel=integer 说明:并行度,该值应小于等于dmp文件数量,或可以为'dumpfile='使用替换变量'%U'。RAC环境中,并行度大于1时,注意目录应该为共享目录。
compression=[ all | data_only | metadata_only | none ] 说明:压缩。
parfile=[directory_path]file_name 说明:指定导出参数文件名称。
filesize=integer[b|kb|mb|gb|tb] 说明:指定每个dmp文件的最大大小。如果此参数小于将要导出的数据大小,将报错ORA-39095。
|
5.2 impdp 参数说明
|
attach=job_name 说明:连接到作业,进入交互模式。
导入模式,以下五个参数互斥。 full=[ Y | N ] 说明:导入dmp文件的所有数据和元数据。
schemas=schema_name[,...] 说明:导入用户。
tables=[schema_name.]table_name[:partition_name][,...] 说明:导入表。
tablespaces=tablespace_name[,...] 说明:导入表空间。
transport_tablespaces=tablespace_name[,...] 说明:导入可移动表空间。
过滤条件,以下三个参数互斥: query=[schema.][table_name:] query_clause 说明:按查询条件导入。
exclude=object_type[:name_clause][,...] 说明:排除特定的对象类型。
include=object_type[:name_clause][,...] 说明:包括特定的对象类型。
其他参数: directory=directory_object 说明:导入路径。
dumpfile=file_name[,...] 说明:导入的文件名。
logfile=file_name 说明:导入的日志文件名。
content=[ all | data_only | metadata_only ] 说明:指定要导入的数据。
parallel=integer 说明:并行度,该值应小于等于dmp文件数量,或可以为'dumpfile='使用替换变量'%U'。
parfile=[directory_path]file_name 说明:指定导入参数文件名称。
REMAP_TABLE=[schema.]old_tablename[.partition]:new_tablename 说明:允许导入期间重命名表名。
REMAP_SCHEMA=source_schema:target_schema 说明:允许导入期间重命名schema名。
REMAP_TABLESPACE=source_tablespace:target_tablespace 说明:允许导入期间重命名表空间名。
SQLFILE=[directory_object:]file_name 说明:根据其他参数,将所有的 SQL DDL 写入指定的文件。
TABLE_EXISTS_ACTION=[ SKIP | APPEND | TRUNCATE | REPLACE ] 说明:default: skip(if content=data_only is specified,then the default is append)
|
6. 注意事项
6.1 directory 相关SQL语句:
|
## 查看目录 select * from dba_directories;
## 创建目录 create directory dump_dir as '/home/oracle/tmp';
## 目录授权 grant read,write on directory my_dir to public; |
6.2 expdp 导出
|
1 、导数的数据库用户需要拥有对directory_object的读写权限。 2 、操作系统中需要已经存在directory_object指定的路径。 3 、oracle用户拥有对directory_object指定路径的读写权限。 4 、system用户导出用户,会将创建用户和授予系统权限的元数据也导出,普通用户不能导出这些元数据。 |
6.3 impdp 导入
|
1 、expdp导出的文件不能使用imp导入,只能通过impdp导入数据库。 2 、导入时遇到已存在的对象,默认会跳过这个对象,继续导入其他对象。 3 、导入时应确认dmp文件和目标数据库的tablespace、schema是否对应。 4 、导入dmp文件时,应确定dmp文件导出时的命令,以便顺利导入数据。 |
6.4 交互模式
|
进入交互可以操作导入导出作业。 进入交互模式的方法: 1 、导入导出命令行执行期间按Ctrl + c 2 、expdp attach=jobname或impdp attach=jobnam 查看导入导出日志可以看到jobname,也可以通过查询dba_datapump_jobs找到jobname。 |
6.5 常见报错
|
系统目录未建立,报错: ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 536 ORA-29283: invalid file operation
impdp 导入exp导出的dmp文件,报错: ORA-39000: bad dump file specification ORA-39143: dump file "/orabak/pams_20190101.dmp" may be an original export dump file
如果导出的数据库版本比导入的数据版本高,需要在导出时加上参数version=要导入的数据库版本。否则报错: ORA-39001: invalid argument value ORA-39000: bad dump file specification ORA-31640: unable to open dump file "/orabak/pams_20190101.dmp" for read ORA-27037: unable to obtain file status |
