腳本說明:
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比鐵門關網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鐵門關網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋鐵門關地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。日志切割與上傳類腳本的功能:腳本自身的運行日志記錄與清理、日志切割與打包、ftp上傳、傳送失敗自動重傳、斷電自動補傳、清理超期舊打包等
--------------------
#!/bin/bash # Cut and upload aclog log # by colin # revision on 2016-06-15 ######################################## # 功能說明:該腳本運用于上傳aclog日志 # # 使用說明: #+ ./aclog_backup_and_upload.sh -f /var/log/host/jnxac.log -t tar -h SDQD-TS-JNX-HLS # 更新說明: # ######################################## #sleep 60 #延時60秒運行 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin RUN_LOG='/var/log/cron_scripts_run.log' [ ! -f ${RUN_LOG} ] && touch ${RUN_LOG} echoGoodLog(){ echo -e "\033[32m`date +%F" "%T":"%N` $*\033[0m" >> ${RUN_LOG} } echoBadLog(){ echo -e "\033[31m`date +%F" "%T":"%N` $*\033[0m" >> ${RUN_LOG} } echoGoodLog "Now, Script: `basename $0` run." RUNLOG_MAX_NUM=100000 RUNLOG_MAX_DELNUM=5000 SEND_MAX_TIME=6 SEND_WHILE_SLEEP_TIME=30 BACK_SAVE_MAX_DAY=180 LOG_CUT_MIN=60 # 日志文件30分鐘切割一次 cleanRunLog(){ CLEANLOGFILE=${1?"Usage: $FUNCNAME log_file_name"} TEMP_WC=`cat ${CLEANLOGFILE} |wc -l` [ "${TEMP_WC}" -gt "${RUNLOG_MAX_NUM}" ] && { sed -i "1,${RUNLOG_MAX_DELNUM}d" ${CLEANLOGFILE} && echoGoodLog "Clean up the ${CLEANLOGFILE}..." } echoGoodLog "Script: `basename $0` run done." exit } ## # 省份數(shù)組 # PROVINCE=( sd hlj sc ) ## # 腳本幫助提示函數(shù) # scriptsHelp(){ echoBadLog "=======================================" echoGoodLog "Usage parameters:" echoGoodLog "./`basename $0` -f/--file /var/log/host/qfgw.log [-t/--tar tar] [-h/--hostname SDQD-TS-CL-WIN]" echoGoodLog "Options:" echoGoodLog " -f/--file)" echoGoodLog " 必須的參數(shù):需要切割與上傳的日志文件" echoGoodLog " -t/--tar)" echoGoodLog " 可選參數(shù):若需打包,使用方法: -t tar " echoGoodLog " -h/--hostname)" echoGoodLog " 可選參數(shù):若一個站點存放了多個地方的相同類型日志,此時就需要設定每個日志所屬站點" echoGoodLog " 比如:昌樂的網(wǎng)關日志,沒有HLS,日志存儲在青島北,那加參數(shù)如下:-h SDQD-TS-CL-WIN" echoGoodLog " -c)" echoGoodLog " 可選參數(shù):用于檢查日志服務器上是否有存儲日志的文件夾,沒有就創(chuàng)建,需使用except命令,會自動安裝" echoGoodLog " 注意:日志服務器必須要開放ssh遠程登錄,或者防火墻需要放行" echoGoodLog " 使用方法:./`basename $0` -c ;-c后面不需要加選項" echoGoodLog "Example:" echoGoodLog "./`basename $0` -f /var/log/host/qfgw.log -t tar -h SDQD-TS-CL-WIN" echoBadLog "=======================================" } checkParameter(){ PARAMETER=${1:-null} PARAMETER_STATUS=`echo "${PARAMETER}" |grep "^-"|wc -l` if [ "${PARAMETER_STATUS}" -eq 1 -o "${PARAMETER}" = "null" ];then scriptsHelp echoBadLog "參數(shù)錯誤,請重新輸入。" exit fi } ## # 判斷是否帶參數(shù) # if [ -z "$*" ];then scriptsHelp exit else ## # 腳本傳參數(shù),調(diào)用相應的函數(shù)功能 # while test -n "$1";do case "$1" in --file|-f) shift checkParameter $1 LOG_NAME=$1 ;; --tar|-t) shift checkParameter $1 CTAR=$1 ;; --hostname|-h) shift checkParameter $1 CHOSTNAME=$1 ;; -c) CCHECK_LOG_SERVER_DIR=$1 ;; *) echoBadLog "Unknown argument: $1" scriptsHelp exit ;; esac shift done fi ## # 需要切割的日志為必須要的參數(shù) # checkParameter $LOG_NAME IS_TAR=${CTAR:-notar} HOSTNAME=${CHOSTNAME:-`hostname`} TEMP_PRO=`echo ${PROVINCE[@]} |sed "s/ /|/g"` STATION=`echo ${HOSTNAME} |awk -F- '{print $3}'|tr [A-Z] [a-z]` STATION_TYPE=`echo ${HOSTNAME} |awk -F- '{print $2}'|tr [A-Z] [a-z]` STATION_SITE=`echo ${HOSTNAME} |awk -F- '{print $1}'|tr [A-Z] [a-z] |grep -Eo "\b${TEMP_PRO}"` ############################# # define the ftp client LOG_TYPE="aclog" LOG_TYPEB="aclog" LCD_DIR="/data/${STATION}_log/${STATION}_${LOG_TYPE}" # 日志服務器上的保存文件夾特例 #---------------------------- case ${STATION_SITE}${STATION_TYPE} in #sdbs) CD_DIR="/${LOG_TYPEB}/${STATION_SITE}/qdjy/${STATION}/";; sdbs) CD_DIR="/${LOG_TYPEB}/${STATION_SITE}/qdjy/";; *) CD_DIR="/${LOG_TYPEB}/${STATION_SITE}/${STATION}/";; esac ############################# # define the ftp server FTPSERVER='IP地址' FTPUSER='用戶名' FTPPASSWD='密碼' SSHPORT='端口' ############################# T=`echo $(date +%k) |sed 's/ //g'` #LAST_T=`echo $(date -d "1 hour ago" +%k) |sed 's/ //g'` DAY_TIME=`date +%Y-%m-%d` LAST_DAY_TIME=`date -d "yesterday" +%Y-%m-%d` LAST_HOUR_TIME=`date +%Y-%m-%d-%H` FILENUM=`expr $(date +%M|sed 's/^0//g') / ${LOG_CUT_MIN}` DAY_LOG_TAR_NUM="${FILENUM}" [[ "${FILENUM}" -eq 0 ]] && { FILENUM=`expr 60 / ${LOG_CUT_MIN}` LAST_HOUR_TIME=`date -d "1 hour ago" +%Y-%m-%d-%H` } ## # 為兼容舊版本,當60分鐘切割一次時,就和之前舊版本文件名一樣咯 # if [ ${LOG_CUT_MIN} -eq 60 ];then LOG_HOUR_NAME="${LOG_TYPE}${LAST_HOUR_TIME}.txt" else LOG_HOUR_NAME="${LOG_TYPE}${LAST_HOUR_TIME}-${FILENUM}.txt" fi LOG_DAY_NAME="${LOG_TYPE}${LAST_DAY_TIME}.txt" case "${IS_TAR}" in "tar") PUT_LOG_DAY_NAME="${LOG_DAY_NAME%.txt}.tar.gz" PUT_LOG_HOUR_NAME="${LOG_HOUR_NAME%.txt}.tar.gz" ;; "notar") PUT_LOG_DAY_NAME="${LOG_DAY_NAME}" PUT_LOG_HOUR_NAME="${LOG_HOUR_NAME}" ;; esac tarLogFile(){ TARTONAME=${1?"Usage: $FUNCNAME tar_file_name need_tar_file_name"} FROMLOG=$2 tar -czf ${TARTONAME} --remove-files ${FROMLOG} if [ -e ${TARTONAME} ];then echoGoodLog "Tar: ${TARTONAME} is successfully." else echoBadLog "Tar: ${TARTONAME} was failed, Please check..." fi } checkProgramExist(){ PROGRAMNAME=${1?"Usage: $FUNCNAME program_install_name"} PROGRAMEXIST=`dpkg -l |grep -wo ${PROGRAMNAME}|wc -l` if [ "${PROGRAMEXIST}" -ge 1 ];then return 0; else /usr/bin/apt-get install ${PROGRAMNAME} -y if [ $? -eq 0 ];then echoGoodLog "Install ${PROGRAMNAME} is successfully." return 0; else echoBadLog "Install ${PROGRAMNAME} was failed, Please check..." return 1; fi fi } checkLogServerDir(){ checkProgramExist expect passwd=${FTPPASSWD} /usr/bin/expect <<-EOF set time 1 spawn ssh -p${SSHPORT} ${FTPUSER}@${FTPSERVER} expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$passwd\r" } } expect "*~$" send "cd /home/upload${CD_DIR}\r" expect { "*No such file or directory" { send "mkdir -p /home/upload${CD_DIR}\r" } } expect "*~$" send "exit\r" interact expect eof EOF echo -e "\r" } [ ! -d ${LCD_DIR} ] && mkdir -p ${LCD_DIR} ## # 若切割后的日志文件存在時,就退出切割命令等 # cd ${LCD_DIR} && [ ! -f ${PUT_LOG_HOUR_NAME} ] && { if [ -s ${LOG_NAME} ];then until [ -f ${LOG_HOUR_NAME} ] do cp ${LOG_NAME} ${LOG_HOUR_NAME} done if [ $? -eq 0 -a -f "${LOG_HOUR_NAME}" ];then echoGoodLog "Create ${LOG_HOUR_NAME} is successfully." else echoBadLog "Create ${LOG_HOUR_NAME} was failed, Please check..." fi [ "${IS_TAR}" = "tar" ] && tarLogFile ${PUT_LOG_HOUR_NAME} ${LOG_HOUR_NAME} if [ "${T}" -eq 0 -a "${DAY_LOG_TAR_NUM}" -eq 0 ];then TEMP_CHECK_LOG=`ls -l ${LOG_DAY_NAME} |awk '{print $5}'` cat ${LOG_NAME} >> ${LOG_DAY_NAME} [ $? -eq 0 ] || cat ${LOG_NAME} >> ${LOG_DAY_NAME} CHECK_LOG=`ls -l ${LOG_DAY_NAME} |awk '{print $5}'` else [ -f ${LOG_TYPE}${DAY_TIME}.txt ] || touch ${LOG_TYPE}${DAY_TIME}.txt TEMP_CHECK_LOG=`ls -l ${LOG_TYPE}${DAY_TIME}.txt |awk '{print $5}'` cat ${LOG_NAME} >> ${LOG_TYPE}${DAY_TIME}.txt [ $? -eq 0 ] || cat ${LOG_NAME} >> ${LOG_TYPE}${DAY_TIME}.txt CHECK_LOG=`ls -l ${LOG_TYPE}${DAY_TIME}.txt |awk '{print $5}'` fi echoGoodLog "CHECK: CHECK_LOG=${CHECK_LOG},TEMP_CHECK_LOG=${TEMP_CHECK_LOG}..." if [ "${CHECK_LOG}" -gt "${TEMP_CHECK_LOG}" ];then cat /dev/null > ${LOG_NAME} [ $? -eq 0 ] && echoGoodLog "Append: ${LOG_HOUR_NAME} to ${LOG_TYPE}_day_log is successfully." else echoBadLog "Append: ${LOG_HOUR_NAME} to ${LOG_TYPE}_day_log was failed, Please check..." fi [ "${T}" -eq 0 -a "${IS_TAR}" = "tar" -a "${DAY_LOG_TAR_NUM}" -eq 0 ] && tarLogFile ${PUT_LOG_DAY_NAME} ${LOG_DAY_NAME} else echoBadLog "Log: ${LOG_NAME} is null or not exist, Please check..." fi } ## # 檢查保存文件夾目錄是否存在 # CHECK_LOG_SERVER_DIR=${CCHECK_LOG_SERVER_DIR:-"f.ck"} [ "${CHECK_LOG_SERVER_DIR}" = "-c" ] && checkLogServerDir FTP_LOG_DIR="/tmp/ftp_err" [ -d ${FTP_LOG_DIR} ] || mkdir -p ${FTP_LOG_DIR} FTP_ERROR_LOG="${FTP_LOG_DIR}/ftp_temp_${LOG_TYPE}_err$$.log" ## # FTP自動化上傳函數(shù) # sendLog(){ SENDLOGFILE=$1 ftp -ivn ${FTPSERVER} 21 >${FTP_ERROR_LOG} << _EOF_ user ${FTPUSER} ${FTPPASSWD} passive bin lcd ${LCD_DIR} cd ${CD_DIR} put ${SENDLOGFILE} bye _EOF_ ## # 統(tǒng)計前面FTP運行輸出的錯誤日志記錄行數(shù) # LOG_COUNT=`grep -w "^226" ${FTP_ERROR_LOG}|wc -l` if [ "${LOG_COUNT}" -eq 1 ];then echoGoodLog "Send: ${SENDLOGFILE} to ftp_server was successfully." TEMP_SEND_STATUS=0 return 0 else echoBadLog "Send: ${SENDLOGFILE} more than $x time." TEMP_SEND_STATUS=1 sleep ${SEND_WHILE_SLEEP_TIME} return 1 fi } REUPLOADLIST="/var/log/reupload_list_${LOG_TYPEB}_${STATION}.log" TEMP_REUPLOADLIST="/var/log/temp_reupload_list_${LOG_TYPEB}_${STATION}.log" [ -f ${TEMP_REUPLOADLIST} ] && rm ${TEMP_REUPLOADLIST} runSendLog(){ SENDLOGNAME=$1 x=1;i=1 until [ "$i" -eq 0 ];do [ "$x" -gt "${SEND_MAX_TIME}" ] && { echoBadLog "Send: ${SENDLOGNAME} to ftp_server was failed, Please check..." echo "${LCD_DIR};;${CD_DIR};;${SENDLOGFILE}" >> ${TEMP_REUPLOADLIST} break } sendLog "${SENDLOGNAME}" i=`echo $?` x=`expr $x + 1` done } [ "${T}" -eq 0 -a -f "${LCD_DIR}/${PUT_LOG_DAY_NAME}" -a "${DAY_LOG_TAR_NUM}" -eq 0 ] && runSendLog ${PUT_LOG_DAY_NAME} [ -f "${LCD_DIR}/${PUT_LOG_HOUR_NAME}" ] && runSendLog ${PUT_LOG_HOUR_NAME} ## # 把上面兩種發(fā)送成功的記錄更新到這個臨時文件 # TEMP_SENDSUCCESFILE="/var/log/temp_send_succes_${LOG_TYPEB}_${STATION}.txt" SENDSUCCESFILE="/var/log/send_succes_${LOG_TYPEB}_${STATION}.txt" [ "${TEMP_SEND_STATUS}" -eq 0 ] && echo "${LCD_DIR};;${CD_DIR};;${SENDLOGFILE};;$(date +%s)" > ${TEMP_SENDSUCCESFILE} [ -e ${SENDSUCCESFILE} ] && { LCD_DIR=`cat ${SENDSUCCESFILE}|awk -F";;" '{print $1}'` CD_DIR=`cat ${SENDSUCCESFILE}|awk -F";;" '{print $2}'` LAST_DATE_SUCCESS=`date -d @"$(cat ${SENDSUCCESFILE}|awk -F";;" '{print $4}')" +%Y-%m-%d` INTERVAL_TIME=`expr $(date +%s) - $(cat ${SENDSUCCESFILE}|awk -F";;" '{print $4}')` LAST_DAY=`echo $(date -d @"$(cat ${SENDSUCCESFILE}|awk -F";;" '{print $4}')" +%d)|sed 's/^0//g'` NOW_DAY=`echo $(date +%d)|sed 's/^0//g'` [ "${LAST_DAY}" -ne "${NOW_DAY}" -a "${INTERVAL_TIME}" -gt 7000 ] && { if [ "${IS_TAR}" = "tar" ];then cd ${LCD_DIR} && { TEMP_SEND_FILES="${LOG_TYPE}${LAST_DATE_SUCCESS}.tar.gz" [ -e ${TEMP_SEND_FILES} ] || tarLogFile ${TEMP_SEND_FILES} ${LOG_TYPE}${LAST_DATE_SUCCESS}.txt } else TEMP_SEND_FILES="${LOG_TYPE}${LAST_DATE_SUCCESS}.txt" fi runSendLog ${TEMP_SEND_FILES} } rm ${SENDSUCCESFILE} } [ -f ${TEMP_SENDSUCCESFILE} ] && mv ${TEMP_SENDSUCCESFILE} ${SENDSUCCESFILE} ## # 重傳上次發(fā)送失敗的文件 #+ 說明:重新上傳函數(shù)的執(zhí)行段,最好是放在后面,因為它依賴前面的FTP發(fā)送函數(shù)生成的TEMP_REUPLOADLIST清單 # reUploadFile(){ TEMP_NEED_DO_FILE=$1 REUPLOADLIST_NUM=`cat ${TEMP_NEED_DO_FILE}|wc -l` [ "${REUPLOADLIST_NUM}" -ge 1 ] && { while read line do LCD_DIR=`echo ${line}|awk -F";;" '{print $1}'` CD_DIR=`echo ${line}|awk -F";;" '{print $2}'` REUPLOADFILENAME=`echo ${line}|awk -F";;" '{print $3}'` [ -f "${LCD_DIR}/${REUPLOADFILENAME}" ] && runSendLog ${REUPLOADFILENAME} done < ${TEMP_NEED_DO_FILE} } [ -e ${TEMP_NEED_DO_FILE} ] && rm ${TEMP_NEED_DO_FILE} } [ -s ${REUPLOADLIST} ] && reUploadFile ${REUPLOADLIST} [ -f ${TEMP_REUPLOADLIST} ] && mv ${TEMP_REUPLOADLIST} ${REUPLOADLIST} [ -f ${FTP_ERROR_LOG} ] && rm ${FTP_ERROR_LOG} ## # 清理超過90天的備份日志 # [ -d ${LCD_DIR} ] && cd ${LCD_DIR} && { for FILENAME in `find . -type f -ctime +"${BACK_SAVE_MAX_DAY}" | awk -F/ '{print $2}'` do rm ${FILENAME} && echoGoodLog "Clear: ${LCD_DIR}/${FILENAME}..." done } cleanRunLog ${RUN_LOG}
--------------------
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞名稱:shell腳本:日志切割與上傳-創(chuàng)新互聯(lián)
標題URL:http://jinyejixie.com/article4/coehie.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、用戶體驗、軟件開發(fā)、網(wǎng)站內(nèi)鏈、建站公司、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容