日志文件删除shell脚本

大日志文件切割shell脚本

#!/bin/bash
# ---------------------------------------------------------------------
# 日志文件300M分割,并zip压缩,每个项目仅保留最新30个文件,
# 文件格式:项目名称-年月日时分秒.zip
# Author   : 
# Date     : 
# Email    : 
# Version  : 1.0
# ---------------------------------------------------------------------

# 日志目录
LOG_DIR=‘/log‘
# 日志文件名称,支持正则表达式,多个使用“,”分隔
FILE_NAME=‘*.log‘
# 文件切割最大大小,单位MB
MAX_FILE_SIZE=300
# 保留文件数
MAX_FILE_NUM=30
# 压缩包后缀
FILE_SUFFIX=zip
# 提示内容
MESSAGE="Usage: $0 LOG_DIR FILE_NAME MAX_FILE_SIZE MAX_FILE_NUM \nFor Example: $0 /log/ nginx.log 100 30"

while [ $# -ge 2 ]
do
    case "$1" in
        -d) LOG_DIR=$2; shift 2;;
        -f) FILE_NAME=$2; shift 2;;
        -s) MAX_FILE_SIZE=$2; shift 2;;
        -n) MAX_FILE_NUM=$2; shift 2;;
        -t) FILE_SUFFIX=$2; shift 2;;
         *) echo "Unknown parameter $1." ; shift 2; continue;;
    esac
done

if [ -z "${LOG_DIR}" -o ! -d "${LOG_DIR}" ]; then
    echo "请输入文件所在目录"
    echo -e ${MESSAGE}
    exit 1
fi

LOG_FILE_SIZE=$(ls ${LOG_DIR}‘/‘${FILE_NAME} | wc -l)
if [ "${LOG_FILE_SIZE}" -eq 0 ]; then
    echo "请输入文件名"
    echo -e ${MESSAGE}
    exit 1
fi

if [ -z "${MAX_FILE_SIZE}" ]; then
    echo "请输入文件以多少MB分割"
    echo -e ${MESSAGE}
    exit 1
fi

if [ -z "${MAX_FILE_NUM}" ]; then
    echo "请输入保留多少个压缩文件"
    echo -e ${MESSAGE}
    exit 1
fi

# -------------------------------------------------
# rolling log file
rolling_file () {
    CUR_TIME=$(date +%Y%m%d%H%M%S)
    LOG_FILE_LIST=($(ls ${LOG_DIR}/${FILE_NAME}))
    cd ${LOG_DIR}

    for LOG_FILE in ${LOG_FILE_LIST[@]}
    do
        BACK_FILE_NAME="${LOG_FILE}-${CUR_TIME}"

        # check file data size
        LOG_FILE_SIZE=$(stat -c %s ${LOG_FILE})
        if [ ${LOG_FILE_SIZE} -gt $((MAX_FILE_SIZE * 1024 * 1024)) ]; then
            cp -f ${LOG_FILE} ${BACK_FILE_NAME}
            echo "" > ${LOG_FILE}
            zip -9q ${BACK_FILE_NAME}.zip ${BACK_FILE_NAME}
            rm -f ${BACK_FILE_NAME}
            echo -e "Backup success. file: ${BACK_FILE_NAME}.zip"
        fi
    done
}

# -------------------------------------------------
# delete expire archive files
delete_expire_bak_file () {
    LOG_FILE_LIST=($(ls ${LOG_DIR}/${FILE_NAME}))
    for LOG_FILE in ${LOG_FILE_LIST[@]}
    do
        FILE_NAME=$(basename ${LOG_FILE})
        CUR_FILE_NUM=$(ls ${LOG_DIR}/${FILE_NAME}*.${FILE_SUFFIX} | wc -l)

        if [ ${CUR_FILE_NUM} -gt ${MAX_FILE_NUM} ]; then
            DEL_FILE_NUM=$((${CUR_FILE_NUM} - ${MAX_FILE_NUM}))
            DEL_FILE_NAME=$(
                stat -c %Y,%n ${FILE_NAME}*.${FILE_SUFFIX} | sort -k 1 -t, -n |                 head -n ${DEL_FILE_NUM} | awk -F "," ‘{print $2}‘
            )
            echo "Delete backup file ${DEL_FILE_NUM} file: \n${DEL_FILE_NAME}"
            rm -f ${DEL_FILE_NAME}
        fi
    done
}

rolling_file
delete_expire_bak_file

小日志文件删除shell脚本

#!/bin/bash

# ------------------------------------------------------------------------------
# 功能:清理新版客服系统go工程运行日志,保留最近3天
# 用法:sh 此脚本 im-customer|im-company|im-static|im-chat-client|im-dbs|im-auto-order-sentinel|consul
# 作者:
# 创建日期:
# ------------------------------------------------------------------------------

# 工程代码
project_code=(${*})

# 保留天数
keep_time=‘+3‘

cs_log_rolling () {
    for i in ${!project_code[@]}
    do
        if [ "${project_code[${i}]}" != "consul" ]; then

            cs_log_dir=/data/www/cs/${project_code[${i}]}/logs

            find ${cs_log_dir}/ -mtime ${keep_time} -name "${project_code[${i}]}-*.log.gz" -exec rm -f {} \;  > /dev/null 2>&1

            echo "${project_code[${i}]} log cleanup succeeded !"

            sleep 1
        fi
    done
}

consul_log_rolling () {
    if [[ "${project_code[@]}" =~ consul ]]; then

        consul_log_dir=/data/log/consul-log

        find ${consul_log_dir}/ -mtime ${keep_time} -name "consul-*.log" -exec rm -f {} \;  > /dev/null 2>&1

        echo "consul log cleanup succeeded !"
    fi
}

cs_log_rolling
consul_log_rolling

日志文件删除shell脚本

上一篇:Python 学习笔记之—— PIL 库


下一篇:IM平台即时聊天功能及服务介绍