天天看点

清除n天以前的日志文件以及mysql-bin文件

前些日子,系统上线了,发现tomcat 下的日志增长得挺快的,写了个脚本清除n天以前的日志

web_def_tomcat_log_path=/Application/tomcat/log  

$web_def_tomcat_log_expire_days=7  

if [ "$web_def_tomcat_log_expire_days" -gt 0 ]  

then 

echo "find $web_def_tomcat_log_path -follow -mtime +$web_def_tomcat_log_expire_days -name '*.log' -exec rm -f {} \;" 

find $web_def_tomcat_log_path -follow -mtime +$web_def_tomcat_log_expire_days -name '*.log' -exec rm -f {} \;  

fi 

由于web的mysql下面挂的同步太多了,每天产生1-2G的mysql-bin文件,因此也写了个清除mysql-bin的文件,这个文件会至少保留最新的一个mysql-bin.0*文件

web_def_host="1.2.3.4" #mysql的ip  

web_def_port=3306 #mysql的端口  

web_def_username="tester" #mysql的用户  

web_def_password="123" #mysql的密码  

web_def_mysql_expire_logs_days=7 #删除7天以前的mysql-bin,但是最后至少保留一个文件  

mysql_exe="mysql -h $web_def_host -P $web_def_port -u $web_def_username --password=$web_def_password -e " 

if [ "$web_def_mysql_expire_logs_days" -gt 0 ]  

#获取n天以前被修改的mysql-bin文件,  

logFileName=`find $web_def_mysql_data_path -follow -atime -$web_def_mysql_expire_logs_days -name 'mysql-bin.0*' |sort|head -1`  

if [ "$logFileName" != "" ]  

logFileName=`basename $logFileName`  

echo $mysql_exe "\"PURGE MASTER LOGS TO '$logFileName'\"" 

$mysql_exe "PURGE MASTER LOGS TO '$logFileName'";  

fi  

本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/610317,如需转载请自行联系原作者

继续阅读