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
  • 相关阅读:
    NavigationBar隐藏
    (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
    Makefile 中:= ?= += =的差别 和条件运行
    C# 使用WinRar命令压缩和解压缩
    C# 字段、属性、成员变量
    js中推断对象详细类型
    Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式
    JavaScript类数组对象参考
    Codeforces Round 190 div.2 322C 321A Ciel and Robot
    Android Application plugin
  • 原文地址:https://www.cnblogs.com/linn/p/7890434.html
Copyright © 2011-2022 走看看