Logrotate使用

github链接:https://github.com/logrotate/logrotate

作用:对log进行压缩&备份

安装步骤

下载源码,https://github.com/logrotate/logrotate/releases/download/3.18.1/logrotate-3.18.1.tar.xz

安装依赖工具包

yum install autoconf automake libtool make popt-devel xz开始安装
cd logrotate-3.18.1
autoreconf -fiv
./configure
make

配置示例

compress

/Data/nginx/logs/access``.log {

daily

rotate 15

nomail

missingok

olddir backup/

dateext

dateformat -%Y%m%d

postrotate

echo '' > ``/Data/nginx/logs/access``.log

endscript

}

|

配置解析

image.png

logrotate每天执行一次,如果按小时或按文件大小备份,需要添加logrotate命令至crontab,按指定的时间执行。

Note that usually logrotate is configured to be run by cron daily. You have to change this configuration and run logrotate hourly to be able to really
rotate logs hourly.

使用中的一些坑

nginx日志清理后新的日志无法记录,nginx需要执行reload命令,于是logrotate配置修改如下:

|

compress

"/Data/nginx/logs/nginx.log" /Data/nginx/logs/access``.log {

daily

rotate 15

maxsize 5G

nomail

missingok

olddir backup/

dateext

dateformat -%Y%m%d

sharedscripts ``#脚本共享,所有日志只执行一次

postrotate

echo '' > ``/Data/nginx/logs/nginx``.log

echo '' > ``/Data/nginx/logs/access``.log

endscript

lastaction ``#脚本执行后的动作,reload nginx以生效新的log file

nginx -s reload

endscript

}

|

你可能感兴趣的:(Logrotate使用)