3月 21

Nginx服务使用的脚本

[编写人:张子萌 2010]

Nginx服务器时使用的脚本.

1.在nginx日志出现错误后发送告警邮件,使用该脚本会在nginx的sbin目录下产生last_error_log,错误文件格式

为nginx默认

#!/bin/sh
##############################################
# AUTHOR: simon-zzm
# back log
# Ver : 1.0 For Production
# description:
# 2010-03-12 simon-zzm@163.com
##############################################
######set ip Email base path
IP=172.2.2.2
admin_mail=simon-zzm@163.com
base_path=/usr/local/nginx

###run
cut_error_log=`/bin/cat ${base_path}/sbin/last_error_log`
last_error_log=`/usr/bin/tail -1 ${base_path}/logs/error.log|/bin/cut -c 1-19`
if [ “$cut_error_log” != “$last_error_log” ];then
/usr/bin/tail -5 ${base_path}/logs/error.log>$base_path/sbin/mail1.txt
/bin/mail -s $IP_nginx_error_log $admin_mail<$base_path/sbin/mail1.txt
/bin/rm -rf $base_path/sbin/mail1.txt
echo $last_error_log>${base_path}/sbin/last_error_log
fi

2.nginx日志切割,每个小时切割一个日志,

#!/bin/sh

##############################################

# AUTHOR: simon-zzm

# back log

# Ver : 1.0 For Production

# description:

# 2010-03-12 simon-zzm@163.com

##############################################

###set base path、use every an hour to cut log
log_dir=”/usr/local/nginx/logs”
date_dir=`date +%Y%m%d%H`

###run
/bin/mv ${log_dir}/access.log ${log_dir}/access_${date_dir}.log
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

3.日志备份部分参照通用性日志备份

http://simon-zzm.blog.163.com/blog/static/88809522201021523828112/

3月 15

日志 压缩 备份 定时删除脚本

[编写整理:simonzhang 2010-03-15 2012-04-08修改]

  在linux下有大量日志需要压缩备份,并定期清理长期保存的备份日志。对于多处日志存放使用数组进行循环处理。脚本修改好后,设置定时任务即可。

#!/bin/sh
##############################################
# AUTHOR: simonzhang
# back log
# Ver : 1.1 For Production
# description: 
# 2010-03-12  
##############################################
####### set log patch
log_path=("/usr/local/nginx/logs/" "/usr/local/tomcat6/logs/" )
####### set backup log patch
bak_log_path=("/usr/local/nginx/logs/back" "/usr/local/tomcat6/logs/back")

####### set backup  3 day ago log
backupdays=3

#######clear 180 day ago compress log
deletedays=180

#######  start
for (( i=0 ; i<${#log_path[@]} ; i++ ))
do
        cd ${log_path[i]}
    if [ ! -f ${bak_log_path[i]} ] ; then
            /bin/mkdir -p ${bak_log_path[i]}
    fi
        /usr/bin/find  * -ctime +$backupdays -maxdepth 0 -not -name *.pid -not -name error* -exec zip -m {}.zip  {} \;
        /bin/mv *.zip ${bak_log_path[$i]}
        cd ${bak_log_path[$i]}
        echo /usr/bin/find  * -ctime +$deletedays -maxdepth 0 -exec rm {} \;
done
############  end