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