Oracle备份与恢复系列 五 逻辑导入导出

许多用户现在依然使用逻辑备份导入导出来备份数据库。并没有使用RMAN物理备份。经常出现归档日志占满磁盘空间的问题(既然采用逻辑备份也不知道这些归档日志有什么用)。我们希望用户能启用RMAN备份,将逻辑备份改为物理备份,但这些是商务的事情,不是单纯的技术上的事了。

下面总结一下逻辑备份的知识点,以备不时之需。

当运行export/import应用程序时,有四种模式,分别代表提取或插入数据库内容时所涉及的工作范围:
  • 完整数据库模式(Full database model):在导出过程中,除了少数内容外,整个数据库的内容都被写入一个文件中。某些用户对象(SYS、ORDSYS、CTXSYS)并不被导出。定义完整数据库模式的导出参数为FULL,导入参数是FULL=Y。
  • 用户模式(User model):在导出过程中,属于特定用户的所有数据库对象被写到一个文件中。可以用owner参数来指定这些用户。属于该用户的所有表、索引、视图、触发器、同义词、数据库链接、对象、存储过程等等,都被写入到导出文件中。导入过程中,在导出文件中属于指定用户的所有数据库对象都在数据库中创建。导入用户是在FROMUSER参数张定义的。
  • 表模式(Table model):在导出过程中,单个表以及相关的对象(例如:索引、约束、触发器、授权)被写入到一个文件中。每个表必须用TABLES参数命令。只有被授权的用户才可以导出属于其他用户的表。再导入过程中,表从导出文件中被读出,并创建在数据库中。定义表模式的导入参数也为TABLES。
  • 表空间模式(Tablespace mode):在导出过程中,对应于所选表空间以及这些表空间中所包含的所有对象的“元数据”被写入一个文件中。实际的表数据并不写入导出文件中。生产的导出文件联通表空间数据文件一起从源数据库发送到目标数据库。在导入过程中,表空间和对象源数据被添加到目标数据库上。
下面看看导入导出共用的一些重要参数
userid     执行导入导出程序的用户提供的数据库用户名和密码,例如userid=snow/snow as sysdba 或者userid=scott/tiger
file       将要创建的导出文件、或是将要导入需要引用的文件。该文件默认生成路径为当前执行导入导出的路径下。
log        执行导入导出操作时候的屏幕输出可以保存在log中。该文件默认生成路径为当前执行导入导出的路径下。
help       帮助文件。输入help=y可以获取帮助信息
tables     指定要导入或者导出的表名
fromuser   指定导出文件中包含的原对象拥有者
touser     指定创建和拥有导入对象的新模式(schema)。例如我们可以导出A的对象,导入到B(schema)模式中。可以使用   FROMUSER=A TOUSER=B

概念性的东西就总结这些,接下来开始试验环节

实验一 交互式导出完整的数据库
[oracle@snow ~]$ exp

Export: Release 11.2.0.3.0 - Production on Sun Aug 10 23:25:40 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


Username: sys as sysdba   <==导出所有对象需要dba的权限,或者EXP_FULL_DATABASE
Password: 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Enter array fetch buffer size: 4096 >  <==默认回车

Export file: expdat.dmp >      <==默认路径为当前路径/home/oracle

(1)E(ntire database), (2)U(sers), or (3)T(ables): (2)U > 1   <==完整数据库

Export grants (yes/no): yes > yes   <==导出所有对象的授权

Export table data (yes/no): yes > yes   <==导出表数据

Compress extents (yes/no): yes > no  <==如果压缩,在导入时建立的对象创建命令会将所有的数据合并到一个厨师域中。如果不压缩,对象将按照它们在导出中已有的域设置被创建

Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8MSWIN1252 character set (possible charset conversion)

About to export the entire database ...
. exporting tablespace definitions
. exporting profiles
. exporting user definitions
. exporting roles
. exporting resource costs
. exporting rollback segment definitions
. exporting database links
. exporting sequence numbers
. exporting directory aliases
. exporting context namespaces
. exporting foreign function library names
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions
....

. exporting statistics
Export terminated successfully with warnings.

执行成功后看下生成的文件,大小为141M
[oracle@snow ~]$ du -sh *
141M    expdat.dmp

查看一下当前的segments大小为1553.5MB
SYS@ALEX >select sum(bytes/1024/1024) MB from dba_segments;

        MB
----------
    1553.5

查看一下当前的数据文件大小为1648.125MB
SYS@ALEX >select sum(bytes/1024/1024) MB from dba_data_files;

        MB
----------
  1648.125

除了上面的交互式逻辑备份命令,也可以使用一行命令来完成上面的操作:
注意sys账号要加入 as sysdba,并且用\转义符来修饰'
[oracle@snow ~]$  exp \'sys/oracle as sysdba\' file=/home/oracle/full.dmp full=y   


实验二 通过参数文件导出完整的数据库

