使用exp/imp匯出匯入資料(邏輯備份恢復)

skyin_1603發表於2016-11-02
使用exp/imp匯出匯入資料:

---建立新的目錄物件存放匯出的資料:
--建立目錄物件:
SQL> create directory homedir as '/home/oracle/homedir';
Directory created.

--授予目錄物件給兩個使用者:
SQL> grant read,write on directory homedir to scott;
Grant succeeded.

SQL> grant read,write on directory homedir to suxing;
Grant succeeded.

---使用exp匯出表emp:
--匯出策略:exp scott/tiger tables=emp  file=emp.dmp 
[oracle@enmo homedir]$ exp scott/tiger tables=emp  file=emp.dmp 
Export: Release 11.2.0.4.0 - Production on Wed Nov 2 20:10:17 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                            EMP         14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[oracle@enmo homedir]$ 
#匯出成功。

--檢視匯出檔案:
[oracle@enmo homedir]$ ls
emp.dmp
[oracle@enmo homedir]$ ll
total 16
-rw-r--r-- 1 oracle oinstall 16384 Nov  2 20:10 emp.dmp
[oracle@enmo homedir]$ 

---匯出Scott模式schema:
--匯出策略1:exp scott/tiger owner=scott file=scott.dmp
[oracle@enmo homedir]$ exp scott/tiger owner=scott file=scott.dmp
Export: Release 11.2.0.4.0 - Production on Wed Nov 2 20:13:38 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT 
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT 
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                          BONUS          0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                            EMP         14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                      HISLOADER          3 rows exported
. . exporting table                       MYLOADER          7 rows exported
. . exporting table                       SALGRADE          5 rows exported
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.
[oracle@enmo homedir]$ 
#匯出成功。匯出schema時候,是匯出使用者的所有資料庫物件。

--匯出schema策略2:exp system/oracle owner=scott file=scotts.dmp   
[oracle@enmo homedir]$ exp system/oracle owner=scott file=scotts.dmp  
Export: Release 11.2.0.4.0 - Production on Wed Nov 2 20:16:37 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT 
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT 
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                          BONUS          0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                            EMP         14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                      HISLOADER          3 rows exported
. . exporting table                       MYLOADER          7 rows exported
. . exporting table                       SALGRADE          5 rows exported
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.
[oracle@enmo homedir]$ 
#匯出成功。可以看到兩個策略匯出的結果是一樣的,也就是說匯出模式時候
DBA角色的使用者以及模式對應的使用者都可以匯出模式裡所有的物件。

--檢視匯出的檔案:
[oracle@enmo homedir]$ ll
total 572
-rw-r--r-- 1 oracle oinstall  16384 Nov  2 20:10 emp.dmp
-rw-r--r-- 1 oracle oinstall 516096 Nov  2 20:24 myspace.dmp
-rw-r--r-- 1 oracle oinstall  24576 Nov  2 20:13 scott.dmp
-rw-r--r-- 1 oracle oinstall  24576 Nov  2 20:16 scotts.dmp
[oracle@enmo homedir]$ 

---匯出MySpace表空間:
--匯出策略:exp system/oracle tablespaces=myspace file=myspace.dmp
[oracle@enmo homedir]$ exp system/oracle tablespaces=myspace file=myspace.dmp
Export: Release 11.2.0.4.0 - Production on Wed Nov 2 21:36:42 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export selected tablespaces ...
For tablespace MYSPACE ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                       YOURTEST          0 rows exported
. . exporting table                             T1        100 rows exported
. exporting referential integrity constraints
. exporting triggers
Export terminated successfully without warnings.
[oracle@enmo homedir]$ 
#匯出成功。

--檢視匯出的檔案:
[oracle@enmo homedir]$ ll
total 572
-rw-r--r-- 1 oracle oinstall  16384 Nov  2 20:10 emp.dmp
-rw-r--r-- 1 oracle oinstall 516096 Nov  2 20:24 myspace.dmp
-rw-r--r-- 1 oracle oinstall  24576 Nov  2 20:13 scott.dmp
-rw-r--r-- 1 oracle oinstall  24576 Nov  2 20:16 scotts.dmp
[oracle@enmo homedir]$ 

---還有用exp匯出全庫,策略:exp system/oracle full=y  file=wholedatabae.dmp

---匯入表emp:
--匯入策略:imp scott/tiger tables=emp file=emp.dmp 
--檢視錶emp資料:
SQL> select count(*) from emp;
  COUNT(*)
----------
        14
--刪除表emp:        
SQL> drop table emp purge;
Table dropped.
               
---匯入表emp到Scott使用者:
[oracle@enmo homedir]$ imp scott/tiger tables=emp file=emp.dmp
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 20:51:04 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table                          "EMP"         14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@enmo homedir]$ 
#匯入完成。

--檢視emp表:
SQL> select count(*) from emp;
  COUNT(*)
----------
        14
#可知匯入成功。
--------------------------------------------------------------------------------------
---匯入表空間myspace:
--匯入策略:imp system/oracle tablespaces=myspace  file=myspace.dmp full=y

--檢視錶空間:
SQL> select tablespace_name from dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
EXAMPLE
MYSPACE
7 rows selected.

--myspace表空間所擁有的表:
SQL> select table_name from dba_tables 
  2   where tablespace_name='MYSPACE';
TABLE_NAME
------------------------------
YOURTEST
T1

--刪除這兩個表:
SQL> drop table t1 purge;
Table dropped.

SQL> drop table yourtest purge;
Table dropped.

--再次檢視:
SQL> select table_name from dba_tables
  2  where tablespace_name='MYSPACE';
no rows selected

--看錶空間:
SQL>  select tablespace_name from dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
EXAMPLE
MYSPACE
7 rows selected.

--匯入表空間:
--策略1:imp system/oracle tablespaces=myspace  file=myspace.dmp full=y

[oracle@enmo homedir]$ imp system/oracle tablespaces=myspace  file=myspace.dmp full=y
Import: Release 11.2.0.4.0 - Production on Wed Nov 2 21:42:15 2016
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SYSTEM's objects into SYSTEM
. importing SUXING's objects into SUXING
. . importing table                     "YOURTEST"          0 rows imported
. importing SCOTT's objects into SCOTT
. . importing table                           "T1"        100 rows imported
Import terminated successfully without warnings.
[oracle@enmo homedir]$ 
#匯入完成。

--檢視空間的資料表:
SQL> select table_name from dba_tables
  2  where tablespace_name='MYSPACE';
TABLE_NAME
------------------------------
YOURTEST
T1
恢復完成。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31392094/viewspace-2127555/,如需轉載,請註明出處,否則將追究法律責任。

相關文章