logrotate简介日志轮转特别适用于具有固定文件名的日志文件,比如MySQL的出错日志、常规查询日志、慢查询日志等。Linux系统有一个非常好用的根据logratate可以实现自动轮转,本文介绍它的原理和用法。
logrotate是管理日志文件的工具,在CentOS系统中,命令的位置在/usr/sbin/logrotate,常用的操作如:
-d, --debug Don't do anything, just test (implies -v)
-f, --force Force file rotation
注意:带有-d参数,并不会产生新日志.
logrotate一般每天由cron运行一次.标准的配置文件是/etc/logrotate.conf,而/etc/logrotate.d目录也是保存配置文件的位置.
logrotate常见选项:
/-----------------------------------------------
选项 | 含义
-----------------------------------------------
compress | 压缩日志文件的所有非当前版本
copy | 复制当前的日志文件,忽略create参数
copytruncate | 复制当前的日志文件,并置空当前文件
daily | 每天轮日志文件i
dateext | 轮换的日志后缀为-YYYYMMDD格式
delaycompress | 压缩除了当前和最近之外的所有其他版本
missingok | 如果日志不存在,不会报错
notifempty | 如果日志为空,则不轮换
rotate n | 在轮换方案中包含n个版本的日志
size=logsize | 如果日志文件大于logsize才轮换
-----------------------------------------------/
默认情况下,logrotate部署为每天运行的cronjob,你可以在目录/etc/cron.daily里找到名为logrotate的配置文件。那么它是在每天的上面时候运行的呢?打开文件/etc/crontab就知道了,下面是我机器上的情况:
代码如下
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
从上面的配置我们可以知道,/etc/cron.daily是在每天凌晨4:02执行。也就是说,每天4:02分/etc/cron.daily/logrotate将会自动执行,下面是它的内容:
代码如下
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
从上面我们可以知道,logratate默认的配置文件是/etc/logrotate.conf,下面是它的内容:
代码如下
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
从/etc/logrotate.conf配置可以知道,这个默认的配置文件将读取目录/etc/logrotate.d,所以我们只要把自己写的配置文件放到/etc/logrotate.d目录下即可。
mysql 配置篇
MySQL本省提供了一个rotate的参考配置文件,在mysql安装目录下的support-files目录里,文件名为mysql-log-rotate,内容如下:
代码如下
# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/data/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = # user= root
#
# where "" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !
/usr/local/mysql/data/mysqld.log {
# create 600 mysql mysql
notifempty
daily
rotate 3
missingok
compress
postrotate
# just if mysqld is really running
if test -x /usr/local/mysql/bin/mysqladmin &&
/usr/local/mysql/bin/mysqladmin ping &>/dev/null
then
/usr/local/mysql/bin/mysqladmin flush-logs
fi
endscript
}
从上面的注释信息可以看到步骤:
1.创建MySQL root密码文件
vi /root/.my.cnf
编辑如下内容
代码如下
[mysqladmin]
password = user= root
注: 是你的root 密码
2.给root 读的权限
chmod 600 /root/.my.cnf
3.把mysql-log-rotate拷贝至/etc/logrotate.d目录下,修改其内容为:
代码如下
/usr/local/mysql/data/*.log {
create 600 mysql mysql
notifempty
daily
rotate 7
missingok
compress
postrotate
# http://www.111com.net # just if mysqld is really running
if test -x /usr/local/mysql/bin/mysqladmin &&
/usr/local/mysql/bin/mysqladmin ping &>/dev/null
then
/usr/local/mysql/bin/mysqladmin flush-logs
fi
endscript
}
4.执行以下命令测试
代码如下
/usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate
nginx 配置篇
将下面代码 写入 /etc/logrotate.d/nginx
代码如下
/usr/local/nginx/log/*.log {
create 600 www www
notifempty
daily
rotate 7
missingok
compress
postrotate
#servic nginx reload
endscript
}