第6章 数据泵(Data Pump)

#创建目录
create directory exp_home as '/backup/expdp';

#普通用户需要授权该目录 (还需要datapump_exp_full_database角色)
grant read,write on directory home to hr;

#查询默认目录对象(专为没有dumpfile和directory参数时使用)
#本机查询结果为 /u01/oracle/app/oracle/admin/hisdb/dpdump/
select directory_path from dba_directories where directory_name='DATA_PUMP_DIR';

expdp导出

#完全导出模式至默认目录(导出除了SYS、ORDSYS、MDSYS外的所有)
#cluster=n 仅允许使用一个数据库实例进程执行导出
#compression=all 开启压缩功能
#parallel=2 2个并行度
#full=y 完全导出模式
#nologfile=y 不输出日志
#reuse_dumpfiles=y 目录中存在同名文件则覆盖
expdp system/oracle cluster=n compression=all full=y parallel=2 nologfile=y dumpfile=EX%U.DMP reuse_dumpfiles=y

#导出表空间至exp_home目录
expdp system/oracle cluster=n tablespaces=users parallel=1 nologfile=y directory=exp_home dumpfile=EX%U.DMP reuse_dumpfiles=y

#导出schema至exp_home目录
expdp system/oracle cluster=n schemas=hr parallel=1 nologfile=y directory=exp_home dumpfile=EX%U.DMP reuse_dumpfiles=y

#导出tables至exp_home目录
expdp system/oracle cluster=n tables=hr.employees,hr.departments parallel=2 nologfile=y directory=exp_home dumpfile=EX%U.DMP reuse_dumpfiles=y

#导出闪回SCN 2675814 数据至exp_home目录
expdp system/oracle cluster=n FLASHBACK_SCN=2811362 tables=hr.employees,hr.departments parallel=1 nologfile=y directory=exp_home dumpfile=EX%U.DMP reuse_dumpfiles=y
#查询2024-05-19 14:30:00时间对应的SCN值
SELECT TIMESTAMP_TO_SCN(TO_TIMESTAMP('2024-05-19 14:30:00', 'YYYY-MM-DD HH24:MI:SS')) AS scn_time FROM dual;

#查询10分钟前时间对应的SCN值
SELECT TIMESTAMP_TO_SCN(SYSDATE - INTERVAL '10' MINUTE) AS scn_time FROM dual;

impdp导入

#全库导入,不指定directory时从默认目录中
impdp system/oracle cluster=n full=y nologfile=y dumpfile=EX01.DMP,EX02.DMP

#导入表空间
impdp system/oracle cluster=n tablespaces=example nologfile=y dumpfile=EX01.DMP

#导入schema
impdp system/oracle cluster=n schemas=hr nologfile=y dumpfile=EX01.DMP

#将hr的schema导入到另一个hr2的schema中
impdp system/oracle cluster=n schemas=hr nologfile=y dumpfile=EX01.DMP remap_schema=hr:hr2

#导入表及相关对象(如果导入对象已存在,则直接跳过)
impdp system/oracle cluster=n tables=hr.employees nologfile=y dumpfile=EX01.DMP

#导入表及相关对象,将employees重命名为employees_new
#exclude=constraint,ref_constraint 表示忽略主键、外键、唯一索引等约束避免重名执行失败
impdp system/oracle cluster=n tables=hr.employees nologfile=y dumpfile=EX01.DMP remap_table=employees:employees_new exclude=constraint,ref_constraint

可传输表空间

导出可传输表空间前必须将目标表空间置只读状态,不支持并行度。

#设置EXAMPLE表空间为只读
alter tablespace EXAMPLE read only;

#使用transport_tablespaces参数将users导出为可传输表空间
#导出的TTS.DMP为元数据泵出文件,实际的数据还在表空间的数据文件 example.dbf 中
#transport_full_check=y 自包含性检测
expdp system/oracle transport_tablespaces=EXAMPLE nologfile=y dumpfile=TTS.DMP transport_full_check=y reuse_dumpfiles=y

#复制数据文件
cp /fra/example.dbf /fra/example.dbf.bak

#恢复example表空间为读写状态
alter tablespace example read write;
#如果已存在users表空间,则需先删除,包括数据文件
drop tablespace example including contents and datafiles;

#还原表空间
mv /fra/example.dbf.bak /fra/example.dbf
impdp system/oracle transport_datafiles='/fra/example.dbf' nologfile=y dumpfile=TTS.DMP

#恢复example表空间为读写状态
alter tablespace example read write;

#查询表空间状态,plugged_in为YES表示表空间是导入而非创建的
select tablespace_name,status,plugged_in from dba_tablespaces;

字符集问题

源端目标端字符集需相同

#查询数据库字符集
select userenv('language') from dual;
SELECT VALUE FROM NLS_DATABASE_PARAMETERS WHERE PARAMETER = 'NLS_CHARACTERSET';

#创建xxzx表空间
create tablespace xxzx datafile '/fra/xxzx01.dbf' 
size 100m autoextend on next 50m;

#创建xxzx用户
create user xxzx identified by xxzx temporary tablespace temp default tablespace xxzx
quota unlimited on xxzx;

#授权xxzx
grant dba to xxzx;

#导出
expdp system/oracle transport_tablespaces=EXAMPLE nologfile=y dumpfile=TTS.DMP transport_full_check=y CHARACTERSET=ZHS16GBK reuse_dumpfiles=y

第1章 重做日志(Redo Log)

第2章 控制文件(Control File)

第3章 补充日志(Supplemental Logging)

第4章 恢复管理器(RMAN)

第5章 恢复编录(Recovery Catalog)

第6章 数据泵(Data Pump)

第7章 参数文件的备份与还原

第8章 控制文件的备份与恢复

第9章 关键数据文件备份与恢复

第10章 普通数据文件备份与恢复

第11章 只读数据文件备份与恢复

第12章 不完全数据库恢复

第13章 无备份情况下的恢复

第14章 闪回技术(Flashback)

第15章 恢复受损的数据块