您的位置:首页 > 数据库 > Oracle

解密Oracle备份工具-exp/imp 推荐

2015-06-08 16:52 423 查看
解密Oracle备份工具-exp/imp无论是运维工作者还是数据库管理员,数据的备份和还原是我们日常工作的重点,制定合理的备份策略,使用合适的备份工具是每个IT人必备的技能,今天就给大家介绍Oracle的备份工具exp和imp。ORACLE数据库有两类备份方法。第一类:为物理备份,该方法实现数据库的完整恢复,但数据库必须运行在归挡模式下(业务数据库在非归挡模式下运行),且需要极大的外部存储设备,例如磁带库;

第二类:备份方式为逻辑备份,业务数据库采用此种方式,此方法不需要数据库运行在归挡模式下,不但备份简单,而且可以不需要外部存储设备。

一、exp的关键字说明:


1、导出某用户下所有表
exp scott/lipengfeifile=scott_all_tables.dmp log=scott_all_tables.log
2、导出scott用户下的部分表
(1)expscott/lipengfei tables=\(emp,salgrade\) file=scott_emp_salgrade.dmplog=scott_emp_salgrade.log
(2)exp scott/lipengfei tables=empfile=scott_emp.dmp log=scott_emp.log
3、参数文件的使用
vi /home/oracle/dept.txt 文件内容如下:
userid=scott/lipengfei
log=/home/oracle/scott_dept.log
file=/home/oracle/scott_dept.dmp
tables=dept
引用参数文件:
exp parfile=/home/oracle/dept.txt
4、按条件导出
(1)参数文件中指定条件

exp parfile=/home/oracle/emp.txt
vi /home/oracle/emp.txt  内容如下:
userid=scott/lipengfei
log=/home/oracle/emp.log
file=/home/oracle/emp.dmp
tables=emp
query='where sal>1000'
(2)条件中是数字
exp scott/lipengfei tables=empquery="'where sal >1000'" file=/home/oracle/emp.dmplog=/home/oracle/emp.log
(3)条件中带有字符串
exp scott/lipengfei tables=empquery="'where sal >1000 and job=''CLERK'''"file=/home/oracle/emp.dmp log=/home/oracle/emp.log
(4)参数文件,处理条件中带有字符串
exp parfile=/home/oracle/emp.txt
vi /home/oracle/emp.txt  内容如下:
userid=scott/lipengfei
log=/home/oracle/emp.log
file=/home/oracle/emp.dmp
tables=emp
query='where sal>1000 and job=''CLERK'''
5、导出某几个用户的所有表
(1)创建表空间及用户、授权
create tablespace li datafile'/oracle/app/oradata/ecom/li.dbf' size 30M AUTOEXTEND OFF;
create user li identified by li defaulttablespace li;
alter user li account unlock;
grant connect,resource to li;
(2)创建表空间及用户、授权
create tablespace peng datafile'/oracle/app/oradata/ecom/peng.dbf' size 30M AUTOEXTEND OFF;
create user peng identified by peng defaulttablespace peng;
alter user peng account unlock;
grant connect,resource to peng;
(3)创建表空间及用户、授权
create tablespace fei datafile'/oracle/app/oradata/ecom/fei.dbf' size 30M AUTOEXTEND OFF;
create user fei identified by fei defaulttablespace fei;
alter user fei account unlock;
grant connect,resource to fei;
(4)创建表及初始化数据
sqlplus li/li
create table haha(id int);
insert into haha values(1);
commit;
(5)创建表及初始化数据
sqlplus peng/peng
create table hehe(id int);
insert into hehe values(1);
commit;
(6)创建表及初始化数据
sqlplus fei/fei
create table hihi(id int);
insert into hihi values(1);
commit;
(7)将上面3个用户全部对象导出
exp \'sys/lipengfei as sysdba\'file=/home/oracle/li_peng_fei.dmp log=/home/oracle/li_peng_fei.logowner=\(li,peng,fei\)
6、不想导出索引、不想导出约束、不想导出授权、不想导出与表相关的触发器等
exp scott/lipengfeifile=scott_all_tables.dmp log=scott_all_tables.log indexes=N constraints=Ngrants=N triggers=N
7、导出的文件太大了,超出文件系统限制【fat32单个文件不能超过4G,ntfs单个文件不能超过2T,ext3理想情况下单个文件不能超过2T】
exp scott/lipengfei filesize=500M  file=scott_all_tables1.dmpscott_all_tables2.dmp log=scott_all_tables.log
如果指定filesize参数,那么file参数也要跟着修改。exp在导出的时候有可能会生成多个dmp文件,因此必须在file参数中为每一个文件分别命名(多个名称间以逗号分隔)

如果file参数指定的文件名多于实际生成的文件,多出指定的文件不会被生成。

如果file参数指定的文件名少于实际生成的文件,exp执行过程中,

在用完用户所指定的文件后,就会提示输入新的文件名。

如果没有人在旁边操作,那么整个导出任务就会停在这里了。

接着你可能就要问,怎么知道要导出的数据一共占用多大空间?
select sum(bytes)/1024/1024"total(M)" from user_segments;

二、imp关键字说明:

1、导入数据
(1)按用户导出数据
exp li/li file=li_all_tables.dmplog=li_all_tables.log
(2)模拟数据丢失
sqlplus li/li
SQL> drop table haha;
(3)将备份数据还原
imp li/li file=li_all_tables.dmplog=li_all_tables.log
2、导入指定表到其他用户
(1)将li用户下的备份集导入到peng用户中
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log
(2)以peng用户登录,验证数据
sqlplus peng/peng
SQL> select  tname from tab;
上面的操作看起来成功?其它并没有,数据虽然成功导入了,但不是严谨的方式,可能无意中给数据库埋了一颗雷。
SQL> show user
SQL> select username,default_tablespacefrom user_users;
SQL> select table_name,tablespace_namefrom user_tables;
奇怪吗?虽然peng用户默认的表空间是peng,但是新导入的haha表被存储到li表空间中。如下方式解决
(3)表空间权限控制
sqlplus / as sysdba
alter user peng quota unlimited on peng;
revoke unlimited tablespace from peng;
(4)清空peng用户刚导入的表及数据
sqlplus peng/peng
drop table haha;
(5)将li用户下的备份集再一次导入到peng用户中
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y
(6)以peng用户登录,验证数据
sqlplus peng/peng
SQL> select table_name,tablespace_namefrom user_tables;
上面使用到了ignore参数,如果要导入的对象已经存在,默认情况导入就会报错。
ignore=N 【默认】,出错对象会被跳过,imp继续后续操作。
ignore=Y,自动忽略对象已存在的事实,继续导入数据,也就会出现重复数据,可能通过手工去重。
3、导入表结构到指定用户
(1)登录peng用户,删除指定表及数据
sqlplus peng/peng
SQL> drop table haha;
(2)利用上面产生的备份集恢复,只还原表结构
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y rows=N
(3)以peng用户登录,验证是否只还原了表结构,没有数据
sqlplus peng/peng
SQL> select tname from tab;
SQL> select * from haha;


很久很久以前,Oracle就开始提供用来提取表、模式或整个数据库的定义,然后导入到其他模式或数据的小工具:那就是exp/imp 那个时候数据库规模都很小(几百M就算超大数据库了),而且对于数据库的要求也没有那么高,不像现如今,动不动就是7*24小时高并发、高可用,以至在某些领域,exp/imp也被视作备份恢复的工具使用并延续至今。如果你使用exp备份几十G、数百G甚至更大规模数据库,并且将这种方式作为生产数据库的备份策略,这就太不合理了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle exp imp