一、介紹
邏輯備份是指使用工具export將數據對象的結構和數據導出到文件的過程。
邏輯恢復是指當數據庫對象被誤操作而損壞后使用工具import利用備份的文件把數據對象導入到數據庫的過程。
物理備份即可在數據庫open的狀態下進行也可在關閉數據庫后進行,但是邏輯備份和恢復只能在open的狀態下進行。
二、備份(導出)
導出分為導出表、導出方案、導出數據庫三種方式。
導出使用exp命令來完成的,該命令常用的選項有:
userid:用於指定執行導出操作的用戶名,口令,連接字符串
tables:用於指定執行導出操作的表
owner:用於指定執行導出操作的方案
full=y:用於指定執行導出操作的數據庫
inctype:用於指定執行導出操作的增量類型
rows:用於指定執行導出操作是否要導出表中的數據
file:用於指定導出文件名
注意:特別說明-->在導入和導出的時候,要到oracle目錄的bin目錄下。
1)、導出表
1.導出自己的表
exp userid=scott/oracle@orcl tables=(emp) file=d:\emp.dmp --導出單個表
exp userid=scott/oracle@orcl tables=(emp,dept) file=d:\emp.dmp --導出多個表
eg、
C:\Users\jiqinlin>cd D:\dev\oracle\product\10.2.0\db_1\bin
C:\Users\jiqinlin>d:
D:\dev\oracle\product\10.2.0\db_1\bin>exp userid=scott/oracle@orcl tables=(emp) file=d:\emp.dmp
2.導出其它方案的表
如果用戶要導出其它方案的表,則需要dba的權限或是exp_full_database的權限,比如system就可以導出scott的表
D:\dev\oracle\product\10.2.0\db_1\bin>exp userid=system/oracle@orcl tables=(scott.emp) file=d:\emp.emp
D:\dev\oracle\product\10.2.0\db_1\bin>exp userid=system/oracle@orcl tables=(scott.emp,scott.dept) file=d:\emp.emp
3. 導出表的結構
exp userid=scott/oracle@orcl tables=(emp) file=d:\emp.dmp rows=n
4. 使用直接導出方式
exp userid=scott/oracle@orcl tables=(emp) file=d:\emp.dmp direct=y
這種方式比默認的常規方式速度要快,當數據量大時,可以考慮使用這樣的方法。
這時需要數據庫的字符集要與客戶端字符集完全一致,否則會報錯...
2)、導出方案
導出方案是指使用export工具導出一個方案或是多個方案中的所有對象(表,索引,約束...)和數據,並存放到文件中。
1. 導出自己的方案
exp userid=scott/oracle@orcl owner=scott file=d:\scott.dmp
2. 導出其它方案
如果用戶要導出其它方案,則需要dba的權限或是exp_full_database的權限,
比如system 用戶就可以導出任何方案
exp userid=system/oracle@orcl owner=(system,scott) file=d:\system.dmp
3)、導出數據庫
導出數據庫是指利用export導出所有數據庫中的對象及數據,要求該用戶具有dba的權限或者是exp_full_database權限
增量備份(好處是第一次備份后,第二次備份就快很多了)
exp userid=system/oracle@orcl full=y inctype=complete file=d:\all.dmp
三、恢復(導入)
導入就是使用工具import將文件中的對象和數據導入到數據庫中,但是導入要使用的文件必須是export所導出的文件。與導出相似,導入也分為導入表,導入方案,導入數據庫三種方式。
imp常用的選項有
userid:用於指定執行導入操作的用戶名,口令,連接字符串
tables:用於指定執行導入操作的表
formuser:用於指定源用戶
touser:用於指定目標用戶
file 用於指定導入文件名
full=y:用於指定執行導入整個文件
inctype:用於指定執行導入操作的增量類型
rows:指定是否要導入表行(數據)
ignore:如果表存在,則只導入數據
1)導入表
1. 導入自己的表
imp userid=scott/oracle@orcl tables=(emp) file=d:\xx.dmp
2. 導入表到其它用戶
要求該用戶具有dba的權限,或是imp_full_database
imp userid=system/oracle@orcl tables=(emp) file=d:\xx.dmp touser=scott
3. 導入表的結構
只導入表的結構而不導入數據
imp userid=scott/oracle@orcl tables=(emp) file=d:\xx.dmp rows=n
4. 導入數據
如果對象(如比表)已經存在可以只導入表的數據
imp userid=scott/oracle@orcl tables=(emp) file=d:\xx.dmp ignore=y
2)導入方案
導入方案是指使用import工具將文件中的對象和數據導入到一個或是多個方案中。如果要導入其它方案,要求該用戶具有dba 的權限,或者imp_full_database
1.導入自身的方案
imp userid=scott/oracle@orcl file=d:\xxx.dmp
2.導入其它方案
要求該用戶具有dba的權限
imp userid=system/oracle@orcl file=d:\xxx.dmp fromuser=system touser=scott
3)導入數據庫(相當於數據庫遷移)
在默認情況下,當導入數據庫時,會導入所有對象結構和數據,案例如下:
imp userid=system/oracle@orcl full=y file=d:\xxx.dmp