用数据泵技术实现逻辑备份
from:https://blog.csdn.net/weixin_41078837/article/details/80618916
逻辑备份概述
逻辑备份时创建数据库对象的逻辑副本,并存入一个二进制转储文件的过程。从本质上来讲逻辑备份与恢复实际就是对数据库事实数据的导入和导出。
导出:
导出就是数据库的逻辑备份,实质是读取一个数据库记录并将这个记录集写入一个文件(扩展名通常是dmp),这些记录的导出与物理位置无关
导入:
导入即数据库的逻辑恢复,实质是读取被导出的二进制转储文件并将其恢复到数据库。
使用数据泵技术导入/导出
数据泵(DATA PUMP)是一种在数据库之间或在数据库与操作系统之间高速传输数据的技术。数据泵工具运行在服务器上,数据库管理员需要指定数据库目录来保存转储的数据。
连接oracle 数据库
创建一个操作目录
Default Locations for Dump, Log, and SQL Files
Because Data Pump is server-based rather than client-based, dump files, log files, and SQL files are accessed relative to server-based directory paths. Data Pump requires that directory paths be specified as directory objects. A directory object maps a name to a directory path on the file system. DBAs must ensure that only approved users are allowed access to the directory object associated with the directory path.
The following example shows a SQL statement that creates a directory object named dpump_dir1
that is mapped to a directory located at /usr/apps/datafiles.
SQL> CREATE DIRECTORY dpump_dir1 AS '/usr/apps/datafiles';
The reason that a directory object is required is to ensure data security and integrity. For example:
-
If you were allowed to specify a directory path location for an input file, then you might be able to read data that the server has access to, but to which you should not.
-
If you were allowed to specify a directory path location for an output file, then the server might overwrite a file that you might not normally have privileges to delete.
On UNIX and Windows operating systems, a default directory object, DATA_PUMP_DIR
, is created at database creation or whenever the database dictionary is upgraded. By default, it is available only to privileged users. (The user SYSTEM
has read and write access to the DATA_PUMP_DIR
directory, by default.)
If you are not a privileged user, then before you can run Data Pump Export or Data Pump Import, a directory object must be created by a database administrator (DBA) or by any user with the CREATE
ANY
DIRECTORY
privilege.
After a directory is created, the user creating the directory object must grant READ
or WRITE
permission on the directory to other users. For example, to allow the Oracle database to read and write files on behalf of user hr
in the directory named by dpump_dir1
, the DBA must execute the following command:
SQL> GRANT READ, WRITE ON DIRECTORY dpump_dir1 TO hr;
Note that READ
or WRITE
permission to a directory object only means that the Oracle database can read or write files in the corresponding directory on your behalf. You are not given direct access to those files outside of the Oracle database unless you have the appropriate operating system privileges. Similarly, the Oracle database requires permission from the operating system to read and write files in the directories.
Data Pump Export and Import use the following order of precedence to determine a file's location:
-
If a directory object is specified as part of the file specification, then the location specified by that directory object is used. (The directory object must be separated from the file name by a colon.)
-
If a directory object is not specified as part of the file specification, then the directory object named by the
DIRECTORY
parameter is used. -
If a directory object is not specified as part of the file specification, and if no directory object is named by the
DIRECTORY
parameter, then the value of the environment variable,DATA_PUMP_DIR
, is used. This environment variable is defined using operating system commands on the client system where the Data Pump Export and Import utilities are run. The value assigned to this client-based environment variable must be the name of a server-based directory object, which must first be created on the server system by a DBA. For example, the following SQL statement creates a directory object on the server system. The name of the directory object isDUMP_FILES1
, and it is located at'/usr/apps/dumpfiles1'
.SQL> CREATE DIRECTORY DUMP_FILES1 AS '/usr/apps/dumpfiles1';
Then, a user on a UNIX-based client system using
csh
can assign the valueDUMP_FILES1
to the environment variableDATA_PUMP_DIR
. TheDIRECTORY
parameter can then be omitted from the command line. The dump fileemployees.dmp
, and the log fileexport.log
, are written to'/usr/apps/dumpfiles1'
.%setenv DATA_PUMP_DIR DUMP_FILES1 %expdp hr TABLES=employees DUMPFILE=employees.dmp
-
If none of the previous three conditions yields a directory object and you are a privileged user, then Data Pump attempts to use the value of the default server-based directory object,
DATA_PUMP_DIR
. This directory object is automatically created at database creation or when the database dictionary is upgraded. You can use the following SQL query to see the path definition forDATA_PUMP_DIR
:SQL> SELECT directory_name, directory_path FROM dba_directories 2 WHERE directory_name='DATA_PUMP_DIR';
If you are not a privileged user, then access to the
DATA_PUMP_DIR
directory object must have previously been granted to you by a DBA.Do not confuse the default
DATA_PUMP_DIR
directory object with the client-based environment variable of the same name.
授予用户操作dump_dir目录的权限
使用EXPDP命令导出数据(可以按照表导出,按照用户模式导出,按照表空间导出和全库导出),使用IMPDP命令导入数据(可以按照表导入,按照用户模式导入,按照表空间导出和全库导入)。
使用基于命令行的数据泵技术对数据库实施导入和导出。
SQL>col DIRECTORY_NAME for a20
SQL>col DIRECTOR_PATH for a60
SQL>col OWNER for a8
创建测试用户tom并授权
导出SCOTT用户下的emp和dept表
[oracle@dbserver~]$ expdp scott/oracle directory=dump_dir dumpfile=scotttab.dmp tables=emp,dept
以SCOTT用户连接,删除SCOTT用户下的EMP表
导入emp表
[oracle@dbserver~]$ impdp scott/oracle directory=dump_dir dumpfile=scotttab.dmp tables=emp
emp表已经导入成功了。
将导出的SCOTT用户下的DEPT表和EMP表导入到tom用户下
[oracle@dbserver~]$ impdp system/oracle11g directory=dump_dir dumpfile=scotttab.dmptables=scott.emp,scott.dept REMAP_SCHEMA=SCOTT:TOM
查看导入的结果,使用tom用户连接
导出表空间
在xx表空间上创建一个表aa,并为表插入记录
导出表空间
删除表空间xx的同时删除数据文件
aa表没有了。
导入表空间
导入表空间之前,需要创建一个xx表空间
导入表空间xx
验证,aa表恢复回来了。
导出全库
[oracle@dbserverorcl]$ expdp system/oracle11g directory=dump_dir dumpfile=full.dmp full=y
导入全库
[oracle@dbserverorcl]$ impdp system/oracle11g directory=dump_dir dumpfile=full.dmp full=y
expdp impdp 数据库导入导出命令详解
create directory dpdata1 as 'd:\test\dump';
二、查看管理理员目录(同时查看操作系统是否存在,因为Oracle并不关心该目录是否存在,如果不存在,则出错)
select * from dba_directories;
三、给scott用户赋予在指定目录的操作权限,最好以system等管理员赋予。
grant read,write on directory dpdata1 to scott;
四、导出数据
1)按用户导
expdp scott/tiger@orcl schemas=scott dumpfile=expdp.dmp DIRECTORY=dpdata1;
2)并行进程 parallel
expdp scott/tiger@orcl directory=dpdata1 dumpfile=scott3.dmp parallel=40 job_name=scott3
3)按表名导
expdp scott/tiger@orcl TABLES=emp,dept dumpfile=expdp.dmp DIRECTORY=dpdata1;
4)按查询条件导
expdp scott/tiger@orcl directory=dpdata1 dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
5)按表空间导
expdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=temp,example;
6)导整个数据库
expdp system/manager DIRECTORY=dpdata1 DUMPFILE=full.dmp FULL=y;
五、还原数据
1)导到指定用户下
impdp scott/tiger DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=scott;
2)改变表的 owner
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
3)导入表空间
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=example;
4)导入数据库
impdb system/manager DIRECTORY=dump_dir DUMPFILE=full.dmp FULL=y;
5)追加数据
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION=append;
1. ATTACH
该选项用于在客户会话与已存在导出作用之间建立关联.语法如下
ATTACH=[schema_name.]job_name
Schema_name 用于指定方案名,job_name用于指定导出作业名.注意,如果使用ATTACH选项,在命令行除了连接字符串和ATTACH选项外,不能指定任何其他选项,示例如下 :
Expdp scott/tiger ATTACH=scott.export_job
2. CONTENT
该选项用于指定要导出的内容.默认值为 ALL
CONTENT={ALL | DATA_ONLY | METADATA_ONLY}
当设置CONTENT为ALL 时,将导出对象定义及其所有数据.为DATA_ONLY时,只导出对象数据,为METADATA_ONLY时,只导出对象定义
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dump
CONTENT=METADATA_ONLY
3. DIRECTORY
指定转储文件和日志文件所在的目录
DIRECTORY=directory_object
Directory_object 用于指定目录对象名称.需要注意,目录对象是使用CREATE DIRECTORY语句建立的对象,而不是OS 目录
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dump
建立目录 :
CREATE DIRECTORY dump as ‘d:dump’;
查询创建了那些子目录 :
SELECT * FROM dba_directories;
4. DUMPFILE
用于指定转储文件的名称,默认名称为 expdat.dmp
DUMPFILE=[directory_object:]file_name [,….]
Directory_object用于指定目录对象名,file_name用于指定转储文件名.需要注意,如果不指定directory_object,导出工具会自动使用DIRECTORY选项指定的目录对象
Expdp scott/tiger DIRECTORY=dump1 DUMPFILE=dump2:a.dmp
5. ESTIMATE
指定估算被导出表所占用磁盘空间分方法.默认值是 BLOCKS
EXTIMATE={BLOCKS | STATISTICS}
设置为BLOCKS时,oracle会按照目标对象所占用的数据块个数乘以数据块尺寸估算对象占用的空间,设置为STATISTICS时,根据最近统计值估算对象占用空间
Expdp scott/tiger TABLES=emp ESTIMATE=STATISTICS
DIRECTORY=dump DUMPFILE=a.dump
6. EXTIMATE_ONLY
指定是否只估算导出作业所占用的磁盘空间,默认值为 N
EXTIMATE_ONLY={Y | N}
设置为Y时,导出作用只估算对象所占用的磁盘空间,而不会执行导出作业,为N时,不仅估算对象所占用的磁盘空间,还会执行导出操作 .
Expdp scott/tiger ESTIMATE_ONLY=y NOLOGFILE=y
7. EXCLUDE
该选项用于指定执行操作时释放要排除对象类型或相关对象
EXCLUDE=object_type[:name_clause] [,….]
Object_type 用于指定要排除的对象类型,name_clause用于指定要排除的具体对象.EXCLUDE和INCLUDE不能同时使用
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dup EXCLUDE=VIEW
8. FILESIZE
指定导出文件的最大尺寸,默认为0,(表示文件尺寸没有限制 )
9. FLASHBACK_SCN
指定导出特定SCN时刻的表数据
FLASHBACK_SCN=scn_value
Scn_value 用于标识SCN值.FLASHBACK_SCN和FLASHBACK_TIME不能同时使用
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dmp
FLASHBACK_SCN=358523
10. FLASHBACK_TIME
指定导出特定时间点的表数据
FLASHBACK_TIME=”TO_TIMESTAMP(time_value)”
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dmp FLASHBACK_TIME=
“TO_TIMESTAMP(’25-08-2004 14:35:00’,’DD-MM-YYYY HH24:MI:SS’)”
指定数据库模式导出,默认为 N
FULL={Y | N}
为Y时,标识执行数据库导出 .
12. HELP
指定是否显示EXPDP命令行选项的帮助信息,默认为 N
当设置为Y时,会显示导出选项的帮助信息 .
Expdp help=y
13. INCLUDE
指定导出时要包含的对象类型及相关对象
INCLUDE = object_type[:name_clause] [,… ]
14. JOB_NAME
指定要导出作用的名称,默认为 SYS_XXX
JOB_NAME=jobname_string
15. LOGFILE
指定导出日志文件文件的名称,默认名称为 export.log
LOGFILE=[directory_object:]file_name
Directory_object用于指定目录对象名称,file_name用于指定导出日志文件名.如果不指定directory_object.导出作用会自动使用DIRECTORY的相应选项值 .
Expdp scott/tiger DIRECTORY=dump DUMPFILE=a.dmp logfile=a.log
16. NETWORK_LINK
指定数据库链名,如果要将远程数据库对象导出到本地例程的转储文件中,必须设置该选项 .
17. NOLOGFILE
该选项用于指定禁止生成导出日志文件,默认值为 N.
18. PARALLEL
指定执行导出操作的并行进程个数,默认值为 1
19. PARFILE
指定导出参数文件的名称
PARFILE=[directory_path] file_name
20. QUERY
用于指定过滤导出数据的where条件
QUERY=[schema.] [table_name:] query_clause
Schema 用于指定方案名,table_name用于指定表名,query_clause用于指定条件限制子句.QUERY选项不能与 CONNECT=METADATA_ONLY,EXTIMATE_ONLY,TRANSPORT_TABLESPACES等选项同时使用 .
Expdp scott/tiger directory=dump dumpfiel=a.dmp
Tables=emp query=’WHERE deptno=20’
21. SCHEMAS
该方案用于指定执行方案模式导出,默认为当前用户方案 .
22. STATUS
指定显示导出作用进程的详细状态,默认值为 0
23. TABLES
指定表模式导出
TABLES=[schema_name.]table_name[:partition_name][,…]
Schema_name 用于指定方案名,table_name用于指定导出的表名,partition_name用于指定要导出的分区名 .
24. TABLESPACES
指定要导出表空间列表
25. TRANSPORT_FULL_CHECK
该选项用于指定被搬移表空间和未搬移表空间关联关系的检查方式,默认为 N.
当设置为Y时,导出作用会检查表空间直接的完整关联关系,如果表空间所在表空间或其索引所在的表空间只有一个表空间被搬移,将显示错误信息.当设置为N时, 导出作用只检查单端依赖,如果搬移索引所在表空间,但未搬移表所在表空间,将显示出错信息,如果搬移表所在表空间,未搬移索引所在表空间,则不会显示错误信息 .
26. TRANSPORT_TABLESPACES
指定执行表空间模式导出
27. VERSION
指定被导出对象的数据库版本,默认值为 COMPATIBLE.
VERSION={COMPATIBLE | LATEST | version_string}
为COMPATIBLE时,会根据初始化参数COMPATIBLE生成对象元数据;为LATEST时,会根据数据库的实际版本生成对象元数据.version_string用于指定数据库版本字符串.调用 EXPDP
使用EXPDP工具时,其转储文件只能被存放在DIRECTORY对象对应的OS目录中,而不能直接指定转储文件所在的OS目录.因此 ,
使用EXPDP工具时,必须首先建立DIRECTORY对象.并且需要为数据库用户授予使用DIRECTORY对象权限.
1.Data Pump 导出 expdp
例子:
sql>create directory dpdata1 as '/u02/dpdata1';
sql>grant read, write on directory dpdata1 to ananda;
$expdp ananda/abc123 tables=CASES directory=DPDATA1 dumpfile=expCASES.dmp job_name=CASES_EXPORT
$expdp ananda/abc123 tables=CASES directory=DPDATA1
dumpfile=expCASES_%U.dmp parallel=4 job_name=Cases_Export
include/exclude 例子:
include=table:"in('DB','TS')"
或者 include=table:"like '%E%'"
或者 include=function,package,procedure,table:"='EMP'"
或者 exclude=SEQUENCE,TABLE:"IN ('EMP','DEPT')"
2.Data Pump 导入 expdp
1) 从expdp中获取数据源 exp.dmp
2)复制某个数据库中的一个schema到另一个数据库中。
3) 在同一个数据库中把一个schema中所有的对象复制到另一个schema中。
例子:
1)impdp 的数据源是expdp 导出来的DMP文件
impdp ananda/abc123 directory=dpdata1 dumpfile=expCASES.dmp job_name=cases_import
2) 复制某个数据库中的一个schema到另一个数据库中。
--1.newwork_link 为目标数据库建立的database_link,
( 用户test 需要 grant exp_full_database to TEST; )
create public database link TOLINK
connect to TEST identified by oracle
using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.20.199)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)';
--2.impdp在目标数据库服务器上执行 只能低版本向高版本 imp
impdp network_link=TOLINK schemas=test remap_schema=test:link2
3) 在同一个数据库中把一个schema中所有的对象复制到另一个schema中。
--1. 创建连接自己的 database link:
create public database link system_self connect to system identified by "system" using 'orcl';
数据库链接已创建。
--2. 复制hr schema到 test schema:
impdp system/system network_link=system_self schemas=hr remap_schema=hr:test