#!/bin/sh # Name: mysql_rotatelogs.sh # Function: rotates mysql general and slow logs # Date: 22 Dec 2006 # Author: Jim Wood - OpSource UK Ltd # Modified by: Matt Reid - OpSource USA # 15/03/07 - Amended to allow portability by resolving hostname. JW # 05/02/08 - Added some variables, changed to sh shell from ksh, added GPL, # - Modified to allow for 1 or both logs to be rotated. #### # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . #### #set -x # Options ROTATE_SLOW="1" # 0 for no rotation, 1 for yes ROTATE_GENERAL="1" # 0 for no rotation, 1 for yes # # Declare Vars # MYUSER="root" MYPASS="pass" LOG_DIR=/var/lib/mysql ROTATE_DIR=/data/mysql/archive GEN_LOG=mysql-gen.log SLOW_LOG=mysql-slow.log DSTAMP=`date '+%d%m%G-%H%M%S'` #### DO NOT EDIT BELOW HERE ###### export LOG_DIR ROTATE_DIR GEN_LOG SLOW_LOG DSTAMP MYUSER MYPASS # Zero the vars GEN_SIZE=0 GEN_LIMIT=0 SLOW_SIZE=0 SLOW_LIMIT=0 DONE_GEN=0 DONE_SLOW=0 # Rotate General log rotate_gen () { #echo "GEN Log is " ${GEN_SIZE} mv ${LOG_DIR}/${GEN_LOG} ${ROTATE_DIR}/${GEN_LOG}.${DSTAMP} } # Rotate Slow log rotate_slow () { #echo "SLOW Log is " ${SLOW_SIZE} mv ${LOG_DIR}/${SLOW_LOG} ${ROTATE_DIR}/${SLOW_LOG}.${DSTAMP} } start_func() { # Flush and compress accordingly test -d ${ROTATE_DIR} || mkdir -p ${ROTATE_DIR} if [ "$ROTATE_GENERAL" = "1" ]; then GEN_SIZE=`ls -l ${LOG_DIR}/${GEN_LOG} | awk -F" " '{ print $5 }'` GEN_LIMIT=500 #echo "GEN_SIZE=$GEN_SIZE" #echo "GEN_LIMIT=$GEN_LIMIT" if [ ${GEN_SIZE} -gt ${GEN_LIMIT} ]; then STATE_GEN="1" rotate_gen fi fi if [ "$ROTATE_SLOW" = "1" ]; then SLOW_SIZE=`ls -l ${LOG_DIR}/${SLOW_LOG} | awk -F" " '{ print $5 }'` SLOW_LIMIT=1500 #echo "SLOW_SIZE=$SLOW_SIZE" #echo "SLOW_LIMIT=$SLOW_LIMIT" if [ ${SLOW_SIZE} -gt ${SLOW_LIMIT} ]; then STATE_SLOW="1" rotate_slow fi fi # Flush MySQL Logs /usr/bin/mysqladmin --user="$MYUSER" --password="$MYPASS" flush-logs # Compress rotated logs if [ "$STATE_GEN" = "1" ]; then /bin/gzip ${ROTATE_DIR}/${GEN_LOG}.${DSTAMP} fi if [ "$STATE_SLOW" = "1" ]; then /bin/gzip ${ROTATE_DIR}/${SLOW_LOG}.${DSTAMP} fi } start_func