Welcome Guest | Login

Cron job to rotate logs?

I've been doing some reading about the best way to run an app in production.  It looks like rotating out logs is a good idea but there are a lot of opinions about the best way to do it.  I can't find anyone who recommends a simple cron job, though.  Am I missing something obvious about why this isn't smart?  

I'm picturing having 4 backup folders:  backup_log/1, backup_log/2, backup_log/3, backup_log/4.  My script would move 1 -> 2, 2 ->3 and 3 -> 4.  Then, it'd copy the production log to 1 and erase it in its original location.  

Any ideas why this won't work?

2008-05-06 07:41 AM

You can run a simple shell script as a cron-job. The shell script should have the following entries.

=

#!/bin/sh

if [ ! -f production1log.tar.gz ]; then
tar -czf production1log.tar.gz production.log
fi
rm -f production3log.tar.gz
if [  -f production2log.tar.gz ]; then
mv production2log.tar.gz production3log.tar.gz
fi
if [  -f production1log.tar.gz ]; then
mv production1log.tar.gz production2log.tar.gz
fi
tar -czf production1log.tar.gz production.log

=

Do test the script and let us know how it goes..

2008-05-06 05:00 PM

Regards,
Rahul
Thanks for the script!  In order for it to run from from the shell prompt, I had to add in some path information, but other than that it ran perfectly.  In addition to rotating logs, I wanted to truncate the one in use, so I added to the end of your script:

: > "$LOG_FILE"

This all works well from the prompt and as a cron job, but I would love confirmation that Rails won't mind my truncating the log.  

I also put in some echos so that my email notification was more user friendly:

=

#!/bin/sh

export LOG_FILE="<your app location>/log/production.log"
export BACKUP_FOLDER="backup_log"

echo ""
echo "Production log backup"
echo "----------------------"
echo "* Checking presence of first backup..."
if [ ! -f $BACKUP_FOLDER/production1log.tar.gz ]; then
tar -czf $BACKUP_FOLDER/production1log.tar.gz $LOG_FILE
fi
echo "* Removing last backup"
rm -f $BACKUP_FOLDER/production3log.tar.gz
echo "* Rotating backups..."
if [  -f $BACKUP_FOLDER/production2log.tar.gz ]; then
mv $BACKUP_FOLDER/production2log.tar.gz $BACKUP_FOLDER/production3log.tar.gz
fi
if [  -f $BACKUP_FOLDER/production1log.tar.gz ]; then
mv $BACKUP_FOLDER/production1log.tar.gz $BACKUP_FOLDER/production2log.tar.gz
fi
echo "* Creating new backup..."
tar -czf $BACKUP_FOLDER/production1log.tar.gz $LOG_FILE
echo "* Truncating log file..."
: > "$LOG_FILE"
echo "----------------------"
echo "Done"
exit 0

=

2008-05-07 08:09 AM

Thanks for adding more features to the script and pasting them here..Also note that Rails application won't mind truncating the log.

2008-05-07 12:57 PM

Regards,
Rahul
I added this to my environment.rb file:

config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 'daily')

will give you log files such at

production.log <--- todays log
production.log.20080507 <-- yesterdays log
production.log.20080506 <-- etc....

I then wrote a simple cron script that just moved the files production.log.* to a new folder on a daily basis

2008-05-08 03:27 PM

http://www.jamzee.com

Hello Guest! In order to post you must be an active client with us, please log in or sign up today!