3

I am new to Linux/CentOS and right now I am running into a little problem.

My CentOS7 Server logs all Maillogs, via Logrotate, on a daily basis and moves them to the folder /var/log/old_maillogs. Right now the logs are saved with a name similar to this:

Maillog-20230721

I would like their names to change, based on how old they are, so that if the Log is one day old it's named:

Maillog-1

And one the day after that, the files name changes to:

Maillog-2

And so on.

Is this even possible, if so, what would be the smartest way to get it working? I have read a lot about scripts that get executed once per day, but then again, how do I code the script, so that it recognizes how many logs are in the folder?

This is my current config:

/var/log/maillog{

daily

rotate 365

postrotate

   Systemctl restart rsyslog.service

endscript

mailfirst

olddi /var/log/old_maillog

}
asktyagi
  • 2,860
  • 2
  • 8
  • 25
Moritz
  • 65
  • 9
  • 4
    I just want to check that you realize that the numbers in your filename above are giving the date for the log. 20230721 is 2023, 07, 21, or the 21st of July 2023. Right now you should be getting a unique file name each day. Numbering the logs with 1, 2, 3, ... etc. is fine if it serves some other purpose for you, but you already have an automatic filename that changes on a daily basis. There are other simple commands you could use to, for example, count the number of log files or find the oldest logs in a directory. – David Jul 24 '23 at 14:34

2 Answers2

5

Please refer logrotate man page option "start count"

start count

This is the number to use as the base for rotation. For example, if you specify 0, the logs will be created with a .0 extension as they are rotated from the original log files. If you specify 9, log files will be created with a .9, skipping 0-8. Files will still be rotated the number of times specified with the count directive.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
  • "start count" seems to be the right thing, but I don't really get, how the parameter works. When I add `start 0` to the config and force a rotation, the logged file is still named `maillog - 20230724`. Am I doing something wrong? I added my config to the original question. – Moritz Jul 24 '23 at 07:26
  • Nevermind, I just made it work. Thank you very much! – Moritz Jul 24 '23 at 07:43
3

As @asktyagi already stated. The right parameter to use is "start count".

Initially it didn't work for me, that's because I didn't disable the parameter for "use data extension for rotated files", which is called nodateext.

So the working config looks like this:

/var/log/maillog{

daily

rotate 365

postrotate

   Systemctl restart rsyslog.service

endscript

mailfirst

olddi /var/log/old_maillog

start 1

nodateext
}

This config saves logs on a daily basis and renames the extension to the number, that is equivalent to how old the log is.

So on day 1 it's called - maillog.1

And on day 2 it's called - maillog.2

And so on.

Moritz
  • 65
  • 9