zoukankan      html  css  js  c++  java
  • shell脚本选择LOG里面特定的行,生成新文件并rsync上传

    rsync.sh

    #!/bin/bash
    
    tool_path=$(cd `dirname $0`; pwd)
    eval `cat ${tool_path}/conf.properties`
    
    rsync_include=${tool_path}/include/rsync_include.lst
    
    export RSYNC_PASSWORD=密码
    
    echo `date '+%Y-%m-%d %T'` >> ${tool_path}/rsync.log
    for((i=0;i<3;i++));
    do
       dstDate=`date -d "-$i day" +"%Y%m%d"`
       localPath=${tool_path}"/tlog/"$dstDate"/" 
      echo $localPath
       if [[ -d "$localPath" ]] ; then
          startTime=`date -d "-$i day" +"%s"`
          echo "start rsync file for $localPath ---------------------" >> ${tool_path}/rsync.log
          rsync -rvzPL --append --port 873 --include-from=${rsync_include} ${localPath} gameserverlog@rsync2.com::game_server_log_rsync/${game_id}/$dstDate
          endTime=`date -d "-$i day" +"%s"`
          echo "finish rsync file for $localPath, used time: $[((endTime - startTime))] seconds --------------------" >> ${tool_path}/rsync.log
         fi
    done

    killrsync.sh

    #!/bin/bash
    tool_path=$(cd `dirname $0`; pwd)
    eval `cat ${tool_path}/conf.properties`
    
    source ~/.bash_profile 
    SYSTEM_TIME=`date '+%Y-%m-%d %T'`
    
    pid=`ps -e -o 'pid,args' |grep ${tool_path}/include|awk '{print $1}'`
    echo "$SYSTEM_TIME kill rsync $pid" >> ${tool_path}/rsync.log
    
    ps -e -o 'pid,args' |grep ${tool_path}/include|grep -v grep|awk '{print $1}'|xargs kill -9

    配置文件conf.properties

    log_root=/data/log
    file_name_prefix=aa
    file_name_suffix=.log
    game_id=games2

    start.sh

    #!/bin/bash
    source /etc/profile
    
    tool_path=$(cd `dirname $0`; pwd)
    eval `cat ${tool_path}/conf.properties`
    echo ${tool_path}
    echo ${log_root} 
    
    ###
    #yyyy-mm-dd
    date_today=`date +"%Y-%m-%d"`
    date_yesterday=`date -d "yesterday" +%Y-%m-%d`
    
    #yyyymmdd
    date_today_hadoop=`date +"%Y%m%d"`
    date_yesterday_hadoop=`date -d "yesterday" +%Y%m%d`
    
    #yyyymd
    file_today=${date_today//-0/-}
    file_yesterday=${date_yesterday//-0/-}
    
    
    ### kill rsync
    ${tool_path}/killrsync.sh
    
    ###
    destFile_yesterday="${tool_path}/tlog/${date_yesterday_hadoop}"  
    if [[ ! -d ${destFile_yesterday} ]]; then
        mkdir -p ${destFile_yesterday}  
    fi
    
    destFile_today="${tool_path}/tlog/${date_today_hadoop}"  
    if [[ ! -d ${destFile_today} ]]; then
        mkdir -p ${destFile_today}  
    fi
    
    ###
    cd ${log_root} 
    list_alldir(){  
        for file2 in `ls -a $1`  
        do  
            if [ x"$file2" != x"." -a x"$file2" != x".." ]; then  
                if [ -d "$1/$file2" ];then  
                    echo "$1/$file2"  
                    cd $1/${file2}
                    folder_name=${1//${log_root}/}
                    cat $1/${file2}/*.${file_yesterday} | grep '20..-.*LOG:.*|.*|.*|.*' | awk '{$1="";$2="";$3="";$4="";sub("    LOG:", "");print}' > ${destFile_yesterday}/tlog_${date_yesterday_hadoop}_${folder_name////_}_${file2}.log
                    cat $1/${file2}/*.${file_today}     | grep '20..-.*LOG:.*|.*|.*|.*' | awk '{$1="";$2="";$3="";$4="";sub("    LOG:", "");print}' > ${destFile_today}/tlog_${date_today_hadoop}_${folder_name////_}_${file2}.log
    
                    list_alldir "$1/${file2}"  
                fi  
            fi  
        done  
    }  
      
    list_alldir ${log_root}  
    
    
    cd ${destFile_yesterday}
    find . -name "*" -type f -size 0c | xargs -n 1 rm -f
    cd ${destFile_today}
    find . -name "*" -type f -size 0c | xargs -n 1 rm -f
    
    
    ### start rsync
    flock -xn ${tool_path}/rsync.lock -c "${tool_path}/rsync.sh"

    创建软连接

    #!/bin/bash
    source /etc/profile
    cd $(dirname $0)
    tool_path=`pwd`
    eval `cat ${tool_path}/conf.properties`
    root="$log_root"
    for file in $root/*;
    do
       fileName=${file##*/}
       datestr=`echo ${fileName} |cut -d '_' -f2`
       destFile="${tool_path}/tlog/${datestr}"  
       if [[ ! -d ${destFile} ]]; then
          mkdir -p ${destFile}  
       fi
       
       if [[ ! -L "${destFile}/${fileName}" ]]; then
         ln -s ${file} ${destFile}   
       fi
    done
  • 相关阅读:
    8.25 欢乐emmm赛
    树专练
    字符串知识点大集合
    8.12 小组解题
    暑假大联欢 happynk 2019.8.11
    游记-多省联考 2019
    图论-匈牙利算法模板
    数论-哈哈哈好快乐
    数论-线性基
    其他-私人♂收藏(比赛记录 Mar, 2019)
  • 原文地址:https://www.cnblogs.com/linn/p/7890434.html
Copyright © 2011-2022 走看看