1. Introduction to Recovery Manager
3个重要的特点:
在线热备不需要ALTER TABLESAPCE …BEGIN BACKUP or END BACKUP,并且不会产生更多日志
Incremental backups and restores. Can recover a NOARCHIVELOG database. However, only take incremental backups of a NOARCHIVELOG database after a consistent shutdown. 只备份HWM以下的blocks
检测数据库坏块(interruption blocks), Computes checksums for each block during a backup
其他作用:
复制一个prd数据库用来测试,建立或者备份一个standby db
Tests whether files can be backed up or restored without actually performing the backup or restore.
Overview of the RMAN Environment
RMAN executable (必须的)
Target database (必须的)
Recovery catalog database
Media management software
2. Connecting to Databases with RMAN
Starting RMAN: Overview
连接3种数据库:
% RMAN TARGET SYS/Oracle@trgt CATALOG RMAN/cat@catdb
--RMAN用户需要RECOVERY_CATALOG_OWNER 角色
% RMAN TARGET SYS/Oracle@trgt NOCATALOG
--目标数据库用户需要sysdba权限,因为是默认,所以不用额外注明as sysdba。NOCATALOG 是默认形式,一旦使用了这种形式,就不能在连接catalog了。
% RMAN TARGET / CATALOG RMAN/cat@catdb AUXILIARY SYS/Oracle@auxdb
或者:
% RMAN
RMAN> CONNECT TARGET SYS/Oracle@trgt
RMAN> CONNECT CATALOG RMAN/cat@catdb
连接RAC(3个instance):
% RMAN TARGET SYS/Oracle@trgt2 CATALOG RMAN/cat@catdb
CONFIGURE DEFAULT DEVICE TYPE TO sbt;
CONFIGURE DEVICE TYPE sbt PARALLELISM 3;
CONFIGURE CHANNEL 1 DEVICE TYPE sbt CONNECT = 'SYS/Oracle@trgt1';
CONFIGURE CHANNEL 2 DEVICE TYPE sbt CONNECT = 'SYS/Oracle@trgt2';
CONFIGURE CHANNEL 3 DEVICE TYPE sbt CONNECT = 'SYS/Oracle@trgt3';
BACKUP DATABASE;
3. Quick Start to Recovery Manager
参考?/rdbms/demo/*.rcv
RMAN基本命令(NOCATALOG mode)
1. Connecting to the Target Database
% RMAN target sys/admin@donnydb2
2. 可以在RMAN中操作数据库
RMAN> STARTUP MOUNT
3. 产生schema reports
RMAN> report schema;
4. 备份数据库
有2中方式:full(all blocks. datafiles, controlfile, parameter file) 和 incremental(only the blocks that have changed since a previous backup)
Full Backup:
RMAN> BACKUP DATABASE;
默认device type 是disk,除非用configure 配置其到tape
创建一个逻辑对象backup set, 包括多个backup piece
Incremental Backup
RMAN> BACKUP INCREMENTAL LEVEL 1 DATABASE;
差异增量,累计增量。
RMAN compares the current datafiles to a base backup, also called a level 0 backup, to determine which blocks to back up.
0级增量和全备份不同,前者可以作为其他增量备份的基础备份,而后者不行。
可能会发现有:no parent backup or copy of datafile 1 found;这说明datafile 1还没有0级备份,RMAN会自动创建。
5. 备份一个表空间
RMAN> BACKUP TABLESPACE SYSTEM;
6. 备份归档日志
RMAN> BACKUP ARCHIVELOG ALL DELETE INPUT;
因为Oracle不能直接将日志归档到tape,所以用RMAN将归档从disk备份到tape。如果指明delete input, RMAN会为每一个log seq num 备份一个copy,并且删除disk上的文件。
7. 拷贝数据文件datafiles
RMAN> copy datafile 4 to 'd:Oracleoradatadonnydb2bakhehe.dbf';
不用在关闭状态吗?这样的拷贝是一致的吗?
8. Listing Backups and Copies
RMAN> list backup;
RMAN> list copy;
9. 检测是否可以restore the backup, 而不用真正的restore
RMAN> restore database validate;
如果不报错,就可以了。
10. Restoring and Recovering the Database
RMAN> RESTORE DATABASE;
RMAN> RECOVER DATABASE;
RMAN> ALTER DATABASE OPEN;
11. Restoring and Recovering a Tablespace
RMAN> SQL 'ALTER TALBESPACE tools OFFLINE IMMEDIATE';
RMAN> RESTORE TABLESPACE tools;
RMAN> RECOVER TABLESPACE tools;
RMAN> SQL 'ALTER TABLESPACE tools ONLINE';
12. 查看RMAN 配置
RMAN> show all;
返回到默认配置,执行CONFIGURE ... CLEAR
使用单独的Recovery Catalog,还是Nocatalog 模式(默认模式,记录在目标数据库controlfile 中)?
当管理多个数据库时,建议使用catalog。要注意平时对catalog的备份。好处如下:
You can store RMAN scripts in the recovery catalog.
When restoring and recovering to a time when the database files that exist in the database are different from the files recorded in the mounted control file, the recovery catalog specifies which files that are needed. Without a catalog, you must first restore a control file backup that lists the correct set of database files.
If the control file is lost and must be restored from backup, and if persistent configurations have been made to automate the tape channel allocation, these configurations are still available when the database is not mounted.
RMAN periodically migrates information from the control file to the recovery catalog. 在哪里配置?
文本Tag: Oracle数据库开发
4. Recovery Manager Architecture
1. Overview of RMAN Architecture
The RMAN client application directs database server sessions to perform all backup and recovery tasks.
If more than one channel is allocated, then RMAN can execute certain commands in parallel so that all of the channels' target database sessions are concurrently executing an RPC call.
2. RMAN Command Interface
存储script:
REPLACE SCRIPT b_whole_l0
{
# back up whole database and archived logs
BACKUP
INCREMENTAL LEVEL 0
TAG b_whole_l0
FILESPERSET 6
DATABASE PLUS ARCHIVELOG;
}
执行:
RUN { EXECUTE SCRIPT b_whole_10 };
查看:
SQL> SELECT * FROM RMAN.RC_STORED_SCRIPT;
3. RMAN Repository
注册DB:
在catalog中注册数据库,registration。注册多个数据库时,要求数据库有不同的DBID,但允许有相同name的数据库。如果通过os拷贝形成的新数据库,则与原来的db有相同的dbid。这是可以使用RMAN 的duplicate 命令注册新db,或者使用DBNEWID改变dbid.
同步recovery catalog:
RMAN会自动检测并执行full / partial 同步。
也可以执行命令:RESYNC CATALOG;
至少每CONTROL_FILE_RECORD_KEEP_TIME天,执行一次。因为控制文件的部分信息是周期被reused,超过这个周期,控制文件中的部分信息有可能会被覆盖。
控制文件中有两部分内容:
circular reuse records:
Log history
Archived redo logs
Backups
Offline ranges for datafiles
noncircular reuse records:
比如datafiles, online redo logs, and redo threads.
对于Recovery Without a Catalog:
建议Enable the control file autobackup feature,景象控制文件,Keep all Recovery Manager backup logs
5. RMAN Concepts I: Channels, Backups, and Copies
1. RMAN Automatic and Manual Channel Allocation
什么是channel:
RMAN channel 代表传输到某个device type的数据流stream以及相应的server session,每个channel对应一个server session。
根据如下配置自动分配channel:
CONFIGURE DEVICE TYPE (DISK) PARALLELISM (3) --每次分配几个channel;
CONFIGURE DEFAULT DEVICE TYPE TO (DISK or SBT) –默认device type
CONFIGURE CHANNEL DEVICE TYPE
(DISK RATE 5M FORMAT="?/oradata/%U" MAXOPENFILES=20;) –模版
CONFIGURE CHANNEL 3 DEVICE TYPE DISK MAXPIECESIZE = 900K; --某个channel的特别设置
RMAN> SHOW DEVICE TYPE; --查看
或手工分配:
RUN
{
ALLOCATE CHANNEL c1 DEVICE TYPE sbt;
ALLOCATE CHANNEL c2 DEVICE TYPE sbt;
BACKUP DATAFILE 5,6;
}
返回到默认值:
CONFIGURE DEVICE TYPE DISK CLEAR;
2. Backup Sets
逻辑概念,可以包括1个或多个backup pieces(os 文件)。压缩特点:即只备份HWM以下的block,当然数据文件镜像是备份全部。
BACKUP DATABASE;
BACKUP TABLESPACE users, tools;
BACKUP (SPFILE) (CURRENT CONTROL FILE); --备份spfile/controlfile
BACKUP BACKUPSET 12; --备份backupset
BACKUP DATAFILECOPY '/tmp/system01.dbf'; --备份数据文件镜像
--备份归档日志:
RMAN> BACKUP ARCHIVELOG ALL DELETE INPUT;
因为Oracle不能直接将日志归档到tape,所以用RMAN将归档从disk备份到tape。如果指明delete input, RMAN会为每一个log seq num 备份一个copy,并且删除disk上的文件。
Delete all input, like… RMAN会删除disk上符合条件的归档copy。
恢复的时候,RMAN会自动将需要归档从tape上restore到disk.
--9i RMAN会搜索不同的achieve distinction, 如果有损坏,则备份其他目录相应文件:
BACKUP ARCHIVELOG FROM SEQUENCE 121 UNTIL SEQUENCE 125;
--如果不指定until的话,RMAN会自动swich 当前在线日志,并且backup 归档。
实际上RMAN 执行了ALTER SYSTEM ARCHIVE LOG CURRENT
-- plus archievelog 表示备份的数据文件可以被archivelog 恢复到一致状态,过程,类似于热备:
Runs ALTER SYSTEM ARCHIVE LOG CURRENT
Runs BACKUP ARCHIVELOG ALL.
Backs up the files specified in the BACKUP command.
Runs ALTER SYSTEM ARCHIVE LOG CURRENT.
Backs up any remaining archived redo logs generated during backup.
--如果设置了backup optimization, RMAN会跳过已经备份的archivelogs
-- Multiplexed(多元)Backup Sets
同时备份多个数据文件,并且混合在只含有一个backup piece的backup set中。
FILESPERSET:一个backup set中包含几个数据文件
MAXOPENFILES:RMAN可以同时最多read几个数据文件
-- Duplexed(复式的) Backup Sets
BACKUP DEVICE TYPE DISK COPIES 3 DATAFILE 7 FORMAT '/tmp/%U','?/oradata/%U','?/%U';
复制3个copy,但是属于同一个backup set
--Parallelization of Backups
BACKUP
(DATAFILE 1,2,3
FILESPERSET = 1
CHANNEL ORA_DISK_1) --3个backup sets
(DATAFILECOPY '/tmp/system01.dbf', '/tmp/tools01.dbf'
FILESPERSET = 2
CHANNEL ORA_DISK_2) --1个backup sets
(ARCHIVELOG FROM SEQUENCE 100 UNTIL SEQUENCE 102 THREAD 1
FILESPERSET = 3
CHANNEL ORA_DISK_3); --1个backup sets
-- Backups of Backup Sets
BACKUP COPIES 2 DEVICE TYPE sbt BACKUPSET ALL;
BACKUP BACKUPSET COMPLETED BEFORE 'SYSDATE-7' DELETE INPUT;
-- backup piece/sets的文件名,大小, I/O Rate
自动生成或者,FORMAT = '/tmp/users_%u%p%c';
MAXPIECESIZE限制每个piece的最大size
MAXSETSIZE限制每个SET的最大size
Rate: 每秒读取的bytes
-- 备份类型
Full backup: 不是增量备份,只备份HWM之下的block。归档和控制文件备份所有blocks。区别于whole备份和0级备份(区别仅在于full不会被包括在增量策略中)。不影响其他的增量备份。
Incremental Backups:只备份上一次增量备份后改变的blocks。需要一个full backup or incremental level 0 backup.
RMAN使用增量备份恢复的时候,不需要restore a base incremental backup。比如可以restore一个non-incremental的image copies, RMAN可以用增量备份恢复文件。
使用增量备份的原因:
1) 节省空间,网络带宽
2) 可以恢复通过nologging建立的对象(对比变化的数据块)。
3) 减小noarchivelog数据库的备份大小,不必每次都作whole备份。只能在正常关闭后作备份。
增量备份种类:n级增量备份以下两种情况
1) 差异增量(differential):备份最近一次n级或者更低级备份以来改变的blocks
2) 积累增量(cumulative):备份最近一次n-1级或者更低级备份以来改变的blocks。更倾向于这种备份,因为恢复的时候只需要应用到少量的增量备份集。
原理:
因为增量备份也需要读取所有blocks以决定备份哪些blocks(通过比较当前block的scn与父备份的scn作判断),所以花费的时间和full backup相当。
策略:
比如full or level 0 backup monthly, level 1 weekly, level 2 daily. 这样每次恢复最多只需要一天的redo. 可以在有50%数据块改变后,作一次level 0;
SELECT FILE#, INCREMENTAL_LEVEL, COMPLETION_TIME, BLOCKS, DATAFILE_BLOCKS
FROM V$BACKUP_DATAFILE
WHERE INCREMENTAL_LEVEL >0 AND BLOCKS / DATAFILE_BLOCKS >.5
ORDER BY COMPLETION_TIME;
比如只作了level1, 当最近依次level 1的大小是level 0的50%,则作一次level 0;
Online: 在线备份,不需要使用ALTER TABLESPACE BEGIN BACKUP命令。RMAN使用不同的方法保证一致性。
Closed: mounted but not open。分为一致和不一致两种。
Consistent: 正常关闭后,mount时作的备份
Inconsistent: open或者非正常关闭后,mount时作的备份。需要恢复到一致状态。
备份如下类型文件:
Database, tablespace(除了locally-manged temporary tablespace),datefiles, cotrolfiles, archived logs, server parameter file, backup sets
不备份如下类型文件:
Online redo logs,
Transported tablespaces before they have been made read/write,
Client-side initialization parameter files or noncurrent server parameter files
locally-manged temporary tablespace
3. backup error
NOT BACKED UP SINCE使得备份从出错处继续。
SET MAXCORRUPT允许最大的坏块数。RMAN将备份时碰到的坏块reformat header并写入backup set,同时在controfile中记录。可以通过V$BACKUP_CORRUPTION and V$COPY_CORRUPTION查到。
4. Control File and Server Parameter File 自动备份
如果CONFIGURE CONTROLFILE AUTOBACKUP on, 则在以下情况下自动备份:
1) 执行backup or copy
2) 在run中出现backup or copy
3) 当数据库结构改变。建议CONFIGURE CONTROLFILE AUTOBACKUP FOR DEVICE TYPE DISK 改变成非disk type
4) 备份datefile 1的时候?
恢复:
RESTORE CONTROLFILE FROM AUTOBACKUP
5. Backup 保留策略
Obsolete means "not needed," whereas expired means "not found."
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS; 7天内保证恢复
保证存在这样的backup: SYSDATE - (SELECT CHECKPOINT_TIME FROM V$DATAFILE) >= 7
(互斥)
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;
CONFIGURE RETENTION POLICY TO NONE; 关闭
REPORT OBSOLETE 显示过期备份
DELETE OBSOLETE 删除过期备份(可以在每周的自动script中运行)
防止被标记为obsolete:
# exempts the backup from retention policy until last day of 2002,不包括log
BACKUP DATABASE KEEP UNTIL TIME "TO_DATE('31-DEC-2002', 'dd-mon-yyyy')" NOLOGS;
# alters status of backup set 231 from KEEP to NOKEEP,可以被obsolete
CHANGE BACKUPSET 231 NOKEEP;
# specifies that this backup and the logs required to recover it are indefinitely exempt
# from the retention policy,不保留logs,说明备份一定是一致的备份。
BACKUP TABLESPACE users KEEP FOREVER NOLOGS;
6. Backup Optimization
启动备份优化后,如果发现相同的文件已经备份过,在不影响备份策略的情况下,则跳过。
7. Restartable Backups
备份失败后,可以从某个时间起,只备份没有备份过的文件。
The unit of restartability is a single backup set.
BACKUP DATABASE FILESPERSET = 8;
BACKUP DATABASE NOT BACKED UP SINCE TIME 'SYSDATE-1';
8. Image Copies
同os拷贝一样,只不过通过server session 完成,会检查坏块,并且记录信息到控制文件中。只能拷贝到disk.
如果想用copy替换数据文件,可以用switch命令。相当于alter database rename datafile;
用户自行的copy: CHANGE ... UNCATALOG
Tag:
BACKUP COPIES 1 DATAFILE 7 TAG foo
SWITCH DATAFILE 3 TO DATAFILECOPY TAG mondaypmcopy;
9. Tests and Integrity Checks for Backups
BACKUP VALIDATE; 实际备份之前作测试。
检查物理坏块,记录到control file and in the alert.log。但是不能检查所有类型的坏块
可以查询V$BACKUP_CORRUPTION and V$COPY_CORRUPTION
RMAN备份的时候,会读取block’ header and footer,判断块是否是fractured,如果是则重读。
6. RMAN Concepts II: Restore, Recovery, and Duplication
1. Restoring Files with RMAN
Restore 到默认的位置,覆盖同名文件
或者set newname,将数据文件restore到新的路径,保留当前默认路径的文件。如果指定switch,则修改control file。
SET NEWNAME FOR DATAFILE '?/oradata/trgt/tools01.dbf' TO '/tmp/tools01.dbf';
RESTORE DATAFILE '?/oradata/trgt/tools01.dbf';
SWITCH DATAFILE '/tmp/tools01.dbf' TO DATAFILECOPY '?/oradata/trgt/tools01.dbf';
(类似于ALTER DATABASE RENAME FILE)
RMAN相比backups,优先使用copy。如果有多个backup/copy,则根据是否指定until选择最新的。