RMAN不但能进行数据库、数据文件等全量备份,而且还能做增量备份。增量备份有两大优点:
减少数据库恢复的时间。
数据库恢复是利用归档日志,不断的应用归档日志,将数据库恢复到最新(或者指定状态)。但是增量备份只备份被修改了的数据块,在执行恢复时,直接用备份出来的数据块去覆盖数据文件中被更改的数据块。
减少备份时间
增量备份只需备份被修改过的数据块,备份时间和备份存储空间都有相应的减少。
做增量备份的前提,是需要一个基准备份,所有的增量都是针对这个基准来说的。要不然,增量备份无从谈起。
增量备份使用INCREMENTAL LEVEL=integer 关键字指定。integer可以是0到4之间的任意整数。
必须将0 级备份作为增量策略的基础备份。0 级增量备份在内容上与完全备份相同,但与完全备份不同的是,0 级备份被看作是增量策略的一部分。如果在运行1 级或更高级别的备份时,0 级备份不存在,RMAN 会自动制作0 级备份。
增量备份的方式分两种:
差异增量备份
n 级备份,备份自最近n 级或更低级别备份以来更改过的所有块。
累计增量备份
n 级备份,它包含自上次n-1 级或更低级别备份以来更改过的所有块。
累计增量备份使用cumulative关键字,如:
BACKUP INCREMENTAL level 2 cumulative DATABASE;
下面我们演示一下增量备份
RMAN> backup incremental level =0 database; Starting backup at 2015/07/08 15:16:44 using channel ORA_DISK_1 channel ORA_DISK_1: starting incremental level 0 datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/devdb/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/devdb/sysaux01.dbf input datafile file number=00005 name=/u01/app/oracle/oradata/devdb/example01.dbf input datafile file number=00003 name=/u01/app/oracle/oradata/devdb/undotbs01.dbf input datafile file number=00006 name=/u01/app/oracle/oradata/devdb/idx01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/devdb/users01.dbf channel ORA_DISK_1: starting piece 1 at 2015/07/08 15:16:44 channel ORA_DISK_1: finished piece 1 at 2015/07/08 15:17:59 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/backupset/2015_07_08/o1_mf_nnnd0_TAG20150708T151644_bssmlwog_.bkp tag=TAG20150708T151644 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:01:15 Finished backup at 2015/07/08 15:17:59 Starting Control File and SPFILE Autobackup at 2015/07/08 15:17:59 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/autobackup/2015_07_08/o1_mf_s_884531879_bssmo893_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 2015/07/08 15:18:00
下面我们对库做一些更改
SQL> insert into emp_new select * from emp_new; 14 rows created. SQL> / 28 rows created. SQL> / 56 rows created. SQL> / 112 rows created. SQL> / 224 rows created. SQL> / 448 rows created.
再执行一个全库的1级备份
RMAN> backup incremental level=1 database; Starting backup at 2015/07/08 15:24:38 using channel ORA_DISK_1 channel ORA_DISK_1: starting incremental level 1 datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/devdb/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/devdb/sysaux01.dbf input datafile file number=00005 name=/u01/app/oracle/oradata/devdb/example01.dbf input datafile file number=00003 name=/u01/app/oracle/oradata/devdb/undotbs01.dbf input datafile file number=00006 name=/u01/app/oracle/oradata/devdb/idx01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/devdb/users01.dbf channel ORA_DISK_1: starting piece 1 at 2015/07/08 15:24:39 channel ORA_DISK_1: finished piece 1 at 2015/07/08 15:25:34 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/backupset/2015_07_08/o1_mf_nnnd1_TAG20150708T152439_bssn1qjd_.bkp tag=TAG20150708T152439 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:55 Finished backup at 2015/07/08 15:25:34 Starting Control File and SPFILE Autobackup at 2015/07/08 15:25:34 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/autobackup/2015_07_08/o1_mf_s_884532334_bssn3h9x_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 2015/07/08 15:25:35
我们查看一下增量备份集文件的大小
11gdg-> ll -h total 1.1G -rw-r----- 1 oracle asmadmin 1.1G Jul 8 15:17 o1_mf_nnnd0_TAG20150708T151644_bssmlwog_.bkp -rw-r----- 1 oracle asmadmin 488K Jul 8 15:25 o1_mf_nnnd1_TAG20150708T152439_bssn1qjd_.bkp
讲完了增量备份的概念,你可能会有一个疑问。RMAN是怎么知道自上次备份后,哪些块被修改了呢?数据文件中的每个数据块中都记录了SCN号,在增量备份期间,RMAN读取数据文件中的每个块的SCN,和父级增量备份集的CHECKPOINT SCN比较。如果块的SCN大于CHECKPOINT SCN,那么此块即可认定被修改过。
这样实现增量备份有什么缺点呢?虽说是增量备份、可是备份时还是需要扫描数据库的所有块。也就是说增量备份所花费的时间并不比全量备份少。只是生成的备份集比较小而已。
在10g中rman做增量备份不再需要扫描整个数据文件了,10g引入的新特性 block change tracking。Block change tracking进程记录自从上一次备份以来数据块的变化,并把这些信息记录在跟踪文件中。RMAN使用这个文件判断增量备份中需要备份的变更数据。这极大的促进了备份性能,RMAN可以不再扫描整个文件以查找变更数据。
block change tracking实现了如下功能:
维护了一个record,记录了自上次备份以来的被修改的数据块。
当redo生成的时候,将这个record写到文件中。
当执行增量备份时,会自动访问这个文件,以加快备份速度。
开启block change tracking
SQL> alter database enable block change tracking using file '/u01/app/oracle/fast_recovery_area/tracking.log'; Database altered.
我们修改一下数据。
SQL> insert into emp_new select * from emp_new; 896 rows created. SQL> commit;
查看一下tracking文件
[root@11gdg fast_recovery_area]# ll -h tracking.log -rw-r----- 1 oracle asmadmin 12M Jul 8 17:24 tracking.log
按照道理来说,emp_new中896条数据,不应该有那么大的tracking文件。我们再往表里添加一些数据,看看tracking文件是否有变化。
SQL> insert into emp_new select * from emp_new; 1792 rows created. SQL> / 3584 rows created. SQL> / 7168 rows created. SQL> / 14336 rows created. SQL> / 28672 rows created. SQL> / 57344 rows created. SQL> commit; Commit complete.
再次查看tracking.log文件大小。
[root@11gdg fast_recovery_area]# ll -h tracking.log -rw-r----- 1 oracle asmadmin 12M Jul 8 17:27 tracking.log
大小没有变化。
通过视图v$block_change_tracking,可以查看tracking文件的详细情况
SQL> col filename for a50 SQL> set linesize 200 pagesize 800 SQL> select * from v$block_change_tracking; STATUS FILENAME BYTES ---------- -------------------------------------------------- ---------- ENABLED /u01/app/oracle/fast_recovery_area/tracking.log 11599872 SQL>
我们再做一次level 1的备份看看
RMAN> backup incremental level=1 database; Starting backup at 2015/07/08 17:39:38 using channel ORA_DISK_1 channel ORA_DISK_1: starting incremental level 1 datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/devdb/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/devdb/sysaux01.dbf input datafile file number=00005 name=/u01/app/oracle/oradata/devdb/example01.dbf input datafile file number=00003 name=/u01/app/oracle/oradata/devdb/undotbs01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/devdb/users01.dbf input datafile file number=00006 name=/u01/app/oracle/oradata/devdb/idx01.dbf channel ORA_DISK_1: starting piece 1 at 2015/07/08 17:39:38 channel ORA_DISK_1: finished piece 1 at 2015/07/08 17:41:33 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/backupset/2015_07_08/o1_mf_nnnd1_TAG20150708T173938_bssvytyy_.bkp tag=TAG20150708T173938 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:01:55 Finished backup at 2015/07/08 17:41:33 Starting Control File and SPFILE Autobackup at 2015/07/08 17:41:34 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/autobackup/2015_07_08/o1_mf_s_884540494_bssw2gsv_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 2015/07/08 17:41:35
这个备份,并没有用到block change tracking 。因为上一次level 1 的备份是我们启动tracking功能之前做的。Oracle无法确定备份到启动tracking之间是否有块被修改了。
我们做些数据修改
SQL> conn scott/tiger Connected. SQL> insert into emp_new select * from emp_new; 114688 rows created. SQL> commit; Commit complete.
我们再做一次备份
RMAN> backup incremental level=1 database; Starting backup at 2015/07/08 17:45:08 using channel ORA_DISK_1 channel ORA_DISK_1: starting incremental level 1 datafile backup set channel ORA_DISK_1: specifying datafile(s) in backup set input datafile file number=00001 name=/u01/app/oracle/oradata/devdb/system01.dbf input datafile file number=00002 name=/u01/app/oracle/oradata/devdb/sysaux01.dbf input datafile file number=00005 name=/u01/app/oracle/oradata/devdb/example01.dbf input datafile file number=00003 name=/u01/app/oracle/oradata/devdb/undotbs01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/devdb/users01.dbf input datafile file number=00006 name=/u01/app/oracle/oradata/devdb/idx01.dbf channel ORA_DISK_1: starting piece 1 at 2015/07/08 17:45:09 channel ORA_DISK_1: finished piece 1 at 2015/07/08 17:45:16 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/backupset/2015_07_08/o1_mf_nnnd1_TAG20150708T174508_bssw99qw_.bkp tag=TAG20150708T174508 comment=NONE channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07 Finished backup at 2015/07/08 17:45:16 Starting Control File and SPFILE Autobackup at 2015/07/08 17:45:16 piece handle=/u01/app/oracle/fast_recovery_area/DEVDB/autobackup/2015_07_08/o1_mf_s_884540716_bssw9dhq_.bkp comment=NONE Finished Control File and SPFILE Autobackup at 2015/07/08 17:45:17
此次备份全库耗时仅有7秒,先前大概都在1分钟左右。可见使用block change tracking技术可以大大的加快增量备份的速度。