Oracle备份恢复五(数据泵)

    平台:redhat linux as5,oracle10g
    Oracle10g 引入了最新的数据泵(Data Dump)技术,使得DBA或应用开发人员可以将数据库的元数据库和数据快速移动到别一个Oracle数据库中,因为它可以导出数据库(表空间等),所以也叫逻辑备份,数据泵导出导入命今行选项非常多,大家可以到联机文档查看各个选项的用法.,本文详细介绍最常用的导出导入数据库表空间,然后介绍如何导入导出整个数据库及数据文件等

表空间

导出表空间
先准备一个表空间,并建表
SQL> conn y/123
SQL> create tablespace test1 datafile '/home/oracle/oracle/oradata/db2/test1.dbf' size 10M;
SQL> create table test1(i number) tablespace test1;
SQL> insert into test1 values(10);
SQL> commit;
SQL> select * from test1;
         I
---------------------
        10
建立dumpdir目录,并给用户y赋权
[oracle@oracle]# mkdir /dumpdir
SQL> create directory dumpdir as '/rman';
SQL> conn sys as sysdba
SQL> grant read,write on directory dumpdir to y;
SQL> grant dba to y;
SQL> conn y/123
分析表test1是否满足导出条件
SQL> alter tablespace test1 read only;
Tablespace altered.
SQL> exec sys.dbms_tts.transport_set_check('test1',true);
PL/SQL procedure successfully completed.
SQL> select * from sys.transport_set_violations;
no rows selected
#可见没有不满足的数据
导出表空间
SQL> ! expdp directory=dumpdir dumpfile=tbs.dmp transport_tablespaces=test1
Username: sys as sysdba
Password:
Export: Release 10.2.0.4.0 - Production on Saturday, 13 June, 2009 1:58:13
Copyright (c) 2003, 2007, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Starting "Y"."SYS_EXPORT_TABLESPACE_01":  y/******** directory=dumpdir dumpfile=ttbs.dmp tablespaces=t
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/COMMENT
. . exported "Y"."T"                                     4.906 KB       1 rows
Master table "Y"."SYS_EXPORT_TABLESPACE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for Y.SYS_EXPORT_TABLESPACE_01 is:
  /dumpdir/tbs.dmp
Job "Y"."SYS_EXPORT_TABLESPACE_01" successfully completed at 01:58:42
没有报错,已成功导出表空间test1

2.导入表空间

    现在把刚才导出的表空间导入到别一个数据库(2)
    在数据库(2)dumpdir目录,把库1导出的tbs.dmp复制到dumpdir同时把库1的数据文件test1.dbf复制到库2/home/oracle/oracle/oradata/db2/
#2IP192.168.1.2
[oracle@oracle22]$ mkdir /dumpdir
[oracle@oracle11]$ scp /dumpdir/tbs.dmp root@192.168.1.2:/dumpdir/
[oracle@oracle11]$ scp /home/oracle/oracle/oradata/test1.dbf
root@192.168.1.2:/home/oracle/oracle/oradata/db2/
开始导入(以下在库2操作)
SQL> ! impdp directory=dumpdir dumpfile=tbs.dmp transport_datafiles='/home/oracle/oracle/oradata/db2/test1.dbf'
Username: sys as sysdba
Password:
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-31655: no data or metadata objects selected for job
Master table "SYS"."SYS_IMPORT_TABLESPACE_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_TABLESPACE_01":  sys/******** AS SYSDBA directory=dumpdir dumpfile=ttbs.dmp tablespaces=tt
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Job "SYS"."SYS_IMPORT_TABLESPACE_01" successfully completed at 04:53:23
导入成功,但这时表空间为offline状态,
现在把表空间online就可以查到表test1的数据了,如下:
SQL> select tablespace_name,status from dba_tablespaces where tablespace_name='TEST';
TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
TEST1                            OFFLINE
把表空间online并查看test1
SQL> alter tablespace test1 online;
SQL> alter tablespace test1 read write;
SQL> select * from test1;
         I
-----------------------
        10
OK,成功把表空间test1从库1导入到库2

其它导入导出

1.导出
expdp directory=dumpdir dumpfile=full.dmp full=y
expdp directory=dumpdir dumpfile=tbs.dmp tablespaces=test1,test2
expdp directory=dumpdir dumpfile=table.dmp tables=test1,test2
expdp directory=dumpdir dumpfile=schma.dmp schemas=test1,test2
2.导入
impdp directory=dumpdir dumpfile=full.dmp full=y
impdp directory=dumpdir dumpfile=tbs.dmp transport_datafiles='/home/oracle/oralce/oradata/db2/test1.dbf', '/home/oracle/oralce/oradata/db2/test2.dbf'
impdp directory=dumpdir dumpfile=table.dmp tables=test1,test2
impdp directory=dumpdir dumpfile=schma.dump schemas=test1,test2

注意事项

    导入导出时注意如下几点
    1.源数据库和目标数据库要有相同的字符集
    2.名称不能相同(导库时库名不能相同,导表空间时表空间名不能相同等)
    3.不能搬移system表空间和有sys用户对象的表空间
    4.要在不同OS上搬移表空间,要保证compatible 设置为10.0以上