解密Oracle備份工具-exp/imp
無論是運維工作者還是數據庫管理員,數據的備份和還原是我們日常工作的重點,制定合理的備份策略,使用合適的備份工具是每個IT人必備的技能,今天就給大家介紹Oracle的備份工具exp和imp。
ORACLE數據庫有兩類備份方法。
第一類:爲物理備份,該方法實現數據庫的完整恢復,但數據庫必須運行在歸擋模式下(業務數據庫在非歸擋模式下運行),且需要極大的外部存儲設備,例如磁帶庫;
第二類:備份方式爲邏輯備份,業務數據庫採用此種方式,此方法不需要數據庫運行在歸擋模式下,不但備份簡單,而且可以不需要外部存儲設備。
一、exp的關鍵字說明:
1、導出某用戶下所有表
exp scott/lipengfeifile=scott_all_tables.dmp log=scott_all_tables.log
2、導出scott用戶下的部分表
(1)expscott/lipengfei tables=\(emp,salgrade\) file=scott_emp_salgrade.dmplog=scott_emp_salgrade.log (2)exp scott/lipengfei tables=empfile=scott_emp.dmp log=scott_emp.log
3、參數文件的使用
vi /home/oracle/dept.txt 文件內容如下: userid=scott/lipengfei log=/home/oracle/scott_dept.log file=/home/oracle/scott_dept.dmp tables=dept
引用參數文件:
exp parfile=/home/oracle/dept.txt
4、按條件導出
(1)參數文件中指定條件
exp parfile=/home/oracle/emp.txt vi /home/oracle/emp.txt 內容如下: userid=scott/lipengfei log=/home/oracle/emp.log file=/home/oracle/emp.dmp tables=emp query='where sal>1000'
(2)條件中是數字
exp scott/lipengfei tables=empquery="'where sal >1000'" file=/home/oracle/emp.dmplog=/home/oracle/emp.log
(3)條件中帶有字符串
exp scott/lipengfei tables=empquery="'where sal >1000 and job=''CLERK'''"file=/home/oracle/emp.dmp log=/home/oracle/emp.log
(4)參數文件,處理條件中帶有字符串
exp parfile=/home/oracle/emp.txt vi /home/oracle/emp.txt 內容如下: userid=scott/lipengfei log=/home/oracle/emp.log file=/home/oracle/emp.dmp tables=emp query='where sal>1000 and job=''CLERK'''
5、導出某幾個用戶的所有表
(1)創建表空間及用戶、授權
create tablespace li datafile'/oracle/app/oradata/ecom/li.dbf' size 30M AUTOEXTEND OFF; create user li identified by li defaulttablespace li; alter user li account unlock; grant connect,resource to li;
(2)創建表空間及用戶、授權
create tablespace peng datafile'/oracle/app/oradata/ecom/peng.dbf' size 30M AUTOEXTEND OFF; create user peng identified by peng defaulttablespace peng; alter user peng account unlock; grant connect,resource to peng;
(3)創建表空間及用戶、授權
create tablespace fei datafile'/oracle/app/oradata/ecom/fei.dbf' size 30M AUTOEXTEND OFF; create user fei identified by fei defaulttablespace fei; alter user fei account unlock; grant connect,resource to fei;
(4)創建表及初始化數據
sqlplus li/li create table haha(id int); insert into haha values(1); commit;
(5)創建表及初始化數據
sqlplus peng/peng create table hehe(id int); insert into hehe values(1); commit;
(6)創建表及初始化數據
sqlplus fei/fei create table hihi(id int); insert into hihi values(1); commit;
(7)將上面3個用戶全部對象導出
exp \'sys/lipengfei as sysdba\'file=/home/oracle/li_peng_fei.dmp log=/home/oracle/li_peng_fei.logowner=\(li,peng,fei\)
6、不想導出索引、不想導出約束、不想導出授權、不想導出與表相關的觸發器等
exp scott/lipengfeifile=scott_all_tables.dmp log=scott_all_tables.log indexes=N constraints=Ngrants=N triggers=N
7、導出的文件太大了,超出文件系統限制【fat32單個文件不能超過4G,ntfs單個文件不能超過2T,ext3理想情況下單個文件不能超過2T】
exp scott/lipengfei filesize=500M file=scott_all_tables1.dmpscott_all_tables2.dmp log=scott_all_tables.log
如果指定filesize參數,那麼file參數也要跟着修改。exp在導出的時候有可能會生成多個dmp文件,因此必須在file參數中爲每一個文件分別命名(多個名稱間以逗號分隔)
如果file參數指定的文件名多於實際生成的文件,多出指定的文件不會被生成。
如果file參數指定的文件名少於實際生成的文件,exp執行過程中,
在用完用戶所指定的文件後,就會提示輸入新的文件名。
如果沒有人在旁邊操作,那麼整個導出任務就會停在這裏了。
接着你可能就要問,怎麼知道要導出的數據一共佔用多大空間?
select sum(bytes)/1024/1024"total(M)" from user_segments;
二、imp關鍵字說明:
1、導入數據
(1)按用戶導出數據
exp li/li file=li_all_tables.dmplog=li_all_tables.log
(2)模擬數據丟失
sqlplus li/li SQL> drop table haha;
(3)將備份數據還原
imp li/li file=li_all_tables.dmplog=li_all_tables.log
2、導入指定表到其他用戶
(1)將li用戶下的備份集導入到peng用戶中
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log
(2)以peng用戶登錄,驗證數據
sqlplus peng/peng SQL> select tname from tab;
上面的操作看起來成功?其它並沒有,數據雖然成功導入了,但不是嚴謹的方式,可能無意中給數據庫埋了一顆雷。
SQL> show user SQL> select username,default_tablespacefrom user_users; SQL> select table_name,tablespace_namefrom user_tables;
奇怪嗎?雖然peng用戶默認的表空間是peng,但是新導入的haha表被存儲到li表空間中。如下方式解決
(3)表空間權限控制
sqlplus / as sysdba alter user peng quota unlimited on peng; revoke unlimited tablespace from peng;
(4)清空peng用戶剛導入的表及數據
sqlplus peng/peng drop table haha;
(5)將li用戶下的備份集再一次導入到peng用戶中
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y
(6)以peng用戶登錄,驗證數據
sqlplus peng/peng SQL> select table_name,tablespace_namefrom user_tables;
上面使用到了ignore參數,如果要導入的對象已經存在,默認情況導入就會報錯。
ignore=N 【默認】,出錯對象會被跳過,imp繼續後續操作。
ignore=Y,自動忽略對象已存在的事實,繼續導入數據,也就會出現重複數據,可能通過手工去重。
3、導入表結構到指定用戶
(1)登錄peng用戶,刪除指定表及數據
sqlplus peng/peng SQL> drop table haha;
(2)利用上面產生的備份集恢復,只還原表結構
imp peng/peng fromuser=li touser=pengfile=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y rows=N
(3)以peng用戶登錄,驗證是否只還原了表結構,沒有數據
sqlplus peng/peng SQL> select tname from tab; SQL> select * from haha;
很久很久以前,Oracle就開始提供用來提取表、模式或整個數據庫的定義,然後導入到其他模式或數據的小工具:那就是exp/imp
那個時候數據庫規模都很小(幾百M就算超大數據庫了),而且對於數據庫的要求也沒有那麼高,不像現如今,動不動就是7*24小時高併發、高可用,以至在某些領域,exp/imp也被視作備份恢復的工具使用並延續至今。如果你使用exp備份幾十G、數百G甚至更大規模數據庫,並且將這種方式作爲生產數據庫的備份策略,這就太不合理了。