本章对RMAN进行基本的熟悉和了解
1.Overview of the RMAN Environment
RMAN运行时需要的最小环境:
target database :RMAN用target命令连接的数据库,在该数据库上执行备份和恢复操作,并备份信息(RMAN reposity 资源库)记录在target数据库的控制文件中.
RMAN client :rman客户端,即RMAN可执行程序.在$ORACLE_HOME/bin下
RMAN运行时可能需要的其它环境:
fast recovery area :默认的RMAN备份保存地点,位于磁盘上,由 DB_RECOVERY_FILE_DEST(位置)和DB_RECOVERY_FILE_DEST_SIZE(大小) 参数一起决定.
media manager :介质管理器.与RMAN进行交互的程序(如磁带驱动器),实现顺序读写、设置标签、加载数据等,一般又叫SBT(SYSTEM BACKUP TO TAPE),就是磁驱
recovery catalog :恢复目录,用来保存一个或多个target数据库的备份信息.如果target数据库的控制文件丢失,可以从catalog中读备份信息,从而进行数据库的恢复。控制文件中的备份信息会过期,但是catalog中的不会过期,除非手动删除
2.Starting RMAN and Connecting to a Database
RMAN连接数据库也需要用户名和密码,与SQLPLUS一样.需要注意的是,用RMAN连接target数据时,需要SYSDBAS权限,并且SYSDBA不写在命令上。如:
% rman
RMAN> CONNECT TARGET SYS@prod
也可以用操作系统验证登录:
3.Showing the Default RMAN Configuration(RMAN的默认配置)
RMAN预先做了一些配置。show all可以显示配置
RMAN>show all;
4.Backing Up a Database(备份数据库)
用backup命令备份数据库,如果不手动指定设备和保存位置,会用配置好的设备来备份和保存备份文件。备份文件默认放在磁盘上的fast recovery area.如果指定了format, 则保存到format的位置,并且会自动给备份文件命名.
默认情况下,生成的文件是备份集,而不是copy(镜像).备份集一个或多个备份片(pieces),备份片只能由RMAN读定,其它程序无法访问.备份集包括了所有使用过的块(不包括未使用的块),备份集可以保存磁盘或磁带上
backup as copy备份生生成一个镜像,镜像是数据文件的复制,相当于操作系统的cp命令.注意 :用操作系统cp命令复制数据文件是无效的,因为文件中的数据块是不一致的,用RMAN进行镜像备份是有效的,因为RMAN可以读取镜像和归档来保证数据块的一致性.
5.Backing Up a Database in ARCHIVELOG Mode备份归档数据库
数据库运行在归档模式下,可以在数据库打开时备份(在线备份),如果运行在非归档模式下,只能在关闭后备份或才用EXP EXPDP进行逻辑备份.在线的备份称为不一致的备份,在进行恢复时需要使用日志文件来保证数据文件的一致性.可以把归档日志也备份下来:
RMAN> BACKUP DATABASE PLUS ARCHIVELOG;
6.Backing Up a Database in NOARCHIVELOG Mode备份非归档的数据
在非归档下备份数据库,数据库必须先一致的关闭(shutdown normal shutdown transaction shutdonw immediate),然后再启动到mount,再用RMAN备份,只有这样样,备份集才是一致的有效的.
过程如下:
RMAN> SHUTDOWN IMMEDIATE;
RMAN> STARTUP FORCE DBA;
RMAN> SHUTDOWN IMMEDIATE;
RMAN> STARTUP MOUNT;
RMAN> BACKUP DATABASE;或者RMAN> BACKUP AS COPY DATABASE;
RMAN> ALTER DATABASE OPEN;
7.Typical Backup Options典型的备份操作
RMAN支持多种备份选项,如:
Option |
Description |
Example |
FORMAT
|
Specifies a location and name for backup pieces and copies. You must use substitution variables to generate unique file names.
The most common substitution variable is %U , which generates a unique name. Others include %d for the DB_NAME, %t for the backup set time stamp,%s for the backup set number, and %p for the backup piece number.
指定备份集搁置和名称格式.默认的名称格式是%U.%d表示DB_NAME,%t表示备份时的时间戳,%s设置备份集序号,p%设置备份片序号
|
BACKUP
FORMAT 'AL_%d/%t/%s/%p'
ARCHIVELOG LIKE '%arc_dest%';
|
TAG
|
Specifies a user-defined string as a label for the backup. If you do not specify a tag , then RMAN assigns a default tag with the date and time. Tags are always stored in the RMAN repository in uppercase.
为本次备份设置一个别名,以便恢复时使用
|
BACKUP
TAG 'weekly_full_db_bkup'
DATABASE MAXSETSIZE 10M;
|
8.Making Incremental Backups 增量备份
增量备份是指在上一次完全备份的基础上只备份修改过的块,通常比完全备份要块.如果没有完全备份,首次执行增量备份会进行完全备份作为0级增量备份.
增量备份分为两种:差异增量备份和累积增量备份.区别是,差异增量备份会备份同级别或以下级别备份以来修改的块,而累积备份只备份自身级别以下级别的备份.在执行恢复时,RMAN以上次的0级别和完全备份为起点来执行恢复.
执行增量备份:
BACKUP INCREMENTAL CUMULATIVE DATABASE;
9.Making Incrementally Updated Backups 增量更新备份
增量更新备份:以镜像备份为基础,将修改过的块合并到原来的镜像里做为一个新的镜像使用.过程如下
1.首先执行镜像备份,指定一个tag
2.执行1级的增量备份,指定上镜像同样的tag.BACKUP FOR RECOVER OF COPY只备份上次镜像以来修改的块.
增量更新备份包含了镜像备份以来修改的块,所以恢复的时候用的更快.
增量更新备份选项:
BACKUP Option |
Description |
Example |
FOR RECOVER OF COPY WITH TAG ' tag_name '
|
Use TAG to identify the tag of the data file copy serving as basis for the backup strategy. RMAN automatically assigns the same tag to every level 1 backup of this copy.
If no level 0 data file copy with the specified tag exists in either the current or parent database incarnation, then RMAN creates a level 0 data file copy with the specified tag.
指定tag,然后RMAN会自动合并镜像.如果没有执行过镜像备份,则RMAN创建镜像备份
|
BACKUP
INCREMENTAL LEVEL 1
FOR RECOVER OF COPY
WITH TAG 'incr_update'
DATABASE;
|
FOR RECOVER OF COPY DATAFILECOPY FORMAT ' format '
|
Specifies where RMAN creates the data file copy if a copy does not exist. If you add a new data file to the database, then you do not need to change your script, because RMAN automatically creates the level 0 copy required by the incremental backup routine.
备份数据文件镜像.
|
BACKUP
INCREMENTAL LEVEL 1
FOR RECOVER OF COPY
DATAFILECOPY FORMAT
'/disk2/df1.cpy'
DATABASE;
|
如:
RECOVER
FOR RECOVER OF COPY WITH TAG 'incr_update'
DATABASE;
10.Validating Database Files and Backups 验证数据文件和备份
用VALIDATE
验证数据文件是否存在,是否有错误.CHECK LOGICAL 命令验证数据文件是否有逻辑错误.
物理验证和逻辑验证数据文件和归档日志:
BACKUP VALIDATE CHECK LOGICAL
DATABASE ARCHIVELOG ALL;
验证数据块:
VALIDATE DATAFILE BLOCK ;
验证备份集:
11.Scripting RMAN Operations RMAN脚本
可以把RMAN命令保存在文本中执行.如:
# my_command_file.txt
CONNECT TARGET /
BACKUP DATABASE PLUS ARCHIVELOG;
LIST BACKUP;
EXIT;
调用
% rman
RMAN> @/my_dir/my_command_file.txt # runs specified command file
或者
% rman @/my_dir/my_command_file.txt
12.Reporting on RMAN Operations RMAN报告
list 和report命令可以显示备份相关信息,show all显示RMAN配置
1) list命令
list backup和list copy
list backup:
Option |
Example |
Explanation |
BY BACKUP
|
LIST BACKUP OF DATABASE BY BACKUP
|
Organizes the output by backup set. This is the default mode of presentation.
|
BY FILE
|
LIST BACKUP BY FILE
|
Lists the backups according to which file was backed up.
|
SUMMARY
|
LIST BACKUP SUMMARY
|
Displays summary output.
|
其它list选项:
Option |
Example |
Explanation |
EXPIRED
|
LIST EXPIRED COPY
显示存在不RMAN资源库但不在磁盘上的备份
|
Lists backups that are recorded in the RMAN repository but that were not present at the expected location on disk or tape during the last CROSSCHECK command. An expired backup may have been deleted by an operating system utility.
|
RECOVERABLE
|
LIST BACKUP RECOVERABLE
显示可用的备份或镜像
|
Lists data file backups or copies that have status AVAILABLE in the RMAN repository and that can be restored and recovered.
|
如:
LIST BACKUP , ;
LIST BACKUP ;
LIST BACKUPSET ;
2)report
report选项:
Option |
Example |
Explanation |
NEED BACKUP
|
REPORT NEED BACKUP DATABASE
在当前的保留策略下需要备份的文件
|
Shows which files need backing up under current retention policy. Use optionalREDUNDANCY and RECOVERY WINDOW parameters to specify different criteria.
|
OBSOLETE
|
REPORT OBSOLETE
报告过期的备份
|
Lists backups that are obsolete under the configured backup retention policy. Use the optional REDUNDANCY and RECOVERY WINDOW parameters to override the default.
|
SCHEMA
|
REPORT SCHEMA
显示表空间和数据文件
|
Reports the tablespaces and data files in the database at the current time (default) or a different time.
|
UNRECOVERABLE
|
REPORT UNRECOVERABLE
显示无法恢复的数据文件
|
Lists all data files for which an unrecoverable operation has been performed against an object in the data file since the last backup of the data file.
|
12.Maintaining RMAN Backups 维护RMAN备份
用来维护RMAN资源库的命令包括:CATALOG
, CHANGE
, CROSSCHECK
, DELETE
.
1)Cross-checking Backups 用crosscheck来检测备份
crosscheck用来检查备份集或状态是否有效并同步到控制文件.应该在删除备份集或镜像前做crosschek.备份在磁盘时,crosscheck检查文件头是否有效,有磁带时,会查询资源库.
检测备份和镜像:
CROSSCHECK BACKUP;
CROSSCHECK COPY;
2)Deleting Obsolete Backups 删除过期备份
delete命令用来从磁盘和磁带删除过期的备份和镜像,并修改控制文件和catalog里的备份信息.在交互式下,delete会先显示需要删除的文件,在用户确认后删除.如:
RMAN
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: SID 设备类型 月 D:\APP\WJ\PRODUCT\..\DB_1\RDBMS\ARC00059_0817241642.
存档日志 月 D:\APP\WJ\PRODUCT\..\DB_1\RDBMS\ARC00060_0817241642.
存档日志 月 D:\APP\WJ\PRODUCT\..\DB_1\RDBMS\ARC00061_0817241642.
存档日志 月 D:\APP\WJ\FLASH_RECOVERY_AREA\ARC00075_0817241642.
存档日志 月 D:\APP\WJ\FLASH_RECOVERY_AREA\ARC00076_0817241642.
存档日志 月 D:\APP\WJ\FLASH_RECOVERY_AREA\ARC00077_0817241642.
存档日志 月 D:\APP\WJ\FLASH_RECOVERY_AREA\ARC00078_0817241642.
存档日志 月 D:\APP\WJ\FLASH_RECOVERY_AREA\ARC00079_0817241642.
是否确定要删除以上对象 (输入 YES 或 NO)? no
13.Diagnosing and Repairing Failures with Data Recovery Advisor 数据恢复顾问
DRA可以检测永久性的错误,提供修复建议或者自动执行修复.
failure指被Health Monitor检测出的数据块错误.每个错误有错误优先级和错误状态.如CRITICAL
, HIGH
, or LOW
和
OPEN
or CLOSED.
list failure会显示错误,advisor failure提供错误修改建议,reparit failure尝试修改错误.
如:
RMAN HIGH One HIGH Datafile : HIGH One HIGH Datafile : . If file Restore ; Perform block media recovery
Strategy: The repair includes complete media recovery with no data loss
Repair script: /disk1/oracle/log/diag/rdbms/prod/prod/hm/reco_660500184.hm
首先尝试人工修复(在某些情况下人工修复比自动修复要快,如控制文件丢失),在人工修复不能完成的情况下使用自动修复
repair 尝试修复错误,会将出现的错误一起修复.
在执行修复前,会进行提示,确认后才会执行.在修复完成后,DRA会把已经修复的错误从当前的错误列表删除.
14.Rewinding a Database with Flashback Database
用flashback将数据库闪回到某个点,不像介质恢复,不需要还原数据文件,速度很快.
在使用flashback database前,必须开启闪回日志(flashback log).闪回操作使用闪回日志把数据块的更新还原,不能用闪回来做介质恢复和数据文件丢失.
数据库必须在mount下进行闪回 ,
如下在RMAN中使用闪回:
SHUTDOWN ;
FLASHBACK ::00','DD-MON-YYYY HH24:MI:SS');
SQL "ALTER DATABASE OPEN READ ONLY";
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
ALTER DATABASE OPEN RESETLOGS;
15.Restoring and Recovering Database Files
1)还原和恢复数据库前需要做的准备.显示可以还原的最小SCN
RESTORE ... PREVIEW 文件命令用来显示可以进行恢复的最小SCN号,在执行这个命令时,RMAN只是读元数据并且备份的文件.
RMAN
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID device B F A DISK B F A DISK A
Recovery must be done beyond SCN
2)Recovering the Whole Database 恢复整个数据库
使用restore database和recover database来还原和恢复数据.前提是必须要有可用的数据库备份.如果不想把文件还原在原来的位置,使用set newname命令来指定新的位置
启动数据库到mount,如果无法启动到mount,执行
RMAN> STARTUP FORCE MOUNT;
RMAN> RESTORE DATABASE;
RMAN> RECOVER DATABASE;
RMAN> ALTER DATABASE OPEN;
3)Recovering Tablespaces 恢复表空间
RESTORE
TABLESPACE
和
RECOVER
TABLESPACE
用来还原和恢复单个表空间,可以在数据库打开的情况下执行.首先,要把表空间脱机,然后执行restore再执行recover,再把表空间联机online
如果需要把数据文件还原到新的位置,设置set newname,然后执行SWITCH DATAFILE ALL,情相当于ALTER DATABASE RENAME FILE 命令来更新控制文件信息.
注意:在执行表空间恢复时,不能将表空间设置为备份模式.
RMAN> SQL 'ALTER TABLESPACE users OFFLINE';
RUN
{
SET NEWNAME FOR DATAFILE '/disk1/oradata/prod/users01.dbf'
TO '/disk2/users01.dbf';
RESTORE TABLESPACE users;
SWITCH DATAFILE ALL; # update control file with new file names
RECOVER TABLESPACE users;
}
RMAN> SQL 'ALTER TABLESPACE users ONLINE';
3)Recovering Individual Data Blocks
用RMAN执行数据文件或备份后验证后,错误的数据块会记录在V$DATABASE_BLOCK_CORRUPTION中,错误信息会记录在alert,trace文件中.
RMAN BLOCK , DATAFILE BLOCK ;修复指定的错误块