Skip to content

Instantly share code, notes, and snippets.

@netkiller
Last active December 30, 2015 23:59
Show Gist options
  • Save netkiller/7904307 to your computer and use it in GitHub Desktop.
Save netkiller/7904307 to your computer and use it in GitHub Desktop.
mysql日志切割工具
#!/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