2

I want to preserve/archive all of my web server logs, and not have any of them be deleted by logrotate. What would a recommended approach be for this? This is a Linux box, running Nginx. Thanks in advance.

(I would prefer to use cronolog, but it appears to not go well with Nginx because of the way Nginx handles logging.)

1 Answer 1

7

Do it with logrotate, just tell it what you want...

/var/log/nginx/*.log {
    daily
    dateext
    missingok
    rotate 7305 # 2 decades
    olddir /var/log/nginx/old
    compress
    delaycompress
    notifempty
    create 644 nginx root
    sharedscripts
    postrotate
      if [ -f /var/run/nginx.pid ]; then
        kill -USR1 `cat /var/run/nginx.pid`
      fi
    endscript
}

I don't use nginx, so I used an example I found for the postrotate... If you have a logrotate script already, start with modifying that.

Key parts:

  • "daily" means every day. You could do weekly or size-based, but that doesn't interact as prettily with "dateext".
  • "dateext" means it'll give the rotated logfiles a name based on the date instead of a simple number; that way it doesn't have to rename every log file every day and you can tell the date of a logfile from the name of the file
  • "rotate 7305" -- this is two decades. Keep more or less... logrotate really prefers to have some sort of retirement, but you can set it ridiculously high.
  • "olddir" has to be on the same filesystem, but that'll keep the logs in a separate dir so you can figure out what's going on
  • "delaycompress" and "create" help make sure it works with software that doesn't want to work with it
5
  • Great answer. I was looking for a log-rotate script. Nov 4, 2009 at 13:37
  • For those using this config: The nginx.pid file may be in /opt/nginx/logs, and logrotate doesn't accept 36525 rotations.
    – user11480
    Jan 3, 2010 at 21:11
  • @lackey: do you know the max that logrotate will accept for rotations? I can fix. :)
    – freiheit
    Jan 4, 2010 at 19:45
  • @freiheit, I took a look at the source and rotateCount is an int, so maybe 32767? Not sure though, nothing in the man page. (A few months ago I tried 1500 which logrotate did accept.)
    – user11480
    Jan 5, 2010 at 20:09
  • @lackey, Okay, I've bumped it down to 20 years (7305); that's still well beyond any likely server lifetime, but under 2^13.
    – freiheit
    Jan 25, 2010 at 21:30

You must log in to answer this question.