BCT(Block Change Tracking)块变化跟踪、增量和差异备份
1. BCT
1.1原理
BCT(Block Change Tracking)是Oracle从10g开始有的特性。BCT的原理是记录数据文件里每个数据块的变化,并把这些变化信息保存在BCT的跟踪文件中。启动BCT后,RMAN或者XTTS进行增量备份时使用BCT跟踪文件里的信息,只读取改化的数据块,而不用再对全部数据文件进行扫描,从而提高了RMAN或者XTTS增量备份的性能。
未启动快速增量备份,RMAN备份必须检查数据文件中所有块的变化(SCN),即扫描所有的数据库,导致备份慢,
启动快速增量备份,块的改变被记录在一个文件中,RMAN只需读取文件中记录的块,加快备份速度。
隐含参数:_bct_bitmaps_per_file
在启用了块更改跟踪的数据库上,如果执行了8个以上的RMAN增量备份而没有将它们合并为完整备份,BCT跟踪文件可能无法使用。这个限制由隐含参数 “_bct_bitmaps_per_file"参数决定。这个参数设置每个数据文件要存储的位图数,其默认值为8。为了避免此问题,可以运行不超过8个增量备份就进行一次全量备份,也可以增加”_bct_bitmaps_per_file"参数。
监视快跟踪
v$backup_datafile的used_change_tracking的列查看是否使用块跟踪
select * from v$backup_datafile order by file#;
select file#,avg(datafile_blocks),avg(blocks_read),avg(blocks_read/datafile_blocks)*100 as pct_read_for_backup, avg(blocks)
from v$backup_datafile
where used_change_tracking = 'YES'
and incremental_level > 0
group by file#;
1.2配置方法
可以通过v$block_change_tracking;查看是否开启
开启
方法一:
配置DB_CREATE_FILE_DEST,默认放在mof或中
SQL> alter system set db_create_file_dest='/opt/soft/rman';
SQL> alter database enable block change tracking;
方法二:
SQL> alter database enable block change tracking using file '/home/oracle/blk.trc';
关闭
enable改为disable
禁用后,bct file会随着禁用而自动被删除清理。
ALTER DATABASE DISABLE BLOCK CHANGE TRACKING;
2. 增量和差异备份
BCT的实施要在全量备份之前,否则增量备份时不会起作用,很多人误以为在增量备份之前即可。
0级全备
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 0 database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
执行0级备份
#全备前开启BTC,否则没效果,还是会扫描全部数据文件
SQL> alter database enable block change tracking using file '/home/oracle/blk.trc';
数据库已更改。
[oracle@db1 ~]$ rman target /
RMAN>
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 0 database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
12> }
#rman控制文件自动备份没开就要加上
#backup current controlfile; #rman控制文件自动备份没开就要加上
#report obsolete;针对的是全备,并非0级,意义不大
查看备份
RMAN> list backup of database;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
87 Incr 0 103.15M DISK 00:00:19 11-MAY-22
BP Key: 87 Status: AVAILABLE Compressed: YES Tag: TAG20220511T101325
Piece Name: /tmp/test/full_0_ORCL_113
List of Datafiles in backup set 87
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
---- -- ---- ---------- --------- ----------- ------ ----
3 0 Incr 3053406 11-MAY-22 NO /u01/app/oracle/oradata/ORCL/sysaux01.dbf
7 0 Incr 3053406 11-MAY-22 NO /u01/app/oracle/oradata/ORCL/users01.dbf
。。。。。。。。
例如:LV为0,类型为增量, file3,7两个备份集,备份片/tmp/test/full_0_ORCL_26
#查看归档
RMAN> list backup of archivelog all;
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
95 13.50K DISK 00:00:00 11-MAY-22
BP Key: 95 Status: AVAILABLE Compressed: YES Tag: TAG20220511T101441
Piece Name: /tmp/test/full_0_ORCL_120
List of Archived Logs in backup set 95
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 7 3053385 11-MAY-22 3053452 11-MAY-22
...........
#注意查看控制文件,如果开启了默认自动备份控制文件,这里将有两个备份,
#一个普通全备一个增量0级,注意大小
RMAN> list backup of controlfile;
备份集列表
===================
BS 关键字 类型 LV 大小 设备类型 经过时间 完成时间
------- ---- -- ---------- ----------- ------------ ----------
27 Incr 0 1.05M DISK 00:00:01 10-5月 -22
BP 关键字: 27 状态: AVAILABLE 已压缩: YES 标记: TAG20220510T213057
段名:/tmp/test/full_0_ORCL_31
包括的控制文件: Ckp SCN: 3250543 Ckp 时间: 10-5月 -22
BS 关键字 类型 LV 大小 设备类型 经过时间 完成时间
------- ---- -- ---------- ----------- ------------ ----------
30 Full 17.95M DISK 00:00:00 10-5月 -22
BP 关键字: 30 状态: AVAILABLE 已压缩: NO 标记: TAG20220510T213136
段名:/u01/app/oracle/product/19.3/dbhome_1/dbs/c-1631360725-20220510-00
包括的控制文件: Ckp SCN: 3250717 Ckp 时间: 10-5月 -22
#如果关闭,则一个正常的全备,说明自动备份控制文件后增量
RMAN> list backup of controlfile;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
96 Full 18.23M DISK 00:00:03 11-MAY-22
BP Key: 96 Status: AVAILABLE Compressed: NO Tag: TAG20220511T101443
Piece Name: /tmp/test/full_0_ORCL_121
Control File Included: Ckp SCN: 3053472 Ckp time: 11-MAY-22
差异增量
与上一次0级全备或1级差异的差异,默认方式
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 1 database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
累积增量
累积增量是指备份自上次0级备份以来所有发生变化的数据块,忽略之前的1级备份。
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 1 cumulative database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
3. 定时脚本
脚本
0级全备脚本rmanbk_0.sh
#!/bin/bash
#incremental level 1 database backup
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/19.3/dbhome_1
#export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
rman target / <<EOF
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 0 database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
exit;
EOF
1级差异增量脚本rmanbk_1_diff.sh
#!/bin/bash
#incremental level 1 database backup
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/19.3/dbhome_1
#export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
rman target / <<EOF
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 1 database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
exit;
EOF
1级累计增量脚本rmanbk_1_cumu.sh
#!/bin/bash
#incremental level 1 database backup
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/19.3/dbhome_1
#export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib
rman target / <<EOF
run {
allocate channel ch1 device type disk format '/tmp/test/full_0_%d_%s';
allocate channel ch2 device type disk format '/tmp/test/full_0_%d_%s';
sql'alter system switch logfile';
backup as compressed backupset incremental level 1 cumulative database plus archivelog delete input;
backup current controlfile;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
release channel ch1;
release channel ch2;
}
exit;
EOF
计划任务
例如这样的备份策略:
周日全备,周一至周三差异,周四累积,周五周六差异
[oracle@db2 ~]$ crontab -l
30 22 * * 0 /bin/bash /home/oracle/rmanbk_0.sh >> /home/oracle/rmanbk_0.log 2>&1
30 22 * * 1,2,3,5,6 /bin/bash /home/oracle/rmanbk_1_diff.sh >> /home/oracle/rmanbk_1_diff.log 2>&1
30 22 * * 4 /bin/bash /home/oracle/rmanbk_1_cumu.sh >> /home/oracle/rmanbk_1_cumu.log 2>&1
备份脚本模板
方便复制修改
#零级备份脚本
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level 0 database plus archivelog delete input ;
backup current controlfile; #rman控制文件自动备份没开就要加上
crosscheck backup;
report obsolete;#针对的是全备,并非0级,意义不大
delete noprompt obsolete;
delete noprompt expired backup;
}
#一级备份脚本
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup incremental level 1 database plus archivelog delete input ;
backup current controlfile;
tag='inc_1';
release channel c1;
release channel c2;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
2021-05-10 nginx配置支持Thinkphp3.2