为什么要从一个RMAN的执行例子开始?就是让读者先有一个直观的感受,即便这个感受是模糊的,但是这非常有利于了解我后面要介绍的内容是什么。文档分为两种,一是学习用的,例如Oracle培训机构提供的Student Guide,由浅入深,前后逻辑清晰;另外一种是官网的Administration Guide,它是用于查询的,前提是读者有一定的基础,知道应该怎么使用这个文档。不幸的是,Student Guide是培训课程提供的,要花钱的。幸运的是,东家给出钱让我让过课。我希望把我的经验以Student Guide的形式分享给大家,遇到困难的时候再去查看官方的Administration Guide。在CSDN中,有很多文章都非常详细的介绍了关于数据库备份的知识,但是过于零散,让初学都很难把握。本文的另外一个目的,就是将这些零散的文章进行整理,系统的展现在读者面前。
本文的所有示例,都是以19C版本为基础的,有些命令和参数在不同的版本可能会有变化。
在数据库系统中使用RMAN执行备份操作:
[oracle ~] $ rman target / --使用rman命令登陆数据库
Recovery Manager: Release 19.0.0.0.0 -Production on Wed May 18 13:22:57 2021
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/orits affiliates. All rights reserved.
connected to target database
RMAN> run { --使用run开始命令,命令内容包括在{}中
2> allocate channel c1 device type disk format '/tmp/c1_%U'; --分配通道,名为c1,数据写入文件系统路径为/tmp,文件名以c1开头
3> allocate channel c2 device type disk format '/tmp/c2_%U'; --分配通道,名为c2,数据写入文件系统路径为/tmp,文件名以c2开头
4> backup database; --备份全量数据库
5> } --命令结束
######################
以下是日志输出
######################
allocated channel: c1 --分配通道c1
channel c1: SID=272 device type=DISK
allocated channel: c2 --分配通道c2
channel c2: SID=4 device type=DISK
Starting backup at 09-MAY-21
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backup set --在通道c1创建备份集,包括以下两个数据文件
input datafile file number=00001name=/u01/app/data/ORCL/system01.dbf
input datafile file number=00004name=/u01/app/data/ORCL/undotbs01.dbf
channel c1: starting piece 1 at 09-MAY-21
channel c2: starting full datafile backupset
channel c2: specifying datafile(s) in backup set --在通道c2创建备份集,包括以下两个数据文件
input datafile file number=00003name=/u01/app/data/ORCL/sysaux01.dbf
input datafile file number=00007name=/u01/app/data/ORCL/users01.dbf
channel c2: starting piece 1 at 09-MAY-21
channel c2: finished piece 1 at 09-MAY-21 --通道c2的备份集完成,保存在/tmp路径下,这是在创建通道时指定的
piece handle=/tmp/c2_1ovue2bn_1_1tag=TAG20210509T151047 comment=NONE
channel c2: backup set complete, elapsedtime: 00:00:03
channel c2: starting full datafile backupset
channel c2: specifying datafile(s) in backup set --通道c2执行第二次备份,备份集包括以下三个文件
input datafile file number=00010name=/u01/app/data/ORCL/pdb01/sysaux01.dbf
input datafile file number=00012name=/u01/app/data/ORCL/pdb01/users01.dbf
input datafile file number=00014name=/u01/app/data/ORCL/pdb01/ts01.dbf
channel c2: starting piece 1 at 09-MAY-21
channel c1: finished piece 1 at 09-MAY-21 --通道c1的备份集完成,保存在/tmp路径下,这是在创建通道时指定的。尽管c1早于c2开始,但是由于数据量或者其它原因,它晚于c2完成,但是这不影响备份。
piece handle=/tmp/c1_1nvue2bn_1_1tag=TAG20210509T151047 comment=NONE
channel c1: backup set complete, elapsedtime: 00:00:04
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backup set --通道c1执行第二次备份,备份集包括以下两个文件
input datafile file number=00009name=/u01/app/data/ORCL/pdb01/system01.dbf
input datafile file number=00011name=/u01/app/data/ORCL/pdb01/undotbs01.dbf
channel c1: starting piece 1 at 09-MAY-21
channel c2: finished piece 1 at 09-MAY-21 --通道c2第二次执行完成
piece handle=/tmp/c2_1pvue2br_1_1tag=TAG20210509T151047 comment=NONE
channel c2: backup set complete, elapsedtime: 00:00:01
channel c2: starting full datafile backupset
channel c2: specifying datafile(s) in backup set --通道c3第三次执行备份,包括以下两个文件
input datafile file number=00005name=/u01/app/data/ORCL/pdbseed/system01.dbf
input datafile file number=00008name=/u01/app/data/ORCL/pdbseed/undotbs01.dbf
channel c2: starting piece 1 at 09-MAY-21
channel c1: finished piece 1 at 09-MAY-21 --通道c1完成第二次备份
piece handle=/tmp/c1_1qvue2br_1_1tag=TAG20210509T151047 comment=NONE
channel c1: backup set complete, elapsedtime: 00:00:01
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backup set --通道c1开始第3次备份,包括一个文件
input datafile file number=00006name=/u01/app/data/ORCL/pdbseed/sysaux01.dbf
channel c1: starting piece 1 at 09-MAY-21
channel c1: finished piece 1 at 09-MAY-21 --通道c1完成第3次备份
piece handle=/tmp/c1_1svue2bs_1_1tag=TAG20210509T151047 comment=NONE
channel c1: backup set complete, elapsedtime: 00:00:01
channel c2: finished piece 1 at 09-MAY-21 --通道c2完成第三次备份
piece handle=/tmp/c2_1rvue2bs_1_1tag=TAG20210509T151047 comment=NONE
channel c2: backup set complete, elapsedtime: 00:00:01
Finished backup at 09-MAY-21 --数据文件备份结束
Starting Control File and SPFILE Autobackup at 09-MAY-21 --备份控制文件和参数文件。RMAN默认在执行任何备份任务后自动触发。
piece handle=/tmp/o1_mf_s_1072105853_j9h2qy08_.bkpcomment=NONE
Finished Control File and SPFILE Autobackup at 09-MAY-21 --备份控制文件和参数文件完成
released channel: c1 --释放通道c1
released channel: c2 --释放通道c2
#######################################################################
再次执行备份任务,备份归档日志。在线备份数据库,必须备份归档日志,否则无法恢复.
#######################################################################
RMAN> run {
2> allocate channel c1 device type
disk format '/tmp/arch_%U'; --分配通道
3> backup archivelog all; --备份所有归档日志
4> }
##################
以下为日志输出
##################
allocated channel: c1
channel c1: SID=272 device type=DISK
Starting backup at 09-MAY-21
current log archived --切换redo log,保证将备份开始时的redo log中的数据保存到归档日志中
channel c1: starting archived log backupset
channel c1: specifying archived log(s) in backup set --第一次执行,包括两个归档文件
input archived log thread=1 sequence=11RECID=5 STAMP=1072104922
input archived log thread=1 sequence=12RECID=6 STAMP=1072105982
channel c1: starting piece 1 at 09-MAY-21
channel c1: finished piece 1 at 09-MAY-21 --第一次备份结束,保存的/tmp路径下
piece handle=/tmp/arch_1uvue2fu_1_1tag=TAG20210509T151302 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:01 --数据库中只有两个归档文件,因此执行一次备份就完成了归档的备份
Finished backup at 09-MAY-21
Starting Control File and SPFILE Autobackup at 09-MAY-21 --自动执行控制文件和参数文件备份
piece handle=/tmp/o1_mf_s_1072105983_j9h2vzq4_.bkpcomment=NONE
Finished Control File and SPFILEAutobackup at 09-MAY-21
released channel: c1 --释放通道,备份结束
###################
以上两个备份任务,可以合并为一条执行
RMAN> backup database plus archivelog;
从以上的示例中,我们可以非常直观的观察到备份的全过程。我们这里做一个总结:
1)创建通道,具体数量依照不同场景设置不同的数值,计算方法属于“调优”范畴,这里暂不做介绍;
2)按照通道数量,系统自动将多个数据文件分配到不同的通道进行备份,每个通道的每一次备份称为"backup set",一个”backup set”包括一个或多个“pieces”;
3)数据文件备份完成后,自动触发控制文件和参数文件备份。默认是自动触发的,可以关闭,改为手工备份,但是不建议这样做,可以会有遗漏的风险;
4)切换当前online redo log,强制将redo日志中的数据归档。否则,无法保证恢复时所需的redo日志可以从归档中读取;
5)备份归档日志;
6)备份控制文件和参数文件;
以上步骤完成了完整的数据库备份,可进行全库恢复。
我相信,在阅读了以上示例后,您肯定会有一些疑问。后面章节,我们将以不同场景中进行备份恢复的操作举例为引子,带大家了解更多的内容。