笔记/Oracle数据库备份与还原

Oracle数据库备份与还原

 

Oracle中的备份与恢复区分为:逻辑备份和物理备份。其中物理备份区分为两类:冷备份和热备份

一、逻辑备份

逻辑备份指利用exp命令进行备份,其简单易行,不会影响正常的数据库操作。可以使用exp -?查看其参数选项,以实现不同的导出策略

其中常用参数包括:full=y、owner=()、tables=()

①不使用任何参数:DBA用户、非DBA用户都可备份自身全部对象,对应情况3

②full=y参数:仅DBA用户使用,备份全库(可通过日志查看其备份内容),对应情况1。非DBA用户使用会报错

③owner参数:DBA用户使用可备份自身及其他多个用户下全部对象,对应情况2。非DBA用户使用,参数内容仅能为自身用以备份自身对象,对应情况3,若参数内容有其他用户会报错

④tables参数:DBA用户使用可备份自身及其他用户下多张表,对应情况4、5。非DBA用户使用参数内容仅可为自身所有表,对应情况4,若参数内容有其他用户所有表将报错

 

1.某一用户(DBA权限)全库备份:

当命令未指定登录到哪个数据库实例,将使用系统环境变量ORACLE_SID所指定的数据库实例(系统默认数据库实例,一般为最后安装的数据库实例)

此命令将默认数据库orcl全库导出(需要正确的system用户密码)

exp system/orcl file=d:\defaulsid_full.dmp full=y

# 如需同步导出日志表:exp system/orcl file=d:\defaultsid_full.dmp log=d:\defaultsid_full.log full=y

2.某一用户(DBA权限)备份库中某些用户:

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不能导出其他用户

exp system/orcl@orcl file=d:\scott.dmp owner=scott

# 成功将scott用户下全部对象导出为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权限)备份其他用户某些表对象:

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

备份总结:

①不使用任何参数将备份用户自身全部对象

②DBA用户方有权限进行全库备份、其他用户备份、其他用户对象备份

③DBA用户使用full=y参数会进行全库备份,非DBA用户使用full=y会报错

④DBA用户使用owner=()参数会备份()中的用户下全部对象(多个或单个)。非DBA用户不能备份其他用户,使用owner参数(参数内容为自身)或不使用任何参数可以备份自身

⑤DBA用户使用tables=()参数可以备份自身表对象或其他用户表对象,非DBA用户只能备份自身表对象,tables参数不可以与owner参数同时使用

 

二、逻辑还原

常用参数:FULL=Y、FROMUSER=()、TOUSER=()、TABLES=()

 

1.使用全库备份文件还原:

①使用全库备份文件还原库:

imp system/orcl@orcl file=d:\orcl_full.dmp

# IMP-00031:必须指定FULL=y或提供FROMUSER/TOUSER或TABLES参数

imp system/orcl@orcl file=d:\orcl_full.dmp FULL=Y

# FULL=Y语句相当于将orcl_full.dmp中所有对象还原于相应的orcl库中的对象

# 执行后警告很多,多数语句执行失败,成功将删掉的test、scott用户还原并且还原了其中的表、函数等对象

②使用全库备份文件还原库内某些用户(被还原用户应存在):

首先删除用户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:用户不存在

观察到全库备份文件还原库内某些用户时,被还原用户必须存在。直接还原库:

imp system/orcl@orcl file=d:\orcl_full.dmp FULL=Y

将test用户的函数对象,scott用户的表对象删除,随后使用命令还原

imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test

# 观察到scott用户的表对象,test的函数对象被成功还原

imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott, test TOUSER=scott, test

# 观察到scott用户的表对象,test的函数对象被成功还原

imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=test

# 观察到orcl_full.dmp文件中的scott用户对象被还原到已有用户test下

③使用全库备份文件还原库内某些用户某些表:

首先删除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才能导入由其他DBA导出的文件

imp system/orcl@orcl file=d:\orcl_full.dmp FROMUSER=scott TOUSER=test TABLES=(dept, emp)

# 可以观察到orcl_full.dmp文件中的scott用户的dept与emp成功被还原到已有用户test下

使用全库备份文件还原总结:

①还原命令必须有FULL=Y、FROMUSER=()、TOUSER=()、TABLES=()等参数

②DBA用户使用full=y参数会全库还原(备份文件包含用户的定义,所以可以还原被删掉的用户)

③DBA用户仅使用FROMUSER参数时,会将FROMUSER参数内的用户的对象对应还原(被还原用户应存在)

