1. 目录设置
1.1. Rman备份说明
? 备份级别说明
1、Level 0级全备是各增量备份的基础;
2、增量备份分两种情况,incremental和cumulitive,前者备份跟自己同级或比自己级别低的,后者只备份比自己级别低的;
3、incremental备份说明:level1备份上一次level1或level0以来的变化,level2备份上一次level2或level1或level0以来的变化;
4、cumulitive备份说明:level1备份上一次level0以来的变化,level2备份上一次level1或level0以来的变化。
? 相关注意说明
1、在数据量比较小、或者数据库服务器性能很强大的情况下,可以每天进行一次全备份;
2、如果备份后需要把备份文件存放到阿里云的OSS存储上,由于OSS存储有4GB大小限制,需要修改以下三个rman配置,限制RMAN备份片的大小
1、level0_backup.rman文件
allocate channel C2 type disk ; --修改前
allocate channel C2 type disk maxpiecesize = 3500M ; --修改后
2、level1_backup.rman文件
allocate channel C3 type disk ; --修改前
allocate channel C3 type disk maxpiecesize = 3500M ; --修改后
3、level2_backup.rman文件
allocate channel C4 type disk ; --修改前
allocate channel C4 type disk maxpiecesize = 3500M ; --修改后
3、配置crontab定时任务,注意避开业务繁忙时段
4、使用oracle用户配置定时任务,如果需要使用root用户配置定时任务,计划任务脚本章节要注意修改环境变量,以及执行脚本注意oracle用户要对相关路径具备读写执行权限。
1.2. 目录说明
daemon库备份脚本路径:/backup/rman/scripts/
归档文件按日期备份: /backup/arch/date +%Y%m%d
控制文件备份: /backup/rman/controlfile
数据库文件按日期备份: /backup/rman/data/date +%Y%m%d
按日期生成备份日志: /backup/rman/logs/date +%Y%m%d
RMAN备份脚本: /backup/rman/scripts
以上所有目录请做好异地备份。
2. rman备份
2.1. 备份策略
? 保留最后5次备份数据:
? 每天凌晨04:45删除多余和无效的备份;
? 每天12点与20点备份归档日志;
? 每天03点20进行数据库的0级完全备份;
2.2. 备份计划
这里采用的每天全备,大家可根据实际需要,周一到周六增备,周日全备。
时间 周一至周日
12:00 日志备份
20:00 日志备份
03:20 完全备份
将脚本加入oracle用户的crontab里面,让其按策略自动运行,可根据实际需求调整计划任务。
$ crontab -e
20 03 * * * /backup/rman/scripts/level0_backup.sh
45 04 * * * /backup/rman/scripts/delobsolete.sh
00 12 * * * /backup/rman/scripts/arch.sh
00 20 * * * /backup/rman/scripts/arch.sh
2.3. 准备工作
将 oracle 数据库置于“安装模块”以启用日志归档模式,归档的日志存储在快速恢复区域中
mkdir -p /backup/arch
mkdir -p /backup/rman/controlfile
mkdir /backup/rman/data
mkdir /backup/rman/logs
mkdir /backup/rman/scripts
chown -R oracle:oinstall /backup
以下数据库连接前设置连接SID
$ export ORACLE_SID=daemon
$ sqlplus / as sysdba
SQL> SELECT log_mode from v$database;
输出的结果为ARCHIVELOG表示已经开启归档
输出的结果为NOARCHIVELOG表示未开启归档
(1)操作开启归档日志
$ sqlplus / as sysdba
SQL> shutdown immediate;
startup mount;
alter database archivelog;
alter database open;
(2)启用更改跟踪功能记录自上次备份以来更改的数据块位置,以提升增量备份的性能。更改跟踪文件默认保存在快速恢复区域中(使用RMAN 增量备份时启动block change tracking以缩短RMAN 备份的时间)。
$ sqlplus / as sysdba
SQL> shutdown immediate;
startup mount
alter system set db_create_file_dest=‘/u01/oracle/fast_recovery_area‘;
alter database enable block change tracking;
alter database open;
(3)查看、修改闪回区大小(大小根据实际情况修改)
SQL> show parameter db_recovery_file_dest;
alter system set db_recovery_file_dest_size=40G scope=both;
shutdown immediate;
startup;
2.4. rman配置
$ rman target /
CONFIGURE RETENTION POLICY TO REDUNDANCY 5;
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 5 DAYS;
上面两句只能使用一种条件,保存5个副本或者5天的恢复窗口(建议使用恢复窗口),请按需调整
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘/backup/rman/controlfile/%F‘;
CONFIGURE SNAPSHOT CONTROLFILE NAME TO ‘/backup/rman/controlfile/snapcf_daemon.f‘;
2.5. rman脚本
1) delobsolete.rman
cd /backup/rman/scripts
$ vi delobsolete.rman
run {
crosscheck backup;
delete noprompt obsolete;
delete noprompt expired backup;
}
exit
2) arch.rman
$ vi arch.rman
sql ‘alter system archive log current‘ ;
run {
allocate channel C1 type disk FORMAT ‘/backup/arch/‘ ;
backup
skip inaccessible
tag arch_daemon
filesperset 10 format ‘/backup/arch/%T/arch_%T_%d_%t_%s_%p‘ archivelog all delete input;
release channel C1;
}
exit
3) level0_backup.rman
$ vi level0_backup.rman
sql ‘PURGE DBA_RECYCLEBIN‘;
sql ‘alter system archive log current‘ ;
run {
allocate channel C2 type disk ;
backup
incremental level 0
skip inaccessible
tag full0_daemon
DATABASE
filesperset 5 format ‘/backup/rman/data/%T/db_0_%T_%d_%t_%s_%p‘;
release channel C2;
}
exit
4) level1_backup.rman
$ vi level1_backup.rman
sql ‘PURGE RECYCLEBIN‘;
sql ‘alter system archive log current‘ ;
run {
allocate channel C3 type disk ;
backup
incremental level 1
skip inaccessible
tag level1_daemon
DATABASE
filesperset 5 format ‘/backup/rman/data/%T/db_1_%T_%d_%t_%s_%p_inc‘ ;
release channel C3;
}
exit
5) level2_backup.rman
$ vi level2_backup.rman
sql ‘PURGE RECYCLEBIN‘;
sql ‘alter system archive log current‘ ;
run {
allocate channel C4 type disk ;
backup
incremental level 2
skip inaccessible
tag level2_daemon
DATABASE
filesperset 5 format ‘/backup/rman/data/%T/db_2_%T_%d_%t_%s_%p_inc‘ ;
release channel C4;
}
exit
2.6. 计划任务脚本
1) delobsolete.sh
$ vi delobsolete.sh
#!/bin/sh
#export LANG=en_US
source /home/oracle/.bash_profile
export ORACLE_SID=daemon
mkdir /backup/rman/logs/`date +%Y%m%d`
rman target / cmdfile=/backup/rman/scripts/delobsolete.rman msglog=/backup/rman/logs/`date +%Y%m%d`/`date +%Y%m%d_%H%M_obsolete.log`
exit
2) arch.sh
$ vi arch.sh
#!/bin/sh
#export LANG=en_US
source /home/oracle/.bash_profile
export ORACLE_SID=daemon
mkdir /backup/arch/`date +%Y%m%d`
mkdir /backup/rman/logs/`date +%Y%m%d`
rman target / cmdfile=/backup/rman/scripts/arch.rman msglog=/backup/rman/logs/`date +%Y%m%d`/`date +%Y%m%d_%H%M_arch.log`
exit
3) level0_backup.sh
$ vi level0_backup.sh
#!/bin/sh
#export LANG=en_US
source /home/oracle/.bash_profile
export ORACLE_SID=daemon
mkdir /backup/rman/data/`date +%Y%m%d`
mkdir /backup/rman/logs/`date +%Y%m%d`
rman target / cmdfile=/backup/rman/scripts/level0_backup.rman msglog=/backup/rman/logs/`date +%Y%m%d`/`date +%Y%m%d_%H%M_0.log`
exit
4) level1_backup.sh
$ vi level1_backup.sh
#!/bin/sh
#export LANG=en_US
source /home/oracle/.bash_profile
export ORACLE_SID=daemon
mkdir /backup/rman/data/`date +%Y%m%d`
mkdir /backup/rman/logs/`date +%Y%m%d`
rman target / cmdfile=/backup/rman/scripts/level1_backup.rman msglog=/backup/rman/logs/`date +%Y%m%d`/`date +%Y%m%d_%H%M_1.log`
exit
5) level2_backup.sh
$ vi level2_backup.sh
#!/bin/sh
#export LANG=en_US
source /home/oracle/.bash_profile
export ORACLE_SID=daemon
mkdir /backup/rman/data/`date +%Y%m%d`
mkdir /backup/rman/logs/`date +%Y%m%d`
rman target / cmdfile=/backup/rman/scripts/level2_backup.rman msglog=/backup/rman/logs/`date +%Y%m%d`/`date +%Y%m%d_%H%M_2.log`
exit