Last active
December 30, 2015 23:59
-
-
Save netkiller/7904307 to your computer and use it in GitHub Desktop.
mysql日志切割工具
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
USER=backup | |
PASS=SaJePoM6BAPOmOFOd7Xo3e1A52vEPE | |
LOGDIR=/backup/dblog | |
DATADIR=/var/lib/mysql | |
LOG=mysql.log | |
LOG_ERROR=mysql_error.log | |
LOG_SLOW_QUERIES=slow.log | |
SOCKET="/var/lib/mysql/mysql.sock" | |
#Number of copies | |
COPIES=365 | |
SHARDING=$(date -d "yesterday" +"%Y-%m-%d.%H:%M:%S") | |
mkdir -p ${LOGDIR}/${SHARDING} | |
while read logfile age | |
do | |
mv ${DATADIR}/$logfile ${LOGDIR}/${SHARDING} | |
done << EOF | |
${LOG} | |
${LOG_ERROR} | |
${LOG_SLOW_QUERIES} | |
EOF | |
mysqladmin -u${USER} -p${PASS} --socket=${SOCKET} flush-logs | |
gzip ${LOGDIR}/${SHARDING}/*.log | |
find $LOGDIR -type f -ctime +$COPIES -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment