mysql全日志自动化脚本
【1】启动脚本
#!/bin/bash open_flag=`mysql -e"show variables like 'general_log';" -N|awk -F" " '{print $2}'` file_url=`mysql -e"show variables like 'general_log_file%';" -N|awk -F" " '{print $2}'` file_dir=`dirname ${file_url}` file_name=`echo $file_url|awk -F'/' '{print $NF}'` if [ $open_flag != "ON" -a $open_flag != "1" ];then mysql -e "set global general_log = on;"
【2】守护进程脚本
#!/bin/bash open_flag=`mysql -e"show variables like 'general_log';" -N|awk -F" " '{print $2}'` now_file_url=`mysql -e"show variables like 'general_log_file%';" -N|awk -F" " '{print $2}'` now_file_dir=`dirname ${now_file_url}` file_name=`echo ${now_file_url}|awk -F'/' '{print $NF}'|awk -F'.' '{print $1 }'` if [ $# -gt 1 ];then day_change=$1 day_expire=$2 else day_change=7 day_expire=30 fi changelog_flag=0 while [ 1 ] do if [ $open_flag != "ON" -a $open_flag != "1" ];then mysql -e "set global general_log = on;" fi if [ $((`date +%d`%${day_change})) -eq 0 -a ${changelog_flag} -eq 0 ];then tmp_date=`date +%F_%H-%M-%S` tmp_date_s=`date +%s` mysql -e "set global general_log=off;" mv ${now_file_url} ${now_file_dir}/${file_name}_${tmp_date}_general.log mysql -e "set global general_log=on;" changelog_flag=1 elif [ $((`date +%s`-${tmp_date_s})) -ge 86400 ];then changelog_flag=0 fi #delete expire find ${now_file_dir} -mtime +${day_expire} -name '*general.log' -exec rm -rf {} \; sleep 3 done