1.概述
Oracle Data Pump技术支持数据和元数据从一个数据库到另一个数据库的高速移动。
Oracle Data Pump由三个不同的组件组成:命令行客户端、expdp和impdp;DBMS_DATAPUMP PL/SQL包(也被称为数据泵API);DBMS_METADATA PL/SQL包(也称为元数据API)。
Oracle Data Pump可以将数据库的所有或部分从非cdb迁移到PDB,在相同或不同cdb的PDB之间迁移,也可以从PDB迁移到非cdb。许多导出和导入操作都需要DATAPUMP_EXP_FULL_DATABASE、DATAPUMP_IMP_FULL_DATABASE角色
Oracle数据泵作业使用一个数据泵控制作业表、一个数据泵控制作业进程和工作进程来执行工作和跟踪进度。
要监视表数据传输,可以使用V$SESSION_LONGOPS动态性能视图来监视Oracle数据泵作业。
2.数据泵导出数据
Oracle Data Pump Export是一个工具,用于将数据和元数据卸载到一组操作系统文件中,这些文件被称为转储文件集。
使用expdp命令启动Oracle数据泵导出工具。
您不能为Oracle数据库导出多个Oracle管理的系统模式,因为它们不是用户模式;它们包含oracle管理的数据和元数据。例如:SYS、ORDSYS和MDSYS。
(1)Oracle数据泵导出接口
命令行接口,参数文件接口,交互式命令接口
(2)查看expdp命令帮助信息
expdp -help
用法:
Format: expdp KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: expdp scott/tiger DUMPFILE=scott.dmp DIRECTORY=dmpdir SCHEMAS=scott
or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
# 注:用户id必须为命令行的第一个参数(scott/tiger)
# 相关参数说明:
========================
scott/tiger:表示用户id,也就是登录连接数据库的用户认证信息
dumpfile:指定目标转储文件名列表[expdat.dmp]。
例如,DUMPFILE = scott1.dmp, scott2.dmp, dmpdir: scott3.dmp。
directory:指定用于存放转储和日志文件的数据库目录对象的名称。它不是实际目录的文件路径
schemas:指定要导出的模式列表,如果不指定该参数,默认为登录连接的模式
tables:指定要导出的表列表。
例如,tables= HR.EMPLOYEES,SH.SALES: SALES_1995。
tablespaces:指定要导出的表空间列表
(3)实践
导出多张表数据:
# 1.将用户scott加到角色datapump_exp_full_database中
sqlplus / as sysdba
grant datapump_exp_full_database to scott;
grant datapump_imp_full_database to scott;
# 2.执行数据泵导出命令:
# 出现问题
[oracle@oracle1 ~]$ expdp scott/tiger parfile=scott.par
Export: Release 19.0.0.0.0 - Production on Thu Sep 8 13:38:02 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-39087: directory name /BACKUP/SCOTT_DUMP is invalid
==================================
查找官网:
ORA-39002: invalid operation
报错原因:directory参数有误输入有误
# 3.创建文件系统目录和数据库目录对象
mkdir -p /backup/scott_dump
chown -R oracle.oinstall /backup/scott_dump
su - oracle
sqlplus / as sysdba
create directory scott_dump as '/backup/scott_dump';
-- 查看刚刚创建的目录对象信息
SYS@orcl> select * from all_directories where directory_name = 'SCOTT_DUMP';
OWNER DIRECTORY_NAME DIRECTORY_PATH ORIGIN_CON_ID
------------------------------ -------------------- ------------------------------ -------------
SYS SCOTT_DUMP /backup/scott_dump 0
SYS@orcl> select * from dba_directories where directory_name = 'SCOTT_DUMP';
OWNER DIRECTORY_NAME DIRECTORY_PATH ORIGIN_CON_ID
------------------------------ -------------------- ------------------------------ -------------
SYS SCOTT_DUMP /backup/scott_dump 0
-- 说明:
-- all_directories:该视图描述用户可访问的所有目录
-- dba_directories:此视图描述为整个数据库指定的所有目录
# 4.再次执行expdp命令导数表数据
[oracle@oracle1 ~]$ expdp scott/tiger directory=scott_dump dumpfile=scott.dmp tables=emp,dept
Export: Release 19.0.0.0.0 - Production on Thu Sep 8 14:16:11 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Starting "SCOTT"."SYS_EXPORT_TABLE_01": scott/******** directory=scott_dump dumpfile=scott.dmp tables=emp,dept
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
. . exported "SCOTT"."EMP" 8.773 KB 14 rows
. . exported "SCOTT"."DEPT" 6.023 KB 4 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
/backup/scott_dump/scott.dmp
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at Thu Sep 8 14:16:33 2022 elapsed 0 00:00:21
# 5.查看对应文件系统目录下的信息,会生成两个文件
[oracle@oracle1 ~]$ ll -h /backup/scott_dump/
total 240K
-rw-r--r-- 1 oracle oinstall 1.3K Sep 8 14:16 export.log
-rw-r----- 1 oracle oinstall 236K Sep 8 14:16 scott.dmp
# 6.通过参数文件,只导数据
[oracle@oracle1 ~]$ cat dept1.par
directory=scott_dump
dumpfile=data_only.dmp
content=data_only
exclude=table:"in ('EMP','DEPT')"
query=dept1:"where deptno=50"
[oracle@oracle1 ~]$ expdp scott/tiger parfile=dept1.par
# 注意:exclude表示哪些对象数据不导
# query表示导出指定表中满足条件的数据
# 7.导出模式
# 没有指定schemas参数时,默认导出当前模式
[oracle@oracle1 ~]$ expdp scott/tiger directory=scott_dump dumpfile=scott_schema.dmp parallel=2
3.数据泵导入数据
Oracle Data Pump Import是一个用于将Oracle导出转储文件集加载到目标系统的实用程序。
使用impdp命令启动Oracle数据泵导入工具。
查看帮助信息:
impdp -help
用法:
Format: impdp KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: impdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp
实践:
# 1.删除表emp,然后利用expdp导出的scott.dmp导出文件,使用impdp导入emp表
sqlplus scott/tiger
drop table emp;
quit
[oracle@oracle1 ~]$ impdp scott/tiger directory=scott_dump dumpfile=scott.dmp tables=emp
Import: Release 19.0.0.0.0 - Production on Thu Sep 8 15:22:15 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Master table "SCOTT"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_TABLE_01": scott/******** directory=scott_dump dumpfile=scott.dmp tables=emp
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."EMP" 8.773 KB 14 rows
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/STATISTICS/TABLE_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
Job "SCOTT"."SYS_IMPORT_TABLE_01" successfully completed at Thu Sep 8 15:22:32 2022 elapsed 0 00:00:15
# 2.登录查询
sqlplus scott/tiger
SCOTT@orcl> select count(*) from emp;
COUNT(*)
----------
14
# 3.截断表salgrade中的数据,利用data_only.dmp导出文件导入salgrade表数据
sqlplus scott/tiger
truncate table salgrade;
quit
[oracle@oracle1 ~]$ impdp scott/tiger directory=scott_dump dumpfile=data_only.dmp tables=salgrade content=data_only
# 登录查看
sqlplus scott/tiger
SCOTT@orcl> select count(*) from salgrade;
COUNT(*)
----------
5




