数据泵

来源:互联网 发布:4g网络优化是什么 编辑:程序博客网 时间:2024/04/25 15:10

---数据泵  DATA PUMP
        数据泵的导入导出操作能够将表、索引、约束、权限、PL/SQL包等对象从数据库导出,并将它们保存在一种非文本格式
        的转储文件--泵出文件内;数据的泵入操作能够将泵出文件中的对象导入数据库。这是一种逻辑备份和还原。
        所有的泵出(expdp)泵入(impdp)都必须制定泵出文件所在目录。ORACLE 利用 CREATE DIRECTORY命令创建目录对象。
        create directory dump_dir as 'path';
        expdp 和  impdp 命令中利用 dumpfile 、 directory 这里两个参数指定泵出文件路径。
        比如:dumpfile=dump_dir:ext.dmp 指 转储文件的路径是 'path\ext.dmp'
                   directory=dump_dir dumpfile=ext.dmp 和上面的效果是一样的,只是表示方法不一样罢了。
        利用 grant read,write  on directory  dump_dir to 用户名 的方式给用户赋予操作权限。默认sys 用户 和 dba角色的获得者有对
        目录对象的 读/写权限。
        同时oracle 也额外提供了一个默认的目录对象,名为 data_pump_dir ,通过 dba_directories数据字典视图获得其真实路径
          select directory_path from dba_directories where directory_name ='DATA_PUMP_DIR';
         
   导出
            expdp 操作的执行者必须获得 datapump_exp_full_database 角色才能导出自身方案以外的其他方案及其对象。
           
            导出程序 EXPDP 能够按照:数据库、对象模式、表及表空间 导出
            1、数据库方式导出中,整个数据库被导出到操作系统文件中,其中包括 用户账号、公用同义词、角色及概要文件。
            2、在对象模式的导出中,构成一个对象模式组的所有数据和元数据均被导出
            3、导出中最小粒度的表的导出,导出的数据包括用于一组表的所有数据和元数据
            4、表空间方式导出(也称可传输表空间)同时提取用于一个表空间中所有对象的数据和元数据,另外还提取依赖于指定
                    表空间列表中所有对象的任何依赖对象。
             用DATA PUMP 导出程序所创建的文件称为 转储文件(dump file),而且单个 data pump 导出作业期间可能会创建一个或多个
             转储文件。来自单个 data pump 导出作业的所有导出转储文件总称为转储文件集。
            
           example:
            expdp system/oracle cluster=n compression=all full=y parallel=2 nologfile=y dumpfile=file_name.dmp reuse_dumpfiles=y
            参数:parallel=2 表示导出工作在两个并行度下进行,即会产生两个泵出文件
                        dumpfile  泵出文件的名称。
                        cluster=n 表示仅允许使用一个数据库实例上的进程执行导出操作。
                        compression=all 即 开启转储文件的压缩功能
                        full=y 表示完全模式导出。
                        reuse_dumpfiles=y 表示若目录中存在同名泵出文件,则将其覆盖
                        nologfile=y 表示不必产生一个记录导出过程细节的文本格式日志文件。
                       
          使用 tablespaces 参数 导出表空间内的所有对象
           expdp system/oracle  cluster=n compression=all tablespaces=users,example parallel=2 nologfile=n dumpfile=dumpfile_name.dmp reuse_dumpfiles=y
          使用 schemas 参数导出 test_user和 test_t 两个方案
          expdp system/oracle cluster=n schemas=test_user,test_t parallel=2 nologfile=y dumpfile=exp%U.dmp reuse_dumpfiles=n;
          使用 tables参数导出 tt 和 tc 两表
          expdp system/oracle cluster=n tables=tt,tc parallel=2 nologfile=y dumpfile=exptb%U.dmp reuse_dumpfiles=y;
          使用 query 参数 限制在 test_user.tt 表中被导出的行
          expdp system/oracle cluster=n tables=test_user.tt query=test_user.tt :\"where id\=\123 and city\=\'beijing\'" parallel=2 nologfile=y dumpfile=ex%U.dmp
          reuse_dumpfiles=y
          采用 flashback_time 和 flashback_scn 参数导出与闪回时间点查询相一致的数据。
          expdp system/oracle cluster=n flashback_time=\"systimestamp \-interval \'10\' minute \" tables=test_user.tt,test_tc parallel=2
            nologfile=y dumpfile=expflashback%U.dmp reuse_dumpfiles=y;
          
  导入
        数据泵的导入操作能够选择性的将泵出文件内的各类数据载回到数据库。
        example:
            将a.dmp b.dmp 两个泵出文件中包含的所有对象与数据导入数据库
            impdp system/oracle cluster=n full=y nologfile=y dumpfile=a.dmp,b.dmp;
            将泵出文件中属于特定表空间的所有对象及数据导入数据库。
            impdp system/oracle cluster=n tablespaces=table_space_name nologfile=y dumpfile=a.dmp;
            将泵出文件中的 test_user 方案导入数据库
            impdp system/oracle cluster=n schemas=test_user nologfile=y dumpfile=b.dmp;
            将泵出文件中的表及其相关的对象(索引、触发器、对象权限等)导入数据库
            impdp system/oracle cluster=n tables=test_user.tt nologfile=y dumpfile=a.dmp;
            使用 query 参数将泵出文件中的表及其满足一定条件的行导入数据库
            impdp system/oracle cluster=n tables=test_user.tt query=test_user.tt:\"where id\=12345\" parallel=2
                nologfile=y dumpfile=a.dmp;
            通过设置参数 table_exists_action  的值,做不同的操作
            table_exists_action : skip ---表示表如果若存在则跳过
                                            append --表示表若存在 仅做插入行
                                            truncate --表示表若存在,则将表截断后再插入行货将表删除并重建后再插入行
                                            replace ---将表及一切依赖该表的对象先删除,然后再重建表,依赖该表的对象是否也能回归
                                                        --取决于泵出文件中是否包含该对象。
                    impdp system/oracle cluster=n tables=test_user.tt nologfile=y dumpfile=a.dmp table_exists_action=replace;       
          如果表存在,又想保持原状,则可以采用remap_table 或 remap_schema 参数形式另辟途径,即换一个名或方案创建对象
          impdp system/oracle cluster=n tables=test_user.tt nologfile=y  dumpfile=ex%U.dmp remap_table=tt:ttr exclude=constraint,ref_constraint;
          exclude=constraint,ref_constraint  表示即使泵出文件中有主键、外键等约束也不用导入。 如果表上由非空约束则会导致失败。
          解决方法是用 remap_schema
          impdp system/oracle cluster=n tables=test_user.tt nologfile=y dumpfile=ex%U.dmp remap_schema=test_user:test_other;
         
          将 test_user 方案中的对象在导入时切换到 test_other方案
          impdp system/oracle cluster=n schema=test_user nologfile dumpfile=ex%U.dmp remap_schema=test_user:test_other;
         
 可传输表空间
        数据泵的可传输表空间是一种特殊的导出表空间内对象的方式,该导出操作将产生两类文件:只读数据文件和元数据泵出文件。
           泵出步骤:
                    1、首先需要将导出的表空间设置为只读
                        alter tablespace table_space_name read only;
                    2、然后使用带有  transport_tablespaces 参数的expdp命令将表空间作为可传输表空间导出
                         expdp system/oracle transport_tablespaces= table_space_name nologfile=y dumpfile=extp%U.dmp reues_dumpfiles=y;
                     此时导出文件称为元数据泵导出文件,仅包含 表空间内所有对象的数据字段描述,实际的数据还待在表空间的数据文件中。
                        
                   expdp 还会检查 导出表空间 是否符合自包含性:单向自包含  双向自包含。
                   单向自包含 指导出的表空间内的对象不依赖任何非导出表空间内的任何对象。使用 transport_full_check=n 做检查(默认设置)。
                   双向自包含 指导出的表空间内的对象不依赖任何非导出表空间内的任何对象,并且非导出的表空间内的对象不依赖任何导出
                                        的表空间内的任何对象,使用参数 transport_full_check=y 做检查。
                                       
                  3、 接下来复制表空间下的数据文件作为   数据备份
                     cp /oracledb/testdb/datafile01.dbf /oracledb/expdp/datafile01.tts
                    
                  4、将表空间改为可读/写状态         
                      alter tablespace table_space_name read write;
             
       导出表空间还原
            导入步骤:
                    1、首先删除表空间
                        drop  tablespace table_space_name including contents and datafiles;
                    2、用数据泵可传输表空间的模式导入数据库
                            impdp \'/ as sysdba\' transport_datafiles='oracledb/expdp/datafile01.dbf' nologfile=n dumpfile=tts.dmp;
                    3、将表空间设置为可读/写
                            alter tablespace table_space_name  read write;
  
 
  监控 DATA PUMP 作业进度
    在执行 导入/导出后,可以按 Ctrl+C 组合键使得客户端进入交互模式(会出现 export 提示符),这样导入/导出的日志信息
    就不会显示在终端了。
    可以在提示符 export 后输入 status  就可以看到当前有哪些任务正在运行。如果在客户端输入 expdp attach =任务名称 就可以再次
    连接到正在运行的任务上了。
    使用 DBA_DATAPUMP_JOBS 视图 监视 数据库中所有 DATA PUMP 作业进度。