添加一个参数文件,输入以下配置信息
vi /home/oracle/export_full.par

#export the entire database
#rows = n: 只导出对象定义,不导出数据
#buffer = 10000  : 提取缓冲区大小设置为10000字节。该参数有助于大型导出更快地运行
userid = "sys/oracle as sysdba"
file = /home/oracle/full_par.dmp
log = /home/oracle/full_par.loge
full = y
rows = n 
buffer = 10000  
compress = n

[oracle@snow ~]$ exp parfile=/home/oracle/export_full.par


实验三 以直接模式导出完整的数据库
看到direct是不是想起了sqlloader的直接路径加载了,没错在exp中定义direct=y可以直接从数据文件中读取,避开了数据库缓冲池。在大型表上采用该参数是一个优化的手段。但是数据量小的状态下并没有什么明显的优势。
vi /home/oracle/export_full_d.par

#export the entire database
userid = "sys/oracle as sysdba"
file = /home/oracle/full_direct.dmp
log = /home/oracle/full_direct.log
full = y
compress = n
direct = y

[oracle@snow ~]$ exp parfile=/home/oracle/export_full_d.par



实验四 导入整个数据库用于显示
如果只想到处文件中包含表和索引定义,可以使indexfile参数。使用时将注释##去掉就可以了

vi /home/oracle/import_full_dis.par
userid = "sys/oracle as sysdba"
file = /home/oracle/full.dmp
log = /home/oracle/full_dis.log
##indexfile= /home/oradata/full.idx
full = y
show = y

如果你也喜欢把secureCRT调成Traditional的那种绿色字体,输入完下面的命令后就会想起电影《黑客帝国》


用户模式的导入/导出
实验五 替换并克隆用户scott

1 使用dba导出用户scott
vi /home/oracle/export_user_scott.par
userid = "sys/oracle as sysdba"
file=/home/oracle/export_user_scott.dmp 
log=/home/oracle/export_user_scott.log 
owner = SCOTT

exp parfile=/home/oracle/export_user_scott.par

使用scott用户导出
vi /home/oracle/export_user_scott2.par
userid = "scott/tiger"
file=/home/oracle/export_user_scott2.dmp 
log=/home/oracle/export_user_scott2.log 

exp parfile=/home/oracle/export_user_scott2.par
或者
exp scott/tiger file=/home/oracle/export_user_scott.dmp log=/home/oracle/export_user_scott.log 

2 删除用户scott
select username from dba_users where username='SCOTT';
drop user scott cascade;
select username from dba_users where username='SCOTT';

3 导入用户scott
grant connect,resource to scott identified by tiger;
alter user scott default tablespace users temporary tablespace TEMP;

使用scott用户
imp userid=scott/tiger file=/home/oracle/export_user_scott2.dmp
使用dba用户
imp userid=\'sys/oracle as sysdba\' file=/home/oracle/export_user_scott.dmp

4 将scott用户数据导入用户lily
只导入结构,不导入数据rows=n
conn / as sysdba
grant connect,resource to lily identified by kitty;
alter user lily default tablespace users temporary tablespace TEMP;

imp \'sys/oracle as sysdba\'  file=/home/oracle/export_user_scott.dmp fromuser=scott touser=lily rows=n;

查看lily下的表结构和表数据
SYS@ALEX >conn lily/kitty                                                                                       
LILY@ALEX >select * from tab;

TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
BONUS                          TABLE
DEPT                           TABLE
EMP                            TABLE
SALGRADE                       TABLE

LILY@ALEX >select * from dept;
no rows selected  <==只导入了结构,没有数据

实验六 导出一个数据库表

1 导出表
我们将用户SCOTT下的salgrade表导出,并且添加query限制条件,只导出grade>3的数据,总计2条。
vi /home/oracle/export_SALGRADE.par

USERID   = "sys/oracle as sysdba"
FILE     = /home/oracle/export_SALGRADE.dmp
LOG      = /home/oracle/export_SALGRADE.log
TABLES   = (SCOTT.SALGRADE)
QUERY    = "WHERE grade > 3"
COMPRESS = N

exp parfile=/home/oracle/export_SALGRADE.par

屏幕输出如下:
Current user changed to SCOTT
. . exporting table                       SALGRADE          2 rows exported


2 删除部分数据
delete from SALGRADE where grade > 3;

commit;

3 导入表

vi /home/oracle/import_SALGRADE.par

USERID   = "sys/oracle as sysdba"
FILE     = /home/oracle/export_SALGRADE.par
TABLES   = (SCOTT.SALGRADE)

注意,需要用到/转义‘ ( )
[oracle@snow ~]$ imp \'sys/oracle as sysdba\' file=/home/oracle/export_SALGRADE.dmp full=y ignore=y
或者
[oracle@snow ~]$ imp \'sys/oracle as sysdba\' file=/home/oracle/export_SALGRADE.dmp fromuser=scott touser=scott tables\(scott.salgrade\) ignore=y

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值