④DBA用户使用FROMUSER与TOUSER参数时,会将FROMUSER参数内的用户的对象还原到TOUSER参数内的用户

⑤DBA用户使用FROMUSER与TOUSER与TABLES参数时,会将FROMUSER参数内的用户内的TABLES参数内的表还原给TOUSER用户

 

2.使用某些用户备份文件还原:

①使用某些用户备份文件还原库内某些用户(被还原用户应存在):

imp system/orcl@orcl file=d:\system_scott.dmp full=y

# 部分语句执行失败,原因XX已存在,scott被删除的四张表被成功还原

imp scott/scott@orcl file=d:\system_scott.dmp full=y

# IMP-00013:只有DBA才能导入由其他DBA导出的文件

imp scott/scott@orcl file=d:\system_scott.dmp fromuser=scott

# IMP-00013:只有DBA才能导入由其他DBA导出的文件

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中

②使用某些用户备份文件还原库内某些用户的某些表:

imp system/orcl@orcl file=d:\system_scott.dmp fromuser=scott touser=test tables=(dept, emp)

# 成功将system_scott.dmp文件中scott用户的dept、emp表还原到已有用户test中

使用某些用户备份文件还原总结:

①DBA用户使用full=y参数会对应还原某些用户备份文件中的所有用户的对象

②DBA用户仅使用FROMUSER参数,会将某些用户备份文件中的FROMUSER参数内用户还原到已有的相应用户

③DBA用户使用FROMUSER参数与TOUSER参数,会将某些用户备份文件中的FROMUSER参数内用户的对象还原到TOUSER参数内用户

④DBA用户使用FROMUSER与TOUSER与TABLES参数时,会将某些用户备份文件中的 FROMUSER参数内用户内的 TABLES参数内的 表还原给TOUSER用户

 

3.使用某一用户自身备份文件还原:

①使用某一用户自身备份文件还原库内某一用户:

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参数

imp system/orcl@orcl file=d:\scott.dmp fromuser=scott touser=scott

# 成功将scott被删除的四张表还原

②使用某一用户自身备份文件还原库内某用户某些表:

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才能将对象导入另一用户

imp system/orcl@orcl file=d:\scott.dmp touser=test tables=(dept, emp)

# 成功将scott.dmp文件中的表dept与emp导入test

使用某一用户备份文件还原总结:

①非DBA用户使用非DBA用户导出备份文件,使用FULL=Y参数会将某一用户备份文件内对象还原到自身

②DBA用户使用FULL=Y参数,会将某一用户备份文件内对象还原到自身

③DBA用户仅使用FROMUSER参数,会将某一用户备份文件内对象还原到自身(FROMUSER参数要与导出用户匹配)

(此处与使用某些用户备份文件还原有区别,即与上述总结第2点有区别)

④DBA用户使用FROMUSER参数与TOUSER参数,会将某一用户备份文件内对象还原到TOUSER参数用户(FROMUSER参数要与导出用户匹配)

⑤用户使用自身导出备份文件,仅使用TABLES参数可还原参数内表

⑥DBA用户使用TOUSER与TABLES参数,会将某一用户备份文件内TABLES参数内的表还原到TOUSER参数内用户

 

4.使用某些表备份文件还原:

区分两种情况:某些表备份文件由自己导出(非DBA)还是由DBA用户导出,如若为自己导出(非DBA),则情况如下:

# scott_tables.dmp为使用scott用户导出的表bonus, salgrade

# 执行下面语句:

imp system/orcl@orcl file=d:\scott_tables.dmp full=y

# 成功将scott_tables.dmp内所有表bonus、salgrade导入system

# 删掉system用户下bonus表,执行下面语句:

imp system/orcl@orcl file=d:\scott_tables.dmp tables=bonus

# 成功将表bonus导入system用户

imp system/orcl@orcl file=d:\scott_tables.dmp touser=test tables=bonus

# 成功将表bonus导入用户test

如若某些表备份文件由其他DBA用户导出,则情况如下:

# scott_tables.dmp为使用system用户导出的scott.bonus, scott.salgrade

# 删除表:scott.bonus, scott.salgrade

# 执行下面的还原语句:

imp system/orcl@orcl file=d:\scott_tables.dmp full=y

# 观察到成功将scott.bonus, scott.salgrade表还原到scott

# 删掉scott用户下bonus表,执行下面语句:

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用户下

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

禅迦叶影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值