使用Oracle进行数据库备份与还原

2023-12-04 0 705
目录
  • 一、逻辑备份
    • 1.某一用户(DBA权限)全库备份:
    • 2.某一用户(DBA权限)备份库中某些用户:
    • 3.某一用户备份自身:
    • 4.某一用户备份自身某些表对象:
    • 5.某一用户(具有DBA权限)备份其他用户某些表对象:
  • 二、逻辑还原
    • 1.使用全库备份文件还原:
      • ①使用全库备份文件还原库:
      • ②使用全库备份文件还原库内某些用户(被还原用户应存在):
      • ③使用全库备份文件还原库内某些用户某些表:
    • 2.使用某些用户备份文件还原:
      • ①使用某些用户备份文件还原库内某些用户(被还原用户应存在):
      • ②使用某些用户备份文件还原库内某些用户的某些表:
    • 3.使用某一用户自身备份文件还原:
      • ①使用某一用户自身备份文件还原库内某一用户:
      • ②使用某一用户自身备份文件还原库内某用户某些表:
    • 4.使用某些表备份文件还原:

    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

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

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

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

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

    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

    exp system/orcl@orcl file=d:\\1.dmp owner=scott tables=(bonus, salgrade)
    # EXP-00026:指定了冲突模式

    备份总结:

    ①不使用任何参数将备份用户自身全部对象②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用户下

    到此这篇关于使用Oracle进行数据库备份与还原的文章就介绍到这了,更多相关Oracle数据库备份与还原内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 Oracle 使用Oracle进行数据库备份与还原 https://www.u-9.cn/database/oracle/67224.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务