应用日志清理脚本(通用)
#!/bin/bash #================================================================ # HEADER #================================================================ # Filename clear-logs.sh # Revision 0.0.3 # Date 2023/11/06 # #================================================================ #%名称(NAME) #% ${SCRIPT_NAME} - 删除 N 天前的日志文件或者单文件size大于 M GB #% #%概要(SYNOPSIS) #% sh ${SCRIPT_NAME} [options] <value> ... #% #%描述(DESCRIPTION) #% 删除 N 天前的日志文件 or 单文件size大于 M GB,仅删除匹配 "*.log*" 的日志文件 #% #%选项(OPTIONS) #% -p <value> 删除日志的路径, #% -d <value> 删除 N 天前的日志文件,即保留 N 天日志,默认:7 #% -s <value> 删除单文件size大于 M 日志,默认:10G #% -u <value> 删除磁盘大于u%,清理业务日志,默认:80 #% --help 帮助信息 #% -v, --version 版本信息 #% #%示例(EXAMPLES) #% #% 1. 清理 7 天前的日志文件,或者单文件size大于10GB #% sh ${SCRIPT_NAME} -p ${MARINES_HOME}/application/logs/ #% sh ${SCRIPT_NAME} -p ${MARINES_HOME}/application/logs/ -d 7 -s 10G #% #% 2. 清理系统 30 天前的日志文件,或者单文件size大于20GB #% sh ${SCRIPT_NAME} -p /var/log/ -d 30 -s 20G #% #% 3. 清理 7 天前的日志文件,或者单文件size大于1GB,当删除磁盘大于50%,清理业务日志 #% sh ${SCRIPT_NAME} -p ${MARINES_HOME}/application/logs/ -d 7 -s 1G -u 50 #% #================================================================ # END_OF_HEADER #================================================================ # header 总行数 SCRIPT_HEADSIZE=$(head -200 "${0}" |grep -n "^# END_OF_HEADER" | cut -f1 -d:) # 脚本名称 SCRIPT_NAME="$(basename "${0}")" # 版本 VERSION="0.0.1" # 默认保留 7 天 DAYS=7 # 脚本执行日志目录 CLEAR_LOGS_LOG_PATH=/tmp/$(whoami)-clear-logs # 日志文件 LOGFILE=${CLEAR_LOGS_LOG_PATH}/${SCRIPT_NAME}-$(date +%Y%m).log # usage function usage() { head -"${SCRIPT_HEADSIZE:-99}" "${0}" \ | grep -e "^#%" \ | sed -e "s/^#%//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g" -e "s/\${VERSION}/${VERSION}/g" } # 初始化创建脚本日志目录 function init() { # 目录不存在,则创建 if [ ! -d "${CLEAR_LOGS_LOG_PATH}" ] then mkdir -p "${CLEAR_LOGS_LOG_PATH}" fi # 日志文件不存在,则创建 if [ ! -f "${LOGFILE}" ] then touch "${LOGFILE}" fi } # 记录 INFO log function infoLog() { echo "$(date "+%Y-%m-%d %H:%M:%S") [ INFO ] ${1}" >> "${LOGFILE}" } # 记录 WARNING log function warningLog() { echo -e "$(date "+%Y-%m-%d %H:%M:%S") \033[33m[ WARNING ]\033[0m ${1}" >> "${LOGFILE}" } # 记录 START log function startLog() { echo -e "$(date "+%Y-%m-%d %H:%M:%S") \033[35m[ START ]\033[0m ${1}" >> "${LOGFILE}" } # 记录 end log function endLog() { echo -e "$(date "+%Y-%m-%d %H:%M:%S") \033[35m[ END ]\033[0m ${1}" >> "${LOGFILE}" } # 记录 error log function errorLog() { echo -e "$(date "+%Y-%m-%d %H:%M:%S") \033[31m[ ERROR ]\033[0m ${1}" >> "${LOGFILE}" } # 磁盘大于u%,清理业务日志 disk_usages() { # 指定要检查的文件夹路径 folder_path="$1" [ -z ${USAGE} ] && USAGE=80 # 获取挂载点`/`的磁盘使用率 disk_usage=$(df -h | grep "/$" | awk '{print $5}' | cut -d'%' -f1) # 检查磁盘使用率是否超过u% if [ $disk_usage -gt ${USAGE} ]; then errorLog "Disk usage is above ${USAGE}%. Deleting files in [${folder_path}]" # 删除指定文件夹中的文件 find $folder_path -type f -mtime +1 -name "*.log*" -delete warningLog "The day before Log Files deleted." disk_usage=$(df -h | grep "/$" | awk '{print $5}' | cut -d'%' -f1) if [ $disk_usage -gt ${USAGE} ]; then find $folder_path -type f -name "*.log*" -delete warningLog "All Log Files deleted." fi else infoLog "Disk usage is below ${USAGE}%. No action needed." fi } # 清理 log function clearLogs() { [ -z "${LOGS_PATH}" ] && LOGS_PATH=${marines_home}application/logs/ startLog "====>> Start cleaning up log files: [${LOGS_PATH}]" cd "${LOGS_PATH}" || exit # 查询要清理的文件(*.log*) clear_log_old_files=$(find "${LOGS_PATH}" -type f -mtime +${DAYS} -name "*.log*") clear_log_big_files=$(find "${LOGS_PATH}" -type f -size +${SIZES} -name "*.log*") # 没有找到匹配过期文件,记录日志退出 if [ -z "${clear_log_old_files}" ] then warningLog "In the [${LOGS_PATH}] directory, the log [${DAYS}] days ago was not found!" else infoLog "-- Clean up expire log file: [${clear_log_old_files}]" # 删除 find "${LOGS_PATH}" -type f -mtime +${DAYS} -name "*.log*" -exec rm -rf {} \; fi # 没有找到匹配大文件,记录日志退出 if [ -z "${clear_log_big_files}" ] then warningLog "In the [${LOGS_PATH}] directory, the log [${SIZES}] was not found!" else infoLog "-- Clean up big log file: [${clear_log_big_files}]" # 删除 find "${LOGS_PATH}" -type f -size +${SIZES} -name "*.log*" -exec truncate -s 0 {} \; fi # 磁盘大于u%,清理业务日志 disk_usages ${LOGS_PATH} endLog "====>> End cleaning up log files: [${LOGS_PATH}]" } # 主方法 function main() { init # 参数必输校验 if [ -z "${LOGS_PATH}" ] then printf "Parameter [-p] is required!\n" exit 1 fi # 目录合法性校验 if [ ! -d "${LOGS_PATH}" ] then printf "[%s] is not a directory!\n" "${LOGS_PATH}" exit 1 fi # 数字合法性校验,且必须大于 0,即至少保留1天 if ! [ "${DAYS}" -gt 0 ] 2>/dev/null then printf "Parameter [-d] must be a number greater than 0!\n" exit 1 fi clearLogs } # 判断参数个数 if [ $# -eq 0 ]; then usage exit 1 fi # getopt 命令行参数 if ! ARGS=$(getopt -o vd:s:p:u: --long help,version -n "${SCRIPT_NAME}" -- "$@") then # 无效选项,则退出 exit 1 fi # 命令行参数格式化 eval set -- "${ARGS}" while [ -n "$1" ] do case "$1" in -p) LOGS_PATH=$2 shift 2 ;; -d) DAYS=$2 shift 2 ;; -s) SIZES=$2 shift 2 ;; -u) USAGE=$2 shift 2 ;; -v|--version) printf "%s version %s\n" "${SCRIPT_NAME}" "${VERSION}" exit 1 ;; --help) usage exit 1 ;; --) shift break ;; *) printf "%s is not an option!" "$1" exit 1 ;; esac done main
标签:shell,LOGS,SCRIPT,清理,#%,PATH,日志,log From: https://www.cnblogs.com/user-sunli/p/18053832