rman备份脚本
rman shell脚本编辑
先在rman里面测试,看是否正常备份,在进行判断语句测试。
备份脚本
sh命令
rmanl0.sh
rman target / msglog=/oracle/ora10g/rmanbak/bakl0.log cmdfile=/oracle/ora10g/rmanbak/script/rman_bak_l0.sql
rmanl1.sh
rman target / msglog=/oracle/ora10g/rmanbak/bakl1.log cmdfile=/oracle/ora10g/rmanbak/script/rman_bak_l1.sql
rmanl2.sh
rman target / msglog=/oracle/ora10g/rmanbak/bakl2.log cmdfile=/oracle/ora10g/rmanbak/script/rman_bak_l2.sql
rman脚本
全备脚本(每月执行一次全备)
[oracle@redhat script]$ cat full_bak.sql
run{
allocate channel c1 type disk;
sql 'alter system archive log current';
backup full tag 'order' database
include current controlfile format '/oracle/ora10g/rmanbak/fullback_%d_%T_%s'
plus archivelog format '/oracle/ora10g/rmanbak/bakarch_%d_%T_%s' delete all input;
delete obsolete;
release channel c1;
}
0级备份
cat rman_bak_l0.sql
run{
allocate channel cha1 type disk;
backup
incremental level 0
format "/oracle/ora10g/rmanbak/inc0_%u_%T"
tag wolf_inc0
database;
release channel cha1;
}
1级备份
cat rman_bak_l1.sql
run{
allocate channel cha1 type disk;
backup
incremental level 1
format "/oracle/ora10g/rmanbak/inc0_%u_%T"
tag wolf_inc1
database;
release channel cha1;
}
2级备份
cat rman_bak_l2.sql
run{
allocate channel cha1 type disk;
backup
incremental level 2
format "/oracle/ora10g/rmanbak/inc0_%u_%T"
tag wolf_inc2
database;
release channel cha1;
}
每周日执行0级备份,周3执行1级备份,其余时间执行2级备份,shell脚本如下:
[oracle@redhat script]$ cat startrman.sh
#!/bin/bash
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/oracle/ora10g
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
export ORACLE_SID=wolf
export ORACLE_TERM=xterm
export PATH=/usr/sbin:$PATH
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/usr/x11R6/lib64
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export LD_ASSUME_KERNEL=2.6.18
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.UTF8"
export PATH=/oracle/ora10g/product/10.2.0/db_1/bin:/usr/sbin:/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin
SH_HOME=/oracle/ora10g/rmanbak
export SH_HOME
DATE=`date +"%Y%m%d-%H%M"`
week=`date +"%w"`
#-------------------------------------
#confirm whether the oracle is runing
#-------------------------------------
ps -ef | grep dbw0_$ORACLE_SID | grep -v grep >>/dev/null
#-------------------------------------
#begin to backup
#-------------------------------------
if [ $? -eq 0 ]; then
if [ $week = "0" ]; then
rman target / @$SH_HOME/script/rman_bak_l0.sql log=$SH_HOME/logs/oraclel0$DATE.log
else if [ $week = "3" ]; then
rman target / @$SH_HOME/script/rman_bak_l1.sql log=$SH_HOME/logs/oraclel1$DATE.log
else
rman target / @$SH_HOME/script/rman_bak_l2.sql log=$SH_HOME/logs/oraclel1$DATE.log
fi
fi
fi
计划任务
计划任务crontab
root执行
cron文件语法:
分 小时 日 月 星期 命令
0-59 0-23 1-31 1-12 0-6 command (取值范围,0表示周日一般一行对应一个任务)
使用root用户编辑
crontab -e -u oracle 指定为oracle用户编辑计划任务
[oracle@redhat script]$ crontab -l
30 3 * * * /oracle/ora10g/rmanbak/script/startrman.sh >> /oracle/ora10g/rmanbak/script/crontab.log
shell脚本的知识不全面,问了朋友才搞定的,当然最好自己系统的学习一下shell脚本
一些脚本的基本知识(可以分段检查shell程序是否能够执行)
注意标点符号
判断语句,先检查是否存在,然后在进行下面的命令
[oracle@redhat /]$ cat 3.sh
ps -ef | grep dbw0_$ORACLE_SID | grep -v grep >>/dev/null
if [ $? -eq 0 ]; then
echo "oracle backup begin"
else
echo "oracle_sid error"
exit 1
fi
[oracle@redhat /]$
ps -ef | grep dbw0_$ORACLE_SID | grep -v grep >>/dev/null
if [ $? -eq 0 ]; then
echo "oracle backup begin"
else
echo "oracle_sid error"
exit 1 这个1可以传递给echo $? 意思是说这个命令执行是否成功
fi
[oracle@redhat /]$ cat 2.sh
ps -ef
if [ $? -eq 0 ]; then
echo 0000000
fi
[oracle@redhat /]$
echo $? 可以用来检查上一个命令是否正确,如果正常输出为0,错误则为1
DATE=`date +"%Y%m%d"` 这些基本的命令都可以在系统里面一个一个测试
week=`date +"%w"`
[oracle@redhat script]$ cat 22222.sh
DATE=`date +"%Y%m%d"`
week=`date +"%w"`
echo $DATE
echo $week
if [ $week = 0 ]; then
echo 0000000000000
else echo error
fi