分享交流
合作共赢!

Oracle数据泵导入导出备份数据库数据详解

数据泵优势:

数据泵是10g推出的功能,导数据比较常用,其导入的时候利用remap参数很方便转换表空间以及schema,并且可以忽略服务端与客户端字符集问题(exp/imp需要排查字符集)。

数据泵劣势:

数据泵也有不方便的地方,如果远程导出导入,必须安装数据库服务端(client不行);需要在数据库中创建一个路径directory(dba_directories);并且主流工具支持exp/imp的导入导出(plsql developer),所以发现数据泵流行程度没有想象中高。

以下实例简单介绍schema的导入导出:
以schema方式导出生产库用户下所有对象,并导入测试库。
注:eamdb为生产库,eamprd为生产库用户,密码为eamprd
eamuat为测试库,eamprduat为测试库用户,密码为eamprduat

一、生产库的导出(以sqlplus命令行的方式)

1.以sys或者system用户身份登录生产数据库

2.创建schema导出路径(DUMP_DIR名称可替换),并在dba_directories中查看

create directory DUMP_DIR as ‘/xxx/xxx’;   --创建directory路径
select * from dba_directories;    --查看directory
附:
drop directory dirname;        --删除directory
create or replace directory dirname as '/xxx/xxx';    --更改directory路径
grant read,write on directory dumpdir to username;   --directory赋权限

3.把导出路径与导出权限授权给eamprd,如果用system等高级帐号导出,则不用。

grant read,write on directory DUMP_DIR to eamprd;
grant exp_full_database to eamprd;

4.退出sqlplus,在oracle系统用户下运行,导出对应的SCHEMA,推荐第二种。

expdp eamprd/eamprd@eamdb DIRECTORY=DUMP_DIR DUMPFILE=eamdb.dmp
expdp system/xxxx@eamdb directory=dump_dir dumpfile=eamdb.dmp schemas=eamprd

导出参考:

导出内容参考:
1)导出用户及其对象 expdp scott/tiger@orcl schemas=scott dumpfile=expdp.dmp directory=dump_dir; 
2)导出指定表 expdp scott/tiger@orcl tables=emp,dept dumpfile=expdp.dmp directory=dump_dir; 
3)按查询条件导 expdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp tables=empquery='where deptno=20'; 
4)按表空间导 expdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=temp,example; 
5)导整个数据库 expdp system/manager@orcl directory=dump_dir dumpfile=full.dmp full=y;    
导出重点参数:
版本:高往低需加version=xx.x  导出某些张表tables=xxxx  content=metadata_only(只要结构)   content=data_only(只要数据)

二、测试库的导入

1.以sys或者system身份登陆测试库。

2.建立导入表空间和临时表空间(名称、路径、表空间大小,请自行替换)。

create tablespace tbs_EAMUAT datafile '/xxxx/xxxxx/EAMUAT.DBF' size 10240M autoextend on next 1024M maxsize 20480M;
create temporary tablespace EAMUAT_TEMP tempfile '/xxx/xxx/EAMUAT_TEMP.DBF' size 5120M;

3.建立用户及赋予权限(也可以不建立用户),导入时最好给予用户dba权限,以防导入时创建某些对象权限不够,注意需要回收其对users表空间的权限。

create user eamprduat identified by eamprduat default tablespace tbs_EAMUAT temporary tablespace EAMUAT_TEMP;
grant connect,resource,create view,create session,dba to eamprduat;
revoke unlimited tablespace from eamprduat;
alter user eamprduat quota unlimited on tbs_EAMUAT;

4.建立导入路径(把eamdb.dmp放在此路径下),并授权。

create directory DUMP_DIR as '/xxxxx/xxxxx';
grant read,write on directory DUMP_DIR to eamprduat;

5.导入数据(在oracle系统用户下运行),注意remap_schema参数,请自行替换

此内容查看价格0.1立即购买
impdp eamprduat/eamprduat@eamuat DIRECTORY=DUMP_DIR DUMPFILE=eamdb.dmp LOGFILE=impdp.log remap_schema=eamprd:eamprduat remap_tablespace=tablespaceOld:tablespaceNew
impdp system/zldc directory=impdp dumpfile=zldc_20181220.dmp logfile=zldc1222.log remap_schema=zldc:zjtjnc TABLE_EXISTS_ACTION=REPLACE

 导入参数:

在正式导入数据前,要先确保要导入的用户已存在,如果没有存在,请先用下述命令进行新建用户
 
--创建表空间 create tablespace tb_name datafile 'D:\tablespace\tb_name.dbf' size 1024m AUTOEXTEND ON;
--创建用户 create user user_name identified by A123456a default tablespace tb_name temporary tablespace TEMP;
--给用户授权
sql>grant read,write on directory dump_dir to user_name;
sql>grant dba,resource,unlimited tablespace to user_name;

1)导入用户(从用户scott导入到用户scott impdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp schemas=scott;
2)导入表(从scott用户中把表deptemp导入到system用户中) 
impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmptables=scott.dept,scott.emp remap_schema=scott:system;
3)导入表空间 impdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=example;
4)导入数据库 impdb system/manager@orcl directory=dump_dir dumpfile=full.dmp full=y;
5)追加数据 impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmp schemas=systemtable_exists_action

导入重点参数:
remap_schema=eamprd:eamprduat,eamxxx:xxx,xxx:xxx 
remap_tablespace=eamdev:eamxxx,eamxxx:xxx,xxx:xxx 
table_exists_action=replace(替换)append(追加) 
exclude=table_statistics 此参数是取消对表的统计信息收集,如果表太多,不取消的话特别慢,之后可以手动收集,或等oracle自动收集。 
execute dbms_stats.gather_table_stats(ownname=>'USERNAME',tabname=>'TABLE_NAME',cascade=>TRUE)
提示:
如导入报错:ORA-31684: Object type USER:"xxxxx" already exists,不用理会,因为之前建立了用户。
也可以在导入的语句中直接remap一个不存在的用户,会自动生成,其密码和权限与导出时候一样,但其表空间如果不想用users,必须手工创建。
附加:
更改表空间:alter user username default tablespace tablespace_Name temporary tablespace temp_Name;

 

赞(2) 打赏
未经允许不得转载:琼杰笔记 » Oracle数据泵导入导出备份数据库数据详解

评论 抢沙发

评论前必须登录!

 

分享交流,合作共赢!

联系我们加入QQ群

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册