Skip navigation.
Home

Sysadmin Shell Joy: Renaming Archived logs to useful names

One of the things that (mildly) irritates me about sysadmining Unix boxes is the log rotation. ie /var/log/messages gets rotated to /var/log/messages.1 then finally /var/log/messages.2.gz.

Which is great for trivial auto-scripting  of what happened in the last log. But sucks if you're trying to easily figure out what happened on the 9th of November and you have a mess of logs all with varying rotations. Now logrotate can be configured to rotate a date onto the log file. Which solves part of the problem: ie from here on. It doesn't solve the "what came before" problem.

I fix this with a simple(ish) one liner, run in the directory where the logs exist:

shopt -s extglob ; for i in *.{?,??}.gz ; do if [ -f $i ]; then j=${i/.@(?|??).gz}; k=$j.$(stat -c "%y" $i | cut -f 1 -d ' ' ).gz ; echo "$i --> $k" ; mv -i $i $k ; fi ; done

Obviously, needs to run as root, or sudo'd as necessary.

This gives output like:

syslog.1.gz --> syslog.2008-11-25.gz
syslog.2.gz --> syslog.2008-11-24.gz
syslog.3.gz --> syslog.2008-11-23.gz
syslog.4.gz --> syslog.2008-11-22.gz
syslog.5.gz --> syslog.2008-11-21.gz
syslog.6.gz --> syslog.2008-11-20.gz

Enjoy! 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

making it stick!

So once you've done that you /really/ should look at the "dateext" option in logrotate.conf

"dateext" uses YYYMMDD, so you'd need to hack up you script a little to be completely compatible.

Arggh. I meant to add

Arggh. I meant to add 'dateext' to the posting in the first place.

Ta. Appreciated!

- S

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Syndicate content