微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux – 如何强制(或解决)logrotate将旧日志移动到不同物理磁盘上的olddir?

我希望logrotate复制并截断日志文件,并在不同的物理磁盘上使用olddir.根据手册,olddir不能在不同的物理磁盘上,因为logrotate的认行为是仅重命名原始日志文件,这对于不同的物理磁盘是不可能的.

好吧,但我使用的是copytruncate指令,它会生成原始文件的副本,然后截断原始文件.因此,将新复制的文件移动到不同物理磁盘上的不同位置应该没有问题.

但是当我运行logrotate时,它仍然抱怨logfile和olddir在不同设备上运行.

它有什么办法吗?可能运行一些自定义的postrotate脚本,将日志文件移动到所需的位置?

解决方法

这是来自logrotate手册页.
olddir directory <br>
        Logs  are moved into directory for rotation. **The directory must be on the 
        same physical device as the log file being rotated**,and is assumed to  be 
        relative  to  the  directory holding the log file unless an absolute path 
        name is specified. When this option is used all old versions of  the  log 
        end  up  in  directory.   This  option  may be overridden by the noolddir 
        option.

olddir在同一物理设备上的存在是有限的.

可以使用以下解决方法.
将olddir设置为同一物理磁盘中的目录,并使用postrotate脚本将olddir的内容移动到不同物理设备上的目录.

示例logrotate配置文件

/var/log/httpd/*log {
        copytruncate
        olddir /var/log/httpd/olddir
        rotate 5
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        mv /var/log/httpd/olddir/* /vagrant/httpd/olddir/
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

原文地址:https://www.jb51.cc/linux/395171.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