I recently turned on compression in logrotate.conf on multiple systems. Old uncompressed logs still remain with their old numbering. I am looking to run a script to put these old logs where they would be numerically in the rotation if compression had not been turned on. These logs would include messages, boot.log, spooler, and others.

[user@test ~]$ ls -ltr /var/log/messages*
-rw------- 1 root root 283380 Jan 22 03:49 /var/log/messages.4
-rw------- 1 root root 212641 Jan 29 03:59 /var/log/messages.3
-rw------- 1 root root 278964 Feb  5 04:01 /var/log/messages.2
-rw------- 1 root root   7828 Feb 16 04:02 /var/log/messages.7.gz
-rw------- 1 root root  12417 Feb 17 04:02 /var/log/messages.6.gz
-rw------- 1 root root   2289 Feb 18 04:02 /var/log/messages.5.gz
-rw------- 1 root root   2135 Feb 19 04:02 /var/log/messages.4.gz
-rw------- 1 root root   2138 Feb 20 04:02 /var/log/messages.3.gz
-rw------- 1 root root   2403 Feb 21 04:02 /var/log/messages.2.gz
-rw------- 1 root root   2339 Feb 22 04:02 /var/log/messages.1.gz
-rw------- 1 root root   8496 Feb 22 10:19 /var/log/messages

If I were to manually rename these I would do a mv messages.2 messages.8.tmp for each log so that no file currently named messages.8 is overwritten. After all files have been renamed I would then do a mv messages.8.tmp messages.8 followed by a gzip -9 messages.8.

I have played around with for and while loops and sed, but I am still a novice scripter.

The numbers for the most recent and oldest uncompressed logs will vary from system to system.

In the future, I will manually compress older logs before turning on compress in logrotate.conf.

In summary, two questions:

  1. Is there a way to have logrotate.conf recognize these older files and compress them by itself?
  2. What is the best way to script the manual rotation to where the old log files would be sequentially, followed by compression?
  • I understand that there is no way to do this with logrotate. What I am asking now is the best way to script the renaming and gzipping of these logs to where they would be in the sequence. I have to do this on a substantial number of systems, so if I can just run a single script that would save a lot of time. – Linux2012 Feb 23 '12 at 14:01

As far as I know, logrotate cannot put old files to their place. So you need to do it by hand/by a small script.

Please notice, that depending on your configuration, logrotate rotates old log file only for limited times and then deletes them. So restoring old backup files may be useless until you changed your logrotate configuration so that it never deletes old logs (configuration variable rotate).

| improve this answer | |
  • As @jofel said, just zip them up. Gzip will add the .gz extension by default, and that's what logrotate is going to be looking for. – Magellan Feb 23 '12 at 0:12

Log rotate can't but gzip messages.? will handle that for you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.