四 RMAN
RMAN体系结构的主要组成部分:
--1 目标数据库(target)
--2 RMAN命令行客户端
--3 通道(channel)
--4 快速恢复区(fast recovery area)
--5 介质管理器
--6 RMAN备份资料库
本章不详细介绍rman的使用和用法,最前面的章节有rman的详细使用备份和恢复,这里讲解rman的基本组成部分~~
[oracle@DSI ~]$ echo $ORACLE_SID orcl [oracle@DSI ~]$ rman target/ --rman target sys/oracle@orcl 以下orcl连接串在tns文件中的片段 [oracle@DSI ~]$ cat /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora orcl = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.15.7.25)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) )
客户端的一次登录会在目标db上创建两个连接
SYS@ orcl >set linesize 999 SYS@ orcl >set pagesize 999 SQL> set sqlprompt "_USER@ _CONNECT_IDENTIFIER >" SYS@ orcl >select sid,serial#,module as x,action from v$session where module like '%rman%'; SID SERIAL# X ACTION ---------- ---------- ----------------------- 133 21 rman@DSI (TNS V1-V3) 0000001 FINISHED70 139 35 rman@DSI (TNS V1-V3)
所谓通道,是指在rman体系中执行备份、还原操作时用来处理I/O流的物理结构和逻辑结构的结合体。
快速恢复区则是rman备份默认的磁盘保存地点,--直观的备份策略,将备份先保留在磁盘上,然后转移到磁带上,注重效率的恢复策略,一旦出了问题,首先考虑磁盘至磁盘的恢复而不是磁带至磁盘的恢复,其作用就像是磁带在磁盘上的缓存。
介质管理器的功能则是使rman可以对第三方存储(磁带)进行读写操作,需要另外安装、配置可使用,备份时,rman向介质管理器传递数据流,还原时介质管理器向rman传递数据流。
RMAN资料库记载了rman备份(备份集,镜像复制,备份集备份,镜像复制备份)的位置和其他相关信息。
1 快速恢复区
是一个默认放置所有和备份恢复操作有关的文件的地方。能够保存在快速恢复区中的文件包括控制文件在线镜像、redo log,archive log、外来归档日志
(用于灾难备份方案dg中的逻辑备用数据库)、控制文件镜像复制、数据文件镜像复制、rman备份片和闪回日志。
在磁盘空间足够大的情况下,整个快速恢复区由oracle自行管理,目录结构与文件命名甚至文件的去留无需dba操心。
Oracle推荐将最近的备份至少在快速恢复区保留一份。如果启用了快速恢复区,rman默认的备份去处,无论是rman备份片、镜像复制、
还是控制文件自动备份,只要没有在rman配置或backup命令中指定路径,全都进入快速恢复区。
--配置与监控
SYS@ orcl >show parameter db_recovery_file_dest NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_recovery_file_dest string ##快速恢复区路径 db_recovery_file_dest_size big integer 0 ##快速恢复区空间大小 [oracle@DSI oracle]$ mkdir -p /u01/app/oracle/fra --alter system set db_recovery_file_dest_size='3G'; --alter system set db_recovery_file_dest ='/u01/app/oracle/fra'; SYS@ orcl >col name format a50 SYS@ orcl >select name,bytes/1024/1024 from v$datafile where name='/u01/app/oracle/oradata/orcl/system01.dbf'; NAME BYTES -------------------------------------------------- ---------- /u01/app/oracle/oradata/orcl/system01.dbf 817889280 [oracle@DSI ~]$ ll /u01/app/oracle/oradata/orcl/system01.dbf -rw-r----- 1 oracle oinstall 817897472 May 27 15:22 /u01/app/oracle/oradata/orcl/system01.dbf
实际的数据文件大小比在datafile中查询出来的要大一个block_size的大小。
快速恢复区下目录结构和文件名是自动控制的--OMF,
db_unique_name=db_name/
--archivelog 短期性
--autobackup 短期性
--controlfile 永久性
--datafile 短期性
--onlinelog 永久性
--flashback 短期性
SYS@ orcl >select file_type,percent_space_used used,percent_space_reclaimable reclaimable,number_of_files files from v$flash_recovery_area_usage; FILE_TYPE USED RECLAIMABLE FILES -------------------- ---------- ----------- ---------- CONTROL FILE 0 0 0 REDO LOG 0 0 0 ARCHIVED LOG 0 0 0 BACKUP PIECE 0 0 0 IMAGE COPY 0 0 0 FLASHBACK LOG 0 0 0 FOREIGN ARCHIVED LOG 0 0 0 --alter system reset log_archive_dest_1; restart instance SYS@ orcl >alter system switch logfile; System altered. SYS@ orcl >/ System altered. SYS@ orcl >select file_type,percent_space_used used,percent_space_reclaimable reclaimable,number_of_files files from v$flash_recovery_area_usage; FILE_TYPE USED RECLAIMABLE FILES -------------------- ---------- ----------- ---------- CONTROL FILE 0 0 0 REDO LOG 0 0 0 ARCHIVED LOG .79 0 2 BACKUP PIECE 0 0 0 IMAGE COPY 0 0 0 FLASHBACK LOG 0 0 0 FOREIGN ARCHIVED LOG 0 0 0 SYS@ orcl >select name from v$archived_Log where sequence#=(select max(sequence#) from v$archived_Log); 2 NAME -------------------------------------------------- /u01/app/oracle/fra/ORCL/archivelog/2019_05_27/o1_ mf_1_277_ggq5qlvl_.arc
发现归档中2个文件在快速恢复区,占用空间3G的0.79%,并且因为reclaimable(可回收)等于0,所以暂时是不可以删除的,这是由保留策略决定的。
如果执行backup database的大小超过了3g的大小,就会报错ora-19815,更严重的,这里将归档日志也放在此目录,快速恢复区占满时ARCn进程会报错ORA-19809:limit exceeded for recovery files,无法完成归档,最终导致数据库无法操作。
--alter system set db_recovery_file_dest_size='30G';
理论上,快速恢复区空间越大其作用就越大,注意,使用快速恢复区关键”其物理空间必须足够大”
2 通道分配
所谓通道,指由服务器进程发起并控制的目标数据库的文件与物理备份设备之间的字节流。
通道工作的三个阶段:读、复制、写。
为了避免产生不可用的备份或将已损坏的备份还原,rman还会自动校验流通道中的数据库,一发现有损坏,立即报错ora-19566。
自动通道分配
--show default service type查看默认,自动通道分配 --show device type 查看所有可用的设备类型 --show channel 查看所欲的已配置通道 --configure default device type to ...修改自动通道的设备类型 --configure device type修改特定的设备类型支持的通道数 --configure channel device type创建或修改特定设备类型的通道模板 --configure channel N device type ...创建或修改特定通道的属性 RMAN> show default device type;##结果是DISK设备 using target database control file instead of recovery catalog RMAN configuration parameters for database with db_unique_name ORCL are: CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default RMAN> show device type; ##通道并行度为1 RMAN configuration parameters for database with db_unique_name ORCL are: CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default RMAN> show channel; RMAN configuration parameters for database with db_unique_name ORCL are: RMAN configuration has no stored or default parameters RMAN> backup tablespace users ; Starting backup at 27-MAY-19 allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=144 device type=DISK channel ORA_DISK_1: starting full datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf channel ORA_DISK_1: starting piece 1 at 27-MAY-19 channel ORA_DISK_1: finished piece 1 at 27-MAY-19 piece handle=/u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T161523_ggq74v7r_.bkp tag=TAG20190527T161523 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 27-MAY-19 Starting Control File and SPFILE Autobackup at 27-MAY-19 piece handle=/u01/app/oracle/fra/ORCL/autobackup/2019_05_27/o1_mf_s_1009383324_ggq74wh5_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 27-MAY-19
提升通道数
--configure device type disk parallelism 2;
通道的数量不一定是rman命令的并行度,通道数大于或等于实际的并行度。
在oracle 11g可以让不同的通道并行地备份同一个文件的不同备份。
RMAN>configure channel 1 device type disk to destination ‘/home/oracle/disk1; RMAN>configure channel 2 device type disk to destination ‘/home/oracle/disk2; 将自动通道的设备修改为SBT RMAN>configure default device type to sbt; Oracle rac备份通道 RMAN> configure channel 1 device type disk connect 'sys/oracle@orcl1'; RMAN> configure channel 2 device type disk connect 'sys/oracle@orcl2'; 手动分配通道 RMAN>configure device type disk 2>(tablespace users channel ora_disk1) 3>(talbespace test channel ora_disk2);
3 备份集备份
backup能产生2中备份:备份集合镜像复制
RMAN> show device type; RMAN configuration parameters for database with db_unique_name ORCL are: CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
RMAN备份集术语:输入文件、备份集、备份片、多路传输
凡是被rman备份的文件,无论种类,都称为输入文件,有控制文件、归档日志、数据文件、参数文件4种。
备份集(backup set)就是输入文件的备份,物理上由备份(backup piece)片组成 Piece Name: /u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T161523_ggq74v7r_.bkp--ol_mf_ncnnf 只包含控制文件 --ol_mf_ncsnf 包含控制文件和参数文件 --ol_mf_nnndf 只包含数据文件 --ol_mf_annnn只包含归档文件 --ol_mf_nnsnf 只包含参数文件 ORCL/autobackup/2019_05_27/o1_mf_s_1009383324_ggq74wh5_.bkp 控制文件自动备份
--plus archivelog,若备份时使用了该子句,rman会做一个”归档收尾”,即切换当前在线日志--归档该日志--备份刚刚产生归档日志,所以,会出现一个额外的只有一个归档日志的备份集。
RMAN> run{ Allocate channel c1 device type disk maxopenfiles 2; Backup database; }
-- maxopenfiles 2,rman每次只读2个数据文件
数据文件增量备份
--backup incremental level 0 database; ##等级0增量备份
--backup incremental level 1 database; ##等级1差异增量备份
--backup cumulative incremental level 1 database; ##等级1累积增量备份
将来介质恢复时,执行recover命令会自动使用增量备份。
4 镜像复制备份
是一种极为简单的备份方式,类似于直接使用操作系统的复制命令,备份文件就是一个输入文件的一份副本,与镜像复制相比,备份集备份就好比是一个压缩包。
镜像复制的数量和大小均和输入文件一模一样,缺点是空间无法优化,优点是可大大缩减还原操作的视觉,就是用重命名代替restore,操作需要的时间很短,与文件大小无关。
RMAN>configure device type disk backup type to copy;
DB处于open状态, 必须是归档状态,backup时必须是mount或open
RMAN> run{ allocate channel c1 device type disk; backup datafile 1,2; }2> 3> 4> released channel: ORA_DISK_1 allocated channel: c1 channel c1: SID=144 device type=DISK Starting backup at 27-MAY-19 channel c1: starting full datafile backup set channel c1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf channel c1: starting piece 1 at 27-MAY-19 channel c1: finished piece 1 at 27-MAY-19 piece handle=/u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T171322_ggqbkldt_.bkp tag=TAG20190527T171322 comment=NONE channel c1: backup set complete, elapsed time: 00:00:07 Finished backup at 27-MAY-19 Starting Control File and SPFILE Autobackup at 27-MAY-19 piece handle=/u01/app/oracle/fra/ORCL/autobackup/2019_05_27/o1_mf_s_1009386809_ggqbksk6_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 27-MAY-19 released channel: c1
RMAN> run{ allocate channel c1 device type disk; backup as copy current controlfile; }2> 3> 4> allocated channel: c1 channel c1: SID=144 device type=DISK Starting backup at 27-MAY-19 channel c1: starting datafile copy copying current control file output file name=/u01/app/oracle/fra/ORCL/controlfile/o1_mf_TAG20190527T171337_ggqbl1k5_.ctl tag=TAG20190527T171337 RECID=1 STAMP=1009386817 channel c1: datafile copy complete, elapsed time: 00:00:01 Finished backup at 27-MAY-19 Starting Control File and SPFILE Autobackup at 27-MAY-19 piece handle=/u01/app/oracle/fra/ORCL/autobackup/2019_05_27/o1_mf_s_1009386818_ggqbl2md_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 27-MAY-19 released channel: c1
注意,rman不会对参数文件做镜像复制备份。
5 rman备份的优化
--备份集优化
备份集具有一个镜像复制不可能具备的功能:压缩,为了节省磁盘空间,备份集可以采取3中方式压缩备份集:空块压缩、未使用压缩、二进制压缩
RMAN>configure compression algorithm 'LOW';
--镜像复制优化
镜像复制最大的缺点是浪费空间,oracle的解决方案是使用等级1增量备份”更新”镜像复制
第一天
RMAN>backup incremental level 1 for recover of copy with tag ‘FLASH’ database;
第二天
RMAN>backup incremental level 1 for recover of copy with tag ‘FLASH’ database;
RMAN>recover copy of database with tag ‘FLASH’;
6 rman相关视图
--v$session_longops 展现rman任务进度的好工具 --select * from v$session_longops; --select case when opname like '%aggregate%' then 'Total' else opname end opname, trunc(sofar*100/totalwork,2)||'%' progress, units from v$session_longops where opname like 'RMAN%' and totalwork>sofar; SYS@ orcl >select name,thread#,sequence#,first_time,creator,applied,status from v$archived_Log; SYS@ orcl > select * from v$backup_set;##当前备份集信息 SYS@ orcl >select * from v$backup_set_details;##包含了v$backup_set的信息,而已还有备份集的大小,所在设备 SYS@ orcl >select * from v$backup_corruption;##记录了备份集中发行损坏的数据块 SYS@ orcl >select * from v$backup_piece where status='a' and handle is not null;##显示备份片段的信息 SYS@ orcl >select * from v$session_longops;##记录数据库中执行时间超过6秒的操作
7 rman资料库
--list eportcrosscheckcatalogdeletevalidatechange等命令 RMAN> list backupset;查看所有备份集 RMAN> list backupset completed after "sysdate-7";查看一周内创建的备份集 RMAN>list backupset completed before "sysdate-7";查看一周前创建的备份集 RMAN>list datafilecopy all;查看所欲的数据文件镜像复制 RMAN>list copy;查看所有的数据文件和控制文件的镜像复制及所有归档日志 RMAN>list backupset of controlfile;查看所有控制文件备份集备份 RMAN>list copy of controlfile;查看控制文件的镜像复制 RMAN>list backupset of tablespace users;查看user表空间的备份集 RMAN>list backupset of datafile 1,3;查看特定编号的数据文件的备份集 RMAN>list backupset of database;查看任意数据文件的备份集 RMAN>list backupset tag 'weekly_backup';查看标记为weekly_backup的备份集 RMAN>report schema;报告当前数据文件的结构 RMAN>report need backup days 4;列出已经4天没有备份的数据文件 RMAN>report need backup incremental 4;列出恢复路径需要超过4个等级1增量备份的数据文件 RMAN>report need backup redundancy 3;列出备份冗余度未到达3的数据文件 RMAN>report need backup tablespace users,system;根据保留策略列出特定表空间中需要备份的数据文件 RMAN>report obsolete;根据保留策略列出已经废弃的备份
资料库的信息有时会与真实情况不符,比如使用操作系统的命令删除备份而不是通过delete obsolete命令,这样rman会认为该备份还存在,可能会导致严重的后果。
例子:
在os层面删除
[root@DSI ~]# rm /u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T171322_ggqbkldt_.bkp
RMAN> report need backup redundancy 1 tablespace users;
为了避免rm干扰rman资料库,在执行list或report之前,考虑使用crosscheck,目的是效验资料库中记录的真实性
RMAN> crosscheck backupset device type disk; backup piece handle=/u01/app/oracle/fra/ORCL/autobackup/2019_05_27/o1_mf_s_1009383324_ggq74wh5_.bkp RECID=32 STAMP=1009383324 crosschecked backup piece: found to be 'EXPIRED'
发现有备份片信息过期found to be 'EXPIRED',再次运行report
report need backup redundancy 1 tablespace users; RMAN> list backupset of tablespace users; RMAN> delete noprompt expired backupset; using channel ORA_DISK_1 List of Backup Pieces BP Key BS Key Pc# Cp# Status Device Type Piece Name ------- ------- --- --- ----------- ----------- ---------- 33 33 1 1 EXPIRED DISK /u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T171322_ggqbkldt_.bkp deleted backup piece backup piece handle=/u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T171322_ggqbkldt_.bkp RECID=33 STAMP=1009386802 Deleted 1 EXPIRED objects
若要删除备份或者归档日志,推荐使用rman的delete obsolete命令,不推荐在os级别进行删除。
若rman资料库中丢失了某备份的信息(比如新建控制文件之后或将备份文件转移后执行了delete expired命令),可以通过catalog命令将其信息寻回,该命令能自动扫描备份文件,判断其是否合法并获取所有必要信息。
RMAN> catalog backuppiece '/u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T171322_ggqbkldt_.bkp';
有时,管理员需要校验一下备份的有效性,可以使用validate,该命令可以认为是一种深度的crosscheck,比如检查备份集是否有效、有无坏块、是否可用于还原操作
RMAN> validate device type disk backupset 33; Starting validate at 28-MAY-19 released channel: ORA_DISK_1 allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=144 device type=DISK RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03002: failure of validate command at 05/28/2019 17:15:03 RMAN-20215: backup set not found RMAN-06159: error while looking up backup set
RMAN> validate device type disk backupset 31; Starting validate at 28-MAY-19 released channel: ORA_DISK_1 allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=144 device type=DISK channel ORA_DISK_1: starting validation of datafile backup set channel ORA_DISK_1: reading from backup piece /u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T161523_ggq74v7r_.bkp channel ORA_DISK_1: piece handle=/u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T161523_ggq74v7r_.bkp tag=TAG20190527T161523 channel ORA_DISK_1: restored backup piece 1 channel ORA_DISK_1: validation complete, elapsed time: 00:00:01 Finished validate at 28-MAY-19
--RMAN>change backupset 31 unavailable; ##将制定备份集改为步可以,导致将来restore命令不可能使用该备份集,注意,在快速恢复区中的备份不能被设置为步可以。 RMAN> change backupset 31 unavailable; ##31号备份集在快速恢复区中 RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03009: failure of unavailable command on ORA_DISK_1 channel at 05/28/2019 17:18:01 ORA-19813: cannot have unavailable file /u01/app/oracle/fra/ORCL/backupset/2019_05_27/o1_mf_nnndf_TAG20190527T161523_ggq74v7r_.bkp in DB_RECOVERY_FILE_DEST RMAN> change backupset 12 unavailable; ##12号备份集不在快速恢复区中 changed backup piece unavailable backup piece handle=/u01/app/oracle/product/11.2.0/db_1/dbs/0gu262eu_1_1 RECID=12 STAMP=1008929246 Changed 1 objects to UNAVAILABLE status --RMAN>change backupset 1 crosscheck; ##等同于crosscheck --RMAN>change backupset 1 delete;##等同于delete --RMAN>change backupset 1 keep until time "sysdate+365"; ##将1号备份集的保留时间限定为1年,即1年内使用delete obsolete命令删除不了此备份,注意,在快速恢复区中的备份不能被设置为until time的保留策略 --RMAN>change backupset 1 nokeep;##将1号备份集的保留策略恢复 --RMAN>change datafile copy '/home/oracle/system01.backup' uncatalog;##将指定数据文件镜像复制从rman资料库删除,uncatalog不支持备份集,但单独输入备份片的名是可行的。 --RMAN>change backup of database validate;##等同于validate,但执行validate backup of database命令会报语法错误。
最后必须澄清一个重要的信息,所有的configure命令的执行结果也保存在rman资料库中。