Home > Database > Oracle > body text

Completely master Oracle database backup and restore

WBOY
Release: 2022-07-12 20:37:47
forward
5341 people have browsed it

本篇文章给大家带来了关于Oracle的相关知识,其中主要介绍了数据库备份与还原的相关问题,Oracle中的备份与恢复区分为:逻辑备份和物理备份。其中物理备份区分为两类:冷备份和热备份,下面一起来看一下,希望对大家有帮助。

Completely master Oracle database backup and restore

推荐教程:《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
Copy after login

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

exp system/orcl@orcl file=d:\orcl_full.dmp full=y
Copy after login

非DBA用户使用full=y参数会报错:

exp scott/scott@orcl file=d:\1.dmp full=y
# EXP-00023:必须是DBA才能执行完整数据库或表空间导出操作
Copy after login

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

exp system/orcl@orcl file=d:\test_scott.dmp owner=(test, scott)
Copy after login
exp scott/scott@orcl file=d:\1.dmp owner=(test, scott)
# EXP-00032:非DBA不能导出其他用户
Copy after login
exp system/orcl@orcl file=d:\scott.dmp owner=scott
# 成功将scott用户下全部对象导出为scott.dmp
Copy after login

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
Copy after login

4.某一用户备份自身某些表对象:

exp scott/scott@orcl file=d:\scott_tables.dmp tables=(emp, dept)
exp scott/scott@orcl file=d:\scott_dept.dmp tables=dept
Copy after login

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
Copy after login
exp system/orcl@orcl file=d:\1.dmp owner=scott tables=(bonus, salgrade)# EXP-00026:指定了冲突模式
Copy after login

备份总结:

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

  • 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用户还原并且还原了其中的表、函数等对象
Copy after login

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

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

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

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

将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下
Copy after login

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

首先删除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下
Copy after login

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

  • 还原命令必须有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中
Copy after login

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

imp system/orcl@orcl file=d:\system_scott.dmp fromuser=scott touser=test tables=(dept, emp)
# 成功将system_scott.dmp文件中scott用户的dept、emp表还原到已有用户test中
Copy after login

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

  • 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被删除的四张表还原
Copy after login

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

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
Copy after login

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

  • 非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
Copy after login

如若某些表备份文件由其他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用户下
Copy after login

推荐教程:《Oracle视频教程

The above is the detailed content of Completely master Oracle database backup and restore. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!