使用 Linux logrotate 命令轮换和归档日志

使用此 Linux 命令保持日志文件新鲜。下载新的 logrotate 速查表。
36 位读者喜欢这个。

日志非常适合找出应用程序正在做什么或排除可能的故障。我们处理的几乎每个应用程序都会生成日志,我们也希望我们自己开发的应用程序也生成日志。日志越详细,我们拥有的信息就越多。但是如果放任不管,日志可能会增长到难以管理的大小,反过来,它们本身也会成为一个问题。因此,最好修剪它们,保留我们需要的部分,并归档其余部分。

基础知识

logrotate 实用程序非常适合管理日志。它可以轮换、压缩、通过电子邮件发送、删除、归档日志,并在您需要时启动新的日志。

运行 logrotate 非常简单——只需运行 logrotate -vs state-file config-file。在上面的命令中,v 选项启用详细模式,s 指定状态文件,最后的 config-file 提及配置文件,您可以在其中指定需要完成的操作。

实践操作

让我们看看一个 logrotate 配置,它在我们的系统上静默运行,管理我们在 /var/log 目录中找到的大量日志。查看该目录中的当前文件。您是否看到很多 *.[number].gz 文件?这就是 logrotate 正在做的事情。您可以在 /etc/logrotate.d/rsyslog 下找到此配置文件。我的看起来像这样

/var/log/syslog
{
        rotate 7
        daily
        missingok
        notifempty
        delaycompress
        compress
        postrotate
                reload rsyslog >/dev/null 2>&1 || true
        endscript
}

/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages

{
        rotate 4
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                reload rsyslog >/dev/null 2>&1 || true
        endscript
}

该文件首先定义了轮换 /var/log/syslog 文件的说明,这些说明包含在后面的花括号内。以下是它们的含义

  • rotate 7:保留最近七次轮换的日志。然后开始删除它们。
  • daily:每天轮换日志。与 rotate 7 一起,这意味着日志将保留最近七天。其他选项包括 weeklymonthlyyearly还有一个 size 参数,如果日志文件的大小超过指定限制(例如 size 10ksize 10Msize 10G 等),它将轮换日志文件。如果未指定任何内容,则每当 logrotate 运行时,日志都将被轮换。您甚至可以在 cron 中运行 logrotate 以在更具体的时间间隔使用它。
  • missingok:如果日志文件丢失也没关系。不要惊慌。
  • notifempty:如果日志文件为空,则不轮换。
  • delaycompress:如果压缩已打开,则将压缩延迟到下一次轮换。这允许至少存在一个轮换但未压缩的文件。如果您希望昨天的日志保持未压缩状态以进行故障排除,这将非常有用。如果某些程序可能仍在写入旧文件直到重新启动/重新加载(如 Apache),这也很有帮助。
  • compress:压缩已打开。使用 nocompress 将其关闭。
  • postrotate/endscript:在轮换后运行此部分内的脚本。有助于进行清理工作。还有一个 prerotate/endscript 用于在轮换开始之前执行操作。

您能弄清楚下一节对上面配置中提到的所有文件执行什么操作吗?第二节中唯一额外的参数是 sharedscripts,它告诉 logrotate 在所有日志轮换完成之前不要运行 postrotate/endscript 中的部分。它可以防止脚本为每个轮换的日志执行,而是在最后运行一次。

新内容

我正在使用以下配置来处理系统上的 Nginx 访问和错误日志。

/var/log/nginx/access.log
/var/log/nginx/error.log  {
        size 1
        missingok
        notifempty
        create 544 www-data adm
        rotate 30
        compress
        delaycompress
        dateext
        dateformat -%Y-%m-%d-%s
        sharedscripts
        extension .log
        postrotate
                service nginx reload
        endscript
}

可以使用以下命令运行上述脚本

logrotate -vs state-file /tmp/logrotate

第一次运行命令会得到以下输出

reading config file /tmp/logrotate
extension is now .log

Handling 1 logs

rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508250'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx

第二次运行它

reading config file /tmp/logrotate
extension is now .log

Handling 1 logs

rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508280'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx

第三次运行它

reading config file /tmp/logrotate
extension is now .log

Handling 1 logs

rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508316'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx

状态文件的内容如下所示

logrotate state -- version 2
"/var/log/nginx/error.log" 2021-08-27-9:0:0
"/var/log/nginx/access.log" 2021-08-27-9:11:56

下载 Linux logrotate 速查表。


本文最初发表在作者的个人博客上,并已获得许可进行改编。

标签
https://ayushsharma.in
我是一名作家和 AWS 解决方案架构师。我与初创公司和企业合作进行软件工程、DevOps、SRE 和云架构。我在 https://ayushsharma.in 上写下我的经验。

评论已关闭。

© . All rights reserved.