logrotate 日志分割

logrotate可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,可以设置logrotate,让/var/log/foo日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预,旧日志也可以通过电子邮件发送

1. 配置

# install logrotate cron  # ubuntu
# install logrotate crontabs # RHEL
# cat /etc/logrotate.d/myserver

# myserver
/var/log/myserver {
  notifempty
  missingok
  monthly
  dateext
  rotate 7
  size=100M
  sharedscripts
  delaycompress
  create 644 root root
  postrotate
    [ ! -f /var/run/myserver.pid ] || kill -USR2 `cat /var/run/myserver.pid`
    #  /usr/bin/killall -HUP rsyslogd
  endscript
}

# for syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

# for zabbix-agent
/var/log/zabbix/zabbix_agentd.log {
    weekly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 0664 zabbix zabbix
}

# for nginx
/var/log/nginx/*log {
    create 0644 nginx nginx
    daily
    rotate 7
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

# for influxdb
/var/log/influxdb/influxd.log {
    daily
    rotate 7
    missingok
    dateext
    copytruncate
    compress
}

2. 参数

完整参数参考  man logrotate
compress             通过gzip 压缩转储以后的日志
nocompress           不压缩
copytruncate         把当前日志备份并截断,先拷贝原日志文件再清空,由于拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate       备份日志文件但是不截断 
create mode owner group    转储文件,使用指定的文件模式创建新的日志文件
nocreate             不建立新的日志文件
delaycompress        和compress一起使用,转储的日志文件到下一次转储时才压缩,仍然需要读取最新归档时很有用
nodelaycompress      覆盖 delaycompress 选项,转储同时压缩。
errors address       专储时的错误信息发送到指定的Email 地址
missingok:           在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
ifempty              即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty           如果是空文件的话,不转储
mail address         把转储的日志文件发送到指定的E-mail 地址
nomail               转储时不发送日志文件
olddir directory     转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir             转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript  在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily                指定转储周期为每天
weekly               指定转储周期为每周
monthly              指定转储周期为每月
rotate count         指定日志文件删除之前转储的次数,0指没有备份,5指保留5个备份,对于第六个归档,时间最久的归档将被删除
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size               当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB(sizek)或者MB (sizem)
notifempty: 如果日志文件为空,轮循不会进行
create 644 root root: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件
postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行
mail address :      把转储的日志文件发送到指定的 E-mail 地址
sharedscripts        postrotate只会运行一次,在旧日志被压缩一次后
dateext              使用当期日期作为命名格式,如果指定的rotate大于1,默认rotate之后的文件名是:xx.log.1

3. 运行

/usr/sbin/logrotate -vf /etc/logrotate.conf

logrotate命令格式解释:
logrotate [OPTION...] < configfile >
-d, --debug :debug模式,测试配置文件是否有错误,并不会真正进行 rotate 或者 compress 操作
-v, --verbose :显示转储过程
-f, --force :强制转储文件
-m, --mail=command :发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。  logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file

4. Logrotate定时任务

logrotate需要的cron任务应该在安装时就自动创建了

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

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1   5   cron.daily      nice run-parts /etc/cron.daily
7   25  cron.weekly     nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly        nice run-parts /etc/cron.monthly

# 使用 anacrontab 轮转的配置文件,日志切割的生效时间是在凌晨 3 点到 22 点之间,而且随机延迟时间是 45 分钟,随机难以控制
mv /etc/anacrontab /etc/anacrontab.bak 

# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
59 23 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

# 或自定义计划任务
*/30 * * * * /usr/sbin/logrotate /etc/logrotate.d/rsyslog > /dev/null 2>&1 &
  • PS

Linux日志文件总管——logrotate
Linux日志切割神器logrotate原理介绍和配置详解
Linux自带神器logrotate详解

原文地址:https://www.cnblogs.com/firewalld/p/15353998.html