Oracle 数据库增量备份周计划脚本-阿里云开发者社区

开发者社区> 数据库> 正文
登录阅读全文

Oracle 数据库增量备份周计划脚本

简介: ####------------------------------------------------------------------------------#### ##    shell db_incremental_database_backup.
####------------------------------------------------------------------------------####
##    shell db_incremental_database_backup.sh          
##    author  zongziheng                                         
##    date    2017.01.20                                         
##    func The path and date are set as required                 
##    use meth    ./rmanbackup_lev_sjz.sh BACKUP_DEST ORACLE_HOME ORACLE_SID           
## alter system set db_create_file_dest = '/u01/app/oracle/oradata/';                                      
## alter database enable block change tracking;                                                                        
## alter database enable block change tracking using file '/../../blcok...';                                  
####------------------------------------------------------------------------------####
  
#!/bin/bash

export LANG=en_US
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
backtime=`date +"20%y%m%d%H%M%S"`

RMAN_BACKUP_DATA_DEST=$1
RMAN_LOG_FILE=${RMAN_BACKUP_DATA_DEST}/rmanback_lev_${backtime}.log

BACKUP_DATE=`date +%d`
TODAY=`date`
CURRENT_USER=`id|cut -d "(" -f2|cut -d ")" -f1`
echo "-----------------$TODAY-------------------">$RMAN_LOG_FILE
 
export ORACLE_HOME=$2 
export RMAN=${ORACLE_HOME}/bin/rman
export ORACLE_SID=$3 
export ORACLE_USER=oracle
 
echo "ORACLE_SID: $ORACLE_SID">>$RMAN_LOG_FILE
echo "ORACLE_HOME:$ORACLE_HOME">>$RMAN_LOG_FILE
echo "ORACLE_USER:$ORACLE_USER">>$RMAN_LOG_FILE
echo "==========================================">>$RMAN_LOG_FILE
echo "BACKUP DATABASE BEGIN......">>$RMAN_LOG_FILE
echo "                   ">>$RMAN_LOG_FILE
 
#touch $RMAN_LOG_FILE
chmod 666 $RMAN_LOG_FILE
 
CURRENT_WEEK_DAILY=`date +%a`
 
case  "$CURRENT_WEEK_DAILY" in
       "Mon")
            BAK_LEVEL=1
            ;;
       "Tue")
            BAK_LEVEL=1
            ;;
       "Wed")
            BAK_LEVEL=1
            ;;
       "Thu")
            BAK_LEVEL=1
            ;;
       "Fri")
            BAK_LEVEL=1
            ;;
       "Sat")
            BAK_LEVEL=0
            ;;
       "Sun")
            BAK_LEVEL=1
            ;;
       "*")
            BAK_LEVEL=error
esac
 
export BAK_LEVEL=$BAK_LEVEL
 
echo "Today is : $CURRENT_WEEK_DAILY  incremental level= $BAK_LEVEL">>$RMAN_LOG_FILE
 
 
RUN_SHELL="
 
BAK_LEVEL=$BAK_LEVEL
export BAK_LEVEL
 
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
 
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
 
$RMAN nocatalog TARGET sys/oracle msglog $RMAN_LOG_FILE append <<EOF
run
{
  allocate channel c1 type disk;
  allocate channel c2 type disk;
  backup incremental level= $BAK_LEVEL skip inaccessible filesperset 5 Database format='"$RMAN_BACKUP_DATA_DEST"/"$ORACLE_SID"_lev_"$BAK_LEVEL"_%U_%T' tag='"$ORACLE_SID"_lev_"$BAK_LEVEL"';
  sql 'alter system archive log current';
  backup archivelog all tag='arc_bak' format='"$RMAN_BACKUP_DATA_DEST"/arch_%U_%T' skip inaccessible  filesperset 5 not backed up 1 times delete input;
  backup current controlfile tag='ctlfile_bak' format='"$RMAN_BACKUP_DATA_DEST"/ctl_file_%U_%T';
  backup spfile tag='spfile_bak' format='"$RMAN_BACKUP_DATA_DEST"/spfile_%U_%T';
  release channel c2;
  release channel c1;
}
report obsolete;
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
list backup summary;
EOF
"
# Initiate the command string
if [ "$CUSER" = "root" ]
then
    echo "Root Command String: $RUN_SHELL" >> $RMAN_LOG_FILE
    su - $ORACLE_USER -c "$RUN_SHELL" >> $RMAN_LOG_FILE
    STATUS=$?
else
    echo "User Command String: $RUN_SHELL" >> $RMAN_LOG_FILE
    /bin/sh -c "$RUN_SHELL" >> $RMAN_LOG_FILE
    STATUS=$?
fi
 
# ---------------------------------------------------------------------------
# Record whether or not the log is completed.
# ---------------------------------------------------------------------------
if [ "$STATUS" = "0" ]
then
    LOGMSG="Ended successfully."
else
    LOGMSG="Ended in error!"
fi
 
echo >> $RMAN_LOG_FILE
echo Script $0 >> $RMAN_LOG_FILE
echo ==== $LOGMSG on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE
 
exit $STATUS

####------------------------------------------------------------------------------####

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: