oracle rman 定时备份脚本

近期需要出个oracle定时备份的方案,可以恢复到最新状态,参考 http://blog.sina.com.cn/s/blog_7756ebc30100tnqy.html ,我又在shell中加工了一下,弄了2个shell脚本,init.sh, oraclebackup.sh,执行init.sh  param1(备份文件路径) param2(归档日志路径),就可以设置定时备份了,脚本会设置定时器,详细内容参见init.sh文件的开头说明,下面贴出脚本代码:
init.sh

#!/bin/sh
# oracle自动定时备份程序
#说明:init.sh 和 oraclebackup.sh是oracle自动定时备份的2个shell脚本。
# 备份工具:RMAN
# 备份方式:增量备份
# 归档模式:是
# 使用示例: ./init.sh /home/dev/databasebackup /home/dev/archivelogbackup
# 第一个参数是oracle备份文件的存储目录,第二个参数是归档重做日志的存储目录
# 每次备份数据库时也会备份归档重做日志,然后删除归档重做日志
# 这2个脚本需要放到oracle服务器
# 环境变量中需要提前配置好ORACLE_BASE,ORACLE_HOME,ORACLE_SID
# 安装后的oracle默认是非归档模式
# init.sh脚本执行过程中需要重启数据库
#init.sh脚本执行流程:
# 1.将2个目录参数替换到oraclebackup.sh
# 2.将ORACLE_BASE,ORACLE_HOME,ORACLE_SID这3个环境变量替换到oraclebackup.sh
# 3.配置RMAN
# 4.将oracle设置为归档模式
# 5.生成定时器文件
# 6.设置定时器,定时对oracle进行备份,备份文件默认保存15天
# 每周日1:00执行0级备份
# 每周一至周三,周五至周六1:00执行2级增量备份
# 每周四1:00执行1级增量备份
# 每周六16:00执行检测
# 每周日18:00删除过期的备份数据
#
# 执行完init.sh后,也可以进行手动备份和恢复数据库
# 进行0级备份:
# oraclebackup.sh -l0  
# 进行1级备份
# oraclebackup.sh -l1  
# 进行2级备份
# oraclebackup.sh -l2 
# 检测备份完整性
# oraclebackup.sh -c
# 删除过期的备份,默认保存15天
# oraclebackup.sh -d
# 恢复数据库到最新状态
# oraclebackup.sh -r
#日志:
# 脚本执行的log在第一个参数(/home/dev/databasebackup)的rmanlog目录下,即/home/dev/databasebackup/rmanlog,
# 可以定期检查这个文件夹下面的log,查看备份信息




DATABASE_PATH=""
ARCHIVELOG_PATH=""
ORACLE_BASE_VAL=""
ORACLE_HOME_VAL=""
ORACLE_SID_VAL=""


KEY1="DATABASE_PATH"
KEY2="ARCHIVELOG_PATH"
KEY3="export ORACLE_BASE"
KEY4="export ORACLE_HOME"
KEY5="export ORACLE_SID"


#配置RMAN,备份保留15天,优化备份,自动备份控制文件,控制文件的备份位置
TEMPLATE_RMANCMD="connect target /;
        run {
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 15 DAYS;
CONFIGURE BACKUP OPTIMIZATION ON;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO 'oldpath_database/controlfile_backup/controlfile_%F';
}"

RMANCMD=""


#替换RMAN命令中的路径参数
function replaceRmanCMD()
{
RMANCMD=`echo ${TEMPLATE_RMANCMD} | sed -n "s#oldpath_database#${DATABASE_PATH}#p"`

echo "==================RMANCMD======================"
echo ${RMANCMD}
echo "==============================================="
}


#执行RMAN命令,配置RMAN
function setRmanConf()
{
rman log=\'${DATABASE_PATH}/rmanlog/paidb_rman_conf.log\' append <<EOF
     ${RMANCMD}
EOF
}


