第十四章: 逻辑备份

1、逻辑备份和物理备份区别

      1)物理备份:备份数据文件以及控制文件,备份整个库的物理结构,主要用于media recover
      2)逻辑备份:备份database里的对象的数据结构和数据,作为物理备份的辅助,不能用于media recover ;先主要用于对单个表的备份或数据迁移
                   恢复时只能恢复到备份点
                 

2、exp/imp的使用(expdp/impdp)

emp 导出

C:\Documents and Settings\liqx>exp -help

Export: Release 10.2.0.1.0 - Production on 星期五 8月 19 13:43:49 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

 

通过输入 EXP 命令和您的用户名/口令, 导出

操作将提示您输入参数:

     例如: EXP SCOTT/TIGER

或者, 您也可以通过输入跟有各种参数的 EXP 命令来控制导出

的运行方式。要指定参数, 您可以使用关键字:

     格式:  EXP KEYWORD=value 或 KEYWORD=(value1,value2,...,valueN)

     例如: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)
               或 TABLES=(T1:P1,T1:P2), 如果 T1 是分区表

USERID 必须是命令行中的第一个参数。

关键字   说明 (默认值)         关键字      说明 (默认值)

--------------------------------------------------------------------------
USERID   用户名/口令           FULL        导出整个文件 (N)
BUFFER   数据缓冲区大小        OWNER        所有者用户名列表
FILE     输出文件 (EXPDAT.DMP)  TABLES     表名列表
COMPRESS  导入到一个区 (Y)   RECORDLENGTH   IO 记录的长度
GRANTS    导出权限 (Y)          INCTYPE     增量导出类型
INDEXES   导出索引 (Y)         RECORD       跟踪增量导出 (Y)
DIRECT    直接路径 (N)         TRIGGERS     导出触发器 (Y)
LOG      屏幕输出的日志文件    STATISTICS    分析对象 (ESTIMATE)
ROWS      导出数据行 (Y)        PARFILE      参数文件名
CONSISTENT 交叉表的一致性 (N)   CONSTRAINTS  导出的约束条件 (Y)

OBJECT_CONSISTENT    只在对象导出期间设置为只读的事务处理 (N)

FEEDBACK             每 x 行显示进度 (0)
FILESIZE             每个转储文件的最大大小
FLASHBACK_SCN        用于将会话快照设置回以前状态的 SCN
FLASHBACK_TIME       用于获取最接近指定时间的 SCN 的时间
QUERY                用于导出表的子集的 select 子句
RESUMABLE            遇到与空格相关的错误时挂起 (N)
RESUMABLE_NAME       用于标识可恢复语句的文本字符串
RESUMABLE_TIMEOUT    RESUMABLE 的等待时间
TTS_FULL_CHECK       对 TTS 执行完整或部分相关性检查
TABLESPACES          要导出的表空间列表
TRANSPORT_TABLESPACE 导出可传输的表空间元数据 (N)
TEMPLATE             调用 iAS 模式导出的模板名

成功终止导出, 没有出现警告。

exp

 1)导出整个database
 [oracle@work dat]$ exp system/oracle file=/home/oracle/dat/prod.dmp log=/home/oracle/dat/prod.log indexes=n full=y
 
 2) 导出schema 所有的对象
 [oracle@work dat]$  exp scott/tiger file=scott.dmp log=scott.log indexes=n owner=scott
 
 3)导出单个表
 [oracle@work dat]$  exp scott/tiger file=emp_dept.dmp log=table.log indexes=n tables=(emp,dept);
 
 -------按条件导出表里的数据
 [oracle@work dat]$  exp scott/tiger file=emp_30.dmp log=emp.log indexes=n tables=emp query=\' where deptno=30\'
 
 imp 导入
 
 
 C:\Documents and Settings\liqx>imp help=y

Import: Release 10.2.0.1.0 - Production on 星期五 8月 19 13:59:20 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

 

通过输入 IMP 命令和您的用户名/口令, 导入

操作将提示您输入参数:

     例如: IMP SCOTT/TIGER

或者, 可以通过输入 IMP 命令和各种参数来控制导入

的运行方式。要指定参数, 您可以使用关键字:

     格式:  IMP KEYWORD=value 或 KEYWORD=(value1,value2,...,valueN)

     例如: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N
               或 TABLES=(T1:P1,T1:P2), 如果 T1 是分区表

USERID 必须是命令行中的第一个参数。

关键字   说明 (默认值)        关键字      说明 (默认值)

