目 录CONTENT

文章目录

Oracle数据泵备份(expdp)

Rain
2021-01-18 / 0 评论 / 0 点赞 / 701 阅读 / 4,733 字 / 正在检测是否收录...

Oracle数据泵备份(expdp)

Oracle备份方式主要分为数据泵导出备份、热备份与冷备份三种,今天首先来实践一下数据泵备份与还原。数据泵导出/导入属于逻辑备份,热备份与冷备份都属于物理备份。oracle10g开始推出了数据泵(expdp/impdp),可以使用并行参数选项,因此,相对于传统的exp命令来说,执行效率更高。

一、知晓expdp命令

C:\Users\risen>expdp -help

Export: Release 11.2.0.1.0 - Production on 星期一 1月 18 15:18:25 2021

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

数据泵导出实用程序提供了一种用于在 Oracle 数据库之间传输
数据对象的机制。该实用程序可以使用以下命令进行调用:

   示例: expdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp

您可以控制导出的运行方式。具体方法是: 在 'expdp' 命令后输入
各种参数。要指定各参数, 请使用关键字:

   格式:  expdp KEYWORD=value 或 KEYWORD=(value1,value2,...,valueN)
   示例: expdp scott/tiger DUMPFILE=scott.dmp DIRECTORY=dmpdir SCHEMAS=scott
               或 TABLES=(T1:P1,T1:P2), 如果 T1 是分区表

USERID 必须是命令行中的第一个参数。

二、准备工作

 1、查询路径信息

   查看已经创建的路径信息:

    SELECT * FROM dba_directories;

  2、创建路径

  创建路径需要sys权限,需要有create any directory权限才可以创建路径。

  选项:DIRECTORY=directory_object

  Directory_object用于指定目录对象名称。需要注意,目录对象是使用CREATE DIRECTORY语句建立的对象,而不是OS目录。

  eg: CREATE OR REPLACE directory backup_path AS 'D:\databack\xxxx';; --创建路径名为dackup_path的路径,并指向硬盘的指定位置

  对新创建的路径进行授权操作:

  eg:grant read,write on directory backup_path to xxxx; --将对路径的读写权限分配各xxxx用户。

 三、操作实例

  执行expdp和impdp命令需要拥有exp_full_database和imp_full_database权限,授权语句如下:

  eg:grant exp_full_database,imp_full_database to xxxx;

   1、导出xxxx这个schema的所用对象[schemas or full]

   eg:expdp xxxx/xxxx@orcl directory=backup_path dumpfile=xxxx.dmp logfile=xxxx.log schemas=xxxx

   2、导出xxxx这个用户下的某些表[tables]

   eg:C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log tables=('TAB_TEST','TAB_A')

   3、只导出xxxx这个用户的元数据[content]

   eg:C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log SCHEMAS=orcldev CONTENT=METADATA_ONLY

   4、只导出xxxx这个用户50%的抽样数据[sample]

   eg:C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log schemas=xxxx sample=50

   5、采用并行方式备份整库[parallel]

    parallel参数只有在oracle10g之后的版本(包含10g)有效。

    oracle_online:you can use the DUMPFILE parameter during export operations to specify multiple dump files, by using a substitution variable (%U) in the filename. This is called a dump file template. The new dump files are created as they are needed, beginning with 01 for %U, then using 02,03,and so on.

   eg:C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx%U.dmp logfile=xxxx.log parallel=4

    "%U"表示自动生成递增的序列号。

   6、导出xxxx这个方案对象,但不包含索引[exclude]

   eg: --可以剔除的对象有:VIEW,PACKAGE,FUNCTION,index,constraints,table,schema,user等等

   1) C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log SCHEMAS=xxxx EXCLUDE=index

   2) C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log SCHEMAS=orcldev EXCLUDE=INDEX:"LIKE 'TEST%'" --导出这个xxxx方案,剔除以TEST开头的索引

   3) C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log EXCLUDE=SCHEMA:"='SCOTT'"

    C:>expdp xxxx/xxxx directory=dackup_path dumpfile=xxxx.dmp logfile=xxxx.log EXCLUDE=USER:"='SCOTT'"

   --备份整库但剔除SCOTT这个用户的对象。  

   注意:include与exclude不能同时使用。

 7、PARFILE选项

    expdp命令可以调用parfile文件,在parfile里可以写备份脚本,可以使用query选项。

    Oracle highly recommends that you place QUERY specifications in a parameter file; otherwise, you might have to use operating system-specific escape characters on the command line before each quotation mark.

  如expdp.txt 内容如下:

   USERID=orcldev/oracle directory=dackup_path dumpfile=orcldev_parfile.dmp logfile=orcldev_parfile.log TABLES='TAB_TEST' QUERY="WHERE TRAN_DATE=TO_DATE('2013-08-31','YYYY-MM-DD')"

    执行方法:expdp parfile=expdp.txt 即可执行备份

    使用parfile好处是使用query选项是不用使用转义字符,如果将query参数放到外边的话,需要将""进行转义。

    eg:

    UNIX写法:

    expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log schemas=orcldev INCLUDE=TABLE:"IN ('TEST_A','TEST_B')" --在Unix系统执行是需要将单引号进行转义操作,否则会报错。

    WINDOWS写法:

    expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log schemas=orcldev INCLUDE=TABLE:"IN ('TEST_A','TEST_B')"

   8、TABLESPACE导出表空间

    eg:expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log tablespaces=user,orcldev

   9、Version选项

    VERSION选项默认值是COMPATIBLE,即兼容模式。在我们备份的时候,可以指定版本号。

    eg:expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log full=Y VERSION=10.2.0.4

   10、FLASHBACK_TIME选项

    指定导出特定时间点的表数据,可以联系一下FLASHBACK功能。

    eg:C:>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_flash.dmp logfile=orcldev_flash.log SCHEMAS=orcldev FLASHBACK_TIME="TO_TIMESTAMP('2013-09-28 14:30:00','DD-MM-YYYY HH24:MI:SS')"

四、数据泵还原(IMPDP命令)
http://rainyun.top/archives/oracle数据泵还原impdpmd

0

评论区