CentOS6.4-RMAN定时任务备份 on 11GR2

时间:2023-03-08 15:51:38
CentOS6.4-RMAN定时任务备份 on 11GR2

1、rman备份脚本位置:

/home/oracle

./scripts/

./bin                    -----存放rman脚本

./log                    -----存放定时任务执行日志

2、/bin脚本规划:

脚本规划
      rmancfg.sh      rman参数配置脚本       执行一次即可   
      inc0.sh      rman零级备份      备份路径参数
      inc1.sh      rman一级备份      备份路径参数
      inc0cron.sh     零级备份定时脚本      oracle环境变量
      inc1cron.sh     一级备份定时脚本      oracle环境变量
      del.sh     删除过期备份数据  
      delcron.sh     过期备份校验删除定时脚本      oracle环境变量

3、rman脚本内容

将用到的环境变量添加到oracle用户的.bash_profile,

在后面cron脚本中source  ~/.bash_profile即可。

vi  ./bin/profile.env

export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
export rman_bakpath=/rman/bak
export dt=`date +"%y%m%d%H%M"`
export days=15
export catlu=rmanbak
export catlp=rmanbak

======================================================================================================

./inc0.sh (使用catalog库存放备份信息)

rman target / catalog $catlu/$catlp << EOF
run{
backup as compressed backupset incremental level 0 database format '$rman_bakpath/%d_level0_%s_%Y%M%D.bak';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '$rman_bakpath/%d_arch_%s_%Y%M%D_%U.bak' delete all input;
backup as compressed backupset current controlfile format '$rman_bakpath/%d_control_%s_%Y%M%D_%U.bak';
}
EOF

======================================================================================================

./inc1.sh  (使用catalog库存放备份信息)

rman target / catalog $catlu/$catlp << EOF
run{
backup as compressed backupset incremental level 1 database format '$rman_bakpath/%d_level1_%s_%Y%M%D.bak';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '$rman_bakpath/%d_arch_%s_%Y%M%D_%U.bak' delete all input;
backup as compressed backupset current controlfile format '$rman_bakpath/%d_control_%s_%Y%M%D_%U.bak';
}
EOF

======================================================================================================

./del.sh

rman target / catalog $catlu/$catlp << EOF
run{
crosscheck archivelog all;
crosscheck backup;
delete noprompt expired backup;
delete noprompt expired archivelog all;
delete noprompt obsolete;
delete noprompt archivelog until time 'sysdate-$days';
delete noprompt  backup until time 'sysdate-$days';
}
EOF

=====================================================================================================

4、crontab定时任务脚本内容

./inc0cron.sh

source  ~/.bash_profile
cd /home/oracle/scripts/bin/ && ./inc0.sh > ../log/inc0_$dt.log

----------------------------------------------------------------------------------------------------------------------------------------

./inc1cron.sh

source  ~/.bash_profile
cd /home/oracle/scripts/bin/ && ./inc1.sh > ../log/inc1_$dt.log

----------------------------------------------------------------------------------------------------------------------------------------

./delcron.sh

source  ~/.bash_profile
cd /home/oracle/scripts/bin/ && ./del.sh > ../log/del_$dt.log

----------------------------------------------------------------------------------------------------------------------------------------

./rmancfg.sh            这个脚本手工执行一次即可。无需source  ~/.bash_profile

cat ~/scripts/bin/profile.env >> ~/.bash_profile

cores=(`lscpu | grep "^CPU(s)"|awk {'print $2'}`)

if [ $cores -gt  16 ]; then
cores=16
fi
rman target / << EOF
run{
CONFIGURE DEVICE TYPE DISK PARALLELISM $cores BACKUP TYPE TO BACKUPSET;
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF $days DAYS;
CONFIGURE BACKUP OPTIMIZATION ON;
}
EOF

----------------------------------------------------------------------------------------------------------------------------------------

5、su - oracle 添加定时任务

$crontab -e

30 23 */14 * *   /home/oracle/scripts/bin/inc0cron.sh
30 23 * * 5   /home/oracle/scripts/bin/inc1cron.sh
0 0 */15 * *  /home/oracle/scripts/bin/delcron.sh

----------------------------------------------------------------------------------------------------------------------------------------

oracle用户的定时任务:

cron定时任务程序只是用oracle用户来执行脚本,而并未登录到oracle用户,
因此,需要在脚本中source好oracle用户环境变量。否则定时任务无法执行。

./*.sh是fork的方法,source *.sh和. *sh(点加空格加脚本文件)是source的方法。
执行子级命令后继续执行父级命令,同时子级设置的环境变量会影响到父级的环境变量。
source与fork的区别是不新开一个sub-shell来执行被调用的脚本,而是在同一个shell中执行.
所以被调用的脚本中声明的变量和环境变量, 都可以在主脚本中得到和使用.

调试执行计划任务过程中,可使用tail -f  /var/spool/mail/oracle监控执行计划任务报错信息。

注意:~/scripts/bin/profile.env需要根据实际环境进行配置。

转:http://m.blog.csdn.net/article/details?id=52537568