DB2 BACKUP数据库 归档日志脚本

xiaoxiao2021-02-28  1

#!/bin/ksh #-------------------------------------------------------------------------- # 名称: backup_easdb.sh # # 功能描述: 提供当前实例下指定数据库的备份功能 #-------------------------------------------------------------------------- ####主程序#### ShellVersion=V0.0.0.1 ShellUpdateTime=2018-03-29-10.48.00 syntax() { echo " " echo " backup_db.sh -d [dbname] -p [backup path] -e -t [Backup type] -c -l -kd -kl " echo " " echo " -d List of Database Name,separated by spaces. Default all local database" echo " -p Backup Path,this path will store database backup file and archive log: <path>/<instance>/<dbname>/database[archivelog]" echo " -t Backup Type. [ONLINE]/[INCREMENTAL]/[INCREMENTAL DELTA]" echo " -c Compress." echo " -l Include Logs." echo " -kd The number of database backup image saved,default 3" echo " -kl The number of archive log backup image saved,default 3" echo " -h Help" echo " " echo " sh backup_easdb.sh -d easdb -p /data/db2/dbbackup -t online -c -l -kd 10 -kl 20 " echo " 程序版本=${ShellVersion}; 程序更新时间=${ShellUpdateTime}" exit; } function f_backupdb {   db2 connect TO ${DBNAME};   DB2INSTANCE=`db2ilist`;   cd ${BKPATH};mkdir -p ${DB2INSTANCE}/${DBNAME}/database;mkdir -p ${DB2INSTANCE}/${DBNAME}/archivelog;   BKPATH_DB=${BKPATH}/${DB2INSTANCE}/${DBNAME}/database;   BKPATH_ARCHLOG=${BKPATH}/${DB2INSTANCE}/${DBNAME}/archivelog;   ArclogPath=`db2 "get db cfg for ${DBNAME}" |grep LOGARCHMETH|grep -v "= OFF"|awk '{print $NF}'|sed 's/DISK://g;s/\/$//g'`   echo "DB2INSTANCE="${DB2INSTANCE}   echo "ArclogPath="${ArclogPath} ###backup database   DBBackupStr="db2 BACKUP DATABASE ${DBNAME} ${BKType} TO ${BKPATH_DB} ${COMPRESS} ${LOGINBK} WITHOUT PROMPTING"   echo "DBBackupStr="${DBBackupStr}   db2 BACKUP DATABASE ${DBNAME} ${BKType} TO ${BKPATH_DB} ${COMPRESS} ${LOGINBK} WITHOUT PROMPTING ;   ###get archive log and database backup information     db_arc_his_str=`db2 list history backup all for $DBNAME |grep "^  B  D .*.LOG .*.LOG" |tail -${Keep_BKDB_Num} |awk -v dbn=${DBNAME} -v insname=${DB2INSTANCE} '{printf "%s.0.%s.DBPART000.%s.001 %s %s ",toupper(dbn),tolower(insname),substr($3,1,14),$6,$7}'|awk '{print $1,$2,$3,$5,$(NF-2),$(NF-1),$NF,NF/3}'`   echo "db_arc_his_str="$db_arc_his_str     sleep 5   set -- $db_arc_his_str; _img1_file=$1; _img1_log1=$2; img1_log2=$3; _old_log1=$4;_img2_file=$5; _img2_log1=$6; img2_log2=$7; _his_num=$8 ;     echo "_img1_file="$1     echo "_img1_log1="$2     echo "img1_log2="$3     echo "_old_log1="$4     echo "_img2_file="$5     echo "_img2_log1="$6     echo "img2_log2="$7     echo "_his_num="$8 ###backup archive log  find ${ArclogPath} -type f -print 2>/dev/null|awk -F / '{if ($NF  >= old_log && $NF < new_log) print $0} ' old_log=${_old_log1} new_log=${_img2_log1} |while read line     do        echo "line="$line        if [ -f ${BKPATH_ARCHLOG}/Archlog$(date "+%Y%m%d").tar ]; then          cd ${BKPATH_ARCHLOG};          tar -rvf ${BKPATH_ARCHLOG}/Archlog$(date "+%Y%m%d").tar $line;_rt_code=$?;echo "Arclog:${_rt_code}:${BKPATH_ARCHLOG}/:tar -rvf ${line}:$(date "+%Y%m%d%H%M%S")"        else          cd ${BKPATH_ARCHLOG};          tar -cvf ${BKPATH_ARCHLOG}/Archlog$(date "+%Y%m%d").tar $line;_rt_code=$?;echo "Arclog:${_rt_code}:${BKPATH_ARCHLOG}/:tar -cvf ${line}:$(date "+%Y%m%d%H%M%S")"        fi     done ###delete backup archive log file  cd ${BKPATH_ARCHLOG};  find ${BKPATH_ARCHLOG} -name 'Archlog*.tar.gz' -ctime +${Keep_BKARCHLOG_Num} -type f -print 2>/dev/null | while read line         do           rm -f $line;_rt_code=$?;echo "#---rm :${_rt_code}:rm -f $line:$(date "+%Y%m%d%H%M%S")"         done ###delete database backup file  cd ${BKPATH_DB};  find ${BKPATH_DB} -type f -name "${DBNAME}*" -print | grep -v 'tar' 2>/dev/null|awk -F / '{if ($NF < old_bkimg) print $0} ' old_bkimg=${_img1_file} |while read line    do      rm -f $line;local _rt_code=$?;echo "##-rmBackup:${_rt_code}:${_BK_Path}:rm -f $line:$(date "+%Y%m%d%H%M%S")"    done ###delete archive log file  find ${ArclogPath} -type f -print 2>/dev/null|awk -F / '{if ($NF < old_log) print $0} ' old_log=${_img1_log1} |while read line    do      rm -f $line;_rt_code=$?;echo "#-#rmArclog:${_rt_code}:${ArclogPath}/:rm -f ${line}:$(date "+%Y%m%d%H%M%S")"    done } [ -f ~/sqllib/db2profile ] && . ~/sqllib/db2profile 1>/dev/null 2>&1 || return 4 shell_name="$(echo $0|awk -F / '{print $NF}')" work_dir="$(echo $0|sed s/${shell_name}//g)" [ -z "${work_dir}" ] && work_dir=$(pwd);cd ${work_dir};work_dir=$(pwd) log_dir=${work_dir}/logs;mkdir -p ${log_dir} logfile="${log_dir}/${shell_name}.log" sqllogfile="${log_dir}/${shell_name}.sqllog" exec >>${logfile} ; echo "#####backup database and archive log at `date`" ###输入参数判断,若无输入则初始化. for inopt in $@ do   case `echo $inopt|tr a-z A-Z` in   -D|DB|-DATABASE|-DATABASES) CurOpt="-D";continue;;   -P|PATH) CurOpt="-P";continue;;   -T|-TYPE) CurOpt="-T";BKType="";continue;;   -C|-COMPRESS) COMPRESS="COMPRESS";continue;;   -L|-LOGINBK)  LOGINBK="INCLUDE LOGS";continue;;   -KD|-KEEPBKNUM) CurOpt="-KD";continue;;   -KL|-KEEPBKNUM) CurOpt="-KL";continue;;   -*) syntax;;   esac   case "${CurOpt}" in   -D) DBNAME=$inopt;;   -P) BKPATH=$inopt;;   -T) BKType=`echo $inopt|tr a-z A-Z`;;   -KD) Keep_BKDB_Num=$inopt;;   -KL) Keep_BKARCHLOG_Num=$inopt;;   esac done echo "BKType1111111111="$BKType if [ ! $Keep_BKDB_Num ] then    Keep_BKDB_Num=3 fi if [ ! $Keep_BKARCHLOG_Num ] then    Keep_BKARCHLOG_Num=3 fi if [ "$BKType" = "INCREMENTAL" ] then    BKType="ONLINE INCREMENTAL" fi if [ "$BKType" = "DELTA" ] then    BKType="ONLINE INCREMENTAL DELTA" fi echo "DBNAME="$DBNAME echo "BKPATH="$BKPATH echo "BKType="$BKType echo "COMPRESS="$COMPRESS echo "LOGINBK="$LOGINBK echo "Keep_BKDB_Num="$Keep_BKDB_Num echo "Keep_BKARCHLOG_Num="$Keep_BKARCHLOG_Num #f_backupdb >>${logfile}; f_backupdb ; echo "###backup database and archive log stop at `date`" #return 0
转载请注明原文地址: https://www.6miu.com/read-1900232.html