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.
logrotate with option dateext adds timestamp of tommorrow to a file rotated today.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With