35

I need to set logrotate to rotate logs files from an application running on the server. I need the date inside the filename.

I set dateext and also dateformat to add a - in the date. The result filename is:whatever.csv_2012-03-03

I would like the timestamp to be part of the filename keeping safe the extension; Whatever_2012-03-03.csv.

Stéphane
  • 3,884
  • 1
  • 30
  • 27
Bernard Sfez
  • 1,329
  • 2
  • 15
  • 18

2 Answers2

73

You should be able to keep the extension apart, e.g. whatever.2012-03-03.csv, with the following configuration:

whatever.csv {
  dateext
  dateformat .%Y-%m-%d
  extension .csv
  ...
}

Note the dateext is deliberately empty.

Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121
Stéphane
  • 3,884
  • 1
  • 30
  • 27
  • I've tried this, but the date is added on each rotation, so I get `file.log`, `file.2014-01-08.log` and `file.2014-01-07.2014-01-08.log` etc. – fadedbee Jan 08 '14 at 17:26
  • 1
    I found that the `extension` should have the `.` so the correct form is `extension .csv`. This is also followed in `compressext` as in `compressext .bz2` – ADTC Jan 24 '14 at 03:12
  • 3
    Also, it should be `dateformat .%Y-%m-%d` (the first dot is for the date, the second for the extension). @chrisdew that is probably because you are using a wildcard, so *logrotate* is trying to rotate both `file.log` and `file.2014-01-07.log`. Either use a fixed name, or use the `olddir` option to move rotated files to a different directory so they don't get picked up by the wildcard. – ADTC Jan 24 '14 at 03:27
  • The dateformat option causes a segfault for me https://fedorahosted.org/logrotate/ticket/39 – Rob Aug 07 '14 at 09:07
  • @Rob sadly fedorahosted is an ex-service: "fedorahosted.org was retired on March 1st, 2017". – Chris Woods Feb 19 '18 at 19:42
26

To insert the date within the filename (and not as extension) of a file under Linux while rotating a file it is correct to use:

# Daily rotation
    daily

# We keep original file live
    copytruncate

# Rotation is 1 so we have always .1 as extension
    rotate 1

# If file is missing keep working
    missingok

    sharedscripts
    postrotate
            day=$(date +%Y-%m-%d)
            mv blabla.csv.1 /var/www/gamelogs/dir/blabla$day.csv
    endscript
}

This is simple and works fine.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
Bernard Sfez
  • 1,329
  • 2
  • 15
  • 18
  • I like this solution. If I get it right, it has a side effect: logrotate will no longer limit the number of kept files. If intended, that's fine. – Marian Jul 08 '14 at 06:59
  • It's easy enough to add a `find $logdir -mtime +$maxage -delete` statement to your postrotate script. – Rob Aug 07 '14 at 09:07
  • 3
    @Rob, so, what's the point of actually using logrotate, then? – cnst Sep 15 '14 at 20:27
  • @cnst Not much, besides putting the log rotation code with other log rotation code, so a future sysadmin can fix it to use the dateext if and when the segfault issue goes away. – Rob Sep 19 '14 at 15:19
  • 1
    The `rotate 1` is too restrictive. – 2rs2ts Nov 17 '15 at 20:05
  • If your logs are moving very fast you will lose some data due to the usage of copytruncate. Did some tests using seq and redirecting the output to a logfile and validated this. – alfredocambera Feb 17 '16 at 20:50