今天就跟大家聊聊有关怎样深入解析RMAN 备份与恢复,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
RMAN 备份与恢复深入解析
一 数据库版本
SQL> select *from v$version;
BANNER
—————————————————————-
OracleDatabase 10g Enterprise Edition Release 10.2.0.4.0 – 64bi
PL/SQL Release10.2.0.4.0 – Production
CORE 10.2.0.4.0 Production
TNS for Linux:Version 10.2.0.4.0 – Production
NLSRTL Version 10.2.0.4.0 -Production
二 工具
1.Rman:Oracle 8i开始就在使用的oracle专业备份恢复工具,这也是广大DBA同志接触最多的备份工具,不仅可以备份单实例还可以在RAC模式下备份,俗话说RMAN在手烦恼没有,下面我们开始深入浅出来讲讲Rman的使用方法和备份策略。
Rman能够干什么:(1)全库备份,这是必然要进行的操作,因为它是一切恢复的源泉
(2)增量备份,目的减少备份的数据量,分为差异和累计
(3)细粒度备份,表空间,数据文件,控制文件,参数文件,归档日志
(4)数据库克隆,使用备份来迁移数据库
(5)设计备份策略
(6)管理备份集
(7)自定义Rman脚本
(8)生成Rman报告
如何学习Rman:很多人见了Rman不知道按照什么规则备份,因为Rman里面有很多参数需要设置,搞不好还会备份错误,备份策略如何设计等等一系列问题。在这里我要说,遇到一个工具最快的掌握方法就是先用起来,用的好用的坏那是另一回事。其实Rman原理并不复杂,就是复制数据库一系列文件打个包放在磁盘or磁带上,需要恢复的时候在拷贝回来。Rman实践要比原理重要的多,因此“实操族”是Rman的最爱。
注意:通过RMAN来备份和恢复数据库时,都必须先启动实例并加载数据库才行,这是给刚入门的朋友一点提示,如果你是大侠请自动忽视吧。
2.登陆RMAN和退出RMAN
RMAN连接本地数据库
[oracle@linuxdbbase]$ rman target / 一气呵成法
RecoveryManager: Release 10.2.0.4.0 – Production on Tue May 7 20:57:49 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connected to target database: BASE (DBID=1845289414) 这个dbid要记好了,很多时候我们需要它
RMAN>
还可以先登陆rman,进入后在连接数据库
[oracle@linuxdbbase]$ rman
RecoveryManager: Release 10.2.0.4.0 – Production on Tue May 7 21:00:31 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
RMAN>connect target /
connectedto target database: BASE (DBID=1845289414) 每个数据库都有一个唯一dbid
RMAN>
RMAN连接远程数据库
[oracle@linuxdbbase]$ rman target sys/oracle@base179 后缀连接串即可
RecoveryManager: Release 10.2.0.4.0 – Production on Tue May 7 21:10:48 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connectedto target database: BASE (DBID=1843237732)
RMAN>
退出RMAN
RMAN>exit
RMAN>quit
这两个命令都可以退出,这是给初学者一点点引航,大侠请肃静and回避
三 备份恢复前的一些准备工作
有备无患是DBA们的口头禅,如果你想做一个赏心悦目的DBA那就要把功课做充分,从下面入手
1. control_file_record_keep_time
control_file_record_keep_time初始化参数:rman元数据在控制文件中保留的最小有效天数,默认为7天。
官方文档中描述它是控制文件保留rman元数据有效的最小天数,如果新增加一条记录到控制文件可重用区reusable,这时最老的记录尚没有超出最小保留天数,那么记录将控制文件的这一部分扩展。如果将该参数设置为0,那么控制文件可重用区将永远不会扩展。
注意:这个参数只应用于控制文件中可循环利用的部分,如归档日志文件,各种备份记录。不应用于诸如数据文件,表空间,重做日志等,这些内容只有当其从对应的表空间中删除后才能重用。
最小天数的理解:假设我们设置为7天,那如果我有10天前(甚至更早)的datafile backup和controlfile backup+至今的全部日志,就不能完全恢复了嘛?这是否定的,因为rman元数据保留在控制文件可重用区,如果7天内的备份记录没有把可重用区装满,就算过了7天你的备份记录还是存在的,可以正常完全恢复。如果7天内可重用区被装满了,控制文件会拿最老的备份记录来覆盖(但会保证最小天数内的记录是有效的)。
我们根据以往经验,把这个值设置为30天
语法:alter system set control_file_record_keep_time=30;
初始化参数control_file_record_keep_time是动态参数可以直接修改,不用重启数据库
SQL>show parameter control
NAME TYPE VALUE
———————————————– ——————————————-
control_file_record_keep_time integer 30
2.启动归档模式
SYS@base>archive log list
Databaselog mode No Archive Mode
Automaticarchival Disabled
Archivedestination USE_DB_RECOVERY_FILE_DEST
Oldestonline log sequence 10
Currentlog sequence 12
首先我们先要创建一个存放archive log的目录
Oracle10g&11g默认归档、闪回、备份都存放在flash_recovery_area中,默认大小2G,一般在生产环境中为了更方便管理这些重要文件,为其专门创建目录存放。
[oracle@linuxdboracle]$ mkdir archdata 创建一个archive log目录
路径:/opt/oracle/archdata
登陆sqlplus,设置归档路径
[oracle@linuxdbarchdata]$ sqlplus / as sysdba
SYS@base> alter system setlog_archive_dest_1='location=/opt/oracle/archdata' scope=both; 已经生效
System altered.
SQL> selectdest_name,destination,status,error from v$archive_dest wheredest_name='LOG_ARCHIVE_DEST_1';
DEST_NAME DESTINATION STATUS ERROR
—————————————————————
LOG_ARCHIVE_DEST_1 /opt/oracle/archdata VALID
已经生效
重启数据库mount状态,开启归档
SYS@base> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@base> startup mount
ORACLE instance started.
Total System Global Area1610612736 bytes
Fixed Size 2084296 bytes
Variable Size 385876536 bytes
Database Buffers 1207959552 bytes
Redo Buffers 14692352 bytes
Database mounted.
SYS@base> alter databasearchivelog; 开启归档模式
Database altered.
SYS@base> alter databaseopen; 打开数据库
Database altered.
凡是alter database操作都是修改“控制文件”内容,走到那说到那嘿
[oracle@linuxdbarchdata]$ ll 刚刚开启归档还没有生成日志,我们手工切换一下
总用量 0
SQL> alter systemswitch logfile; 手动切换不会触发checkpoint,自动切换会触发checkpoint
System altered
[oracle@linuxdbarchdata]$ ll 这时已经有归档日志生成了
总用量 22208
-rw-r—–1 oracle oinstall 22736384 5月 8 15:35 1_13_814444678.dbf
-rw-r—–1 oracle oinstall 1024 5月 8 15:35 1_14_814444678.dbf
SQL> selectsequence#,name,archived,applied from v$archived_log; 数据库层面查看
SEQUENCE# NAME ARCHIVED APPLIED
———————————————————– ——– ——-
13 /opt/oracle/archdata/1_13_814444678.dbf YES NO
14 /opt/oracle/archdata/1_14_814444678.dbf YES NO
SYS@base> archivelog list;
Database logmode Archive Mode 归档模式
Automaticarchival Enabled 自动归档启动
Archivedestination /opt/oracle/archdata 归档日志目录
Oldest online logsequence 13 旧在线日志序号,已经归档完的
Next log sequence toarchive 15 下一个将要归档的日志序号
Current logsequence 15 当前在线日志序号
写的很清楚,从这里我们就可以判断归档日志的情况了,有多少归档,现在是几号日志,已经完成归档是几号等等。
3.安装rlwrap-0.37-1.el5.x86_64.rpm包
大家有没有在sqlplus中不能使用键盘的上下左右键,打错了连删除字符都不行,没天理啊,作为一名“键盘族”这是忍受不了的,熟可忍熟不可忍。那有没有什么法宝可以解决这个看似小问题其实大问题的问题(周鸿祎说过任何理由都应该与用户体验为准绳)
rlwrap-0.37-1.el5.x86_64.rpm包就可以解决这个问题,但在安装这个包之前需要先安装2个依赖包
one:readline-devel-6.0-4.el6.x86_64.rpm
two:ncurses-devel-5.7-3.20090208.el6.x86_64.rpm
oracle用户环境变量中添加
[oracle@linuxdb~]$ vim .bash_profile
alias sqlplus="rlwrap sqlplus" 添加一个别名
[oracle@linuxdb~]$ . .bash_profile 环境变量生效
[oracle@linuxdb~]$ sqlplus / as sysdba 登陆sqlplus见证奇迹的时刻来临
上下左右 backspace 全都可以使用了对吧是不是很爽啊~nice go~
4.RMAN环境变量
为什么要设置RMAN环境变量
答:这是一个好问题,做什么事之前问一个“为什么”可以有助于你在别人面前提升自己多动脑的光辉形象:)
进入正题,RMAN环境变量和OS环境变量有同工异曲之意,设置好后,对全局生效,不用每次都指定目录指定名称,这是懒人的福音~阿门!不设置可不可以呢,oracle是个开放的软件,当然可以,这就需要在命令中写好参数一次性完成,下次再做继续再写,比较适合我这样喜欢狂敲键盘的人。下面我们来配置配置吧~咚咚波
因RMAN配置信息都是放在数据库控制文件中的,因此我们先要连接到目标库才能显示环境变量
[oracle@linuxdb~]$ rman target /
RecoveryManager: Release 10.2.0.4.0 – Production on Wed May 8 16:48:50 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connected to target database: BASE (DBID=1845289414) 有dbid证明已连接到目标库
RMAN>show all;
using target database control file instead of recovery catalog 使用控制文件来代替恢复目录数据库存放rman信息
RMANconfiguration parameters are:
CONFIGURERETENTION POLICY TO REDUNDANCY 1; # default
CONFIGUREBACKUP OPTIMIZATION OFF; # default
CONFIGUREDEFAULT DEVICE TYPE TO DISK; # default
CONFIGURECONTROLFILE AUTOBACKUP OFF; # default
CONFIGURECONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGUREDEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGUREDATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREMAXSETSIZE TO UNLIMITED; # default
CONFIGUREENCRYPTION FOR DATABASE OFF; # default
CONFIGUREENCRYPTION ALGORITHM 'AES128'; # default
CONFIGUREARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURESNAPSHOT CONTROLFILE NAME TO'/opt/oracle/product/10.2.0/db_1/dbs/snapcf_base.f'; # default
这只是一部分,如想多多学习请参考【参】Books-> Backup and Recovery Reference -> CONFIGURE
(1)配置RMAN默认备份介质保存目录 /opt/oracle/backup
[oracle@linuxdboracle]$ mkdir backup 创建保存目录
RMAN> configure channel device type disk format'/opt/oracle/backup/DB_%U';
usingtarget database control file instead of recovery catalog
使用目标库“控制文件”代替“恢复目录数据库”存放rman信息
old RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters are successfully stored
新RMAN配置参数生效
(2)配置控制文件自动备份并保存到 /opt/oracle/backup/control
注:当控制文件内容有变化时会自动触发备份
[oracle@linuxdbbackup]$ mkdir control 创建保存目录
RMAN> configure controlfile autobackup on; 启动控制文件自动备份
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP ON;
new RMANconfiguration parameters are successfully stored
RMAN> configure controlfile autobackup format for device type diskto '/opt/oracle/backup/control/cf_%F'; 配置控制文件自动备份保存目录和格式
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
new RMANconfiguration parameters are successfully stored
(3)配置备份介质保留期为7天
RMAN> configure retention policy to recovery window of 7 days;
new RMANconfiguration parameters:
CONFIGURERETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
new RMANconfiguration parameters are successfully stored
设置好后我们再来看一下rman环境变量
RMAN>show all;
RMANconfiguration parameters are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS; 恢复窗口7天
CONFIGUREBACKUP OPTIMIZATION OFF; # default
CONFIGUREDEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON; 启动控制文件自动备份,目录和格式
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
CONFIGUREDEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGUREDATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U'; 备份介质保存目录
CONFIGUREMAXSETSIZE TO UNLIMITED; # default
CONFIGUREENCRYPTION FOR DATABASE OFF; # default
CONFIGUREENCRYPTION ALGORITHM 'AES128'; # default
CONFIGUREARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURESNAPSHOT CONTROLFILE NAME TO '/opt/oracle/product/10.2.0/db_1/dbs/snapcf_base.f';# default
我们目前配置这4个变量就可以了,其它的什么时候用什么时候做或者直接在命令行中指定。
四 备份与恢复实例
1. 用RMAN分别作数据库,表空间和数据文件的备份和数据库,表空间和数据文件损坏后的恢复实例
数据库级备份与恢复
全库压缩备份与全库非压缩备份应用场景:
如果你的系统有专用“备份磁阵”或者有足够的磁盘空间来让您随心所欲的用,那说明你很幸运,遇到一个不差钱的boss,往往事与愿违,我们可以自由支配的磁盘空间非常有限,这也验证了国人勤俭节约的优良传统。在有限的空间里如何装下更多的备份呢,这里就用上了压缩属性,把原来很大的文件尽可能压缩,提高空间利用率,当然备份和恢复的时间窗口会长一些,这就是时间换空间的精髓。
Come on 我们先来备个全库吧
全库压缩备份脚本
backup as compressed backupset full database format 命令行中直接指定压缩选项即可
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelogformat '/opt/oracle/backup/arch_bk1_%u%p%s.rmn' delete all input;
全库非压缩备份脚本
backupfull database format
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
全库使用默认通道默认配置备份脚本,同时删除备份过的归档日志
backup as compressed backupset full databaseinclude current controlfile plus archivelog delete all input;
上面有三种不同情况的备份脚本,我们用第二个,这个我想应该是大众最常用的。
备份之前检查archive log ,有三个归档日志
[oracle@linuxdb archdata]$ ll
总用量 64644
-rw-r—– 1 oracle oinstall 22736384 5月 8 15:35 1_13_814444678.dbf
-rw-r—– 1 oracle oinstall 1024 5月 8 15:35 1_14_814444678.dbf
-rw-r—– 1 oracle oinstall 43454464 5月 9 12:36 1_15_814444678.dbf
[oracle@linuxdb ~]$ rman target / 进入rman
connected to target database: BASE(DBID=1845289414) 一定要连接到数据库才行哦
RMAN> backup full database format
2>'/opt/oracle/backup/full_bk1_%u%p%s.rmn' include current controlfile
3> plus
4> archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
Starting backup at 09-MAY-13 备份开始时间
current log archived 一般都从归档日志备份
using target database control file instead ofrecovery catalog
allocated channel: ORA_DISK_1 分配默认通道
channel ORA_DISK_1: sid=145 devtype=DISK
channel ORA_DISK_1: starting archive logbackupset
channel ORA_DISK_1: specifying archive log(s)in backup set 归档日志列表 13~16
input archive log thread=1 sequence=13recid=1 stamp=814894510
input archive log thread=1sequence=14 recid=2 stamp=814894549
input archive log thread=1sequence=15 recid=3 stamp=814970205
input archive log thread=1sequence=16 recid=4 stamp=814982236
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 备份片名称arch_bk1_01o9792t11.rmn
piecehandle=/opt/oracle/backup/arch_bk1_01o9792t11.rmn tag=TAG20130509T155717comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用时2秒
channel ORA_DISK_1: deleting archive log(s) 删除备份过的归档日志13~16
archive logfilename=/opt/oracle/archdata/1_13_814444678.dbf recid=1 stamp=814894510
archive logfilename=/opt/oracle/archdata/1_14_814444678.dbf recid=2 stamp=814894549
archive logfilename=/opt/oracle/archdata/1_15_814444678.dbf recid=3 stamp=814970205
archive logfilename=/opt/oracle/archdata/1_16_814444678.dbf recid=4 stamp=814982236
Finished backup at 09-MAY-13
Starting backup at 09-MAY-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafilebackupset 备份数据文件
channel ORA_DISK_1: specifying datafile(s) inbackupset 数据文件列表 1~6
input datafile fno=00005name=/opt/oracle/oradata/base/sinojfs_01.dbf
input datafile fno=00006name=/opt/oracle/oradata/base/sinojfs2_01.dbf
input datafile fno=00001 name=/opt/oracle/oradata/base/system01.dbf
input datafile fno=00002name=/opt/oracle/oradata/base/undotbs01.dbf
input datafile fno=00003name=/opt/oracle/oradata/base/sysaux01.dbf
input datafile fno=00004name=/opt/oracle/oradata/base/users01.dbf
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 备份片名称full_bk1_02o9793012.rmn
piecehandle=/opt/oracle/backup/full_bk1_02o9793012.rmn tag=TAG20130509T155720comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:03 用时3秒
channel ORA_DISK_1: starting full datafilebackupset
channel ORA_DISK_1: specifying datafile(s) inbackupset
including current control file inbackupset 同时随便把控制文件和参数文件也备份了
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 备份片名称full_bk1_03o9793313.rmn
piecehandle=/opt/oracle/backup/full_bk1_03o9793313.rmn tag=TAG20130509T155720comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用时2秒
Finished backup at 09-MAY-13
Starting backup at 09-MAY-13
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archive logbackupset
channel ORA_DISK_1: specifying archive log(s)in backup set 最后收尾在做一次归档日志备份
input archive log thread=1sequence=17 recid=5 stamp=814982245 这里面存放着数据库最后动作的信息
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 备份片名称arch_bk1_04o9793514.rmn
piecehandle=/opt/oracle/backup/arch_bk1_04o9793514.rmn tag=TAG20130509T155725comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用时2秒
channel ORA_DISK_1: deleting archive log(s) 删除17号归档
archive logfilename=/opt/oracle/archdata/1_17_814444678.dbf recid=5 stamp=814982245
Finished backup at 09-MAY-13
当数据库结构有变化时,自动触发控制文件和参数文件备份
Starting Control File and SPFILEAutobackup at 09-MAY-13
piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-00comment=NONE
Finished Control File and SPFILE Autobackupat 09-MAY-13
操作系统层面看看有没有这些文件,删除没删除“备份过的旧归档日志”
[oracle@linuxdbbackup]$ ll 四个备份集全都有
总用量 382264
-rw-r—–1 oracle oinstall 72907264 5月 9 15:57arch_bk1_01o9792t11.rmn
-rw-r—–1 oracle oinstall 3584 5月 9 15:57arch_bk1_04o9793514.rmn
drwxr-xr-x2 oracle oinstall 4096 5月 9 15:57 control
-rw-r—–1 oracle oinstall 242302976 5月 9 15:57 full_bk1_02o9793012.rmn
-rw-r—–1 oracle oinstall 76218368 5月 9 15:57full_bk1_03o9793313.rmn
[oracle@linuxdbcontrol]$ ll 控制文件自动备份也有了
总用量 74464
-rw-r—–1 oracle oinstall 76251136 5月 9 15:57 cf_c-1845289414-20130509-00
[oracle@linuxdbarchdata]$ ll 归档日志全删了
总用量 0
SYS@base>archive log list 新归档日志从18号开始,17号之前都已经备份并删除
Databaselog mode Archive Mode
Automaticarchival Enabled
Archivedestination /opt/oracle/archdata
Oldestonline log sequence 16
Next logsequence to archive 18
Currentlog sequence 18
一切准备就绪后,我们开始搞破坏吧:)全部改名
[oracle@linuxdbbase]$ mv system01.dbf system01.dbf.bak
[oracle@linuxdbbase]$ mv sinojfs_01.dbf sinojfs_01.dbf.bak
[oracle@linuxdbbase]$ mv sinojfs2_01.dbf sinojfs2_01.dbf.bak
[oracle@linuxdbbase]$ mv sysaux01.dbf sysaux01.dbf.bak
SYS@base>shutdown abort 强制关库
ORACLEinstance shut down.
SYS@base>startup 启动
ORACLEinstance started.
TotalSystem Global Area 1610612736 bytes
FixedSize 2084296 bytes
VariableSize 385876536 bytes
DatabaseBuffers 1207959552 bytes
RedoBuffers 14692352 bytes
Databasemounted.
ORA-01157:cannot identify/lock data file 1 – see DBWR trace file
ORA-01110:data file 1: '/opt/oracle/oradata/base/system01.dbf
提示找不到1号文件,oracle都是先从1号文件开始访问
Good 破坏完毕,赶集恢复,我这是用试运行项目数据库做测试的,而且备份仅此一份,这要是出了岔子就不好玩了,如有雷同纯属巧合。
SYS@base>select status from v$instance; 启动数据库到mount状态
STATUS
————
MOUNTED
进入RMAN看一看备份集,这些信息都是从control file读出的。
RMAN>list backupset;
usingtarget database control file instead of recovery catalog
List of Backup Sets
第一个备份片 大小 保存设备 用时 备份日期
BSKey Size Device Type Elapsed Time Completion Time
—————– ———– ———— —————
1 69.53M DISK 00:00:02 09-MAY-13
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155717 如果压缩这会是YES
Piece Name: /opt/oracle/backup/arch_bk1_01o9792t11.rmn 备份片路径和名称
List of Archived Logs in backup set 1 包含的文件
Thrd Seq Low SCN Low Time Next SCN Next Time
—- ——- ———- ——— ——————-
1 13 335077 07-MAY-13 362020 08-MAY-13
1 14 362020 08-MAY-13 362034 08-MAY-13
1 15 362034 08-MAY-13 398238 09-MAY-13
1 16 398238 09-MAY-13 403789 09-MAY-13
第二个备份片信息,由于保存的是数据文件,明显容量很大
BSKey Type LV Size Device Type Elapsed Time Completion Time
———– — ———- ———– ———— —————
2 Full 231.07M DISK 00:00:03 09-MAY-13
BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155720
Piece Name:/opt/oracle/backup/full_bk1_02o9793012.rmn 备份片路径和名称
List of Datafiles in backup set 2
File LV Type Ckp SCN Ckp Time Name 文件列表
—- — —- ———- ——— —-
1 Full 403795 09-MAY-13/opt/oracle/oradata/base/system01.dbf
2 Full 403795 09-MAY-13/opt/oracle/oradata/base/undotbs01.dbf
3 Full 403795 09-MAY-13/opt/oracle/oradata/base/sysaux01.dbf
4 Full 403795 09-MAY-13/opt/oracle/oradata/base/users01.dbf
5 Full 403795 09-MAY-13/opt/oracle/oradata/base/sinojfs_01.dbf
6 Full 403795 09-MAY-13/opt/oracle/oradata/base/sinojfs2_01.dbf
第三个备份片信息,保存的是控制文件
BSKey Type LV Size Device Type Elapsed Time Completion Time
———– — ———- ———– ———— —————
3 Full 72.67M DISK 00:00:01 09-MAY-13
BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155720
Piece Name:/opt/oracle/backup/full_bk1_03o9793313.rmn
Control File Included: Ckp SCN: 403796 Ckp time: 09-MAY-13
第四个备份片信息,保存的是收尾的归档日志
BSKey Size Device Type Elapsed Time Completion Time
—————– ———– ———— —————
4 3.00K DISK 00:00:01 09-MAY-13
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155725
Piece Name:/opt/oracle/backup/arch_bk1_04o9793514.rmn
List of Archived Logs in backup set 4
Thrd Seq Low SCN Low Time Next SCN Next Time
—- ——- ———- ——— ——————-
1 17 403789 09-MAY-13 403800 09-MAY-13
第五个备份片信息,保存的是控制文件和参数文件自动备份
BSKey Type LV Size Device Type Elapsed Time Completion Time
———– — ———- ———– ———— —————
5 Full 72.70M DISK 00:00:01 09-MAY-13
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155728
Piece Name:/opt/oracle/backup/control/cf_c-1845289414-20130509-00
Control File Included: Ckp SCN: 403806 Ckp time: 09-MAY-13
SPFILE Included: Modification time: 08-MAY-13
万事俱备只欠东风,我们来恢复吧!请关注备份集的大小,这是影响恢复快慢的重要指标
RMAN>restore database;
Startingrestore at 09-MAY-13
allocatedchannel: ORA_DISK_1
channelORA_DISK_1: sid=155 devtype=DISK
channelORA_DISK_1: starting datafile backupset restore 利用备份已经复制回来数据文件了
channelORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /opt/oracle/oradata/base/system01.dbf
restoring datafile 00002 to /opt/oracle/oradata/base/undotbs01.dbf
restoring datafile 00003 to /opt/oracle/oradata/base/sysaux01.dbf
restoring datafile 00004 to /opt/oracle/oradata/base/users01.dbf
restoring datafile 00005 to /opt/oracle/oradata/base/sinojfs_01.dbf
restoring datafile 00006 to /opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: reading from backup piece /opt/oracle/backup/full_bk1_02o9793012.rmn
channelORA_DISK_1: restored backup piece 1
piecehandle=/opt/oracle/backup/full_bk1_02o9793012.rmn tag=TAG20130509T155720
channelORA_DISK_1: restore complete, elapsed time: 00:01:46
Finishedrestore at 09-MAY-13
###########################################################################################
[oracle@linuxdbbase]$ ll
-rw-r—– 1 oracle oinstall 10737426432 5月 9 18:11 sinojfs_01.dbf
-rw-r—–1 oracle oinstall 10737426432 5月 9 17:28 sinojfs_01.dbf.bak
-rw-r—– 1 oracle oinstall 10737426432 5月 9 18:10 sinojfs2_01.dbf
-rw-r—–1 oracle oinstall 10737426432 5月 9 15:57 sinojfs2_01.dbf.bak
-rw-r—– 1 oracle oinstall 125837312 5月 9 18:11 sysaux01.dbf
-rw-r—–1 oracle oinstall 125837312 5月 9 17:27 sysaux01.dbf.bak
-rw-r—– 1 oracle oinstall 314580992 5月 9 18:11 system01.dbf
-rw-r—–1 oracle oinstall 314580992 5月 9 17:28 system01.dbf.bak
操作系统层面都是可以看到的对不对
###########################################################################################
RMAN>recover database; 不仅要restore还需要应用archive和redo log进行介质恢复
Startingrecover at 09-MAY-13
usingchannel ORA_DISK_1
starting media recovery
mediarecovery complete, elapsed time: 00:00:07
Finishedrecover at 09-MAY-13
RMAN>alter database open; 把数据库恢复到最新状态才能顺利open
databaseopened
SYS@base>select status from v$instance;
STATUS
————
OPEN
SYS@base>archive log list 因为我们应用到最后一个日志,因此日志会接着前面继续顺延
Databaselog mode Archive Mode
Automaticarchival Enabled
Archivedestination /opt/oracle/archdata
Oldestonline log sequence 17
Next logsequence to archive 19
Currentlog sequence 19
###########################################################################################
表空间级别备份和恢复
使用默认通道默认备份介质保存目录
RMAN>backup tablespace sinojfs2;
Startingbackup at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting full datafile backupset
channelORA_DISK_1: specifying datafile(s) in backupset
inputdatafile fno=00006 name=/opt/oracle/oradata/base/sinojfs2_01.dbf 这个表空间就包含一个数据文件
channelORA_DISK_1: starting piece 1 at 09-MAY-13
channelORA_DISK_1: finished piece 1 at 09-MAY-13 备份片路径和名称
piecehandle=/opt/oracle/backup/DB_06o97i9d_1_1 tag=TAG20130509T183421 comment=NONE
channelORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finishedbackup at 09-MAY-13
看又自动备份控制文件和参数文件了
StartingControl File and SPFILE Autobackup at 09-MAY-13
piecehandle= /opt/oracle/backup/control/cf_c-1845289414-20130509-01 comment=NONE
FinishedControl File and SPFILE Autobackup at 09-MAY-13
操作系统层面查看
[oracle@linuxdbbackup]$ ll
总用量 383112
-rw-r—–1 oracle oinstall 72907264 5月 9 15:57arch_bk1_01o9792t11.rmn
-rw-r—–1 oracle oinstall 3584 5月 9 15:57arch_bk1_04o9793514.rmn
drwxr-xr-x2 oracle oinstall 4096 5月 9 18:34 control
-rw-r—– 1 oracle oinstall 868352 5月 9 18:34 DB_06o97i9d_1_1 表空间的备份集
-rw-r—–1 oracle oinstall 242302976 5月 9 15:57 full_bk1_02o9793012.rmn
-rw-r—–1 oracle oinstall 76218368 5月 9 15:57full_bk1_03o9793313.rmn
[oracle@linuxdbcontrol]$ ll
总用量 148928
-rw-r—–1 oracle oinstall 76251136 5月 9 15:57 cf_c-1845289414-20130509-00
-rw-r—– 1 oracle oinstall 76251136 5月 9 18:34 cf_c-1845289414-20130509-01 这是第二次自动备份集
如果没有指定保存目录,使用RMAN参数中默认的备份介质保存目录/opt/oracle/backup
###########################################################################################
进入sqlplus删除sinojfs2表空间
SYS@base>drop tablespace sinojfs2 including contents and datafiles; 删除sinojfs2表空间
Tablespacedropped.
SYS@base>select * from v$tablespace; 已经删除了吧
TS# NAME INC BIG FLA ENC
—————————————- — — — —
0 SYSTEM YES NO YES
1 UNDOTBS1 YES NO YES
2 SYSAUX YES NO YES
3 TEMP NO NO YES
4 USERS YES NO YES
5 SINOJFS YES NO YES
6 rowsselected.
利用表空间备份进行恢复,进入RMAN
RMAN>restore tablespace sinojfs2;
Startingrestore at 09-MAY-13
usingtarget database control file instead of recovery catalog
allocatedchannel: ORA_DISK_1
channelORA_DISK_1: sid=155 devtype=DISK
RMAN-00571:===========================================================
RMAN-00569:=============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571:===========================================================
RMAN-03002: failure of restore command at 05/09/2013 19:14:39
RMAN-20202: tablespace not found in the recovery catalog
RMAN-06019: could not translate tablespace name "SINOJFS2"
大家知道为什么找不到sinojfs2表空间吗?数据库结构是不是存放在控制文件中的,刚才我们是怎么破坏的表空间的?使用了drop tablespace sinojfs2 includingcontents and datafiles; 这条语句,它做的动作是删除表空间同时把控制文件中的表空间信息也一并删除,因此后来在使用控制文件恢复表空间时报找不到信息。怎么办,这个不行那就换一种方法。
首先重新恢复数据库到原来状态,过程省略,刚才已经讲过了,再来一遍估计就要抛砖啦!
重新创建一个新sinojfs2表空间
SQL> createtablespace sinojfs2 datafile '/opt/oracle/oradata/base/sinojfs2_01.dbf' size10G autoextend off;
Tablespace created
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
————————————————————————————————-
6/opt/oracle/oradata/base/sinojfs2_01.dbf ONLINE
在备份一次表空间
RMAN>backup tablespace sinojfs2;
Startingbackup at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting full datafile backupset
channelORA_DISK_1: specifying datafile(s) in backupset
inputdatafile fno=00006 name=/opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: starting piece 1 at 09-MAY-13
channelORA_DISK_1: finished piece 1 at 09-MAY-13
piecehandle=/opt/oracle/backup/DB_0fo97o6p_1_1 tag=TAG20130509T201521comment=NONE
channelORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finishedbackup at 09-MAY-13
StartingControl File and SPFILE Autobackup at 09-MAY-13
piecehandle=/opt/oracle/backup/control/cf_c-1845289414-20130509-03 comment=NONE
FinishedControl File and SPFILE Autobackup at 09-MAY-13
我们这回直接删除表空间对应的数据文件
[oracle@linuxdbbase]$ rm -rf sinojfs2_01.dbf
SYS@base>alter tablespace sinojfs2 offline; 让表空间offline
altertablespace sinojfs2 offline
*
ERROR atline 1:
ORA-01116:error in opening database file 6
ORA-01110:data file 6: '/opt/oracle/oradata/base/sinojfs2_01.dbf' 找不到对应数据文件
ORA-27041:unable to open file
Linux-x86_64Error: 2: No such file or directory
Additionalinformation: 3
SYS@base>alter database datafile 6 offline; 先脱机数据文件
Databasealtered.
此时可以在数据库open状态下恢复sinojfs2表空间
RMAN>restore tablespace sinojfs2; 复制文件
Startingrestore at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting datafile backupset restore
channelORA_DISK_1: specifying datafile(s) to restore from backup set
restoringdatafile 00006 to /opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: reading from backup piece /opt/oracle/backup/DB_0fo97o6p_1_1
channelORA_DISK_1: restored backup piece 1
piecehandle=/opt/oracle/backup/DB_0fo97o6p_1_1 tag=TAG20130509T201521
channelORA_DISK_1: restore complete, elapsed time: 00:00:55
Finishedrestore at 09-MAY-13
RMAN>recover tablespace sinojfs2; 介质恢复
Startingrecover at 09-MAY-13
usingchannel ORA_DISK_1
startingmedia recovery
mediarecovery complete, elapsed time: 00:00:01
Finishedrecover at 09-MAY-13
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
————————————————————————————————-
6/opt/oracle/oradata/base/sinojfs2_01.dbf OFFLINE
SYS@base> alter databasedatafile 6 online; 把offline启动成online
Database altered.
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
————————————————————————————————-
6/opt/oracle/oradata/base/sinojfs2_01.dbf ONLINE
到此表空间sinojfs2完整恢复回来,真是惊心动魄啊!数据文件恢复也是如此,命令如下这里就不演示了
restore datafile 6;
recover datafile 6;
来一点小提示,我们备份了这么多备份集,如何检查rman元数据和操作系统上的备份集对应关系呢
RMAN> crosscheck backupset; 交叉校验,看看对象数是否一致,我们这里一共9个备份集,再看看操作系统上即可
using channel ORA_DISK_1
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/arch_bk1_09o97nhr19.rmn recid=7stamp=814997052
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/full_bk1_0ao97nhu110.rmn recid=8stamp=814997055
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/full_bk1_0bo97ni5111.rmn recid=9stamp=814997061
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/arch_bk1_0co97ni7112.rmn recid=10stamp=814997064
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-01recid=11 stamp=814997066
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-02recid=12 stamp=814997575
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/DB_0fo97o6p_1_1 recid=13stamp=814997721
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-03recid=14 stamp=814997723
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-04recid=15 stamp=814998546
Crosschecked 9 objects
delete backup 删除所有备份
delete expired backup 删除所有过期备份
小结:我们成功进行了数据库级别、表空间、数据文件级别的备份与恢复,当你数据库处于稳定状态时一定要记住做一个全备“以备后患”。Good 今天就讲到这里。
RMAN backupset catalog 全库备份 差异增量备份 累积增量备份
看完上述内容,你们对怎样深入解析RMAN 备份与恢复有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/200233.html