2013-12-11 15:16:51

MySQL 数据库日志切割脚本

出处: http://netkiller.github.io/
#!/bin/bash
USER=backup
PASS=SaJePoM6BAPOmOFOd7Xo3e1A52vEPE
LOGDIR="/tmp/backup"
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=30
SHARDING=$(date -d "yesterday" +"%Y-%m-%d")
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
find $LOGDIR -type f -ctime +$COPIES -delete
发表回复