使用logrotate来进行轮换mysql的慢日志

#!/bin/bash
SLOWCFG=/etc/my.cnf
DATADIR=`awk /^datadir/ $SLOWCFG|awk -F"=" '{print $2}'`
SLOWDIR=`cat $SLOWCFG|grep "^slow_query_log_file"|awk -F"=" '{ print $2 }'`
SLOWNAME=`echo $SLOWDIR|awk -F"/" '{print $5}'`
SLOWUSER=root
SLOWPASS=shiyiwen
SLOWSCRIPT=/data/mysql-slow-ratate
NEWSLOWDIR=`find $DATADIR -name "$SLOWNAME*"|xargs ls -lt|awk 'NR==1 { print $9 }'`
OLDDIR=/var/log/old_log if [[ -z $SLOWDIR || -z $NEWSLOWDIR || -z $SLOWNAME || -z $DATADIR ]];then
echo "Please check configure...."
exit 1
fi if [ ! -d $OLDDIR ];then
mkdir -p $OLDDIR
fi cat >$SLOWSCRIPT<<EOF
$NEWSLOWDIR {
create 600 mysql mysql
notifempty
dateext
daily
rotate 7
missingok
copytruncate
olddir $OLDDIR
#compress
postrotate
# just if mysqld is really running
if test -x /usr/bin/mysqladmin &&
/usr/bin/mysqladmin ping &>/dev/null
then
/usr/bin/mysqladmin -u$SLOWUSER -p$SLOWPASS flush-logs
fi
endscript
}
EOF if [ $UID -ne 0 ];then
echo "Please use root running.."
exit 1
fi echo "59 23 * * * /usr/sbin/logrotate -f $SLOWSCRIPT">>/var/spool/cron/root
/etc/init.d/crond restart
echo -e "\033[34m OK! Sucess\033[0m"
上一篇:什么是TF-IDF算法?


下一篇:PGM:贝叶斯网的参数估计2