因为公司需要一个处理日志并迁移到指定服务器的脚本,所以就写了以下这个,虽然比较简陋,但还是可以使用的…

思路:

从指定的目录数组中(DIR_ARRAY) 查找这些目录下,所有以SUFFIX_ARRAY数组后缀,并且不是IGNORE_ARRAY中的文件名的所有文件并且进行每100MB进行切割和压缩.然后将切割和压缩的文件移动到SAVE_DIR目录里。

最后,将SAVE_DIR目录里的所有文件,同步到指定的LOG_SERVER上,成功后再删除这些文件.

#! /usr/bin/env bash

#set -x

NOW="$(date +'%Y-%m-%d_%H-%M-%S')"

DELETE_FILE="/tmp/delete.file.list"

DIR_ARRAY=('/home/your-name/logs')

EX_FILE_NGINX_GREP="access.log"
EX_FILE_TOMCAT_GREP="catalina.out"
EX_FILE_PATTERN="-iname ${EX_FILE_NGINX_GREP} -o -iname ${EX_FILE_TOMCAT_GREP}"

MOVE_FILE_NAME="/tmp/move.file.list"

IN_FILE_PATTERN=" -mtime +0 -iname *.log -o  -mtime +0  -iname *.txt"

SAVE_DIR="/home/your-name/save-log"

LOG_SERVER_USER="your-user-name"
LOG_SERVER="your-ip"
LOG_SERVER_SSH_PORT="your-port"
LOG_SERVER_SAVE_DIR="/home/${LOG_SERVER_USER}/all/logs"

# 创建保存的目录
function mkdirSaveLog(){
    mkdir -p ${SAVE_DIR}
    echo "created dir ${SAVE_DIR}"
}

# 清除要删除的文件列表内容
function cleanDeleteFile(){
    echo "" > ${DELETE_FILE}
    echo "" > ${MOVE_FILE_NAME}
    echo "clean delete file OK"
}

# 要处理的文件名的后缀
function getInclude(){
    echo ${IN_FILE_PATTERN}
}

# 不处理的指定文件名
function getExclcude(){
    echo ${EX_FILE_PATTERN}
}

# 开始查找文件
function findFile(){
    #set -x
    for i in ${DIR_ARRAY[@]}; do
        find ${i} $(getInclude) -type f  | grep -v "${EX_FILE_TOMCAT_GREP}\$" | grep -v "${EX_FILE_NGINX_GREP}\$" >> ${DELETE_FILE}
    done
    echo "delte file content:"
    cat ${DELETE_FILE}
    echo "find file OK."
}

# 将忽略的文件名,复制备份
function dump(){
    for i in ${DIR_ARRAY[@]}; do
#        find ${i} $(getExclcude) -type f | xargs -I {} -n 1 -P 0 bash -c "cp {} {}.${NOW}.log && echo \"\" > {}"
        find ${i} $(getExclcude) -type f | xargs -I {} -n 1 -P 0 bash -c "cp {} {}.${NOW}.log"
    done
    echo "dump file OK"
}

# 开始分割文件
function splitFile(){
    #set -x
#    cat ${DELETE_FILE} | xargs -t -n 1 -I {} -P 0 bash -c "split -d --bytes=100M --filter='gzip > \$FILE.sync.gz' {} {}.${NOW} && rm -rf {}"

    cat ${DELETE_FILE} | xargs -t -n 1 -I {} -P 0 bash -c "split -d --bytes=100M --filter='gzip > \$FILE.sync.gz' {} {}.${NOW}"
    echo "split file ok"
}

# 移动文件到指定的保存目录中
function moveFile(){
    for i in ${DIR_ARRAY[@]}; do
        find ${i} -type f \( -iname *.sync.gz \) >> ${MOVE_FILE_NAME}
    done
    echo "move file content:"
    cat ${MOVE_FILE_NAME}
    cat ${MOVE_FILE_NAME} | xargs -t -n 1 -I {} -P 0 dirname {} | sort -u | xargs -t -n 1 -I {} -P 0 mkdir -p "${SAVE_DIR}{}"
    cat ${MOVE_FILE_NAME} | xargs -t -n 1 -I {} -P 0 dirname {} | sort -u | xargs -t -n 1 -I {} -P 0 bash -c "mv {}/*.sync.gz \"${SAVE_DIR}{}/\""
    echo "move file to save dir ok"
}



# copy to server
function copyToServer(){
    cd ${SAVE_DIR}
    ls *.sync.gz |  xargs -t -n 1 -I {} -P 0 bash -c "rsync -avzh \"-e ssh -p ${LOG_SERVER_SSH_PORT}\" {}  ${LOG_SERVER_USER}@${LOG_SERVER}:${LOG_SERVER_SAVE_DIR}/{} &&  rm -rf {}"
    echo "copy to server ok"
}

# 主函数
function main(){
    mkdirSaveLog
    cleanDeleteFile
    dump
    findFile
    splitFile
    moveFile
    #copyToServer
}

# 执行
main