Do you have a very large mongoDB daemon log file?
You can install a log rotate package, or you can implement some dead simple bash script that will do more or less the same job. This is what this blog post is about.
Make sure the script is adjusted for your installation before running it. If you have a very large daemon log file, many gb, you might want to just keep a portion of this for the first run. That's the idea of the variable MAX (nbo lines). Do some checks up front to find a good value for this parameter. Default here is set to 20000 lines. If you do a "tail -20000 mongod.log|more" you will get an idea of how many days back that will evaluate to. If you want to preserve the entire file you really should copy the file up front and store it on a filesystem that has capacity to hold it.
You can install a log rotate package, or you can implement some dead simple bash script that will do more or less the same job. This is what this blog post is about.
Make sure the script is adjusted for your installation before running it. If you have a very large daemon log file, many gb, you might want to just keep a portion of this for the first run. That's the idea of the variable MAX (nbo lines). Do some checks up front to find a good value for this parameter. Default here is set to 20000 lines. If you do a "tail -20000 mongod.log|more" you will get an idea of how many days back that will evaluate to. If you want to preserve the entire file you really should copy the file up front and store it on a filesystem that has capacity to hold it.
And for God's sake, make sure the variable LOGFILE actually points to the daemon logfile and not some file that is part of the database.
This script can easily be tailored to rotate any type of logs.
Make sure the os user has access rights to create and operate on the archive directory.
#/bin/bash
# by kp
# adjust the variable MAX to suit your needs
# it is ment for those of you with very large log
# install this in cron on a weekly basis
# 00 07 * * 0 bash /root/mongod_clean.bash > /root/mongod_clean.out
# consider the -- quiet switch in mongoDB to reduce the amount of log info
export LOGFILE=/var/log/mongodb/ mongod.log
export ARCHIVE=/var/log/mongod_log_ archive
export TODAY=`date +"%Y%m%d"`
export DAYS=30
export MAX=20000
if [ ! -d $ARCHIVE ]
then
mkdir $ARCHIVE
fi
if [ -f $ARCHIVE/mongod.log.$TODAY ]
then
echo "already cleaned today, rename copy from today under $ARCHIVE"
exit 1
fi
if [ -f $LOGFILE ]
then
lines=`wc -l $LOGFILE |awk '{ print $1 }'`
if [ $lines -gt $MAX ]
then
tail -$MAX $LOGFILE > $ARCHIVE/mongod.log.$TODAY
else
cp -p $LOGFILE $ARCHIVE/mongod.log.$TODAY
fi
cat /dev/null > $LOGFILE
find $ARCHIVE -type f -mtime +$DAYS -name 'mongod.log.*' |xargs rm -f
echo "$LOGFILE truncated ok"
exit 0
else
echo "$LOGFILE missing"
exit 1
fi
No comments:
Post a Comment