Skip to content


Mysql and Top pruner

The server had a setup in which a cronjob was used to dump output of top command to a directory accessible via http once every 5 minutes. To get rid of the logs and also to prune mysql logs the following script was made.


#! /bin/sh

LOGGER_PATH='/usr/local/apache/htdocs/top/'
N_DAY=3
MYSQL_DATA_DIR='/var/lib/mysql'
LOGSTOKEEP=3

################################################
#find and delete all files
#in logger path older than n days
################################################

find $LOGGER_PATH -mtime +$N_DAY -exec rm -f {} \;

################################################
#mysql pruner section.
#flush logs first,
#find the last filename
#and purge binary logs upto that
################################################

#mysql flush logs here

FILENAMES=`ls $MYSQL_DATA_DIR | grep -e "^mysql-bin.0\+[[:digit:]]\+$"|sort|tail -n $LOGSTOKEEP`
LINECOUNT=`echo $FILENAMES|wc -w`

#linecount will be lesser than or equal to the number of logs we want to keep

if [ $LINECOUNT -lt $LOGSTOKEEP ]; then
echo "$LOGSTOKEEP files were not found. So no purge is being done."
else
LASTFILENAME=`echo $FILENAMES|cut -d' ' -f 1`
#do purge upto lastfilename
fi
exit

Posted in Tutorial.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. obzczzadtue says

    DMwfIi gzvysyjmfgyu, [url=http://hlbddcdbameb.com/]hlbddcdbameb[/url], [link=http://llgxbzlbcbsz.com/]llgxbzlbcbsz[/link], http://krluipfbnrjo.com/



Some HTML is OK

or, reply to this post via trackback.