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 / <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 / <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.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 / <run{
delete noprompt archivelog all completed before 'sysdate-30';
}
EOF
done

4.4rman删除备份后的空文件夹命令:

vi /home/oracle/del_db_bak_empty_folder.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 `du -h --max-depth=1 $Rdir |grep 4.0K |cut -d "/" -f 6`;
do

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

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


备注:du -h --max-depth=1 $Rdir |grep 4.0K |cut -d "/" -f 6  $Rdir的路径长度不同,-f 后面的数字要做相应的更改

4.5备份删除脚本:
如果不使用rman策略删除法可以使用如下脚本删除,弊端不能保证一定最老的备份是全备,只能判断保留距当前最近的15天的备份文件夹,建议使用rman保留备份策略来执行删除备份
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_empty_folder.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.sh >> del_arclog.log 2>&1

./del_db_bak_empty_folder.sh >> del_empty_folder.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.sh >> /home/oracle/rmanbackup/del.log 2>&1
10 3 * * 1  /home/oracle/del_db_bak_empty_folder.sh >> /home/oracle/rmanbackup/del.log 2>&1

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

每周1 4 6 的0:10分执行全备脚本
每周2 3 5 日的0:10执行归档日志备份脚本
每天2:10执行删除数据库归档日志脚本
每周一的3:10执行删除备份后剩下的空文件的脚本按下shift+: 输入wq 退出 crontab
备注:
crontab -l 查看已经建立的定时任务

crontab里表示时间的五列数字取值范围和含义如下:

分钟 (0-59)
小时 (0-23)
日期 (1-31)
月份 (1-12)
星期 (0-6)    // 0 代表星期天