使用Oracle進(jìn)行數(shù)據(jù)庫備份與還原
目錄
- 一、邏輯備份
- 1.某一用戶(DBA權(quán)限)全庫備份:
- 2.某一用戶(DBA權(quán)限)備份庫中某些用戶:
- 3.某一用戶備份自身:
- 4.某一用戶備份自身某些表對象:
- 5.某一用戶(具有DBA權(quán)限)備份其他用戶某些表對象:
- 二、邏輯還原
- 1.使用全庫備份文件還原:
- ①使用全庫備份文件還原庫:
- ②使用全庫備份文件還原庫內(nèi)某些用戶(被還原用戶應(yīng)存在):
- ③使用全庫備份文件還原庫內(nèi)某些用戶某些表:
- 2.使用某些用戶備份文件還原:
- ①使用某些用戶備份文件還原庫內(nèi)某些用戶(被還原用戶應(yīng)存在):
- ②使用某些用戶備份文件還原庫內(nèi)某些用戶的某些表:
- 3.使用某一用戶自身備份文件還原:
- ①使用某一用戶自身備份文件還原庫內(nèi)某一用戶:
- ②使用某一用戶自身備份文件還原庫內(nèi)某用戶某些表:
- 4.使用某些表備份文件還原:
Oracle中的備份與恢復(fù)區(qū)分為:邏輯備份和物理備份。其中物理備份區(qū)分為兩類:冷備份和熱備份
一、邏輯備份
邏輯備份指利用exp命令進(jìn)行備份,其簡單易行,不會影響正常的數(shù)據(jù)庫操作。可以使用exp -?查看其參數(shù)選項(xiàng),以實(shí)現(xiàn)不同的導(dǎo)出策略
其中常用參數(shù)包括:full=y、owner=()、tables=()
①不使用任何參數(shù):DBA用戶、非DBA用戶都可備份自身全部對象,對應(yīng)情況3
②full=y參數(shù):僅DBA用戶使用,備份全庫(可通過日志查看其備份內(nèi)容),對應(yīng)情況1。非DBA用戶使用會報(bào)錯
③owner參數(shù):DBA用戶使用可備份自身及其他多個用戶下全部對象,對應(yīng)情況2。非DBA用戶使用,參數(shù)內(nèi)容僅能為自身用以備份自身對象,對應(yīng)情況3,若參數(shù)內(nèi)容有其他用戶會報(bào)錯
④tables參數(shù):DBA用戶使用可備份自身及其他用戶下多張表,對應(yīng)情況4、5。非DBA用戶使用參數(shù)內(nèi)容僅可為自身所有表,對應(yīng)情況4,若參數(shù)內(nèi)容有其他用戶所有表將報(bào)錯
1.某一用戶(DBA權(quán)限)全庫備份:
當(dāng)命令未指定登錄到哪個數(shù)據(jù)庫實(shí)例,將使用系統(tǒng)環(huán)境變量ORACLE_SID所指定的數(shù)據(jù)庫實(shí)例(系統(tǒng)默認(rèn)數(shù)據(jù)庫實(shí)例,一般為最后安裝的數(shù)據(jù)庫實(shí)例)
此命令將默認(rèn)數(shù)據(jù)庫orcl全庫導(dǎo)出(需要正確的system用戶密碼)
exp system/orcl file=d:\defaulsid_full.dmp full=y # 如需同步導(dǎo)出日志表:exp system/orcl file=d:\defaultsid_full.dmp log=d:\defaultsid_full.log full=y
此命令將orcl數(shù)據(jù)庫全庫導(dǎo)出(需要正確的system用戶密碼)
exp system/orcl@orcl file=d:\orcl_full.dmp full=y
非DBA用戶使用full=y參數(shù)會報(bào)錯:
exp scott/scott@orcl file=d:\1.dmp full=y # EXP-00023:必須是DBA才能執(zhí)行完整數(shù)據(jù)庫或表空間導(dǎo)出操作
2.某一用戶(DBA權(quán)限)備份庫中某些用戶:
exp system/orcl@orcl file=d:\test_scott.dmp owner=(test, scott)
exp scott/scott@orcl file=d:\1.dmp owner=(test, scott) # EXP-00032:非DBA不能導(dǎo)出其他用戶
exp system/orcl@orcl file=d:\scott.dmp owner=scott # 成功將scott用戶下全部對象導(dǎo)出為scott.dmp
3.某一用戶備份自身:
exp scott/scott@orcl file=d:\scott.dmp # 同exp scott/scott@orcl file=d:\scott.dmp owner=scott exp system/orcl@orcl file=d:\system.dmp # 同exp system/orcl@orcl file=d:\system.dmp owner=system
4.某一用戶備份自身某些表對象:
exp scott/scott@orcl file=d:\scott_tables.dmp tables=(emp, dept) exp scott/scott@orcl file=d:\scott_dept.dmp tables=dept
5.某一用戶(具有DBA權(quán)限)備份其他用戶某些表對象:
exp system/orcl@orcl file=d:\scott_bonus_salgrade.dmp tables=(scott.bonus, scott.salgrade) exp system/orcl@orcl file=d:\scott_dept.dmp tables=scott.dept
exp system/orcl@orcl file=d:\1.dmp owner=scott tables=(bonus, salgrade) # EXP-00026:指定了沖突模式
備份總結(jié):
①不使用任何參數(shù)將備份用戶自身全部對象
②DBA用戶方有權(quán)限進(jìn)行全庫備份、其他用戶備份、其他用戶對象備份
③DBA用戶使用full=y參數(shù)會進(jìn)行全庫備份,非DBA用戶使用full=y會報(bào)錯
④DBA用戶使用owner=()參數(shù)會備份()中的用戶下全部對象(多個或單個)。非DBA用戶不能備份其他用戶,使用owner參數(shù)(參數(shù)內(nèi)容為自身)或不使用任何參數(shù)可以備份自身
⑤DBA用戶使用tables=()參數(shù)可以備份自身表對象或其他用戶表對象,非DBA用戶只能備份自身表對象,tables參數(shù)不可以與owner參數(shù)同時使用
二、邏輯還原
常用參數(shù):FULL=Y、FROMUSER=()、TOUSER=()、TABLES=()
1.使用全庫備份文件還原:
①使用全庫備份文件還原庫:
imp system/orcl@orcl file=d:\orcl_full.dmp # IMP-00031:必須指定FULL=y或提供FROMUSER/TOUSER或TABLES參數(shù) imp system/orcl@orcl file=d:\orcl_full.dmp FULL=Y # FULL=Y語句相當(dāng)于將orcl_full.dmp中所有對象還原于相應(yīng)的orcl庫中的對象 # 執(zhí)行后警告很多,多數(shù)語句執(zhí)行失敗,成功將刪掉的test、scott用戶還原并且還原了其中的表、函數(shù)等對象
②使用全庫備份文件還原庫內(nèi)某些用戶(被還原用戶應(yīng)存在):
首先刪除用戶test與scott,隨后使用命令還原
imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test # IMP-00003:遇到oracle錯誤1435 ORA-01435:用戶不存在 imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test touser=scott, test # IMP-00003:遇到oracle錯誤1435 ORA-01435:用戶不存在
觀察到全庫備份文件還原庫內(nèi)某些用戶時,被還原用戶必須存在。直接還原庫:
imp system/orcl@orcl file=d:\orcl_full.dmp FULL=Y
將test用戶的函數(shù)對象,scott用戶的表對象刪除,隨后使用命令還原:
imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test # 觀察到scott用戶的表對象,test的函數(shù)對象被成功還原 imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test TOUSER=scott, test # 觀察到scott用戶的表對象,test的函數(shù)對象被成功還原 imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=test # 觀察到orcl_full.dmp文件中的scott用戶對象被還原到已有用戶test下
③使用全庫備份文件還原庫內(nèi)某些用戶某些表:
首先刪除scott用戶下dept、emp表,隨后使用命令還原:
imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=scott TABLES=(dept, emp) # 可以觀察到soctt用戶被刪除的兩張表被成功還原 imp scott/scott@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=scott TABLES=(dept, emp) # IMP-00013:只有DBA才能導(dǎo)入由其他DBA導(dǎo)出的文件 imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=test TABLES=(dept, emp) # 可以觀察到orcl_full.dmp文件中的scott用戶的dept與emp成功被還原到已有用戶test下
使用全庫備份文件還原總結(jié):
①還原命令必須有FULL=Y、FROMUSER=()、TOUSER=()、TABLES=()等參數(shù)
②DBA用戶使用full=y參數(shù)會全庫還原(備份文件包含用戶的定義,所以可以還原被刪掉的用戶)
③DBA用戶僅使用FROMUSER參數(shù)時,會將FROMUSER參數(shù)內(nèi)的用戶的對象對應(yīng)還原(被還原用戶應(yīng)存在)
④DBA用戶使用FROMUSER與TOUSER參數(shù)時,會將FROMUSER參數(shù)內(nèi)的用戶的對象還原到TOUSER參數(shù)內(nèi)的用戶
⑤DBA用戶使用FROMUSER與TOUSER與TABLES參數(shù)時,會將FROMUSER參數(shù)內(nèi)的用戶內(nèi)的TABLES參數(shù)內(nèi)的表還原給TOUSER用戶
2.使用某些用戶備份文件還原:
①使用某些用戶備份文件還原庫內(nèi)某些用戶(被還原用戶應(yīng)存在):
imp system/orcl@orcl file=d:\system_scott.dmp full=y # 部分語句執(zhí)行失敗,原因XX已存在,scott被刪除的四張表被成功還原 imp scott/scott@orcl file=d:\system_scott.dmp full=y # IMP-00013:只有DBA才能導(dǎo)入由其他DBA導(dǎo)出的文件 imp scott/scott@orcl file=d:\system_scott.dmp fromuser=scott # IMP-00013:只有DBA才能導(dǎo)入由其他DBA導(dǎo)出的文件 imp system/orcl@orcl file=d:\system_scott.dmp fromuser=scott # 成功將scott用戶被刪除的四張表還原 imp system/orcl@orcl file=d:\system_scott.dmp fromuser=scott touser=test # 成功將system_scott.dmp文件中scott用戶對象還原到已有用戶test中
②使用某些用戶備份文件還原庫內(nèi)某些用戶的某些表:
imp system/orcl@orcl file=d:\system_scott.dmp fromuser=scott touser=test tables=(dept, emp) # 成功將system_scott.dmp文件中scott用戶的dept、emp表還原到已有用戶test中
使用某些用戶備份文件還原總結(jié):
①DBA用戶使用full=y參數(shù)會對應(yīng)還原某些用戶備份文件中的所有用戶的對象
②DBA用戶僅使用FROMUSER參數(shù),會將某些用戶備份文件中的FROMUSER參數(shù)內(nèi)用戶還原到已有的相應(yīng)用戶
③DBA用戶使用FROMUSER參數(shù)與TOUSER參數(shù),會將某些用戶備份文件中的FROMUSER參數(shù)內(nèi)用戶的對象還原到TOUSER參數(shù)內(nèi)用戶
④DBA用戶使用FROMUSER與TOUSER與TABLES參數(shù)時,會將某些用戶備份文件中的 FROMUSER參數(shù)內(nèi)用戶內(nèi)的 TABLES參數(shù)內(nèi)的 表還原給TOUSER用戶
3.使用某一用戶自身備份文件還原:
①使用某一用戶自身備份文件還原庫內(nèi)某一用戶:
imp scott/scott@orcl file=d:\scott.dmp full=y # 成功將scott被刪除的四張表還原 imp system/orcl@orcl file=d:\scott.dmp full=y # 觀察到將scott.dmp文件中的對象還原到system用戶中 imp system/orcl@orcl file=d:\scott.dmp fromuser=scott # 觀察到將scott.dmp文件中的對象還原到system用戶中 imp system/orcl@orcl file=d:\scott.dmp touser=scott # IMP-00031:必須指定FULL=Y或提供FROMUSER/TOUSER或TABLES參數(shù) imp system/orcl@orcl file=d:\scott.dmp fromuser=scott touser=scott # 成功將scott被刪除的四張表還原
②使用某一用戶自身備份文件還原庫內(nèi)某用戶某些表:
imp scott/scott@orcl file=d:\scott.dmp tables=(dept, emp) # 成功將scott被刪除的兩張表還原 imp scott/scott@orcl file=d:\scott.dmp touser=test tables=(dept, emp) # IMP-00007:必須是DBA才能將對象導(dǎo)入另一用戶 imp system/orcl@orcl file=d:\scott.dmp touser=test tables=(dept, emp) # 成功將scott.dmp文件中的表dept與emp導(dǎo)入test
使用某一用戶備份文件還原總結(jié):
①非DBA用戶使用非DBA用戶導(dǎo)出備份文件,使用FULL=Y參數(shù)會將某一用戶備份文件內(nèi)對象還原到自身
②DBA用戶使用FULL=Y參數(shù),會將某一用戶備份文件內(nèi)對象還原到自身
③DBA用戶僅使用FROMUSER參數(shù),會將某一用戶備份文件內(nèi)對象還原到自身(FROMUSER參數(shù)要與導(dǎo)出用戶匹配)
(此處與使用某些用戶備份文件還原有區(qū)別,即與上述總結(jié)第2點(diǎn)有區(qū)別)
④DBA用戶使用FROMUSER參數(shù)與TOUSER參數(shù),會將某一用戶備份文件內(nèi)對象還原到TOUSER參數(shù)用戶(FROMUSER參數(shù)要與導(dǎo)出用戶匹配)
⑤用戶使用自身導(dǎo)出備份文件,僅使用TABLES參數(shù)可還原參數(shù)內(nèi)表
⑥D(zhuǎn)BA用戶使用TOUSER與TABLES參數(shù),會將某一用戶備份文件內(nèi)TABLES參數(shù)內(nèi)的表還原到TOUSER參數(shù)內(nèi)用戶
4.使用某些表備份文件還原:
區(qū)分兩種情況:某些表備份文件由自己導(dǎo)出(非DBA)還是由DBA用戶導(dǎo)出,如若為自己導(dǎo)出(非DBA),則情況如下:
# scott_tables.dmp為使用scott用戶導(dǎo)出的表bonus, salgrade # 執(zhí)行下面語句: imp system/orcl@orcl file=d:\scott_tables.dmp full=y # 成功將scott_tables.dmp內(nèi)所有表bonus、salgrade導(dǎo)入system # 刪掉system用戶下bonus表,執(zhí)行下面語句: imp system/orcl@orcl file=d:\scott_tables.dmp tables=bonus # 成功將表bonus導(dǎo)入system用戶 imp system/orcl@orcl file=d:\scott_tables.dmp touser=test tables=bonus # 成功將表bonus導(dǎo)入用戶test
如若某些表備份文件由其他DBA用戶導(dǎo)出,則情況如下:
# scott_tables.dmp為使用system用戶導(dǎo)出的scott.bonus, scott.salgrade # 刪除表:scott.bonus, scott.salgrade # 執(zhí)行下面的還原語句: imp system/orcl@orcl file=d:\scott_tables.dmp full=y # 觀察到成功將scott.bonus, scott.salgrade表還原到scott # 刪掉scott用戶下bonus表,執(zhí)行下面語句: imp system/orcl@orcl file=d:\scott_tables.dmp fromuser=scott touser=scott tables=bonus # 成功將備份文件中的bonus表還原到scott用戶下 imp system/orcl@orcl file=d:\scott_tables.dmp fromuser=scott touser=test tables=bonus # 成功將備份文件中bonus表還原到test用戶下
到此這篇關(guān)于使用Oracle進(jìn)行數(shù)據(jù)庫備份與還原的文章就介紹到這了,更多相關(guān)Oracle數(shù)據(jù)庫備份與還原內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
