導出(exp) & 導入(imp)
利用Export可將數據從數據庫中提取出來,就是將select的結果存到一個FS二進制文件上
利用Import則可將提取出來的數據送回到Oracle數據庫中去。
要讀寫數據文件內數據,所以數據庫必須open
不能備份活躍頻繁數據
exp滿足select的所有特性 比如權限或讀一致性
imp滿足insert的所有特性 比如主鍵或空間不足
Oracle支持三種方式基本類型的輸出:
1.表方式(T方式),將指定表的數據導出。
2.用戶方式(U方式),將指定用戶的所有對象及數據導出。
3.全庫方式(Full方式),數據庫中的所有對象導出。
數據導入(Import)的過程是數據導出(Export)的逆過程,
分別將數據文件導入數據庫和將數據庫數據導出到數據文件。
交互模式導出exp表
[oracle@dba ~]$ exp
Export: Release 10.2.0.1.0 - Production on Mon Apr 4 16:58:41 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Username: scott
Password:
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Enter array fetch buffer size: 4096 >
Export file: expdat.dmp > scott_tab_emp.dmp
(2)U(sers), or (3)T(ables): (2)U > T
Export table data (yes/no): yes >
Compress extents (yes/no): yes >
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
Table(T) or Partition(T:P) to be exported: (RETURN to quit) > emp
. . exporting table EMP 14 rows exported
Table(T) or Partition(T:P) to be exported: (RETURN to quit) > dept
. . exporting table DEPT 4 rows exported
Table(T) or Partition(T:P) to be exported: (RETURN to quit) > 回車退出
Export terminated successfully with warnings.
[oracle@dba ~]$
查看備份文件
[oracle@dba ~]$ ll -thr scott_tab_emp.dmp
-rw-r--r-- 1 oracle oinstall 24K 04-04 17:00 scott_tab_emp.dmp
[oracle@dba ~]$ file scott_tab_emp.dmp
scott_tab_emp.dmp: DBase 3 data file (1380929624 records)
[oracle@dba ~]$ strings scott_tab_emp.dmp
導出的文件是二進制文件 如果要經歷FTP傳輸 一定要使用binary傳輸模式傳 如果使用ASCII模式傳輸會損壞文件
windows cmd里的ftp 默認是ascii傳輸模式
linux lftp ftp等工具默認基本都是binary模式
修改方法
C:\>ftp
ftp> status
Not connected.
Type: ascii; Verbose: On ; Bell: Off ; Prompting: On ; Globbing: On
Debugging: Off ; Hash mark printing: Off .
ftp>
ftp> help
Commands may be abbreviated.
! delete
? debug
append dir
ascii disconnect
bell get
binary glob
bye hash
cd help
close lcd
ftp>
[oracle@dba ~]$ ftp 127.0.0.1
Connected to 127.0.0.1 (127.0.0.1).
220 (vsFTPd 2.0.5)
Name (127.0.0.1:oracle):
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> status
Connected to 127.0.0.1.
No proxy connection.
Mode: stream; Type: binary; Form: non-print; Structure: file
Verbose: on; Bell: off; Prompting: on; Globbing: on
Store unique: off; Receive unique: off
Case: off; CR stripping: on
Ntrans: off
Nmap: off
Hash mark printing: off; Use of PORT cmds: on
Tick counter printing: off
ftp>
[oracle@dba ~]$ lftp oracle@127.0.0.1
口令:
lftp oracle@127.0.0.1:~> help get
用法: get [OPTS] <rfile> [-o <lfile>]
Retrieve remote file <rfile> and store it to local file <lfile>.
.........
-a use ascii mode (binary is the default)
.........
lftp oracle@127.0.0.1:~> help put
用法: put [OPTS] <lfile> [-o <rfile>]
Upload <lfile> with remote name <rfile>.
.........
-a use ascii mode (binary is the default)
.........
lftp oracle@127.0.0.1:~>
備份出來的是什么內容 只是對象的元數據 + 用戶數據
[oracle@dba exp_dir]$ strings cmd_scott_tab.dmp | grep -i 'CREATE' --color
CREATE TABLE "EMP" ("EMPNO" NUMBER(4, 0), "ENAME" VARCHAR2(10), ....
CREATE UNIQUE INDEX "PK_EMP" ON "EMP" ("EMPNO" ) ....
CREATE TABLE "DEPT" ("DEPTNO" NUMBER(2, 0), "DNAME" VARCHAR2(14), ....
CREATE UNIQUE INDEX "PK_DEPT" ON "DEPT" ("DEPTNO" ) ....
命令模式備份表
[oracle@dba exp_dir]$ exp USERID=SCOTT/seker TABLES=EMP,DEPT FILE=cmd_scott_tab.dmp
Export: Release 10.2.0.1.0 - Production on Mon Apr 4 17:16:22 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table EMP 14 rows exported
. . exporting table DEPT 4 rows exported
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
交互模式備份schema
[oracle@dba exp_dir]$ exp
Export: Release 10.2.0.1.0 - Production on Mon Apr 4 17:37:00 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Username: scott
Password:
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Enter array fetch buffer size: 4096 >
Export file: expdat.dmp > schema_scott.dmp
(2)U(sers), or (3)T(ables): (2)U > U
Export grants (yes/no): yes >
Export table data (yes/no): yes >
Compress extents (yes/no): yes >
Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
...............
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
命令模式備份schema
[oracle@dba exp_dir]$ exp userid=scott/seker file=schema.scott.dmp
.............
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
只導出部分數據
[oracle@dba exp_dir]$ exp userid=scott/seker tables=emp query=\'where deptno=10\' statistics=none file=./emp_deptno_10.dmp log=./emp_deptno_10.log
表上有統計信息的時候會報EXP-0091錯誤,添加statistics=none即可
Export: Release 10.2.0.1.0 - Production on Mon Apr 4 17:59:29 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table EMP
.
3 rows exported
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
只導出表結構
導出時一定要加COMPRESS=n的選項 否則導入表是原始表的高水位 且沒數據
一個表大小是5M
COMPRESS=n 時得到元數據: STORAGE(INITIAL 65536)
COMPRESS=y(默認) 時得到元數據:STORAGE(INITIAL 5242880) 這樣創建的表很大 又沒數據
[oracle@dba ~]$ exp scott/seker tables=ob rows=n file=./ob.dmp
[oracle@dba ~]$ exp scott/seker tables=ob rows=n COMPRESS=n file=./ob2.dmp
SQL> select segment_name,sum(bytes)/1024/1024 from user_extents where segment_name='OB' group by segment_name;
SEGMENT_NAME SUM(BYTES)/1024/1024
------------------------- --------------------
OB 5
SQL> drop table OB PURGE;
Table dropped.
SQL> !
[oracle@dba ~]$ ls
Desktop ob2.dmp ob.dmp
[oracle@dba ~]$ imp userid=scott/seker file=ob.dmp
[oracle@dba ~]$ exit
SQL> select segment_name,sum(bytes)/1024/1024 from user_extents where segment_name='OB' group by segment_name;
SEGMENT_NAME SUM(BYTES)/1024/1024
------------------------- --------------------
OB 5
SQL> !
[oracle@dba ~]$ exit
exit
SQL> drop table ob purge;
Table dropped.
SQL> !
[oracle@dba ~]$ imp userid=scott/seker file=ob2.dmp
SQL> select segment_name,sum(bytes)/1024/1024 from user_extents where segment_name='OB' group by segment_name;
SEGMENT_NAME SUM(BYTES)/1024/1024
------------------------- --------------------
OB .0625
SQL>
使用參數文件
[oracle@dba exp_dir]$ cat exp.txt
userid=scott/seker
tables=emp
query='where deptno=10'
statistics=none
file=./emp_deptno_10.dmp
buffer=100000
feedback=2
log=./emp_deptno_10.log
[oracle@dba exp_dir]$ exp PARFILE=exp.txt
Export: Release 10.2.0.1.0 - Production on Mon Apr 4 17:59:52 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table EMP
.
3 rows exported
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
當query中帶有單引號時 用兩個引號代替
[oracle@dba exp_dir]$ cat exp.txt
userid=scott/seker
tables=emp
query='where deptno=10 and ename=''KING'''
statistics=none
file=./emp_deptno_10.dmp
buffer=100000
feedback=2
log=./emp_deptno_10.log
[oracle@dba exp_dir]$
閃回導出(導出歷史上某一時間點的數據狀態,依賴undo老鏡像):
--/home/oracle/exp.txt--
userid=system/oracle
tables=scott.emp
file=/home/oracle/expdept.dmp
flashback_time="to_timestamp('2011-03-28 15:25:06','yyyy-mm-dd hh24:mi:ss')"
buffer=100000
feedback=5000
log=/home/oracle/expdept.log
備份的常用參數
FEEDBACK display progress every x rows (0)
FILESIZE maximum size of each dump file
INDEXES export indexes (Y)
TRIGGERS export triggers (Y)
LOG log file of screen output
導入imp
指定表導入
[oracle@dba exp_dir]$ imp userid=scott/seker file=schema.scott.dmp tables=t1 ;
Import: Release 10.2.0.1.0 - Production on Mon Apr 4 18:36:26 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
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table "T1" 24672 rows imported
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
導入帶有外鍵約束的表 要連同主鍵表一起帶入 否則建立外鍵報錯 帶有trigger的級聯表也要這樣
[oracle@dba exp_dir]$ imp userid=scott/seker file=schema.scott.dmp tables=emp ;
Import: Release 10.2.0.1.0 - Production on Mon Apr 4 18:36:59 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
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table "EMP" 14 rows imported
IMP-00017: following statement failed with ORACLE error 942:
"ALTER TABLE "EMP" ADD CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO") REFEREN"
"CES "DEPT" ("DEPTNO") ENABLE NOVALIDATE"
IMP-00003: ORACLE error 942 encountered
ORA-00942: table or view does not exist
About to enable constraints...
IMP-00017: following statement failed with ORACLE error 2430:
"ALTER TABLE "EMP" ENABLE CONSTRAINT "FK_DEPTNO""
Import terminated successfully with warnings.
[oracle@dba exp_dir]$
SQL> drop table emp purge;
Table dropped.
SQL>
[oracle@dba exp_dir]$ imp userid=scott/seker file=schema.scott.dmp tables=emp,dept ;
Import: Release 10.2.0.1.0 - Production on Mon Apr 4 18:38:09 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
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table "DEPT" 4 rows imported
. . importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
導入到其他用戶
這樣導入到的是system模式中去了
[oracle@dba exp_dir]$ imp userid=system/oracle file=schema.scott.dmp tables=emp,dept ;
Import: Release 10.2.0.1.0 - Production on Mon Apr 4 18:41:30 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SYSTEM
. importing SCOTT's objects into SYSTEM
. . importing table "DEPT" 4 rows imported
. . importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
使用touser參數 導入到指定用戶 但userid的用戶必須有權限 覆蓋touser的權限的人才可以這樣做
[oracle@dba exp_dir]$ imp userid=system/oracle touser=scott file=schema.scott.dmp tables=emp,dept ;
Import: Release 10.2.0.1.0 - Production on Mon Apr 4 18:42:01 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. . importing table "DEPT" 4 rows imported
. . importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
導入時追加數據
表不能有主鍵
正常導入時先建表 表存在就報錯
加ignore=y選項 就是忽略表存在 追加數據
導入到其他用戶
SQL> create tablespace ts99 datafile '/u01/oracle/oradata/ora10g/ts99.dbf' size 20M;
Tablespace created.
SQL> create user u99 default tablespace ts99 identified by u99;
User created.
SQL> grant connect,resource to u99;
Grant succeeded.
SQL>
SQL> select owner,SEGMENT_NAME,TABLESPACE_NAME from dba_extents where SEGMENT_NAME in ('EMP','DEPT');
no rows selected
SQL>
[oracle@dba exp_dir]$ imp userid=system/oracle touser=u99 file=schema.scott.dmp tables=emp,dept ;
導入成功 但存儲位置卻不是 ts99 這個u99的默認表空間 沒達到我們的目的
SQL> select owner,SEGMENT_NAME,TABLESPACE_NAME from dba_extents where SEGMENT_NAME in ('EMP','DEPT');
OWNER SEGMENT_NAME TABLESPACE_NAME
------------------------------ ------------------------- ---------------
U99 DEPT USERS
U99 EMP USERS
SQL>
原因是U99用戶具備所有表空間不受限的權限 剔除這個權限 只給他默認表空間的權限
刪除剛導入的表 重新導入
SQL> revoke UNLIMITED TABLESPACE from u99;
Revoke succeeded.
SQL>
SQL> alter user u99 quota unlimited on ts99;
User altered.
SQL>
[oracle@dba exp_dir]$ imp userid=system/oracle touser=u99 file=schema.scott.dmp tables=emp,dept ;
SQL> select owner,SEGMENT_NAME,TABLESPACE_NAME from dba_extents where SEGMENT_NAME in ('EMP','DEPT');
OWNER SEGMENT_NAME TABLESPACE_NAME
------------------------------ ------------------------- ---------------
U99 DEPT TS99
U99 EMP TS99
SQL> 達到目的
導入時追加數據
表不能有主鍵
正常導入時先建表 表存在就報錯
加ignore=y選項 就是忽略表存在 追加數據
導入的常用參數
SHOW just list file contents (N)
COMMIT commit array insert (N)
BUFFER size of data buffer
很重要的兩個參數 大表我們必須限制使用批量提交 以免回滾段小等問題
commit=y --降低導入的時候對回滾的壓力。
n -- 每張表數據導入完成后提交;
y -- buffer滿了以后就提交,一定要增大buffer的尺寸。
FULL import entire file (N)
indexfile=a.dmp
indexes=N
備份表空間模式
只是備份表空間中的存儲對象 並不備份表空間自身的結構
[oracle@dba exp_dir]$ exp userid=system/oracle tablespaces=ts99 file=./tbs_ts99.dmp log=./tbs_ts99.log
SQL> drop tablespace ts99 including contents and datafiles;
Tablespace dropped.
SQL>
[oracle@dba exp_dir]$ imp userid=system/oracle file=./tbs_ts99.dmp full=y
.........
IMP-00003: ORACLE error 959 encountered
ORA-00959: tablespace 'TS99' does not exist
IMP-00017: following statement failed with ORACLE error 959:
.........
SQL> create tablespace ts99 datafile '/u01/oracle/oradata/ora10g/ts99.dbf' size 20M;
Tablespace created.
SQL>
[oracle@dba exp_dir]$ imp userid=system/oracle file=./tbs_ts99.dmp full=y
Import: Release 10.2.0.1.0 - Production on Tue Apr 5 03:18:47 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
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SYSTEM's objects into SYSTEM
. importing U99's objects into U99
. . importing table "DEPT" 4 rows imported
. . importing table "EMP" 14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
全庫模式:
exp parfile=c:\exp.txt
----------------------
userid=system/oracle
full=y
filesize=50m 定義文件大小,切割備份,為將來傳輸方便。
file=(c:\exp_full_1.dmp,c:\exp_full_2.dmp) 定義各種文件的名字,如果名字不夠的話,導出進程會掛起要文件名字。
buffer=10000000
feedback=10000
log=c:\exp_full.log
imp parfile=c:\imp.txt
-----------------------
userid=system/oracle
full=y
filesize=50m
file=(c:\exp_full_1.dmp,c:\exp_full_2.dmp)
buffer=10000000
feedback=10000
log=c:\imp_full.log
表空間傳輸
從一個數據庫中將一個表空間遷移至另一個數據庫
1.將本地表空間設置為只讀
2.導出表空間元數據,將導出文件和表空間包含的數據文件拿到遠程
3.遠程庫要手動建立和本地同樣的用戶
4.導入表空間元數據,並指定原來的datafile
5.本地和遠程表空間都恢復讀寫 正常使用
1.將本地表空間設置為只讀
SQL> alter tablespace ts99 read only;
Tablespace altered.
SQL>
2.導出表空間元數據,將導出文件和表空間包含的數據文件拿到遠程
[oracle@dba exp_dir]$ exp userid=\''sys/oracle as sysdba'\' tablespaces=ts99 transport_tablespace=y file=./trs_tbs_ts99.dmp
Export: Release 10.2.0.1.0 - Production on Tue Apr 5 03:27:01 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace TS99 ...
. exporting cluster definitions
. exporting table definitions
. . exporting table DEPT
. . exporting table EMP
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.
[oracle@dba exp_dir]$
SQL> ! cp /u01/oracle/oradata/ora10g/ts99.dbf /u01/oracle/oradata/orcl/
SQL>
3.遠程庫要手動建立和本地同樣的用戶
SQL> select file_name,file_id,tablespace_name from dba_data_files;
FILE_NAME FILE_ID TABLESPACE_NAME
--------------------------------------------- ---------- ---------------
/u01/oracle/oradata/orcl/users01.dbf 4 USERS
/u01/oracle/oradata/orcl/sysaux01.dbf 3 SYSAUX
/u01/oracle/oradata/orcl/undotbs01.dbf 2 UNDOTBS1
/u01/oracle/oradata/orcl/system01.dbf 1 SYSTEM
SQL> create user u99 identified by u99;
User created.
SQL> grant connect,resource to u99;
Grant succeeded.
SQL>
4.導入表空間元數據,並指定原來的datafile
[oracle@dba exp_dir]$ export ORACLE_SID=newdb
[oracle@dba exp_dir]$ imp userid=\''sys/oracle as sysdba'\' file=./trs_tbs_ts99.dmp tablespaces=ts99 transport_tablespace=y datafiles=\''/u01/oracle/oradata/orcl/ts99.dbf'\'
Import: Release 10.2.0.1.0 - Production on Tue Apr 5 03:36:10 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 AL32UTF8 character set and AL16UTF16 NCHAR character set
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing U99's objects into U99
. . importing table "DEPT"
. . importing table "EMP"
. importing SYS's objects into SYS
Import terminated successfully without warnings.
[oracle@dba exp_dir]$
SQL> select file_name,file_id,tablespace_NAME FROM DBA_DATA_FILES;
FILE_NAME FILE_ID TABLESPACE_NAME
--------------------------------------------- ---------- ---------------
/u01/oracle/oradata/newdb/users01.dbf 4 USERS
/u01/oracle/oradata/newdb/sysaux01.dbf 3 SYSAUX
/u01/oracle/oradata/newdb/undotbs01.dbf 2 UNDOTBS1
/u01/oracle/oradata/newdb/system01.dbf 1 SYSTEM
/u01/oracle/oradata/orcl/ts99.dbf 5 TS99
SQL>
5.本地和遠程表空間都恢復讀寫 正常使用
SQL> alter tablespace ts99 read write;
Tablespace altered.
SQL> conn u99/u99
Connected.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
SQL>