在数据库的管理工作中,难免会遇到使用Export/Import或者是EXPort Data Pump/IMPort Data Pump工具来执行逻辑备份、恢复的场景。在有些时候,我们可能会对产生的dumpfile的内容感兴趣。本文就这两种工具产生的逻辑备份文件分别描述如何获取数据定义语句(Data Definition Language)的脚本?
1 先来看,如何从传统的Export工具产生的dumpfile里获取DDL语句?
首先,导出HR用户的所有对象:
03
uid=1101(oracle) gid=1000(oinstall)
groups
=1000(oinstall),1201(asmdba),1300(dba),1301(oper)
13
drwxrwxr-x 17 root root 4096 May 26 21:20 12419321
15
-rw-r--r-- 1 oracle oinstall 15626 Aug 31 14:28 db.rsp
17
drwxr-x--- 3 oracle oinstall 4096 Aug 31 15:03 oradiag_oracle
19
rac1-> exp hr/hr
file
=exp_hr.dmp log=exp_hr.log owner=hr
21
Export: Release 11.2.0.1.0 - Production on Mon Nov 21 16:50:22 2011
23
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
25
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
27
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
29
Data Mining and Real Application Tes
31
Export
done
in
AL32UTF8 character
set
and AL16UTF16 NCHAR character
set
33
. exporting pre-schema procedural objects and actions
35
. exporting foreign
function
library names
for
user HR
37
. exporting PUBLIC
type
synonyms
39
. exporting private
type
synonyms
41
. exporting object
type
definitions
for
user HR
43
About to
export
HR's objects ...
45
. exporting database links
47
. exporting sequence numbers
55
Export terminated successfully without warnings.
61
drwxrwxr-x 17 root root 4096 May 26 21:20 12419321
63
-rw-r--r-- 1 oracle oinstall 15626 Aug 31 14:28 db.rsp
65
-rw-r--r-- 1 oracle oinstall 24166400 Nov 21 16:51 exp_hr.dmp
67
-rw-r--r-- 1 oracle oinstall 2493 Nov 21 16:51 exp_hr.log
69
drwxr-x--- 3 oracle oinstall 4096 Aug 31 15:03 oradiag_oracle
我们已经顺利导出了一份exp_hr.dmp的dumpfile,那么该如何从该文件里获取DDL语句呢?
我们可以利用传统的导入工具Import附带SHOW=y选项来获取:
001
rac1-> imp hr/hr
file
=exp_hr.dmp log=show_ddl.log show=y
003
Import: Release 11.2.0.1.0 - Production on Mon Nov 21 17:03:26 2011
005
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
007
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
009
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
011
Data Mining and Real Application Tes
013
Export
file
created by EXPORT:V11.02.00 via conventional path
015
import
done
in
AL32UTF8 character
set
and AL16UTF16 NCHAR character
set
017
. importing HR's objects into HR
025
Import terminated successfully without warnings.
033
drwxrwxr-x 17 root root 4096 May 26 21:20 12419321
035
-rw-r--r-- 1 oracle oinstall 15626 Aug 31 14:28 db.rsp
037
-rw-r--r-- 1 oracle oinstall 24166400 Nov 21 16:51 exp_hr.dmp
039
-rw-r--r-- 1 oracle oinstall 2493 Nov 21 16:51 exp_hr.log
041
drwxr-x--- 3 oracle oinstall 4096 Aug 31 15:03 oradiag_oracle
043
-rw-r--r-- 1 oracle oinstall 28766 Nov 21 17:03 show_ddl.log
045
rac1->
head
-30 show_ddl.log
047
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
049
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
051
Data Mining and Real Application Tes
053
Export
file
created by EXPORT:V11.02.00 via conventional path
055
import
done
in
AL32UTF8 character
set
and AL16UTF16 NCHAR character
set
057
. importing HR's objects into HR
061
"sys.dbms_logrep_imp.instantiate_schema(schema_name=>SYS_CONTEXT('USERENV','"
063
"CURRENT_SCHEMA'), export_db_name=>'DEVDB', inst_scn=>'41155490');"
067
"CREATE TYPE "
MY_TEST_TYPE
" TIMESTAMP '2011-10-12:09:59:12' OID 'AF115DDE620"
069
"59E4AE04010ACBF000672' as object(id number,name varchar2(10));"
071
"CREATE SEQUENCE "
LOCATIONS_SEQ
" MINVALUE 1 MAXVALUE 9900 INCREMENT BY 100 S"
073
"TART WITH 3300 NOCACHE NOORDER NOCYCLE"
075
"CREATE SEQUENCE "
DEPARTMENTS_SEQ
" MINVALUE 1 MAXVALUE 9990 INCREMENT BY 10 "
077
"START WITH 280 NOCACHE NOORDER NOCYCLE"
079
"CREATE SEQUENCE "
EMPLOYEES_SEQ
" MINVALUE 1 MAXVALUE 99999999999999999999999"
081
"99999 INCREMENT BY 1 START WITH 207 NOCACHE NOORDER NOCYCLE"
083
"CREATE CLUSTER "
DEPT_EMP_CLUSTER
" ("
DEPARTMENT_ID
" NUMBER(4, 0)) PCTFREE 1"
085
"0 PCTUSED 40 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 262144 NEXT 1048576 MI"
087
"NEXTENTS 1 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "
U"
091
"CREATE INDEX "
IDX_DEPT_EMP_CLUSTER
" ON CLUSTER "
DEPT_EMP_CLUSTER
" PCTFREE 1"
093
"0 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 F"
095
"REELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "
USERS
""
097
"CREATE TABLE "
BIGFILE_TAB
" ("
ID
" NUMBER, "
NAME
" VARCHAR2(10)) PCTFREE 10 P"
099
"CTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXT"
101
"ENTS 1 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "
BIG_T"
从上,我们可以看到,我们在IMPORT命令行上带上SHOW=Y的选项时,IMPORT工具并没有真正的执行数据导入,而是生成了一份含有DDL语句的日志文件,我们可以直接编辑该日志文件,以获取我们所需的脚本。
2 那么,对于EXPDP产生的dumpfile,又该如何获取DDL语句呢?
先以EXPDP导出dumpfile:
03
/u01/app/oracle/mig_dir
09
rac1-> expdp hr/hr directory=mig_dir dumpfile=expdp_hr.dmp logfile=expdp_hr.log schemas=hr
11
Export: Release 11.2.0.1.0 - Production on Mon Nov 21 17:16:45 2011
13
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
15
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
17
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
19
Data Mining and Real Application Testing options
23
Job
"HR"
.
"SYS_EXPORT_SCHEMA_01"
successfully completed at 17:19:48
29
-rw-r----- 1 oracle asmadmin 24797184 Nov 21 17:19 expdp_hr.dmp
31
-rw-r--r-- 1 oracle asmadmin 3299 Nov 21 17:19 expdp_hr.log
该如何从expdp_hr.dmp文件里获取DDL语句呢?IMPDP工具给我们提供了SQLFILE的命令行选项,同样是只获取DDL语句,并未真正的执行数据导入。
01
rac1-> impdp hr/hr directory=mig_dir dumpfile=expdp_hr.dmp logfile=impdp_hr.log schemas=hr sqlfile=hr_ddl.sql
03
Import: Release 11.2.0.1.0 - Production on Mon Nov 21 17:23:23 2011
05
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
07
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
09
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
11
Data Mining and Real Application Testing options
13
Master table
"HR"
.
"SYS_SQL_FILE_SCHEMA_01"
successfully loaded/unloaded
19
Job
"HR"
.
"SYS_SQL_FILE_SCHEMA_01"
successfully completed at 17:23:47
25
-rw-r----- 1 oracle asmadmin 24797184 Nov 21 17:19 expdp_hr.dmp
27
-rw-r--r-- 1 oracle asmadmin 3299 Nov 21 17:19 expdp_hr.log
29
-rw-r--r-- 1 oracle asmadmin 103892 Nov 21 17:23 hr_ddl.sql
31
-rw-r--r-- 1 oracle asmadmin 1672 Nov 21 17:23 impdp_hr.log
33
rac1->
head
-30 hr_ddl.sql
37
ALTER SESSION SET EVENTS
'10150 TRACE NAME CONTEXT FOREVER, LEVEL 1'
;
39
ALTER SESSION SET EVENTS
'10904 TRACE NAME CONTEXT FOREVER, LEVEL 1'
;
41
ALTER SESSION SET EVENTS
'25475 TRACE NAME CONTEXT FOREVER, LEVEL 1'
;
43
ALTER SESSION SET EVENTS
'10407 TRACE NAME CONTEXT FOREVER, LEVEL 1'
;
45
ALTER SESSION SET EVENTS
'10851 TRACE NAME CONTEXT FOREVER, LEVEL 1'
;
47
ALTER SESSION SET EVENTS
'22830 TRACE NAME CONTEXT FOREVER, LEVEL 192 '
;
49
-- new object
type
path: SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
53
sys.dbms_logrep_imp.instantiate_schema(schema_name=>SYS_CONTEXT(
'USERENV'
,
'CURRENT_SCHEMA'
), export_db_name=>
'DEVDB'
, inst_scn=>
'41161309'
);
61
-- new object
type
path: SCHEMA_EXPORT/TYPE/TYPE_SPEC
63
CREATE TYPE
"HR"
.
"MY_TEST_TYPE"
65
OID
'AF115DDE62059E4AE04010ACBF000672'
as object(
id
number,name varchar2(10));
69
ALTER TYPE
"HR"
.
"MY_TEST_TYPE"
73
PLSQL_OPTIMIZE_LEVEL= 2
75
PLSQL_CODE_TYPE= INTERPRETED
77
PLSQL_DEBUG= FALSE PLSCOPE_SETTINGS=
'IDENTIFIERS:NONE'
81
-- new object
type
path: SCHEMA_EXPORT/SEQUENCE/SEQUENCE
83
CREATE SEQUENCE
"HR"
.
"LOCATIONS_SEQ"
MINVALUE 1 MAXVALUE 9900 INCREMENT BY 100 START WITH 3300 NOCACHE NOORDER NOCYCLE ;
最后,简单一句话总结:
对于使用传统工具EXPORT导出的dumpfile ,我们可以使用IMPORT加上SHOW=Y的选项获取DDL ;
对于使用数据泵EXPDP导出的dumpfile ,我们可以使用IMPDP加上SQLFILE的选项获取DDL