日志按大小进行分割

#!/bin/bash
## 按文件大小切割日志文件
## 注:实际测试中发现,对log文件进行切割后,rsyslogd短时间内会继续往mv后的文件写日志

## 文件日志所在的目录
LOG_PATH=/opt/logs/router-log

## 日志文件超过多少字节后,进行切割,最多进行100份切割,超过后,按时间为后缀
## 100M : 104857600
## 500M : 524288000
## 1G   : 1073741824

LOG_MAX_SIZE=5240

currentDate1=`date "+%Y%m%d_%H%M"`
currentLogDate=`date "+%Y-%m-%d"`

echo "start log_rotate.sh $currentDate1"
echo "LOG_PATH:$LOG_PATH"
echo "LOG_MAX_SIZE:$LOG_MAX_SIZE"

sendHUP=0

for name in $LOG_PATH/*
do
        if [ -f "$name" ]; then
                if [ "${name##*.}" == "log" ]; then
                        filesize=$(stat -c '%s' $name)
                        if [[ $filesize -ge $LOG_MAX_SIZE ]]; then
                        rotating=0

                        for((i=1;i<=100;i++))
                        do
                                if [ ! -f "$name.$i" ]; then
                                        echo "mv $name $name.$i"
                                        mv $name $name.$i
                                        rotating=1
                                        sendHUP=1
                                        break
                                fi


                        done

                        if [ $rotating -eq 0 ]; then
                                currentDate=`date "+%Y%m%d_%H%M"`
                                mv $name $name.$currentDate
                                sendHUP=1
                        fi
                        fi
                fi

        fi

        if [ -d "$name" ]; then

                if [ -d "$name/$currentLogDate" ]; then

                        for name2 in $name/$currentLogDate/*
                        do
                                if [ -f "$name2" ]; then
                                        if [ "${name2##*.}" == "log" ]; then
                                        filesize=$(stat -c '%s' $name2)
                                        if [[ $filesize -ge $LOG_MAX_SIZE ]]; then
                                                rotating=0

                                                for((i=1;i<=100;i++))
                                                do
                                                        if [ ! -f "$name2.$i" ]; then
                                                                echo "mv $name2 $name2.$i"
                                                                mv $name2 $name2.$i
                                                                rotating=1
                                                                sendHUP=1
                                                                break
                                                        fi


                                                done

                                                if [ $rotating -eq 0 ]; then
                                                        currentDate=`date "+%Y%m%d_%H%M"`
                                                        mv $name2 $name2.$currentDate
                                                        sendHUP=1
                                                fi
                                        fi
                                        fi
                                fi
                        done
                fi

        fi
done

if [ $sendHUP -eq 1 ]; then
        echo "send HUP to rsyslogd.pid:$(cat /var/run/syslogd.pid)"
        kill -HUP $(cat /var/run/syslogd.pid)
else
        echo "not send HUP"
fi
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048