📜 ⬆️ ⬇️

We win delaycompress in logrotate

Notice: this post is more likely for novice sysadmins, experienced ones will be able to reach it themselves.
This article describes how to configure logrotate in Debian 5.
The “delaycompress” option in logrotate is used for deferred archiving of logs (during the next rotation).
The option is created for programs that can not close the logs immediately, which makes it impossible to archive while logrotate is running. Info: “copytruncate” does not help. Verified
In general, the struggle against this restriction made me keep the fact that on some servers (or rather, on VPS) a limited amount of disk space is available, and the logs, nevertheless, take up a lot of space.
When using “delaycompress” by the end of each day, we already have 2 high-volume logs, instead of one. In my case, this is an extra ~ 200-300MB, which is noticeable for a server with limited resources.
So, how to force logrotate to archive logs at once?


To remedy the situation, we will use the “postrotate ... endscript” section in the rotation configuration. In this section, you can write a custom shell script, which will perform the necessary actions.
In the general case, after the system script, it suffices to add “gzip / path / to / log.1” to the config - and the problem is solved.
Instead of 1, we substitute what you described in the "start" section. 1 is the default.
However, I'm used to using the dateext option - it allows you to add the current date to the name of the rotated log. Convenient for search by logs. Well and besides, the logs of, say, nginx for different hosts I have in the directories below the document_root are on the same level. In this case, the task becomes more complicated.
Not to be unfounded, I will give an example of my config for logrotate, where the rotation of nginx logs is described.

/www-data/*/log/nginx_*.log {
daily
missingok
rotate 30
compress
delaycompress
dateext
create 600 www-data www-data
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
for i in `find /www-data/*/log/*.log -type f`; do
if [ -e $i-`date +%Y%m%d` ]; then
gzip $i-`date +%Y%m%d`;
chown www-data:www-data $i-`date +%Y%m%d`.gz;
chmod 0600 $i-`date +%Y%m%d`.gz;
fi;
done;
endscript
}


In the postrotate section, we describe all the paths to the logs, check for the presence of an uncompressed log file, and if it exists, compress and set the necessary rights for it.
In general, everything.
Important note: comments # in the postrotate section do not work - cause an error. Be careful.
')

Source: https://habr.com/ru/post/71597/


All Articles