linux logrotate, rotate file with underscore

232 Views Asked by At

I have a logrotate configuration which is given below.

/var/log/Test.log {
    size 50k
    missingok
    rotate 10
    notifempty
    create 0640 root root
}

The file is rotating successfully once it reached the size of 50kb in the below format

Test.log.1
Test.log.2
Test.log.3

What I can do if I want to rotate files in the below format

Test_1.log
Test_2.log
Test_3.log
2

There are 2 best solutions below

1
rezshar On

Try this one,it should help you.

/var/log/Test.log {
...

postrotate 
    /usr/bin/mv Test.log.1 Test_1.log
    ... 
endscript
}
0
parttimeturtle On

OK, so the problem here is that logrotate uses these filename extensions to keep track of its rotations. So if you have x.log, it inherently needs a predictable, sequential extension to keep track of what is what; i.e. x.log.1 is the first rotation, x.log.2 is the second, etc.

By renaming the file like that, you're moving the extension to the middle of the file and subsequently leaving each file with the same extension: .log. So as far as logrotate is concerned, it will think those are all unrelated files, not rotations of a previous log.

E.g., if you rename Test.log.1 to Test_1.log, and leave that file in the same folder, logrotate won't know that it's a rotation of Test.log and create a new Test.log.1 upon next rotation. And so on, and so on.

So the short answer is no, there is no logrotate directive that will rename a file like you want. You will have to create this functionality yourself using a postrotate script, and you will also have to (at some point) move these files out of the var/log/ directory once they're renamed, using either said postrotate script or the olddir directive.

The dateext directive might also be useful, as in my opinion date extensions are typically more user-friendly than numerical ones.