Oracle逻辑备份工具主要包括以下两类:
- exp导出、imp导入(废弃工具)
- expdp导出、impdp导入
说明:expdp和impdp工具(数据泵)在oracle 10g中引入,oracle 10g以后,oracle公司不在对exp和imp工具提供技术支持(属于废弃工具)。
注意点:
1)exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。
2)expdp和impdp是服务端的工具程序,他们只能在oracle服务端使用,不能在客户端使用。
3)imp只适用于exp导出的文件,不适用于expdp导出文件;impdp只适用于expdp导出的文件,而不适用于exp导出文件。
4)对于10g以上的服务器,使用exp通常不能导出0行数据的空表,而此时必须使用expdp导出。
5)以上工具只能在数据库的open阶段可以使用,否则用不了,备份的只是数据,与物理文件无任何关系,操作的都是二进制文件。
用途:
1)数据库迁移,与平台无关迁移工具。
2)历史数据归档。
3)重新组织表:处理行迁移,回收空间。
4)转移数据。
5)物理备份辅助手段。
使用方式:
1)交互模式;
2)命令行模式(重点):
3)参数模式:
4)图形模式:
使用以上工具进行备份/恢复的必要条件:
- 具有可以使用的逻辑目录(directory)
创建oracle可操作目录对象:
create or replace directory data_pump_dir as ‘/u01/app/oracle/admin/orcl/dpdump’;
查询当前oracle可操作目录对象:
select * from dba_directories;
- 具有导入导出相关权限
--系统权限:
datapump_exp_full_database
datapump_imp_full_database
exp_full_database
imp_full_database
Impdp full database
--对象权限:
grant read,write on directory data_pump_dir to username;
两种重要的备份还原语句示例:
- expdp备份数据库
expdp system/oracle@orcl directory=data_pump_dir dumpfile=expdp_date.dmpdp logfile=expdp_date.log schema=username01,username02 reuse_dumpfiles=y cluster=n compression=all parallel=8 |
- impdp还原数据库
准备工作:
- 创建必要用户并授予必要权限:
create user username identified by password account unlock default tablespace tbs_new temporary tablespace temp profile default; |
grant connect,resource,dba to username; grant read,write on directory data_pump_dir to username; |
2、执行还原操作
impdp system/oracle@orcl directory=data_pump_dir dumpfile=expdp_date.dmpdp logfile=expdp_date.log schema=username01,username02 remap_schema=username01:username01,username02:username02 remap_tablespace=tbs_old:tbs_new transform=oid:n,segment_attributes:n cluster=n parallel=8 |
重要参数解释:
- expdp相关参数
compression:指定导出时是否采用压缩格式,好处在于指定压缩格式后导出文件体积会明显变小,常用方式compression=all;
exclude(排除某些对象)和include(包括哪些对象):这是一对对立参数,不能同时出现在一条语句中,可以指定schema、表、statistics等信息,例如:
导出全库,但不包含sys和system用户下schema可以指定:full=y exclude=sys,system
不导出统计信息可以指定:exclude=statistics
导出fasp和efmis用书可以指定:include=fasp,efmis 或者 schemas=fasp,schema
network_link:可以直接导出远程数据库对象数据到本地,需要结合db link使用。
parallel:指定任务并行度,并行对不应该大于cpu核数。
schemas:指定要导出的方案,如果为多个方案中间用逗号分隔。
reuse_dumpfiles:导出dump文件如果存在,是否替换,默认为n表示不替换,可以指定值为y。
- impdp相关参数
exclude(排除某些对象)和include(包括哪些对象):这是一对对立参数,不能同时出现在一条语句中,可以指定schema、表、statistics等信息,例如:
导入全库,但不包含sys和system用户下schema可以指定:full=y exclude=sys,system
不导入统计信息可以指定:exclude=statistics
导入fasp和efmis用书可以指定:include=fasp,efmis 或者 schemas=fasp,schema
network_link:可以直接远程数据库导入到本地数据库,中间不产生dump文件,需要结合db link使用。
parallel:指定任务并行度,并行对不应该大于cpu核数。
remap_schema:重映射schema(用户名称)。
remap_tablespace:重映射表空间。
Schemas:需要导入的用户,相对于dump文件中schema而言。
table_exists_action:导入表如果存在采取哪些行动。
transform:处理对象存储属性,常用属性oid和segment_attributes,一般性测试环境需要指定该参数为transform=oid:n,segment_attributes:n。
3)针对于expdp和impdp,还有一些交互式界面监控指令,这里不做说明,详情参考附件。
操作过程中附加注意事项:
imp/impdp注意点:
- 操作对象所属表空间
需要通过create tablespace命令或者remap_tablespace参数指定导入用户对象所属表空间。
- 导入表空间是否满足恢复需要
导入的表空间剩余空间是否能够容纳还原的数据库。
附:
--检查表空间使用率语句: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- select a.tablespace_name, round((a.maxbytes / 1024 / 1024 / 1024), 2) "sum G", round((a.bytes / 1024 / 1024 / 1024), 2) "datafile G", round(((a.bytes - b.bytes) / 1024 / 1024 / 1024), 2) "used G", round(((a.maxbytes - a.bytes + b.bytes) / 1024 / 1024 / 1024), 2) "free G", round(((a.bytes - b.bytes) / a.maxbytes) * 100, 2) "percent_used(%)" from (select tablespace_name, sum(sumnow) bytes, sum(summax) maxbytes from (select t1.tablespace_name, sum(t1.bytes) sumnow, sum(t1.maxbytes) summax from dba_data_files t1 where t1.maxbytes <> 0 group by t1.tablespace_name union all select t2.tablespace_name, sum(t2.bytes) sumnow, sum(t2.bytes) summax from dba_data_files t2 where t2.maxbytes = 0 group by t2.tablespace_name) group by tablespace_name) a, (select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) b where a.tablespace_name = b.tablespace_name order by ((a.bytes - b.bytes) / a.maxbytes) desc; |
--空间不够还原后,添加数据文件语句(DBA权限执行): SQL>alter tablespace [tablespace_name] add datafile [‘/datafile_dir’] size 2g autoextend on next 200m maxsize unlimited; 其中[]中内容需要根据实际情况确定,注意ofm管理的表空间(rac数据库是一个典型的ofm管理的情况)datafile_dir可以不指定,不了解的情况下指定绝对路径肯定没错。 |
exp/expdp注意点:
- 恢复目录磁盘空间是否满足要求:通过如下命令检查磁盘空间使用情况
#df -h
示例
第一阶段:用户级导入、导出
- 准备数据库
首先查看初建数据库包括哪些目录对象:
SQL> select * from dba_directories;
OWNER DIRECTORY_NAME DIRECTORY_PATH
------------------------------ ------------------------------ --------------------------------------------------------------------------------
SYS XMLDIR /u01/app/oracle/product/11.2.0.4/dbhome_1/rdbms/xml
SYS ORACLE_OCM_CONFIG_DIR /u01/app/oracle/product/11.2.0.4/dbhome_1/ccr/hosts/node6/state
SYS DATA_PUMP_DIR /u01/app/oracle/admin/dptest/dpdump/
SYS ORACLE_OCM_CONFIG_DIR2 /u01/app/oracle/product/11.2.0.4/dbhome_1/ccr/state
可以看到初始创建的数据库仅包含4个directory目录对象,其中data_pump_dir是我们最为常用的目录对象,也带有许多的默认特性。如果想要使用自定义的directory对象,我们需要通过create directory命令创建directory对象,同时我们要手动在操作系统层次创建directory对象的物理路径并chown授予oracle可操作的权限,如下:
# mkdir /dptest
# chown -R oracle:oinstall /dptest/
SQL> create or replace directory mydump as '/dptest';
Directory created
再次查看oracle库的directory对象信息:
SQL> select * from dba_directories;
OWNER DIRECTORY_NAME DIRECTORY_PATH
------------------------------ ------------------------------ --------------------------------------------------------------------------------
SYS MYDUMP /dptest
SYS XMLDIR /u01/app/oracle/product/11.2.0.4/dbhome_1/rdbms/xml
SYS ORACLE_OCM_CONFIG_DIR /u01/app/oracle/product/11.2.0.4/dbhome_1/ccr/hosts/node6/state
SYS DATA_PUMP_DIR /u01/app/oracle/admin/dptest/dpdump/
SYS ORACLE_OCM_CONFIG_DIR2 /u01/app/oracle/product/11.2.0.4/dbhome_1/ccr/state
到这里,我们已经准备好了进行逻辑备份可用的目录对象了。根据使用数据泵的必要条件章节我们知道,如果想要成功运行数据泵客户端工具,我们还需要授予相应的权限,例如,我们使用scott用户做expdp导出操作:
SQL> grant read,write on directory mydump to scott;
Grant succeeded
此时,可以进行如下导出示例操作:
1)导出自己数据:
oracle$expdp scott/oracle@dptest directory=mydump dumpfile=scott_201705251346.dmpdp logfile=scott_201705251346.log schemas=scott
2)导出其他用户数据(因为避免权限问题,这里使用system用户):
oracle$expdp system/oracle@dptest directory=mydump dumpfile=user_01_201705251346.dmpdp logfile=user_01_201705251346.log schemas=user_01
3)进行多个用户同时导出(因为避免权限问题,这里使用system用户):进行多用户备份时可以在schemas参数后同时指定多个用户,中间用逗号分隔即可。
oracle$expdp system/oracle@dptest directory=mydump dumpfile=scott-user_01-user_02_20170526.dmpdp logfile=scott-user01-02_20170526_imp.log schemas=scott,user_01,user_02
以下是针对于不同导出方式下的导入操作示例:
- 在新环境还原数据库:
oracle$impdp scott/oracle@dptest directory=mydump dumpfile=scott_201705251346.dmpdp logfile=scott_20170526_imp.log schemas=scott
在新环境下导入数据到新用户下(因为避免权限问题,这里使用system用户):
oracle$impdp system/oracle@dptest directory=mydump dumpfile=scott_201705251346.dmpdp logfile=scott_20170526_imp.log schemas=scott remap_schema=scott:user_01
- 从多用户导出文件中恢复其中一个用户并重命名:
oracle$impdp system/oracle@dptest directory=mydump dumpfile=scott-user_01-user_02_20170526.dmpdp logfile=scott-user_20170526_imp.log schemas=scott remap_schema=scott:user_03
从多用户导出文件中恢复其中两个用户并重命名schema:
oracle$impdp system/oracle@dptest directory=mydump dumpfile=scott-user_01-user_02_20170526.dmpdp logfile=scott-user_20170526_imp.log
schemas=scott,user_01 remap_schema=scott:user_04,user_01:user_05
从多用户导出文件中恢复其中全部用户:
oracle$impdp system/oracle@dptest directory=mydump dumpfile=scott-user_01-user_02_20170526.dmpdp logfile=scott-user_20170526_imp.log
schemas=scott,user_01,user_02 remap_schema=scott:user_11,user_01:user_12,user_02:user_13
数据泵导入、导出监控:
常用监控方式有三种:
A:日志监控:通过tail -f logfile_name查看输出日志方式进行监控。
B:交互界面监控:通过attach参数进入交互界面或者通过ctrl+c组合进入监控界面,通过status参数进行监控。
C:数据字典监控:通过v$session_longops和dba_datapump_jobs视图监控。
以下分别介绍不同监控方式:
- 日志监控
默认运行expdp命令或impdp命令后会在shell界面输出导出、导入日志信息,通过监控日志输出可以了解数据库导出、导入恢复进度;有时,初次expdp、impdp任务交互界面被关闭,而此时我们还需要监控导出、导入进度信息,可以进入directory对象物理目录,通过shell命令tail -f查看恢复进度。例如:
$ cd /dptest/
$ tail -f scott_20170526.log
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."DEPT" 5.929 KB 4 rows
. . exported "SCOTT"."EMP" 8.562 KB 14 rows
. . exported "SCOTT"."SALGRADE" 5.859 KB 5 rows
- 交互式界面监控:
在启动导入导出任务后的日志输出界面下,想要进入交互式界面可以使用ctrl+c组合键,且只有这一种方法。在进入交互式界面后,shell界面会出现
Import>
或者
Expdp>
提示符,在这里我们可以通过help指令查看交互窗口中可以使用的命令,在交互式界面下,我们一般通过status指令进行导入、导出进度监控。
如果此时已经退出了最初导入的shell界面,那么我们可以通过attach参数指定job_name(见下一节)进入交互式界面,例如:
$ expdp system/oracle attach=SYS_EXPORT_FULL_02
......
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Job: SYS_EXPORT_FULL_02 --job名称
Owner: SYSTEM --任务发起用户
Operation: EXPORT --操作类型:impdp或expdp
Creator Privs: TRUE
GUID: 34BE6BC8A1900E13E0530100007FF2CA
Start Time: Wednesday, 08 June, 2016 17:37:24
Mode: FULL
Instance: orcl --数据库实例名称
Max Parallelism: 4 --并行度
EXPORT Job Parameters:
Parameter Name Parameter Value:
CLIENT_COMMAND system/********@orcl directory=data_pump_dir dumpfile=full_20160608.dmpdp logfile=full_20160608.log full=y compression=all parallel=4
COMPRESSION ALL --发起导入、导出任务的语句
State: RUNNING --任务状态
Bytes Processed: 5,827,487,432
Percent Done: 41 --完成任务百分比
Current Parallelism: 4 --当前并行度
Job Error Count: 0
Dump File: /u01/app/oracle/admin/orcl/dpdump/full_20160608.dmpdp --dump文件位置
......
Export> --在这里可以输入status命令,继续监控任务进度。
导入和导出的参数略有不同,其交互界面页略有不同,但是整理思路是相同的。
- 数据字典监控:
Oracle导入、导出任务都会在任务开始前创建一个job名称,如下:
并且,在oracle导入、导出开始并生成job后,导出、导出的job信息会在dba_datapump_jobs视图里自动插入一行数据,用于标记job运行状态,如下:
SQL> select OWNER_NAME,JOB_NAME,OPERATION,STATE,DEGREE from dba_datapump_jobs;
OWNER_NAME JOB_NAME OPERATION STATE DEGREE
------------------------------ ------------------------------ ------------------------------------------------------------ ------------------------------ ----------------------------------------------------------------
SYSTEM SYS_EXPORT_SCHEMA_01 EXPORT RUNNING 2
SYSTEM SYS_EXPORT_SCHEMA_01 EXPORT NOT RUNNING 0
SYSTEM SYS_IMPORT_FULL_01 IMPORT RUNNING 4
SYSTEM SYS_IMPORT_FULL_01 IMPORT NOT RUNNING 0
字段解释:
job_name字段:表示job名称,同上图红色部分名称一致。
state字段:显示的导入、导出任务运行状态:running表示导入导出任务正在执行中,还没有完成;not running表示导入、导出已经完成或者终止,可以结合日志监控信息进行准确判断。
degree字段:表示运行job任务设置的并行度(parallel参数指定的值),默认情况下,仅开启一个工作进程导入、导出数据,可以在启动导入导出任务时通过parallel参数指定并行度,也可以在运行过程中进入交互式界面,通过交互式界面下parallel参数动态调整并行度。
Oracle数据库对于运行的长任务会在v$session_longops视图中生成一条监控信息,我们可以通过这里的信息大致了解已经恢复了多少数据,大约还有多长时间完成任务。
SQL> select SID,
SERIAL#,
OPNAME,
TARGET_DESC,
SOFAR, --到目前为止已经完成工作
TOTALWORK, --总共工作
UNITS,
START_TIME, --任务开始时间
LAST_UPDATE_TIME, --最后一次更新时间
TIME_REMAINING,
ELAPSED_SECONDS,
MESSAGE, --任务消息
USERNAME
from v$session_longops;
SID SERIAL# OPNAME TARGET_DESC SOFAR TOTALWORK UNITS START_TIME LAST_UPDATE_TIME TIME_REMAINING ELAPSED_SECONDS MESSAGE USERNAME
---------- ---------- ---------------------------------------------------------------- -------------------------------- ---------- ---------- -------------------------------- ----------- ---------------- -------------- --------------- ---------- --------------------------------
361 397 SYS_IMPORT_SCHEMA_01 IMPORT 12597 12598 MB 2017/5/27 9:56:29 2017/5/27 10:11:04 0 875 SYS_IMPORT_SCHEMA_01: IMPORT : 12597 out of 12598 MB done SYSTEM
附件1:help说明
1)expdp的帮助说明
C:\Users\TianPan>expdp -help |
2)impdp的帮助说明
C:\Users\TianPan>impdp -help |