Chinaunix首页 | 论坛 | 博客
  • 博客访问: 323876
  • 博文数量: 114
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 1110
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-23 17:34
文章分类

全部博文(114)

文章存档

2011年(2)

2010年(22)

2009年(56)

2008年(34)

我的朋友

分类: LINUX

2010-06-21 17:44:18

Apache 2.2 log轮询及分期归档功能手册

 

OS版本:Redhat ES5

Kernel2.6.18-164.el5

Apache版本:2.2.3-31(系统自带)

注意:以下操作均在root账号下进行

 

1.       更改apache日志生成样式

vim /etc/httpd/conf/httpd.conf(修改apache主配置文件)

log定义字段,追加以下内容:

ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/error_log.%Y-%m-%d~week  604800 480"477行)

CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log.%Y-%m-%d~week  604800 480" common506行)

其中,

%Y-%m-%d-%H-%M-%S分别对应:年---小时-分钟-

604800为秒计的一周时间,480为北京时间相对于格林威治时间的偏移值,既480分钟(8小时)

service httpd restart,(重启apache服务器,新的日志将按规定格式生成)

 

2.       建立日志的分类归档目录

mkdir /var/log/httpd/log_history(建立log_history目录做为主目录)

mkdir /var/log/httpd/log_history/2010-{01,02,03,04,05,06,07,08,09,10,11,12}(建立2010年度12个月对应的目录)

3.       建立自动分类归档脚本

vim /root/ http_log_bak.sh(建立文件)

追加以下脚本内容:

#!/bin/bash

l_time=$(date --date "1 months ago" +"%Y-%m")

to_path="/var/log/httpd/log_history/$l_time/"

if [ -d $to_path ] ;

then

  cp -r /var/log/httpd/access_log.$l_time* $to_path

  cp -r /var/log/httpd/error_log.$l_time* $to_path

else

  mkdir -p $to_path

  cp -r /var/log/httpd/access_log.$l_time* $to_path

  cp -r /var/log/httpd/error_log.$l_time* $to_path

fi

其中, l_time变量是计算当前时间的上个月(1 months ago);

If语句判断目标目录是否存在,如过存在,直接拷贝LOG文件,如果不存在,先建立目标目录,再拷贝文件。

存盘退出vi编辑器。

chmod a+x /root/ http_log_bak.sh(为脚本赋予执行权限)

 

4.       利用crontab命令自动调用脚本执行

crontab –e(追加cron任务)

1 0 7 * * /bin/sh /root/http_log_bak.sh

(说明:第一位“1”,代表1分钟,第二位“0代表0小时,既凌晨0点,第三位“7”代表每月第7天,既7号,第四位“*”代表每周1-7天斗执行,第五位“*”代表全年1-12月每月都执行,总体意思是:每月的70001分执行/root/http_log_bak.sh脚本文件,将/var/log/httpd/下的对应log文件copy/var/log/httpd/log_history/下的对应目录中)

退出编辑并存盘。

crontab –l(显示当前cron任务队列,应该可以看到刚才追加的任务)

 

完成

 

发现的问题:以上方案是基于“周”为单位的LOG归档,在此方案中,apache并不是以重启的时间或者某月、某年1号零时为时间起点,而是以周四的零点做时间起点,这个让我很费解!所以才出现了在crontab中,是每月7号零点1分才开始执行脚本!
阅读(1151) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~