Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a date inside the filename with logrotate

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.

like image 725
Bernard Sfez Avatar asked Mar 04 '12 14:03

Bernard Sfez


People also ask

What is Dateext in logrotate?

logrotate with option dateext adds timestamp of tommorrow to a file rotated today.

What is Sharedscripts in logrotate?

The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated. Note that the double quotes around the first filename at the beginning of this section allows logrotate to rotate logs with spaces in the name.

What is Missingok in logrotate?

missingok : If the log file is missing, go on to the next one without issuing an error message. noolddir : Logs are rotated in the same directory the log normally resides in (this overrides the olddir option). daily : Log files are rotated every day.

What is Postrotate in logrotate?

Postrotate. Logrotate runs the postrotate script each time it rotates a log specified in a configuration block. You usually want to use this script to restart an application after the log rotation so that the app can switch to a new log.


2 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.

like image 58
Stéphane Avatar answered Oct 13 '22 20:10

Stéphane


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.

like image 37
Bernard Sfez Avatar answered Oct 13 '22 20:10

Bernard Sfez