logrotating 目录及其子目录中的文件

ith*_*cat 76 directory logrotate recursive

是否可以logrotate考虑目录及其所有子目录中的日志文件?(即没有明确列出子目录。)

Dan*_*n R 113

你的子目录有多深?

/var/log/basedir/*.log /var/log/basedir/*/*.log {
    daily
    rotate 5
}
Run Code Online (Sandbox Code Playgroud)

将旋转所有的.log在BASEDIR /文件,以及所有的.log在BASEDIR的任何直接的子文件。如果您还需要更深一层,只需添加另一个,/var/log/basedir/*/*/*.log直到覆盖每个级别。

这可以通过使用单独的 logrotate 配置文件进行测试,该文件包含不会满足的约束(高最小化),然后在详细模式下运行 logrotate 自己

logrotate -d testconfig.conf
Run Code Online (Sandbox Code Playgroud)

-d 标志将列出它考虑轮换的每个日志文件。

  • 谢谢!看起来 `-d` 实际上将 logrotate 置于试运行模式(即实际上没有改变任何东西)。 (8认同)
  • 不是真正直接相关,但可能对某人有用。`-f` 选项告诉 logrotate“强制运行”。命令末尾的一个词是要使用的配置文件,而不是默认值。所以`logrotate -f /some/config` 意味着使用该配置文件运行,并且即使配置文件说现在还不是运行的时候也总是运行。在我未受过训练的眼睛和我的前任看来,它似乎只是在指定配置文件。相当混乱。 (2认同)
  • 它实际上是 _debug_ `-d, --debug 打开调试模式并暗示 -v。在调试模式下,不会对日志或 logrotate 状态文件进行任何更改。 (2认同)

cra*_*raq 5

就我而言,子目录的深度可以在没有警告的情况下更改,因此我设置了一个 bash 脚本来查找所有子目录并为每个目录创建一个配置条目。

在轮换后保持子目录的结构对我来说也很重要,通配符(即@DanR 的答案)似乎没有这样做。如果您正在执行每日日志轮换,则可以将此脚本放入每日 cron 作业中。

basedir=/var/log/basedir/
#destdir=${basedir} # if you want rotated files in the same directories
destdir=/var/log/archivedir/ #if you want rotated files somewhere else
config_file=/wherever/you/keep/it
> ${config_file} #clear existing config_file contents

subfolders = $(find ${basedir} -type d)

for ii in ${subfolders}
do
    jj=${ii:${#basedir}} #strip off basedir, jj is the relative path

    #append new entry to config_file
    echo "${basedir}${jj}/* {
        olddir ${destdir}${jj}/
        daily
        rotate 5
    }" >> ${config_file}

    #add one line as spacing between entries
    echo "\n" >> ${config_file}

    #create destination folder, if it doesn't exist
    [ -d ${destdir}${jj} ] || mkdir ${destdir}${jj}
done
Run Code Online (Sandbox Code Playgroud)

就像@DanR 建议的那样,测试 logrotate -d


小智 5

这是旧线程,但您可以执行以下操作:

/var/log/basedir/**/*.log {
    daily
    rotate 5
}
Run Code Online (Sandbox Code Playgroud)

这两颗星将匹配零个或多个目录。不过,您必须小心如何定义要轮换的日志文件,因为您可以轮换已经轮换的文件。我在这里引用logrotate的手册。

请谨慎使用通配符。如果指定 *,logrotate 将轮换所有文件,包括之前轮换的文件。解决这个问题的方法是使用 olddir 指令或更精确的通配符(例如 *.log)。

  • 这个通配符模式对我不起作用。RHEL 7.3 上的 Logrotate 3.8.6 (6认同)