温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

再续解密Oracle备份工具-expdp/impdp

发布时间:2020-06-14 05:23:28 来源:网络 阅读:1789 作者:lipengfei123 栏目:关系型数据库

再续解密Oracle备份工具-expdp/impdp

在这个信息的时代,数据规模和数据量的增长以爆炸式的速度扩张。之前几百M或数G规模的数据量都堪称庞大。现如今测试系统所占空间都是这一数据的几十倍甚至百倍。原生imp/exp工就这两匹老马在处理这么大的数据量就力不从心了。从10g开始,data pump横空出世,中文名叫数据泵。

 

数据泵的优点

a.为数据及数据对象提供更细微级别的选择性(使用exclude,include,content参数)

b. 可以设定数据库版本号(主要是用于兼容老版本的数据库系统)

c. 并行执行

d.预估导出作业所需要的磁盘空间(使用estimate_only参数)

e.支持分布式环境中通过数据库链接实现导入导出

f.支持导入时重新映射功能(即将对象导入到新的目标数据文件,架构,表空间等)

g. 支持元数据压缩及数据采样


要使用data pump工具,要指定一个directory对象。

 

什么是directory对象?

a. 字面意思就是目录,它不是实体,只是一个指向,指向操作系统中的一个具体路径。

b. 每个directory对象都有read、write两个权限,通过grant授权给指定用户。

c.拥有directory对象的read/write权限的用户就可以读/写该directory对象实际指定的操作系统路径下的文件。

 

一、目录操作

1、在操作系统层面创建目录

           mkdir /home/oracle/expdp

2、在库中创建目录、并授权给scott用户

           sqlplus / as sysdba
           SQL> create directory expdp as '/home/oracle/expdp';
           SQL> grant read,write on directory expdp to scott;

3、查看当前库中所有目录

           SQL> select OWNER','DIRECTORY_NAME','DIRECTORY_PATH from dba_directories;

 

二、expdp 参数说明:

再续解密Oracle备份工具-expdp/impdp

1、导出某用户下所有对象

          expdp scott/lipengfei directory=expdp dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log

2、导出部分表

          (1)expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp tables=\(emp,dept\) logfile=scott_emp.log
          (2)expdp scott/lipengfei directory=expdp dumpfile=scott_E_D.dmp tables=\(scott.E%,scott.D%\);

3、指定条件导出

          expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp logfile=scott_emp.log tables=emp query=\"where sal \>1000\"

4、导出时除某对象【静态收集信息,序列,视图,表】

          expdp scott/lipengfei  exclude=STATISTICS,SEQUENCE,VIEW,TABLE:\" IN \(\'EMP\',\'DEPT\'\)\" directory=expdp dumpfile=scott_2015_06_02.dmp logfile=scott_2015_06_02.log

5、导出同时压缩dmp文件(compression=ALL,此值在11g中才有)

         (1)创建测试表,并insert大量数据
          create table li nologging as select * from all_objects;
          insert into li select * from li;
            /
            /
            /
            /
           commit;
         (2)查看测试表所在物理大小
          select segment_name,bytes/1024/1024 from user_segments where segment_name=’LI';
         (3)导出测试表,并加上compression=ALL参数
          expdp scott/lipengfei directory=EXPDP dumpfile=scott_all_compression.dmp SCHEMAS=SCOTT logfile=scott_all_compression.log compression=ALL 
         (4)导出测试表
          expdp scott/lipengfei directory=EXPDP dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log
         (5)查看2次导出dmp文件的大小
          ls -lh *.dmp 【通过查看文件大小,可以看出加上compression=ALL参数后,压缩比堪比"gzip -9"】
         (6)对dmp文件进行第二次压缩
         zip scott_all_compression.zip scott_all_compression.dmp

6、content为all 时,将导出对象定义及其所有数据.为data_only时,只导出对象数据,为metadata_only时,只导出对象定义

         expdp scott/lipengfei directory=EXPDP dumpfile=scott_metadata_only.dmp content=metadata_only logfile=scott_metadata_only.log
         ls -lh *.dmp【通过查看文件大小,可以只导出对象定义,dmp文件很小】

 

三、impdp 参数说明:

再续解密Oracle备份工具-expdp/impdp

