推荐一款日志切割神器,很好很强大( 三 )

当日志文件 >= log-size 的时候就转储 。以下为合法格式:(其他格式的单位大小写没有试过)size = 5 或 size 5 (>= 5 个字节就转储)size = 100k 或 size 100ksize = 100M 或 size 100M小示例:下面一个切割nginx日志的配置
[root@master-server ~]# vim /etc/logrotate.d/nginx/usr/local/nginx/logs/*.log {dailyrotate 7missingoknotifemptydateextsharedscriptspostrotate if [ -f /usr/local/nginx/logs/nginx.pid ]; then kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` fiendscript}分享一例曾经使用过的nginx日志切割处理脚本:
1)logrotate日志分割配置
[root@bastion-IDC ~# vim /etc/logrotate.d/nginx/data/nginx_logs/*.access_log {nocompress daily copytruncate create ifempty olddir /data/nginx_logs/days rotate 0 }2)日志分割脚本
[root@bastion-IDC ~# vim /usr/local/sbin/logrotate-nginx.sh#!/bin/bash#创建转储日志压缩存放目录mkdir -p /data/nginx_logs/days#手工对nginx日志进行切割转换/usr/sbin/logrotate -vf /etc/logrotate.d/nginx#当前时间time=$(date -d "yesterday" +"%Y-%m-%d")#进入转储日志存放目录cd /data/nginx_logs/days#对目录中的转储日志文件的文件名进行统一转换for i in $(ls ./ | grep "^\(.*\)\.[[:digit:]]$")domv ${i} ./$(echo ${i}|sed -n 's/^\(.*\)\.\([[:digit:]]\)$/\1/p')-$(echo $time)done#对转储的日志文件进行压缩存放 , 并删除原有转储的日志文件 , 只保存压缩后的日志文件 。 以节约存储空间for i in $(ls ./ | grep "^\(.*\)\-\([[:digit:]-]\+\)$")dotar jcvf ${i}.bz2 ./${i}rm -rf ./${i}done#只保留最近7天的压缩转储日志文件find /data/nginx_logs/days/* -name "*.bz2" -mtime 7 -type f -exec rm -rf {} \;3)crontab定时执行
[root@bastion-IDC ~# crontab -e#logrotate0 0 * * * /bin/bash -x /usr/local/sbin/logrotate-nginx.sh > /dev/ 2>手动执行脚本 , 测试下看看:
[root@bastion-IDC ~# /bin/bash -x /usr/local/sbin/logrotate-nginx.sh[root@bastion-IDC ~# cd /data/nginx_logs/days[root@bastion-IDC days# lshuantest.access_log-2017-01-18.bz2php脚本切割一例:
[root@huanqiu_web1 ~]# cat /etc/logrotate.d/php/Data/logs/php/*log { daily rotate 365 missingok notifempty compress dateext sharedscripts postrotate if [ -f /Data/app/php5.6.26/var/run/php-fpm.pid ]; then kill -USR1 `cat /Data/app/php5.6.26/var/run/php-fpm.pid` fi endscript postrotate /bin/chmod 644 /Data/logs/php/*gz endscript}[root@huanqiu_web1 ~]# ll /Data/app/php5.6.26/var/run/php-fpm.pid-rw-r--r-- 1 root root 4 Dec 28 17:03 /Data/app/php5.6.26/var/run/php-fpm.pid[root@huanqiu_web1 ~]# cd /Data/logs/php[root@huanqiu_web1 php]# lltotal 25676-rw-r--r-- 1 root root 0 Jun 1 2016 error.log-rw-r--r-- 1 nobody nobody 182 Aug 30 2015 error.log-20150830.gz-rw-r--r-- 1 nobody nobody 371 Sep 1 2015 error.log-20150901.gz-rw-r--r-- 1 nobody nobody 315 Sep 7 2015 error.log-20150907.gz..................nginx日志切割一例
[root@huanqiu_web1 ~]# cat /etc/logrotate.d/nginx/Data/logs/nginx/*/*log { daily rotate 365 missingok notifempty compress dateext sharedscripts postrotate /etc/init.d/nginx reload endscript}[root@huanqiu_web1 ~]# ll /Data/logs/nginx/www.huanqiu.com/..........-rw-r--r-- 1 root root 1652 Jan 1 00:00 error.log-20170101.gz-rw-r--r-- 1 root root 1289 Jan 2 00:00 error.log-20170102.gz-rw-r--r-- 1 root root 1633 Jan 3 00:00 error.log-20170103.gz-rw-r--r-- 1 root root 3239 Jan 4 00:00 error.log-20170104.gz系统日志切割一例
[root@huanqiu_web1 ~]# cat /etc/logrotate.d/syslog/var/log/cron/var/log/maillog/var/log/messages/var/log/secure/var/log/spooler