oracle——expdp与impdp备份使用方法
一、引言
原来oracle备份一直跟mysql一样导出sql语句,最近发现有新方法,尝试了下,跟住时代潮流。其实oracle的备份方法挺多的:
Oracle备份分成物理备份与逻辑备份。
物理备份: 把实际物理文件从一处拷贝到另一处(可能是原样拷贝,也可能经过压缩), 操作系统备份,使用RMAN的备份,冷备份,热备份。
逻辑备份: 利用SQL从数据库中提取数据,并将其保存到文件中,这些数据可以在以后需求时重新导入数据库,或者导入其他数据库。Oracle提供EXP, IMP, EXPDP, IMPDP工具。
二、使用方法
1、创建逻辑目录
该命令不会在操作系统创建真正的目录,最好以system等管理员创建。
create or replace directory dump as 'd:\test\dump';
默认DATA_PUMP_DIR,所在目录C:\app\Administrator\admin\orcl\dpdump\
2、查看管理理员目录
同时查看操作系统是否存在,因为Oracle并不关心该目录是否存在,如果不存在,则出错
select * from dba_directories;
3、给scott用户赋予在指定目录的操作权限,最好以system等管理员赋予。
grant read,write on directory dump to jgs;
4、导出数据
1)按用户导expdp scott/[email protected] schemas=scott dumpfile=scott.dmp;
2)并行进程parallel
expdp scott/[email protected] directory=DATA_PUMP_DIR dumpfile=scott3.dmp parallel=40 job_name=scott3
3)按表名导
expdp scott/[email protected] TABLES=emp,dept dumpfile=expdp.dmp DIRECTORY=DATA_PUMP_DIR;
4)按查询条件导
expdp scott/[email protected] directory=DATA_PUMP_DIR dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
5)按表空间导
expdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=tablespace.dmp TABLESPACES=temp,example;
6)导整个数据库
expdp system/[email protected] dumpfile=full.dmp full=y;
有时候导出会出错,说找到不到日志之类的,这个时候在最后面加上cluster=n,如下:
expdp\"/ as sysdba\" directory=dumpdir dumpfile=user1.dmp schemas=jgs cluster=n;
5、还原数据
1)导到指定用户下
impdp system/[email protected] DUMPFILE=scott.dmp REMAP_SCHEMA=scott:scott2; (加上一直出错?DIRECTORY=DATA_PUMP_DIR)
对于后面的scott2,系统中可以有也可以没有,如果没有系统会自动建立这个用户。
2)改变表的owner
impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
3)导入表空间
impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=tablespace.dmp TABLESPACES=example;
4)导入数据库
impdb system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=full.dmp FULL=y;
5)追加数据
impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION=append;
导入到普通数据库
impdp jgs/123 directory=dump dumpfile=xxx.dmp REMAP_SCHEMA=xxx:xxx3 full=y;
导入到系统数据库
impdp \"/ as sysdba\" directory=dump dumpfile=xxx.dmp REMAP_SCHEMA=xxx:xxx2 full=y;
自己常用的就是用户导入,下面几句:
//准备
create or replace directory dump as 'C:\app\xcy\oradata';
grant read,write on directory dump to sde;
//导出,第一句不行用第二句
expdp sde/[email protected] schemas=sde dumpfile=sde.dmp directory=dump;
expdp sde/[email protected] schemas=sde dumpfile=sde.dmp directory=dump cluster=n;
//导入
impdp sde/[email protected] directory=dump dumpfile=sde.dmp remap_schema=sde:sde
三、总结
- 数据库备份方法种类;
- expdp、impdp备份方法步骤;