1、导入某用户所有对象

        (1)创建表空间
        SQL> create tablespace lipengfei datafile '/home/oracle/app/oracle/oradata/ecom/lipengfei.dbf' size 100M AUTOEXTEND OFF;
        (2)创建用户指定密码及默认表空间
        SQL> create user lipengfei identified by lipengfei default tablespace lipengfei;
        (3)解锁新创建的用户
        SQL> alter user lipengfei account unlock;
        (4)给新建的用户授权最基本的角色及权限
        SQL> grant connect,resource to lipengfei;
        SQL> grant create table to lipengfei;
        SQL> grant create view to lipengfei;
        (5)将数据泵指定的directory读写权限授权lipengfei
        SQL> grant read, write on directory EXPDP to lipengfei ;
        (6)登录lipengfei,创建表及数据初始化
        sqlplus lipengfei/lipengfei
        create table hehe(a int,b varchar2(10));
         insert into hehe values(2,'d');
         insert into hehe values(4,'e');
         insert into hehe values(6,'f');
         commit;
        create view nimei as select a from hehe;
        create table haha(id int);
        insert into haha values(1);
        commit;
        (7)导出lipengfei用户所有对象
        expdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp SCHEMAS=LIPENGFEI logfile=lipengfei_all.log 
        (8)登录lipengfei模拟数据全部丢失
        sqlplus lipengfei/lipengfei
        drop view nimei;
        drop table hehe;
        drop table haha;
        (9)把上面导出的数据还原回lipengfei用户
        impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp logfile=lipengfei_all.log

2、导入的对象已存在

当使用IMPDP完成数据库导入时,如遇到表已存在时,Oracle提供给我们如下四种处理方式:

a.忽略(SKIP,默认行为);

b.在原有数据基础上继续增加(APPEND);

c.先DROP表,然后创建表,最后完成数据插入(REPLACE);

d.先TRUNCATE,再完成数据插入(TRUNCATE)

         impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp TABLE_EXISTS_ACTION=TRUNCATE logfile=lipengfei_all.log

3、lipengfei用户数据 导入 shiqiang用户

        (1)创建表空间
        SQL> create tablespace shiqiang datafile '/home/oracle/app/oracle/oradata/ecom/shiqiang.dbf' size 100M AUTOEXTEND OFF;
        (2)创建用户指定用户及默认表空间
        SQL> create user shiqiang identified by shiqiang default tablespace shiqiang;
        (3)将新建用户解锁
        SQL> alter user shiqiang account unlock;
        (4)授权新建用户最基本的角色及权限
        SQL> grant connect,resource to shiqiang;
        SQL> grant create table to shiqiang;
        SQL> grant create view to shiqiang;
        (5)将数据泵指定的directory读写权限授权shiqiang
        SQL> grant read, write on directory EXPDP to shiqiang ;
        (6)将lipengfei用户的导出的dmp文件,导入shiqiang用户中
        impdp shiqiang/shiqiang directory=expdp remap_schema=lipengfei:shiqiang remap_tablespace=lipengfei:shiqiang dumpfile=lipengfei_all.dmp logfile=lipengfei_shiqiang.log ;

 4、只导入部分表

        (1)模拟数据丢失
        sqlplus lipengfei/lipengfei
        drop view nimei;
        drop table hehe;
        drop table haha;
        (2)将之前备份的dmp文件还原回lipengfei用户,并指定只恢复haha表
        impdp lipengfei/lipengfei directory=expdp tables=haha dumpfile=lipengfei_all.dmp logfile=lipengfei_only_haha.log
        (3)数据恢复后查看
        sqlplus lipengfei/lipengfei
        select * from hehe;
        select * from haha;

5、高版本导入低版本

        (1)11g导出,并指定version参数
        expdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp SCHEMAS=SHIQIANG logfile=shiqiang_11g_all.log version=10.2.0.1.0
        (2)10g导入
        impdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp logfile=shiqiang_11g_all.log


         虽然oracle对自己产品的宣传一向有夸大的传统,不过大家要理解,有了前面原生的exp/imp工具的铺垫,对比来看,数据泵的导入和导出,有了很大的提升。

         如果你之前有使用过exp/imp工具,通过上面expdp/impdp的一些例子,我相信你可以真正感受到,什么叫高效!



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI