Home » Linux » How to insert a date inside the filename with logrotate

How to insert a date inside the filename with logrotate

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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.

Questions:
Answers:

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.