logrotate – linux 系统集成的日志转储压缩工具

全局配置文件: /etc/logrotate.conf ,单独系统配置文件放置在/etc/logrotate.d/目录下

参数说明

参数            功能说明
compress            通过gzip 压缩转储以后的日志
nocompress          不需要压缩时,用这个参数

copytruncate        用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空
                   ,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate      备份日志文件但是不截断

create mode owner group  转储文件,使用指定的文件模式创建新的日志文件。轮转时指定创
                          建新文件的属性,如create 0600 root root
nocreate            不建立新的日志文件

delaycompress       和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress     覆盖 delaycompress 选项,转储同时压缩

ifempty             即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty          如果日志文件为空时,不转储

errors address      转储时的错误信息发送到指定的Email 地址
mail address        转储的日志文件发送到指定的E-mail 地址
nomail              转储时不发送日志文件

olddir directory    转储后的日志文件放入指定的目录,必须和当前日志文件在同一文件系统
noolddir            转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;
                     这两个关键字必须单独成行;
postrotate/endscript 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 
                     某个服务!必须独立成行;

daily              指定转储周期为每天
weekly             指定转储周期为每周
monthly            指定转储周期为每月

rotate count       指定日志文件删除之前转储的个数,0 指没有备份,5 指保留5个备份
tabootext [+] list 让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, 
                   .rpmsave, v, 和 ~

size               size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)
                   以及KB (sizek)或者MB (sizem).
missingok          如果日志丢失,不报错继续滚动下一个日志
sharedscripts      运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。
                   如果没有配置这个,那么每个日志轮转后都会执行一次脚本

dateext           使用当期日期作为命名格式
dateformat .%s    配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配
                  合dateext使用,只支持 %Y %m %d %s 这四个参数 自己测试了%H也可以。
                  不过如果需要支持分的话logrotate需要升级到3.9版本 ,

测试配置结果:

#debug测试 -d, --debug   Don't do anything, just test (implies -v)
logrotate -d configfile

示例

1、列出多个文件,统一配置策略

#Please note:
#nocreate and copytruncate in configuration file are mutually exclusive and can't be configured at the same time,otherwise nocreate have no effect
#create and copytruncate in configuration file are mutually exclusive and can't be configured at the same time,otherwisecreate have no effect
/var/log/startup.log
/var/log/boot.log
/var/log/backup_conf.log
/var/log/cloud-init-output.log
/var/log/cloud-init.log
/var/log/daemon.log
/var/log/cron.log
/var/log/kern.log
/var/log/syslog
/var/log/unused.log
/var/log/sysmonitor.log
/var/log/tuned/*.log
/var/log/wtmp
/var/log/btmp
{
	missingok
	compress
	notifempty
	copytruncate
	rotate 10
	size +4000k
}

2、多个同目录下多个log 文件

/var/log/openvswitch/*.log {
	missingok
	notifempty
	size 2048k
	rotate 10
	copytruncate
}

3

# Logrotate file for psacct RPM

/var/account/pacct {
    compress
    delaycompress
    notifempty
    daily
    rotate 31
    create 0600 root root
    postrotate
       if /usr/bin/systemctl --quiet is-active psacct.service ; then
           /usr/sbin/accton /var/account/pacct | /usr/bin/grep -v "Turning on process accounting, file set to '/var/account/pacct'." | /usr/bin/cat
       fi
    endscript
}

4、mariadb

/var/log/mariadb/mariadb.log {
        create 600 mysql mysql
        notifempty
        daily
        rotate 3
        missingok
        compress
    postrotate
	# just if mysqld is really running
        if [ -e /run/mariadb/mariadb.pid ]
        then
           kill -1 $(</run/mariadb/mariadb.pid)
        fi
    endscript
}

Leave a Reply

Your email address will not be published. Required fields are marked *