RMAN 是ORACLE 提供的一个备份与恢复的工具,可以用来备份和还原数据库文件、归档日志和控制文件。它也可以用来执行完全或不完全的数据库恢复。RMAN 可以由命令行接口或者OEM的Backup Manager GUI来控制。个人认为DBA最重要的工作之一就是备份,当出现数据丢失或损坏的情况时,之前可用的备份的价值就能立竿见影的体现出来,因此很有必要把rman学习研究透(在此先感谢网友飞翔分享的rman文档和boobooke小布老师的视频讲座),下面先来简要介绍下rman的基本概念和常见备份方法;
1:RMAN 主要包括以下组件:
Target Database: (目标数据库)就是需要RMAN 对其进行备份与恢复的数据库,RMAN 可以备份数据文件,控制文件,归档日志,spfile。(注意:RMAN 不能用于备份联机日志、初始化参数文件和口令文件);
Server Session: (服务器会话) RMAN 启动数据库上的Oracle 服务器进程,将建立一个与目标数据库的会话。由目标数据库上的服务器进程进行备份、还原、恢复的实际操作;
服务器进程:RMAN 的服务进程是一个后台进程,用于与RMAN 工具与数据库之间的通信,也用于RMAN 工具与磁盘/磁带等I/O 设置之间的通信,服务进程负责备份与恢复的所有工作;
Channel: (通道) 一个通道是RMAN 和目标数据库之间的一个连接,"allocate channel"命令在目标数据库,启动一个服务器进程,同时必须定义服务器进程执行备份或者恢复操作使用的I/O类型;
通道控制命令可以用来:
控制RMAN 使用的O/S资源,影响并行度
指定I/O带宽的限制值(设置limit read rate 参数)
定义备份片大小的限制(设置limit kbytes)
指定当前打开文件的限制值(设置limit maxopenfiles)
recovery catalog: (恢复目录)用来保存备份与恢复信息的一个数据库,不建议创建在目标数据库上。RMAN 利用恢复目录记载的信息去判断如何执行需要的备份恢复操作。如果不采用恢复目录,备份信息可以存在于目标数据库的control file中;
2:概念述语
Backup Sets (备份集合):备份集合的特性:包括一个或多个数据文件或归档日志,以oracle 专有的格式保存,有一个完全的所有的备份片集合构成,构成一个完全备份或增量备份;
Backup Pieces (备份片):一个备份集由若干个备份片组成。每个备份片是一个单独的输出文件。一个备份片的大小是有限制的;如果没有大小的限制, 备份集就只由一个备份片构成。备份片的大小不能大于使用的文件系统所支持的文件长度的最大值;
Image Copies镜像备份:镜像备份是独立文件(数据文件、归档日志、控制文件)的备份。它很类似操作系统级的文件备份。它不是备份集或备份片,也没有被压缩;
Full backup Sets全备份集合:全备份是一个或多个数据文件中使用过的数据块的的备份。没有使用过的数据块是不被备份的,也就是说,oracle 进行备份集合的压缩;
Incremental backup sets增量备份集合:增量备份是指备份一个或多个数据文件的自从上一次同一级别的或更低级别的备份以来被修改过的数据块。与完全备份相同,增量备份也进行压缩;
File multiplexing:多个数据文件可以在一个备份集中;
1:备份整个数据库:
[oracle@jsb-ylw-5024 ~]$ rman target / nocatalog //nocatalog 表示不使用恢复目录
Recovery Manager: Release 11.2.0.1.0 - Production on Mon Oct 4 16:10:12 2010
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: YANG (DBID=3449196592)
using target database control file instead of recovery catalog
RMAN> list backup; //查看当前备份信息
specification does not match any backup in the repository
RMAN> backup database format '/u01/oracle/backup/bleach_%U'; //其中%U代表的是唯一的文件名
Starting backup at 04-OCT-10
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=96 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=00009 name=/u01/oradata/yang/rman_ts01.dbf
input datafile file number=00001 name=/u01/oradata/yang/system01.dbf
input datafile file number=00010 name=/u01/oradata/yang/system02.dbf
input datafile file number=00011 name=/u01/oradata/yang/sysaux02.dbf
input datafile file number=00002 name=/u01/oradata/yang/sysaux01.dbf
input datafile file number=00003 name=/u01/oradata/yang/undotbs01.dbf
input datafile file number=00007 name=/u01/oradata/yang/bleach01.dbf
input datafile file number=00005 name=/u01/oradata/yang/test.dbf
input datafile file number=00006 name=/u01/oradata/yang/test01.dbf
input datafile file number=00008 name=/u01/oradata/yang/bleach02.dbf
input datafile file number=00004 name=/u01/oradata/yang/users01.dbf
channel ORA_DISK_1: starting piece 1 at 04-OCT-10
channel ORA_DISK_1: finished piece 1 at 04-OCT-10
piece handle=/u01/oracle/backup/bleach_08lpk7kf_1_1 tag=TAG20101004T161055 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:56
Finished backup at 04-OCT-10
Starting Control File and SPFILE Autobackup at 04-OCT-10
piece handle=/u01/flash_recovery_area/YANG/autobackup/2010_10_04/o1_mf_s_731520711_6bm327d8_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 04-OCT-10
引用
RMAN> list backup;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
7 Full 1.13G DISK 00:00:49 04-OCT-10
BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20101004T161055
Piece Name: /u01/oracle/backup/bleach_08lpk7kf_1_1
List of Datafiles in backup set 7
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
1 Full 2379309 04-OCT-10 /u01/oradata/yang/system01.dbf
2 Full 2379309 04-OCT-10 /u01/oradata/yang/sysaux01.dbf
3 Full 2379309 04-OCT-10 /u01/oradata/yang/undotbs01.dbf
4 Full 2379309 04-OCT-10 /u01/oradata/yang/users01.dbf
5 Full 2379309 04-OCT-10 /u01/oradata/yang/test.dbf
6 Full 2379309 04-OCT-10 /u01/oradata/yang/test01.dbf
7 Full 2379309 04-OCT-10 /u01/oradata/yang/bleach01.dbf
8 Full 2379309 04-OCT-10 /u01/oradata/yang/bleach02.dbf
9 Full 2379309 04-OCT-10 /u01/oradata/yang/rman_ts01.dbf
10 Full 2379309 04-OCT-10 /u01/oradata/yang/system02.dbf
11 Full 2379309 04-OCT-10 /u01/oradata/yang/sysaux02.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
8 Full 9.45M DISK 00:00:00 04-OCT-10
BP Key: 8 Status: AVAILABLE Compressed: NO Tag: TAG20101004T161151
Piece Name: /u01/flash_recovery_area/YANG/autobackup/2010_10_04/o1_mf_s_731520711_6bm327d8_.bkp
SPFILE Included: Modification time: 04-OCT-10
SPFILE db_unique_name: YANG
Control File Included: Ckp SCN: 2379354 Ckp time: 04-OCT-10
RMAN> show controlfile autobackup; //可以使用“show all;”命令来查看rman的一些默认参数配置,例如备份并发,备份副本数等;可以使用configrue或者set命令来修改,set命令只在当前的rman session中有效,也可以在末尾使用clear参数来恢复初始值
RMAN configuration parameters for database with db_unique_name YANG are:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
[oracle@jsb-ylw-5024 ~]$ ls -lh /u01/oracle/backup/
总计 1.2G
-rw-r----- 1 oracle oinstall 1.2G 10-04 16:11 bleach_08lpk7kf_1_1
2: 删除备份集:
RMAN> delete backupset 7,8; //7和8代表的是备份集的编号,该命令同时删除文件系统上的备份文件
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=96 device type=DISK
List of Backup Pieces
BP Key BS Key Pc# Cp# Status Device Type Piece Name
------- ------- --- --- ----------- ----------- ----------
7 7 1 1 AVAILABLE DISK /u01/oracle/backup/bleach_08lpk7kf_1_1
8 8 1 1 AVAILABLE DISK /u01/flash_recovery_area/YANG/autobackup/2010_10_04/o1_mf_s_731520711_6bm327d8_.bkp
Do you really want to delete the above objects (enter YES or NO)? yes
deleted backup piece
backup piece handle=/u01/oracle/backup/bleach_08lpk7kf_1_1 RECID=7 STAMP=731520655
deleted backup piece
backup piece handle=/u01/flash_recovery_area/YANG/autobackup/2010_10_04/o1_mf_s_731520711_6bm327d8_.bkp RECID=8 STAMP=731520711
[oracle@jsb-ylw-5024 ~]$ ls -lh /u01/oracle/backup/
总计 0
Deleted 2 objects
指定备份片的大小和每个backupset 的最大文件数
RMAN> run {
2> allocate channel backup1 device type disk format '/u01/oracle/backup/yang_%U' maxpiecesize=100M;
3> backup database filesperset 3;
4> }
………………………………输出省略………………………………
Starting Control File and SPFILE Autobackup at 04-OCT-10
piece handle=/u01/flash_recovery_area/YANG/autobackup/2010_10_04/o1_mf_s_731521419_6bm3rd5p_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 04-OCT-10
released channel: backup1
[oracle@jsb-ylw-5024 ~]$ ls -lh /u01/oracle/backup/
总计 1.2G
-rw-r----- 1 oracle oinstall 6.3M 10-04 16:22 yang_0alpk8ad_1_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:22 yang_0blpk8ae_1_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:22 yang_0blpk8ae_2_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:22 yang_0blpk8ae_3_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:22 yang_0blpk8ae_4_1
-rw-r----- 1 oracle oinstall 69M 10-04 16:22 yang_0blpk8ae_5_1
-rw-r----- 1 oracle oinstall 85M 10-04 16:22 yang_0clpk8b1_1_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_1_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_2_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_3_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_4_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_5_1
-rw-r----- 1 oracle oinstall 100M 10-04 16:23 yang_0dlpk8b8_6_1
-rw-r----- 1 oracle oinstall 1.3M 10-04 16:23 yang_0dlpk8b8_7_1
3: 备份单个数据文件:
SQL> select file_id,file_name from dba_data_files;
FILE_ID FILE_NAME
---------- ----------------------------------------
4 /u01/oradata/yang/users01.dbf
3 /u01/oradata/yang/undotbs01.dbf
2 /u01/oradata/yang/sysaux01.dbf
1 /u01/oradata/yang/system01.dbf
5 /u01/oradata/yang/test.dbf
6 /u01/oradata/yang/test01.dbf
7 /u01/oradata/yang/bleach01.dbf
8 /u01/oradata/yang/bleach02.dbf
10 /u01/oradata/yang/system02.dbf
11 /u01/oradata/yang/sysaux02.dbf
9 /u01/oradata/yang/rman_ts01.dbf
11 rows selected.
RMAN> backup datafile 4,9 format '/u01/oracle/backup/data_%U'; //4和9代表的是file_id
[oracle@jsb-ylw-5024 ~]$ ls -lh /u01/oracle/backup/data_*
-rw-r----- 1 oracle oinstall 6.3M 10-04 16:32 /u01/oracle/backup/data_0flpk8tb_1_1
4: 备份表空间:
RMAN> report schema;
Report of database schema for database with db_unique_name YANG
List of Permanent Datafiles
===========================
File Size(MB) Tablespace RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1 680 SYSTEM *** /u01/oradata/yang/system01.dbf
2 560 SYSAUX *** /u01/oradata/yang/sysaux01.dbf
3 75 UNDOTBS1 *** /u01/oradata/yang/undotbs01.dbf
4 5 USERS *** /u01/oradata/yang/users01.dbf
5 20 TEST *** /u01/oradata/yang/test.dbf
6 20 TEST *** /u01/oradata/yang/test01.dbf
7 50 BLEACH *** /u01/oradata/yang/bleach01.dbf
8 10 BLEACH *** /u01/oradata/yang/bleach02.dbf
9 2048 RMAN_TS1 *** /u01/oradata/yang/rman_ts01.dbf
10 600 SYSTEM *** /u01/oradata/yang/system02.dbf
11 600 SYSAUX *** /u01/oradata/yang/sysaux02.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1 29 TEMP 32767 /u01/oradata/yang/temp01.dbf
RMAN> backup tablespace bleach,system format '/u01/oracle/backup/ts_%U';
5: 备份控制文件和spfile的四种方法:
使用configure命令开启rman自动备份控制文件和spfile
RMAN> backup current controlfile; //单独备份当前的控制文件和spfile
RMAN> backup tablespace bleach,system format '/u01/oracle/backup/ts_%U' include current controlfile;
RMAN> sql "alter database backup controlfile to ''/tmp/contrl.ctl''";
6: 备份arichivelog,只备份归档完毕的redo日志文件,而且是全备份,"delete all input"表示备份完毕后,删除归档日志文件
RMAN> backup archivelog all format '/u01/oracle/backup/archived/ac_%U' delete all input;
也可以在backup语句后面加上“plus archivelog"来备份归档完毕的redo日志文件
RMAN> backup database plus archivelog format '/u01/oracle/backup/full_%U' tag 'full backup';
RMAN> list backup tag "full backup";
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
24 45.63M DISK 00:00:01 06-OCT-10
BP Key: 34 Status: AVAILABLE Compressed: NO Tag: FULL BACKUP
Piece Name: /u01/oracle/backup/full_0qlpooko_1_1
List of Archived Logs in backup set 24
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 83 2385551 04-OCT-10 2397425 06-OCT-10
1 84 2397425 06-OCT-10 2397648 06-OCT-10
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
26 47.00K DISK 00:00:01 06-OCT-10
BP Key: 36 Status: AVAILABLE Compressed: NO Tag: FULL BACKUP
Piece Name: /u01/oracle/backup/full_0slpoomd_1_1
List of Archived Logs in backup set 26
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 85 2397648 06-OCT-10 2397701 06-OCT-10
7: 增量备份,增量备份的等级分为0-4,5个等级,其中0代表的是全备份,0级备份是其他级别备份的基石:
RMAN> run {
2> allocate channel d1 type disk;
3> backup incremental level 0 database format '/u01/oracle/backup/level0_%U';
4> }
RMAN> run {
2> allocate channel d2 type disk;
3> backup incremental level 1 database format '/u01/oracle/backup/level1_%U';
4> }
[oracle@jsb-ylw-5024 ~]$ ll -h /u01/oracle/backup/
总计 1.2G
drwxr-xr-x 2 oracle oinstall 4.0K 10-06 09:30 archived
-rw-r----- 1 oracle oinstall 1.2G 10-06 09:34 level0_0ulpop2v_1_1
-rw-r----- 1 oracle oinstall 2.5M 10-06 09:37 level1_10lpop75_1_1
8:镜像拷贝,可以看出镜像拷贝和操作系统层面上的复制命令是一样的:
RMAN> copy datafile 7 to '/u01/oracle/backup/b01.dbf';
RMAN> backup as copy datafile 8 format '/u01/oracle/backup/b02.dbf';
[oracle@jsb-ylw-5024 ~]$ ll -h /u01/oradata/yang/bleach0*
-rw-r----- 1 oracle oinstall 51M 10-06 09:47 /u01/oradata/yang/bleach01.dbf
-rw-r----- 1 oracle oinstall 11M 10-06 09:48 /u01/oradata/yang/bleach02.dbf
[oracle@jsb-ylw-5024 ~]$ ll -h /u01/oracle/backup/b*
-rw-r----- 1 oracle oinstall 51M 10-06 09:47 /u01/oracle/backup/b01.dbf
-rw-r----- 1 oracle oinstall 11M 10-06 09:48 /u01/oracle/backup/b02.dbf