-----------------------------------------------------------------------
USERID   用户名/口令           FULL       导入整个文件 (N)
BUFFER   数据缓冲区大小        FROMUSER    所有者用户名列表
FILE     输入文件 (EXPDAT.DMP)  TOUSER     用户名列表
SHOW     只列出文件内容 (N)     TABLES      表名列表
IGNORE   忽略创建错误 (N)    RECORDLENGTH  IO 记录的长度
GRANTS   导入权限 (Y)          INCTYPE     增量导入类型
INDEXES   导入索引 (Y)         COMMIT       提交数组插入 (N)
ROWS     导入数据行 (Y)        PARFILE      参数文件名
LOG     屏幕输出的日志文件    CONSTRAINTS    导入限制 (Y)
DESTROY                覆盖表空间数据文件 (N)
INDEXFILE              将表/索引信息写入指定的文件
SKIP_UNUSABLE_INDEXES  跳过不可用索引的维护 (N)
FEEDBACK               每 x 行显示进度 (0)
TOID_NOVALIDATE        跳过指定类型 ID 的验证
FILESIZE               每个转储文件的最大大小
STATISTICS             始终导入预计算的统计信息
RESUMABLE              在遇到有关空间的错误时挂起 (N)
RESUMABLE_NAME         用来标识可恢复语句的文本字符串
RESUMABLE_TIMEOUT      RESUMABLE 的等待时间
COMPILE                编译过程, 程序包和函数 (Y)
STREAMS_CONFIGURATION  导入流的一般元数据 (Y)
STREAMS_INSTANTIATION  导入流实例化元数据 (N)

下列关键字仅用于可传输的表空间

TRANSPORT_TABLESPACE 导入可传输的表空间元数据 (N)
TABLESPACES 将要传输到数据库的表空间
DATAFILES 将要传输到数据库的数据文件
TTS_OWNERS 拥有可传输表空间集中数据的用户

------imp 在导入的时候,先创建对象,建立数据结构,然后再导入数据

 Oracle在恢复数据的过程中,当恢复某个表时,该表已经存在,就要根据ignore参数的设置来决定如何操作。

 
 若ignore=y,Oracle不执行CREATE TABLE语句,直接将数据插入到表中,如果插入的记录违背了约束条件,
 比如主键约束,则出错的记录不会插入,但合法的记录会添加到表中。
 若ignore=n,Oracle执行CREATE TABLE语句,同时也不会将数据插入到表中,而是忽略该表的错误,继续恢复下一个表。
 
 [oracle@work dat]$ imp scott/tiger file=emp_dept.dmp tables=emp fromuser=scott touser=scott ignore=y
 
 -------如果,两个对象之间有参考性约束(主外键),应该先导入父表,再导入子表
 [oracle@work dat]$ imp scott/tiger file=emp_dept.dmp tables=dept  fromuser=scott touser=scott ignore=y
 
 [oracle@work dat]$ imp scott/tiger file=emp_dept.dmp tables=emp fromuser=scott touser=scott ignore=y
 
 案例:
 ----------误删除user及所有的对象,恢复
 1)创建用户,并分配权限
 06:14:01 SQL> create user scott identified by tiger default tablespace users ;                                                          

User created.

06:15:55 SQL> grant connect ,resource to scott;                                                                                         

Grant succeeded.

06:16:06 SQL>

2)导入备份

[oracle@work dat]$ imp system/oracle file=scott.dmp ignore=y fromuser=scott touser=scott;

Import: Release 10.2.0.1.0 - Production on Fri Aug 19 06:16:36 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path

Warning: the objects were exported by SCOTT, not by you

import done in US7ASCII character set and AL16UTF16 NCHAR character set

import server uses WE8ISO8859P1 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
. . importing table                        "BONUS"          0 rows imported
. . importing table                         "DEPT"          4 rows imported
. . importing table                          "EMP"         14 rows imported
. . importing table                         "EMP1"        512 rows imported
. . importing table                         "LX01"          9 rows imported
. . importing table                     "SALGRADE"          5 rows imported
. . importing table                         "TEST"         10 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@work dat]$

3)验证

06:17:11 SQL> conn scott/tiger                                                                                                          
Connected.
06:17:16 SQL>
06:17:16 SQL> select * from tab;                                                                                                        

TNAME                          TABTYPE  CLUSTERID

------------------------------ ------- ----------
BONUS                          TABLE
DEPT                           TABLE
EMP                            TABLE
EMP1                           TABLE
LX01                           TABLE
SALGRADE                       TABLE
SYS_TEMP_FBT                   TABLE
TEST                           TABLE

8 rows selected.

-------------利用exp/imp 传输表空间

1、建立表空间

17:08:41 SQL> col file_name for a50                                                                                                     

17:08:48 SQL> select file_id,file_name,tablespace_name from dba_data_files                                                              
17:08:57   2  ;                                                                                                                         

   FILE_ID FILE_NAME                                          TABLESPACE_NAME