#把oracle设置为归档模式
function setArchiveLog()
{
sqlplus / as sysdba <<EOF
 shutdown immediate;
startup mount;
alter database archivelog;
alter system set control_file_record_keep_time=30 scope=both;
alter system set log_archive_dest_1='location=${ARCHIVELOG_PATH}/archivelog' scope=both;
alter database open;
alter system archive log start;
exit; 
EOF
}


#创建定时器文件crontabfile,设置定时器
#每周日1:00执行0级备份
#每周一至周三,周五至周六1:00执行2级增量备份
#每周四1:00执行1级增量备份
#每周六16:00执行检测
#每周日18:00删除过期的备份数据
function setCrontab()
{
if [ -e ./crontabfile ]
then
rm ./crontabfile
fi

touch ./crontabfile

chmod 777 ./crontabfile

#每周日1:00执行0级备份
echo "0 1 * * 0 `pwd`/oraclebackup.sh -l0" >> crontabfile

#每周一至周三,周五至周六1:00执行2级增量备份
echo "0 1 * * 1-3,5-6 `pwd`/oraclebackup.sh -l2" >> crontabfile

#每周四1:00执行1级增量备份
echo "0 1 * * 4 `pwd`/oraclebackup.sh -l1" >> crontabfile

#每周六16:00执行检测
echo "0 18 * * 6 `pwd`/oraclebackup.sh -c" >> crontabfile

#每周日18:00删除过期的备份数据
echo "0 18 * * 0 `pwd`/oraclebackup.sh -d" >> crontabfile

crontab ./crontabfile
}


#创建子目录,归档日志目录下创建archivelog
#数据库备份目录下创建rmanlog,controlfile_backup,database_backup目录
#如果已经存在,则不创建
function makeSubDir()
{
if [ ! -d "${ARCHIVELOG_PATH}/archivelog" ]
then
mkdir ${ARCHIVELOG_PATH}/archivelog
fi

if [ ! -d "${ARCHIVELOG_PATH}/rmanlog" ]
then
mkdir ${DATABASE_PATH}/rmanlog
fi

if [ ! -d "${ARCHIVELOG_PATH}/controlfile_backup" ]
then
mkdir ${DATABASE_PATH}/controlfile_backup
fi

if [ ! -d "${ARCHIVELOG_PATH}/database_backup" ]
then  
mkdir ${DATABASE_PATH}/database_backup
fi
}


#把数据库备份目录参数替换到oraclebackup.sh
function replaceDatabaseBakPath()
{
sed -i "s#^\(${KEY1} *= *\)\([^ ]*\)\( *.*\)#\1${DATABASE_PATH}\3#" ./oraclebackup.sh
}


#把归档日志目录参数替换到oraclebackup.sh
function replaceArchivelogBakPath()
{
sed -i "s#^\(${KEY2} *= *\)\([^ ]*\)\( *.*\)#\1${ARCHIVELOG_PATH}\3#" ./oraclebackup.sh
}


#把ORACLE_HOME等参数替换到oraclebackup.sh
function replaceOracleHome()
{
sed -i "s#^\(${KEY3} *= *\)\([^ ]*\)\( *.*\)#\1${ORACLE_BASE_VAL}\3#" ./oraclebackup.sh
sed -i "s#^\(${KEY4} *= *\)\([^ ]*\)\( *.*\)#\1${ORACLE_HOME_VAL}\3#" ./oraclebackup.sh
sed -i "s#^\(${KEY5} *= *\)\([^ ]*\)\( *.*\)#\1${ORACLE_SID_VAL}\3#" ./oraclebackup.sh
}


function usage()
{
echo "------------------------------------------------------------------------"
echo 
echo "Example:"
echo "      Sample:    ./init.sh  /aaa/bbb/databaseBackupPath  /aaa/bbb/archivelogBackupPath"
echo 
echo "note: The path must be exist!"
echo "------------------------------------------------------------------------"
}


#指定的路径不存在
function pathNotExist()
{
echo "************************************************************************"
echo 
echo "Path: ${1} not Exist!"
echo 
echo "************************************************************************" 
}


#环境变量没找到
function envNotFound()
{
echo "************************************************************************"
echo 
echo "env ${1} not found!"
echo 
echo "************************************************************************" 
}


