一.RMAN备份相关概念
1.RMAN备份中表空间不需要处于backup模式下,它备份数据文件,归档日志文件,控制文件,spfile和备份集片,但不备份联机重做日志文件,临时文件和口令文件。
2.备份集由备份片组成,可以包含一个或者多个备份片。备份片可以在操作系统中直接看到,其大小和操作系统有关,32位的操作系统最大文件是4G。
3.数据文件备份集支持完整和增量备份,即只备份使用过的块,而归档日志文件备份集不支持这个。
4.在RMAN下的常用查看命令,show all查看备份的默认参数,report schema查看数据表空间和数据文件的信息,list back查看已经备份文件的信息。
在show all中有个默认控制文件备份问题,控制文件备份默认是不备份的,
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
如果备份系统表空间的文件,那么无论是off还是on状态,控制文件自动备份。
5.常用参数,maxpiecesize规定备份片的大小,maxsetsize备份集的大小,filesperset表示一个备份集中所含有的输入文件个数,%U=%u_%p_%c生成唯一的名称。
6.在RMAN备份中,可以把备份文件分成两块,一块是数据文件备份集,另一块是归档日志文件,控制文件和spfile在一起的压缩打包文件。
二.RMAN备份的方式
1.非归档模式下,必须进行一致性的备份,执行RMAN一致性备份要求数据库处于加载模式下,并且干净的关闭,备份可以是完整的或者增量的。
2.在归档模式下,可以一致性备份也可以非一致性备份,在非一致性备份中必须备份归档日志文件,备份可以是全部的,局部的,完整的或者增量的。
备份可以分为全部的,局部的,完整的或者增量的备份,只有RMAN可以实现增量备份。
三.RMAN备份各种文件
(一)备份数据文件
Report schema可以看到各种表空间和数据文件等的信息。
RMAN> report schema;
Report of database schema for database with db_unique_name WILSON
List of Permanent Datafiles
===========================
File Size(MB) Tablespace RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1 760 SYSTEM *** /u01/oradata/wilson/system01.dbf
2 580 SYSAUX *** /u01/oradata/wilson/sysaux01.dbf
3 135 UNDOTBS1 *** /u01/oradata/wilson/undotbs01.dbf
4 28 USERS *** /u01/oradata/wilson/users01.dbf
5 100 EXAMPLE *** /u01/oradata/wilson/example01.dbf
6 20 PAUL *** /u01/oradata/wilson/paul01.dbf
7 20 SUN *** /u01/oradata/wilson/sun01.dbf
8 2 SMALLUNDO *** /u01/oradata/wilson/smallundo1.dbf
9 100 ASSM *** /u01/oradata/wilson/assm_1.dbf
10 100 MSSM *** /u01/oradata/wilson/mssm_1dbf
11 10 PAUL *** /u01/oradata/wilson/paul02.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1 29 TEMP 32767 /u01/oradata/wilson/temp01.dbf
2 100 MYTEMP 100 /u01/oradata/wilson/mytemp01.dbf
或者在sql*plus下也可以看到
SQL> select file#,name from v$datafile;
FILE# NAME
---------- -----------------------------------
1 /u01/oradata/wilson/system01.dbf
2 /u01/oradata/wilson/sysaux01.dbf
3 /u01/oradata/wilson/undotbs01.dbf
4 /u01/oradata/wilson/users01.dbf
5 /u01/oradata/wilson/example01.dbf
6 /u01/oradata/wilson/paul01.dbf
7 /u01/oradata/wilson/sun01.dbf
8 /u01/oradata/wilson/smallundo1.dbf
9 /u01/oradata/wilson/assm_1.dbf
10 /u01/oradata/wilson/mssm_1dbf
11 /u01/oradata/wilson/paul02.dbf
11 rows selected.
使用下面的命令都可以备份成功,
RMAN> backup datafile 4 format='/u01/backup/md_%U';
或者
RMAN> backup datafile '/u01/oradata/wilson/users01.dbf' format='/u01/backup/md_%U';
Starting backup at 23-AUG-13
using channel ORA_DISK_1
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/oradata/wilson/users01.dbf
channel ORA_DISK_1: starting piece 1 at 23-AUG-13
channel ORA_DISK_1: finished piece 1 at 23-AUG-13
piece handle=/u01/backup/md_1hoi1t5t_1_1 tag=TAG20130823T202948 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 23-AUG-13
(二)备份表空间
和备份数据文件一样,report schema来查看表空间的信息。只有命令上有点区别,如下
RMAN> backup tablespace sun format='/u01/backup/ts_%U';
Starting backup at 23-AUG-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/u01/oradata/wilson/sun01.dbf
channel ORA_DISK_1: starting piece 1 at 23-AUG-13
channel ORA_DISK_1: finished piece 1 at 23-AUG-13
piece handle=/u01/backup/ts_1ioi1tdv_1_1 tag=TAG20130823T203407 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 23-AUG-13
(三)备份控制文件
1.开启自动备份控制文件
RMAN> show all;
RMAN configuration parameters for database with db_unique_name WILSON are:
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/backup/ctl_%F';
。。。。。。
可以看到默认情况下是off的,修改为on,
RMAN> configure controlfile autobackup on;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored
再查看确认一下,
RMAN> show all;
RMAN configuration parameters for database with db_unique_name WILSON are:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/backup/ctl_%F';
对于CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/backup/ctl_%F';命令可以控制文件备份到所需要的地址上。
这个自动备份控制文件,会把spfile也一起备份的。
2.直接备份,但是其不会备份spfile,命令如下,
RMAN> backup current controlfile;
Starting backup at 23-AUG-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
channel ORA_DISK_1: starting piece 1 at 23-AUG-13
channel ORA_DISK_1: finished piece 1 at 23-AUG-13
piece handle=/tmp/back/1joi1unc_1_1 tag=TAG20130823T205612 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:04
Finished backup at 23-AUG-13
3.可以在备份数据文件或者表空间时来添加备份,但是其不会备份spfile,命令如下
RMAN> backup datafile 4 include current controlfile;
或者
RMAN> backup tablespace sun include current controlfile;
(四)备份spfile
在开启自动备份控制文件时,会自动备份spfile。
也可以用如下命令备份,
RMAN> backup spfile format '/u01/backup/sp_%U';
Starting backup at 23-AUG-13
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=44 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 23-AUG-13
channel ORA_DISK_1: finished piece 1 at 23-AUG-13
piece handle=/u01/backup/sp_1qoi23po_1_1 tag=TAG20130823T222247 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 23-AUG-13
Starting Control File and SPFILE Autobackup at 23-AUG-13
piece handle=/u01/backup/ctl_c-3925834152-20130823-0a comment=NONE
Finished Control File and SPFILE Autobackup at 23-AUG-13
可以看到虽然命令只是备份spfile,但是控制文件也被备份了。
(五)备份归档日志文件
备份归档日志文件是把全部内容复制下来,命令如下,
RMAN> backup archivelog all format='/u01/backup/ac_%U' delete all input;
delete all input是把原来的归档日志文件删除掉。
在执行这个命令时,会检查归档日志文件是否损坏,如果发现一个地方的归档日志文件损坏,那么自动会去读取另一个地方好的归档日志文件,若都有损坏,就拒绝备份。
为了确保归档日志的一致性,在对归档日志文件做备份时,做了如下的操作,
1.在备份命令开始后,首先是运行alter system archive log current命令,把联机重做日志文件复制到归档日志文件中。
2.对归档日志文件进行备份或者是数据文件和归档日志文件一起备份。
3.再一次运alter system archive log current命令(由于上一步会的时间可能会很长,这个时间段又会产生重做信息,所以要把联机重做日志文件再复制到归档日志文件中)。
4.再做一次归档日志文件的备份
或者用下面的命令,
backup archivelog sequence between 139 and 141 thread 1 delete (all) input;
可以选择sequence的备份范围;
或者
backup archivelog from time "sysdate-15" until time "sysdate-7";
可以选择时间的范围来备份归档日志文件;
或者在备份数据文件时把归档日志文件一起备份,
backup database plus archivelog format=’/u01/backup/db_%U’。