当前位置: 代码迷 >> 综合 >> 使用 logrotate 配置 supervisor 进行日志管理按天备份
  详细解决方案

使用 logrotate 配置 supervisor 进行日志管理按天备份

热度:6   发布时间:2024-01-20 02:49:01.0

supervisor 默认的日志备份策略是按大小备份,经常不知道去哪个文件查日志的烦恼。本文通过设置 logrotate 来进行日志安排备份

logrotate 的配置文件默认存放在 /etc/logrotate.d 下。新建一个文件

vim /etc/logrotate.d/log-file
/var/log/supervisor/log_file*.log {dailyrotate 30 . # num of backupsdateextdateyesterday # 用昨天的日期做后缀copytruncatedelaycompress    # today and yesterday will not compresscompressmissingoknotifempty
}

daily: 日志按天轮询。也可以设为weekly、monthly、yearly
rotate : 备份数,超过的会删除
dateext: 备份文件名包含日期信息
dateyesterday 用昨天的日期做后缀,因为日志一般是凌晨备份前一天的数据,如果不用这个参数,会造成,日志文件显示的日期和实际不是一天
copytruncate: 首先将目标文件复制一份,然后在做截取(truncate)。这样做就防止了直接将原目标文件重命名引起的问题。
delaycompress :与compress选项一起用,delaycompress选项指示logrotate不将最近的归档压缩,压缩将在下一次轮循周期进行 就是最新两个日志文档不压缩
compress: 压缩文件。如果不想压缩 可以和delaycompress 一起去掉
missingok: 忽略错误
notifempty: 如果没有日志 不进行轮询

测试:
手动运行 logrotate 查看运行效果

logrotate /etc/logrotate.d/log-file

log-file 运行指定的配置。如果想运行所有的 使用logrotate /etc/logrotate.conf

测试logrotate -d /etc/logrotate.d/log-file 强制执行,如果轮询的条件不满足,上面的命令不会执行,这个时候可以用下面这条命令来达到效果logrotate -vf /etc/logrotate.d/log-file -f 强制轮询  -v 打印logrotate  日志

问题总结
1: 如果配置不起作用,还是按文件大小进行备份 需要在/etc/supervisor/config/xxx 配置文件中 加上如下配置

# no limit on the size
stdout_logfile_maxbytes=0
stderr_logfile_maxbytes=0# no backup with supervisor
stdout_logfile_backups=0
stderr_logfile_backups=0

2: 如果运行的过程当前 报 权限不足 需要在/etc/logrotate.d/xxx 配置文件的首行加上
如下配置

su root root

参考:
https://linux.cn/article-4126-1.html
https://ihower.tw/blog/archives/3565
https://linux.die.net/man/8/logrotate