逻辑备份与恢复(数据泵)
exp/imp的缺点是速度太慢, 在大型生产库中尤其明显。从10g开始, oracle设计了数据泵, 这是一个服务器端的工具(exp/imp生成的文件存放在客户端,而数据泵生成的文件存放于服务端), 它为Oracle数据提供高速并行及大数据的迁移。
imp/exp可以在客户端调用, 但是expdp/impdp只能在服务端, 因为在使用expdp/impdp之前需要在数据库中创建一个Directory(供转储文件和日志文件使用的目录对象)。
在expdp进行导出时,先创建了MT表, 并把对象的信息插入到MT表,之后进行导出动作;导出完成后,MT表也导出到转储文件中;导出任务完成后、或者删除了导出任务后,MT表自动删除;如果导出任务异常终止,MT表仍然保留。
expdp/impdp也具有四种模式:
-
数据库模式:直接导出整个库中的所有对象
-
表空间模式:导出一个或多个表空间中的所有对象
-
用户模式:导出一个用户模式中的所有对象
-
表模式:导出一个或多个指定的表或表分区
一、expdp的重要参数
directory | 供转储文件和日志文件使用的目录对象。 |
job_name | 指定的任务的名称 |
content |
指定要导出的数据, 其中有效关键字值为: (ALL) 导出对象定义及其所有数据 |
reuse_dumpfiles=[y/n] | 如果导出文件已经存在,是否覆盖。 |
compression | 压缩导出文件 |
estimate | 指定估算被导出表所占用磁盘空间分方法,默认值是BLOCKS |
estimate only | 是否只估算导出占用的磁盘空间,而不进行真正的导出,默认是N。 |
exclude | 用于指定执行操作时要排除对象类型或相关对象 |
include | 用于指定执行操作时要包含的对象类型或相关对象 |
query | 导出符合条件的行 |
attch | 连接到现有的作业, 可以用在中断导出任务后重新启动导出任务 |
二、expdp实践
1. 创建目录对象,并赋予用户权限
create directory MY_DIR as '/u01/app/oracle/backupfile'; grant read,write on directory MY_DIR to scott;
2. 导出scott的student和address表
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)"; impdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp -- 恢复测试
3. 导出scott的student和address的表结构,不导出数据
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)" content=metadata_only reuse_dumpfiles=y;
# reuse_dumpfiles : 如果导出文件已经存在,是否覆盖
4. 导出数据,但不导出表结构
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables="(stu,address)" content=data_only reuse_dumpfiles=y;
5. 导出scott和loto用户的所有内容
expdp system/123456@orcl directory=MY_DIR dumpfile=expdp_system.dmp schemas="(scott,loto)";
6. 导出stu表,以及表中的约束,但不导出索引
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables=stu exclude=index;
注: exclude用于排除指定的类型,如不指明exclude,则会导出stu中的所有对象。
7. scott下,导出其他所有表,但不导出stu和address表
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott.dmp exclude=table:"in('STU')" exclude=table:"in('ADDRESS')";
8. 导出student表的sno>1的记录,和address表的sno>2的记录
1. 编辑参数文件expdp1.txt directory=MY_DIR dumpfile=expdp_scott tables=stu,address query=stu:"where sno>1",address:"where sno=2"; 2. 执行expdp命令 expdp scott/tiger@orcl parfile=expdp1.txt
三、 impdp的重要参数
-
content:指定要加载的数据, 其中有效关键字值为:(ALL) ,DATA ONLY和METADATA ONLY
-
estimate:估算所占用磁盘空间分方法.默认值是BLOCKS
-
remap_schema:用于将对象从一个用户下导入到另一个用户下。
-
remap_tablespace:用于将对象从一个表空间下导入到另一个表空间下。
-
remap datafile:用于在不同文件系统的平台间, 切换数据文件路径。
四、impdp实践
1. 导出的scott的用户文件,导入给loto;
注:需要提前给loto用户赋予MY_DIR的读写权限
impdp loto/123456@orcl directory=MY_DIR dumpfile=expdp_scott.dmp tables=student remap_schema=scott:loto;
2. scott的导出文件默认是属于USERS表空间的,当导入到loto用户下也默认是USERS表空间,可以使用remap_tablespaces参数将其导入到指定表空间下。
expdp scott/tiger@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp schemas=scott; impdp loto/123456@orcl directory=MY_DIR dumpfile=expdp_scott1.dmp remap_schema=scott:loto remap_tablespace=users:tb1;