3

I have a script which runs as a cronjob every 30 mins and takes mysqldump of some tables. The dump files are stored in /var/log/ as

/var/log/mysqldump/dbname/tablename/2014-06-15-18-30-dbname-tablename.sql
/var/log/mysqldump/dbname2/tablename2/2014-06-15-19-00-dbname2-tablename2.sql

I'm using logrotate for file rotation, the config file has the following structure:

/var/log/mysqldump/dbname/tablename/*.sql {
  daily
  rotate 30
  compress
  nocreate
}

Now my problem is that although logrotate works fine, it's appending "1" or "2" at the end of the gzipped file names, for instance this is what I have

/var/log/mysqldump/dbname2/tablename2/2014-06-15-19-30-dbname2-tablename2.sql.1.gz
/var/log/mysqldump/dbname1/tablename1/2014-06-15-19-00-dbname1-tablename1.sql.2.gz

I execute logrotate from within the script as

logrotate -f /path/to/mysqldump-logrotate.conf

What am I doing wrong here?

4 Answers 4

4

Logrotate will default to adding a number to the file. This is so that if it's used for filenames like e.g. /var/log/messages, the rotated logs will be named /var/log/messages.0 and /var/log/messages.1 etc.

Your situation is somewhat different since you already have a timestamp in the filename, so that the extension is unnecessary. Logrotate doesn't have an option to not use an extension - but there's a workaround that you might use.

The alternative to using a number sequence is to use a date/timestamp. This defaults to -%Y%m%D, e.g. -20140618. But you can configure what the string should look like - which in your case would be an empty string. You'd do this using the following configuration:

dateext          # to use dateformat string instead of sequential numbers
dateformat ''    # to use an empty string as the dateext
3
  • Thanks, I should probably set logrotate to run from cron.
    – reflektor
    Jun 19, 2014 at 6:04
  • 1
    On Centoss 6.6, if you use dateformat '', logrotate appends the actual quotes to the file name. I get myserver.log''.gz
    – lreeder
    Nov 19, 2014 at 17:13
  • same on debian 8 - you get the actual '' in the filename. you can avoid this by not putting anything after dateformat on that line. Feb 15, 2017 at 5:28
4

You are doing nothing wrong this is the default and expected behaviour. If you want a different extension e.g a date then you can specify dateext

dateext Archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number. The extension may be configured using the dateformat option.

From the above you can also see the defaut behaviour is expected to be simply adding a number.

2

You have the option rotate 30 in your configuration, which means that 30 older versions are kept before deleting. This number is how logrotate differentiate the versions. So, in effect you are doing nothing wrong and the program behaves as designed.

1

This is normal behaviour for logrotate as you described it.

Normally you would want to logrotate your logfiles not via script but via cronjob(s).

For gzipping the files via logrotate you could not include the date and rather let logrotate handle the dateext.

So for a "solution" to your issue you could use the logrotate options

dateext 

and skip writing the day into your filename.

This would "remove" the 1 you see in the filename.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .