RAC RMAN定期备份及删除脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RAC RMAN定期备份及删除脚本相关的知识,希望对你有一定的参考价值。

oracle rman备份及删除脚本、
1.环境说明:
os--linux
oracle--11.2.03
一台服务器上两个库orcl prod
RAC双节点环境,采用节点1执行定制备份及删除脚本

2.备份策略说明:
1)周1 周4 周6 执行全库备份,加上备份产生的归档,周2 周3 周日执行归档日志备份
备注:因为数据库为10g以下的小库,采用全库备份;如果是大库而且变化较小的库,采用增量备份;
2)备份保存15天,15天以前的备份删除
3)oracle数据库的归档保存30天

举例以一台服务器上多个库(orcl 和prod两个库的情况下的脚本,如果一个库可以适当的去掉循环)

3.数据库配置
3.1更改控制文件记录的备份时长
 alter system set control_file_record_keep_time=31;
 
 备份及删除脚本上有配置configure retention policy to recovery window of 15 days;
 备注:
 CONTROL_FILE_RECORD_KEEP_TIME参数默认保留7天。
 建议CONTROL_FILE_RECORD_KEEP_TIME参数值设置不小于选中数据库的备份保存周期的值。
 理论上CONTROL_FILE_RECORD_KEEP_TIME的天数必须大于等于CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF xx DAYS

4.备份及删除脚本如下:
4.1全库备份脚本:
使用oracle用户创建如下脚本
vi /home/oracle/full_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
current_day=`date +%Y%m%d`
basdir=/home/oracle/rmanbackup
for i in {orcl,prod}
do
export ORACLE_SID=${i}"1"
Rdir=$basdir/$i
if [ ! -d $Rdir/$current_day ]
then
    mkdir -p $Rdir/$current_day

rman target / <<EOF
run{
allocate channel d1 type disk maxpiecesize=30G;
allocate channel d2 type disk maxpiecesize=30G;
backup as compressed backupset database format '$Rdir/$current_day/full_%d_%T_%u.bkp';
sql 'alter system archive log current';
sql 'alter system archive log current';
sql 'alter system archive log current';
backup archivelog all not backed up 1 times format '$Rdir/$current_day/arclog_%d_%T_%u.bkp';
backup spfile format '$Rdir/$current_day/spfile_%d_%T_%u.bkp';
backup current controlfile format '$Rdir/$current_day/controlfile_%d_%T_%u.bkp';
crosscheck backup;
delete noprompt expired backup;

configure retention policy to recovery window of 15 days;
delete noprompt obsolete;

release channel d1;
release channel d2;
}
EOF

else
    echo "$i's full db backup  on $current_day has been completed;"  
fi
done

4.2归档日志备份脚本:
vi /home/oracle/arclog_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
current_day=`date +%Y%m%d`
basdir=/home/oracle/rmanbackup
for i in {orcl,prod}
do
export ORACLE_SID=${i}"1"
Rdir=$basdir/$i
if [ ! -d $Rdir/$current_day ]
then
    mkdir -p $Rdir/$current_day
    
rman target / <<EOF
run{
allocate channel d1 type disk maxpiecesize=30G;
allocate channel d2 type disk maxpiecesize=30G;
sql 'alter system archive log current';
backup archivelog all not backed up 1 times format '$Rdir/$current_day/arclog_%d_%T_%u.bkp';
backup spfile format '$Rdir/$current_day/spfile_%d_%T_%u.bkp';
backup current controlfile format '$Rdir/$current_day/controlfile_%d_%T_%u.bkp';
crosscheck backup;
delete noprompt expired backup;

configure retention policy to recovery window of 15 days;
delete noprompt obsolete;

release channel d1;
release channel d2;
}
EOF

else
    echo "$i's archivelog backup  on $current_day has been completed;"  
fi
done


说明:
      %d:Oracle数据库名称
      %T:当前时间的年月日格式(YYYYMMDD)
      %u:是一个由备份集编号和建立时间压缩后组成的8字符名称。利用%u可以为每个备份集生成一个唯一的名称
      
      
    

4.3日志删除脚本:

vi /home/oracle/del_arclog_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
basdir=/home/oracle/rmanbackup
current_day=`date +%Y%m%d`
for i in {orcl,prod}
do
export ORACLE_SID=${i}"1"
rman target / <<EOF
run{
delete noprompt archivelog all completed before 'sysdate-30';
}
EOF
done


4.4备份空文删脚本:
vi /home/oracle/del_db_bak.sh
#!/bin/bash
removedir=/home/oracle/rmanbackup
dt=`date +%Y%m%d -d "15 day ago"`
current_day=`date +%Y%m%d`
for i in {orcl,prod}
do
Rdir=$removedir/$i
for subdir in `ls $Rdir`;
do

if [ "${subdir}" -lt "${dt}" ];
then
rm -rf $Rdir/$subdir >/dev/null

echo "on $current_day the directory $Rdir/$subdir has been removed."
fi
done
 
done

5.调试脚本
5.1更改脚本具有可执行权限
su - oracle
chmod u+x full_bak.sh
chmod u+x arclog_bak.sh
chmod u+x del_arclog_bak.sh
chmod u+x del_db_bak.sh

5.2设置定时任务前调试脚本
于脚本当前路径下执行,测试脚本
./full_bak.sh >> full_bak.log 2>&1
./arclog_bak.sh >> arclog_bak.log 2>&1
./del_arclog_bak.sh >> del_arclog.log 2>&1
./del_db_bak.sh >> del_bak.log 2>&1

6.配置定时任务执行脚本
在节点1上执行:
=========================


输入crontab -e

按下a键进入到编辑模式

输入如下
10 0 * * 1,4,6  /home/oracle/full_bak.sh >> /home/oracle/rmanbackup/backup.log 2>&1
10 0 * * 2,3,5,0  /home/oracle/arclog_bak.sh >> /home/oracle/rmanbackup/backup.log 2>&1
10 2 * * *  /home/oracle/del_arclog_bak.sh >> /home/oracle/rmanbackup/del.log 2>&1
10 3 * * *  /home/oracle/del_db_bak.sh >> /home/oracle/rmanbackup/del.log 2>&1

同时按下ctrl+c退出编辑模式

按下shift+: 输入wq 退出 crontab

crontab -l 查看已经建立的定时任务






以上是关于RAC RMAN定期备份及删除脚本的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 11g R2 rac通过rman 恢复到单实例数据库

RAC 之 RMAN 恢复

RAC 之 RMAN 备份

RMAN 的优缺点及RMAN 备份及恢复步骤

大话RAC介质恢复---只有备份文件的恢复

RAC_RMAN冷备份