一、EXP实现逻辑备份
EXP/IMP是Oracle比较传统的数据库逻辑备份工具,实现全库或表空间的逻辑备份,不支持用户的交互模式,基于客户端。
EXP导出方式
a)传统路径方式:采用SQL语句把要导出的表的数据检索出来,通过网络将这些数据传输到客户端,并生成转储文件。既然是SQL语句的方式,就需要在shared_pool中解析生成执行计划以后,将数据缓存到buffer中,然后传到客户端,该方式是默认的导出方式。
b)直接路径方式:直接获得数据块,并绕过shared_pool和buffer cache,将数据块里的数据直接抽出来后直接传递到客户端上,速度较快。导出时需加上direct=y,有一些限制,不能导出含有LOB,BFILE等大对象的表。
EXP指令
在Windows平台,可以输入EXP HELP=Y,查看帮助信息。
D:\backup>exp help=y
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 09:06:54 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
You can let Export prompt you for parameters by entering the EXP
command followed by your username/password:
Example: EXP SCOTT/TIGER
Or, you can control how Export runs by entering the EXP command followed
by various arguments. To specify parameters, you use keywords:
Format: EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)
or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
USERID must be the first parameter on the command line.
Keyword Description (Default) Keyword Description (Default)
--------------------------------------------------------------------------
USERID username/password FULL export entire file (N)
BUFFER size of data buffer OWNER list of owner usernames
FILE output files (EXPDAT.DMP) TABLES list of table names
COMPRESS import into one extent (Y) RECORDLENGTH length of IO record
GRANTS export grants (Y) INCTYPE incremental export type
INDEXES export indexes (Y) RECORD track incr. export (Y)
DIRECT direct path (N) TRIGGERS export triggers (Y)
LOG log file of screen output STATISTICS analyze objects (ESTIMATE)
ROWS export data rows (Y) PARFILE parameter filename
CONSISTENT cross-table consistency(N) CONSTRAINTS export constraints (Y)
OBJECT_CONSISTENT transaction set to read only during object export (N)
FEEDBACK display progress every x rows (0)
FILESIZE maximum size of each dump file
FLASHBACK_SCN SCN used to set session snapshot back to
FLASHBACK_TIME time used to get the SCN closest to the specified time
QUERY select clause used to export a subset of a table
RESUMABLE suspend when a space related error is encountered(N)
RESUMABLE_NAME text string used to identify resumable statement
RESUMABLE_TIMEOUT wait time for RESUMABLE
TTS_FULL_CHECK perform full or partial dependency check for TTS
TABLESPACES list of tablespaces to export
TRANSPORT_TABLESPACE export transportable tablespace metadata (N)
TEMPLATE template name which invokes iAS mode export
Export terminated successfully without warnings.
D:\backup>
演示基于EXP的数据导出
1)EXP导出整个数据库
D:\backup>exp full=y file=wholedb.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:07:47 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export the entire database ...
. exporting tablespace definitions
. exporting profiles
. exporting user definitions
. exporting roles
. exporting resource costs
. exporting rollback segment definitions
. exporting database links
. exporting sequence numbers
. exporting directory aliases
. exporting context namespaces
. exporting foreign function library names
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions
. exporting system procedural objects and actions
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
AMERICAN_AMERICA.AL32UTF8
----------------------中间过程省略-----------------------------
. exporting synonyms
. exporting views
. exporting referential integrity constraints
. exporting stored procedures
. exporting operators
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting triggers
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting user history table
. exporting default and system auditing options
. exporting statistics
Export terminated successfully without warnings.
2)EXP导出全库,不包括数据
D:\backup>exp full=y rows=n file=wholedb_without_data.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:14:32 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
Note: table data (rows) will not be exported
About to export the entire database ...
. exporting tablespace definitions
. exporting profiles
. exporting user definitions
. exporting roles
. exporting resource costs
----------------------中间过程省略-----------------------------
注:参数rows=n,表示只导出数据块不包括数据
3)EXP指令导出特定的表
D:\backup>exp tables=scott.emp,scott.dept file=scott.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:22:47 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
Current user changed to SCOTT
. . exporting table EMP 14 rows exported
. . exporting table DEPT 5 rows exported
Export terminated successfully without warnings.
4)EXP指令导出当前用户的表
D:\backup>exp scott/scott tables=dept,emp file=current_user_data.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:27:07 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table DEPT 5 rows exported
. . exporting table EMP 14 rows exported
Export terminated successfully without warnings.
5)EXP指令导出指定的用户
D:\backup>exp owner=scott file=scott.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:28:41 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table DEPT 5 rows exported
. . exporting table EMP 14 rows exported
. . exporting table SALGRADE 5 rows exported
. . exporting table T_COMMISSION_BALANCE 25298 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.
6)EXP导出指定的表空间
D:\backup>exp tablespaces=users file=user_ts.dmp
Export: Release 11.2.0.1.0 - Production on Tue Jun 27 12:38:28 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export selected tablespaces ...
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table DEPT 5 rows exported
. . exporting table EMP 14 rows exported
. . exporting table SALGRADE 5 rows exported
. . exporting table T_COMMISSION_BALANCE 25298 rows exported
. . exporting table CATEGORIES_TAB 22 rows exported
. . exporting table PRODUCT_REF_LIST_NESTEDTAB 288 rows exported
. . exporting table SUBCATEGORY_REF_LIST_NESTEDTAB 21 rows exported
. . exporting table PURCHASEORDER 132 rows exported
. . exporting table EMP 14 rows exported
. . exporting table T1 100 rows exported
. . exporting table TEST 2 rows exported
. . exporting table T_FLASH 100 rows exported
. exporting referential integrity constraints
. exporting triggers
Export terminated successfully without warnings.
二、IMP实现逻辑恢复
在执行导入命令的过程中,按下面的顺序导入:
创建表结构
导入表数据
创建索引
导入触发器
对导入的表启用完整性约束
建立索引
通过IMP HELP=Y查看该命令的帮助信息
D:\backup>imp help=y
Import: Release 11.2.0.1.0 - Production on Tue Jun 27 13:37:40 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
You can let Import prompt you for parameters by entering the IMP
command followed by your username/password:
Example: IMP SCOTT/TIGER
Or, you can control how Import runs by entering the IMP command followed
by various arguments. To specify parameters, you use keywords:
Format: IMP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N
or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
USERID must be the first parameter on the command line.
Keyword Description (Default) Keyword Description (Default)
--------------------------------------------------------------------------
USERID username/password FULL import entire file (N)
BUFFER size of data buffer FROMUSER list of owner usernames
FILE input files (EXPDAT.DMP) TOUSER list of usernames
SHOW just list file contents (N) TABLES list of table names
IGNORE ignore create errors (N) RECORDLENGTH length of IO record
GRANTS import grants (Y) INCTYPE incremental import type
INDEXES import indexes (Y) COMMIT commit array insert (N)
ROWS import data rows (Y) PARFILE parameter filename
LOG log file of screen output CONSTRAINTS import constraints (Y)
DESTROY overwrite tablespace data file (N)
INDEXFILE write table/index info to specified file
SKIP_UNUSABLE_INDEXES skip maintenance of unusable indexes (N)
FEEDBACK display progress every x rows(0)
TOID_NOVALIDATE skip validation of specified type ids
FILESIZE maximum size of each dump file
STATISTICS import precomputed statistics (always)
RESUMABLE suspend when a space related error is encountered(N)
RESUMABLE_NAME text string used to identify resumable statement
RESUMABLE_TIMEOUT wait time for RESUMABLE
COMPILE compile procedures, packages, and functions (Y)
STREAMS_CONFIGURATION import streams general metadata (Y)
STREAMS_INSTANTIATION import streams instantiation metadata (N)
DATA_ONLY import only data (N)
The following keywords only apply to transportable tablespaces
TRANSPORT_TABLESPACE import transportable tablespace metadata (N)
TABLESPACES tablespaces to be transported into database
DATAFILES datafiles to be transported into database
TTS_OWNERS users that own data in the transportable tablespace set
Import terminated successfully without warnings.
演示基于IMP的数据导入
1)恢复特定的表
D:\backup>imp tables=emp fromuser=scott touser=scott file=scott.dmp
Import: Release 11.2.0.1.0 - Production on Tue Jun 27 13:55:28 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. . importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
2)IMP指令恢复指定的用户
D:\backup>imp ignore=y fromuser=scott touser=scott file=scott.dmp
Import: Release 11.2.0.1.0 - Production on Tue Jun 27 14:18:44 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. . importing table "DEPT" 5 rows imported
. . importing table "EMP" 14 rows imported
. . importing table "SALGRADE" 5 rows imported
. . importing table "T_COMMISSION_BALANCE" 25298 rows imported
About to enable constraints...
Import terminated successfully without warnings.
D:\backup>
注意:在导出导入中发现有些表没有导入,原因是这些表为空表,当表为空表时,不分配Segment,以节省空间。
解决办法:
SQL> conn scott/scott@orcl
Connected.
SQL> SELECT 'alter table ' || t.table_name || ' allocate extent'
2 FROM user_tables t
3 WHERE t.num_rows = 0;
'ALTERTABLE'||T.TABLE_NAME||'ALLOCATEEXTENT'
----------------------------------------------------------
alter table BONUS allocate extent
SQL> alter table BONUS allocate extent;
Table altered.
SQL>
D:\backup>imp ignore=y fromuser=scott touser=scott file=scott.dmp
Import: Release 11.2.0.1.0 - Production on Tue Jun 27 14:31:57 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Username: sys as sysdba
Password:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. . importing table "BONUS" 0 rows imported
. . importing table "DEPT" 5 rows imported
. . importing table "EMP" 14 rows imported
. . importing table "SALGRADE" 5 rows imported
. . importing table "T_COMMISSION_BALANCE" 25298 rows imported
About to enable constraints...
Import terminated successfully without warnings.