logrotate使用

时间:2022-07-22
本文章向大家介绍logrotate使用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

logrotate介绍

日志文件包含了关于系统或者程序的日志信息,在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,所以如何清理日志文件是一个问题。 logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。配置完后,logrotate的运作完全自动化,它会结合crontab来每天执行一次,不必进行任何进一步的人为干预

$ cat /etc/cron.daily/logrotate 
#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo ""$logfile" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

使用介绍

logrotate在系统中一般是默认就有安装的,默认配置文件: /etc/logrotate.conf,我们可以通过在logrotate.conf中指定include配置,例如:

include /etc/logrotate.d

我们可以在上面的目录中,配置多个文件,来达到对多个日志文件,不同类型日志文件进行管理的效果,例如:

$ tree /etc/logrotate.d/
/etc/logrotate.d/
├── apport
├── apt
├── dpkg
├── lxd
├── nginx
├── record_nohup
├── rsyslog
├── tcpdump
├── ufw
├── unattended-upgrades
└── zabbix-agent

配置介绍

下面看一个示例

$ cat tcpdump 
/data/tcpdump/*.pcap  /var/log/nginx/*.log {     #可以指定多个日志文件,空格隔开
    su root 
    copytruncate
    daily
    rotate 2
    compress
    dateext
    dateformat .%Y%m%d
    delaycompress
    missingok
    notifempty
    create 777 root root
    postrotate
    endscript
}

这里是对/data/tcpdump目录下的pcap结尾的文件进行管理,如下:

下面看下各个参数的介绍

su root 新建日志文件指定用户和用户组
dateext 日志文件切割时添加日期后缀
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件   create 644 root root
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份   rotate 5
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).   size 20M 
sharedscripts  所有的文件切割之后只执行一次下面脚本
postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi

测试执行

debug模式执行,只是测试,并不会真正执行

logrotate -d -vf /etc/logrotate.d/tcpdump

由于logratote是结合crontab来执行的,所以我们可以自定义crontab的内容,示例:

$ cat /etc/cron.daily/logrotate
PATH=/sbin:/usr/sbin:/bin:/usr/bin

00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx >/dev/null 2>&1
00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat >/dev/null 2>&1
00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tcpdump >/dev/null 2>&1

重启下crontab即可,这样可以分别对不同的日志做不同的管理策略。