Oracle 使用 expdp 和 impdp 导出和导入数据

1 expdp/impdp 和 和 exp/imp 的区别

1、exp 和 imp 是客户端工具,他们即可以在客户端使用,也可以在服务端使用。 2、expdp 和 impdp 是服务端的工具,它们只能在 oracle 服务端使用,不能在客户端使用 3、imp 只适用 exp 导出的文件,不适用 expdp 导出的文件,impdp 只适用 expdp 导出的文 件,不适用 exp 导出的文件。 4、对于 oracle10G 以上的服务器,使用 exp 通常不能导出 0 行数据的空表,而此时只能使 用 expdp 导出,或做一些系统设置。 5、expdp/impdp 在备份和恢复时间上要比 exp/imp 有着优势,并且管理灵活。 6、expdp/impdp 可以指定导出和导入任意对象,exp/imp 只能指定表。

2 使用 expdp 导出数据

2.1 准备工作

2.1.1 在服务器上创建备份目录

$ mkdir -p /oradata/backup
$ chown -R oracle:dba /oradata

2.1.2. 创建逻辑目录

# su - oracle
$ sqlplus / as sysdba
SQL> create directory scott_data_dir as '/oradata/backup'

这里的 scott_data_dir 是自己取的名字

2.1.3. 查看管理员目录是否存在

SQL> select * from dba_directories;

2.1.4 给 scott 用户赋予在指定目录操作的权限

SQL> grant read,write on directory scott_data_dir to scott;

2.2 导出数据

2.2.1. 按照用户导出

$ expdp scott/tiger@orcl schemas=scott dumpfile=expd.dmp directory=scott_data_dir
或
$ expdp scott/tiger@127.0.0.1/orcl schemas=scott dumpfile=expd.dmp directory=scott_data_dir

2.2.2 并行进程 parallel

$  expdp  scott/tiger@orcl  schemas=scott  dumpfile=expd.dmp  directory=scott_data_dir parallel=40 job_name=scottjob1

2.2.3 按照表名导出

$ expdp scott/tiger@orcl tables=emp,dept dumpfile=expd.dmp directory=scott_data_dir

2.2.4 按照条件导出

$ expdp scott/tiger@orcl dumpfile=expd.dmp directory=scott_data_dir tables=emp query=’where dept=10’

2.2.5 按照表空间导出

$  expdp system/密码@orcl dumpfile=expd.dmp directory=scott_data_dir tablespaces=temp,scott

2.2.6 导出整个库

$ expdp system/密码@orcl dumpfile=expd.dmp directory=scott_data_dir full=y

3 使用 impdp 导入数据

3.1 导入到指定用户下

$  impdp  scott/tiger@orcl  dumpfile=expd.dmp  directory=scott_data_dir  schemas=scott logfile=impdp.log

3.2 改变表的所有者

$ impdp system/password@orcl dumpfile=expd.dmp directory=scott_data_dir
remap_schemas=scott:system logfile=impdp.log

3.3 导入表空间

$ impdp system/password@orcl dumpfile=expd.dmp directory=scott_data_dir tablespaces=example

3.4 导入数据库

$ impdp system/password@orcl dumpfile=expd.dmp directory=scott_data_dir full=y

3.5 追加导入

$ impdp system/password@orcl dumpfile=expd.dmp directory=scott_data_dir schemas=system
table_exists_action

说明: remap_schema=scott:system 表示将数据的 schema 从 scott 转换为 system remap_tablespace=A:B 表示将数据的 tablespace 从 A 转换为 B