---------- -------------------------------------------------- ------------------------------
         4 /u01/app/oracle/oradata/ocmtest1/users01.dbf       USERS
         3 /u01/app/oracle/oradata/ocmtest1/sysaux01.dbf      SYSAUX
         2 /u01/app/oracle/oradata/ocmtest1/undotbs01.dbf     UNDOTBS1
         1 /u01/app/oracle/oradata/ocmtest1/system01.dbf      SYSTEM
         5 /u01/app/oracle/oradata/ocmtest1/example01.dbf     EXAMPLE

17:08:58 SQL> create tablespace test                                                                                                    

17:09:05   2   datafile '/u01/app/oracle/oradata/ocmtest1/test01.dbf' size 100m;                                                        

Tablespace created.

17:09:18 SQL> conn scott/tiger                                                                                                          

Connected.
17:09:23 SQL>

17:09:29 SQL> create table t01 (id int) tablespace test;                                                                                

Table created.

17:09:33 SQL> insert into t01 values (1);                                                                                               

1 row created.

17:09:38 SQL> insert into t01 values (2);                                                                                               

1 row created.

17:09:39 SQL> insert into t01 values (3);                                                                                               

1 row created.

17:09:40 SQL> commit;                                                                                                                   

Commit complete.

17:09:41 SQL> conn /as sysdba                                                                                                           

Connected.
17:10:12 SQL>
17:10:12 SQL> alter tablespace test read only;                                                                                          

Tablespace altered.

2、查看表空间是否自包含

17:10:20 SQL> exec sys.dbms_tts.transport_set_check('test',true);                                                                       

PL/SQL procedure successfully completed.

17:11:37 SQL> select * from sys.transport_set_violations;                                                                               

no rows selected

17:11:43 SQL>

将传输的tablespace 设置成 read  only 模式。
15:37:37 SQL> alter tablespace test read only;

Tablespace altered.

3、导出可传输表空间(必须以sysdba权限)

[oracle@ocmtest1 ~]$ exp userid=\'sys/oracle as sysdba\' file=test.dmp log=test.log transport_tablespace=y tablespaces=test

Export: Release 10.2.0.1.0 - Production on Mon Aug 15 17:13:24 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace TEST ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                            T01
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.
[oracle@ocmtest1 ~]$

4、启动另外一个数据库

[oracle@ocmtest1 ~]$ export ORACLE_SID=catdb

[oracle@ocmtest1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Aug 15 17:15:25 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to an idle instance.

17:15:25 SQL>

17:15:25 SQL> startup                                                                                    
ORACLE instance started.

Total System Global Area  167772160 bytes

Fixed Size                  1218316 bytes
Variable Size              75499764 bytes
Database Buffers           88080384 bytes
Redo Buffers                2973696 bytes
Database mounted.
Database opened.
17:15:41 SQL> select tablespace_name from dba_tablespaces;                                               

TABLESPACE_NAME

------------------------------
SYSTEM
UNDOTBS1
SYSAUX
TEMP
USERS
EXAMPLE
CATTBS

7 rows selected.

5、在目标数据库下做imp操作,导入表空间

[oracle@ocmtest1 ~]$ cp /u01/app/oracle/oradata/ocmtest1/test01.dbf /u01/app/oracle/oradata/catdb/

 
-------------将表空间数据文件从源数据库拷贝到目标数据库下

[oracle@ocmtest1 ~]$ imp userid=\'sys/oracle as sysdba\' tablespaces=test     file=test.dmp transport_tablespace=y datafiles=/u01/app/oracle/oradata/catdb/test01.dbf

Import: Release 10.2.0.1.0 - Production on Mon Aug 15 17:17:23 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path

About to import transportable tablespace(s) metadata...
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8ISO8859P1 character set (possible charset conversion)
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing SCOTT's objects into SCOTT
. . importing table                          "T01"
. importing SYS's objects into SYS
Import terminated successfully without warnings.

6、验证

[oracle@ocmtest1 ~]$

17:16:53 SQL> select tablespace_name from dba_tablespaces;                                               

TABLESPACE_NAME

------------------------------
SYSTEM
UNDOTBS1
SYSAUX
TEMP
USERS
EXAMPLE
CATTBS
TEST

8 rows selected.

17:17:28 SQL> select * from scott.t01;                                                                   

        ID

----------
         1
         2
         3

17:17:33 SQL>

17:28:03 SQL> select tablespace_name,status from dba_tablespaces;                                                                       

TABLESPACE_NAME                STATUS

------------------------------ ---------
SYSTEM                         ONLINE
UNDOTBS1                       ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE
EXAMPLE                        ONLINE
CATTBS                         ONLINE
TEST                           READ ONLY

8 rows selected.

17:28:12 SQL> alter tablespace test read write;                                                                                         

Tablespace altered.

17:28:20 SQL>