function check()
{
if [ ! -d "$DATABASE_PATH" ]
then
pathNotExist ${1}
exit 0
fi

if [ ! -d "$ARCHIVELOG_PATH" ]
then
pathNotExist ${2}
exit 0
fi

if [ ! $ORACLE_BASE ]
then
envNotFound "ORACLE_BASE"
exit 0
fi

if [ ! $ORACLE_HOME ]
then
envNotFound "ORACLE_HOME"
exit 0
fi

if [ ! $ORACLE_SID ]
then
envNotFound "ORACLE_SID"
exit 0
fi
}


function main()
{
if (( $# < "2"))
then
usage
exit 0
fi 

DATABASE_PATH=$1

ARCHIVELOG_PATH=$2

DATABASE_PATH=${DATABASE_PATH%/}

ARCHIVELOG_PATH=${ARCHIVELOG_PATH%/}

check

ORACLE_BASE_VAL=$ORACLE_BASE

ORACLE_HOME_VAL=$ORACLE_HOME

ORACLE_SID_VAL=$ORACLE_SID

replaceOracleHome

makeSubDir

replaceDatabaseBakPath

replaceArchivelogBakPath

replaceRmanCMD

setRmanConf

echo "--------->start conf archive log"

setArchiveLog 


echo "--------->end conf archive log"

chmod 777 ./oraclebackup.sh

setCrontab


}


main $1 $2


oraclebakcup.sh

#!/bin/sh
export ORACLE_BASE=
export ORACLE_HOME=
export ORACLE_SID=
export PATH=$ORACLE_HOME/bin:$PATH


DATABASE_PATH=""
ARCHIVELOG_PATH=""


#====================================================
#0级备份rman命令
TEMPLATE_RMANCMD_LEVEL0=" connect target /;
        run {
                allocate channel c1 type disk;
                backup incremental level=0 database format 'oldpath_database/database_backup/paidb_level0_%U' tag='level0';
                sql 'alter system archive log current';
                backup archivelog all format 'oldpath_archive/archivelog/paidb_arch_%U' delete input;
                release channel c1;
        }
";


#1级备份rman命令
TEMPLATE_RMANCMD_LEVEL1=" connect target /;
        run {
                allocate channel c1 type disk;
                backup incremental level=1 database format 'oldpath_database/database_backup/paidb_level1_%U' tag='level1';
                sql 'alter system archive log current';
                backup archivelog all format 'oldpath_archive/archivelog/paidb_arch_%U' delete input;
                release channel c1;
        }
";


#2级备份rman命令
TEMPLATE_RMANCMD_LEVEL2=" connect target /;
        run {
                allocate channel c1 type disk;
                backup incremental level=2 database format 'oldpath_database/database_backup/paidb_level2_%U' tag='level2';
                sql 'alter system archive log current';
                backup archivelog all format 'oldpath_archive/archivelog/paidb_arch_%U' delete input;
                release channel c1;
        }
";
#======================替换路径后的RMAN命令=======================
RMANCMD_LEVEL0=""
RMANCMD_LEVEL1=""
RMANCMD_LEVEL2=""
#==============================================================


#替换0级备份命令中的路径参数
function replaceRmanCMDl0()
{
RMANCMD_LEVEL0=`echo ${TEMPLATE_RMANCMD_LEVEL0} | sed -n "s#oldpath_database#${DATABASE_PATH}#p"`


RMANCMD_LEVEL0=`echo ${RMANCMD_LEVEL0} | sed -n "s#oldpath_archive#${ARCHIVELOG_PATH}#p"`

echo "##################RMANCMD######################"
echo ${RMANCMD_LEVEL0}
echo "###############################################"
}


#替换1级备份命令中的路径参数
function replaceRmanCMDl1()
{
RMANCMD_LEVEL1=`echo ${TEMPLATE_RMANCMD_LEVEL1} | sed -n "s#oldpath_database#${DATABASE_PATH}#p"`


RMANCMD_LEVEL1=`echo ${RMANCMD_LEVEL1} | sed -n "s#oldpath_archive#${ARCHIVELOG_PATH}#p"`

echo "##################RMANCMD######################"
echo ${RMANCMD_LEVEL1}
echo "###############################################"
}


#替换2级备份命令中的路径参数
function replaceRmanCMDl2()
{
RMANCMD_LEVEL2=`echo ${TEMPLATE_RMANCMD_LEVEL2} | sed -n "s#oldpath_database#${DATABASE_PATH}#p"`


RMANCMD_LEVEL2=`echo ${RMANCMD_LEVEL2} | sed -n "s#oldpath_archive#${ARCHIVELOG_PATH}#p"`

echo "##################RMANCMD######################"
echo ${RMANCMD_LEVEL2}
echo "###############################################"
}


#====================================================
#0级备份
function backupLevel0()
{
rman log=\'${DATABASE_PATH}/rmanlog/paidb_rman_level0.log\' append <<EOF
 ${RMANCMD_LEVEL0}
EOF
}


#1级备份
function backupLevel1()
{
rman log=\'${DATABASE_PATH}/rmanlog/paidb_rman_level1.log\' append <<EOF
 ${RMANCMD_LEVEL1}
EOF
}


#2级备份
function backupLevel2()
{
rman log=\'${DATABASE_PATH}/rmanlog/paidb_rman_level2.log\' append <<EOF
 ${RMANCMD_LEVEL2}
EOF
}


#检查oracle备份文件,如果有损坏的记录,会放进V$DATABASE_BLOCK_CORRUPTION视图中,
#需要DBA定期检查V$DATABASE_BLOCK_CORRUPTION视图,块被修复后,
#V$DATABASE_BLOCK_CORRUPTION中相应的记录会被自动删除
function backupcheck()
{
rman log=\'${DATABASE_PATH}/rmanlog/rmancheck.log\' append <<EOF
 connect target /;
run {
#check database
backup validate check logical database;
#check backup set
restore database validate check logical;
#crosscheck backup
crosscheck backup;
}
EOF
}


#删除过期的备份文件
function backupDelete()
{
rman log=\'${DATABASE_PATH}/rmanlog/rmandelete.log\' append <<EOF
 connect target /;
run {
delete noprompt obsolete;
delete noprompt expired backup;
}
EOF
}


#恢复数据库到最新状态
function backupRestore()
{
rman log=\'${DATABASE_PATH}/rmanlog/rmanrestore.log\' append <<EOF
 connect target /;
run {
restore database;
recover database;
}
EOF
}


#====================================================
#0级备份
function backupl0()
{
replaceRmanCMDl0

backupLevel0 
}


#1级备份
function backupl1()
{
replaceRmanCMDl1
    
    backupLevel1 
}


#2级备份
function backupl2()
{
replaceRmanCMDl2

    backupLevel2 
}


#检查备份文件
function backupck()
{
backupcheck 
}


#删除过期的备份文件
function backupdel()
{
backupDelete 
}


#恢复数据库到最新状态
function backupres()
{
backupRestore
}


#====================================================


function usage()
{
echo "------------------------------------------------------------------------"
echo 
echo "Example:"
echo "      Sample:    ./oraclebackup.sh  -l0|-l1|-l2|-c|-d|-r"
echo 
echo "note: -l0 backup level0 0级备份
-l1 backup level1 1级备份
-l2 backup level2 2级备份
-c  backup check 检查备份
-d  backup delete 删除过期备份
-r  backup restore" 恢复数据库到最新状态
echo "------------------------------------------------------------------------"
}


function main()
{
if (( $# < "1" ))
then
usage
exit 0
fi

case "$1" in
"-l0")
backupl0
exit 0
;;
"-l1")
backupl1
exit 0
;;
"-l2")
backupl2
exit 0
;;
"-c")
backupck
exit 0
;;
"-d")
backupdel
exit 0
;;  
"-r")
backupres
exit 0
;;  
esac

usage
}


main $1

你可能感兴趣的:(rman)