logrotate 使用详解

logrotate是个十分有用的工具,它能够自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,你能够设置logrotate,让/var/log/foo日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运做彻底自动化,没必要进行任何进一步的人为干预。node

主流Linux发行版上都默认安装有logrotate包,若是出于某种缘由,logrotate没有出如今里头,你可使用apt-get或yum命令来安装。vim

在Debian或Ubuntu上:dom

# apt-get install logrotate cron

在Fedora,CentOS或RHEL上:ide

# yum install logrotate crontabs

logrotate的配置文件是/etc/logrotate.conf,一般不须要对它进行修改。日志文件的轮循设置在独立的配置文件中,它(们)放在/etc/logrotate.d/目录下。工具

Logrotate有许多可配置参数,也可以使用man命令来查询:post

compress                        经过gzip压缩转储之后的日志
nocompress                      不压缩
copytruncate                    用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate                  备份日志文件可是不截断
create mode owner group         转储文件,使用指定的文件模式建立新的日志文件
nocreate                        不创建新的日志文件
delaycompress 和 compress        一块儿使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress                 覆盖 delaycompress 选项,转储同时压缩。
errors address                   专储时的错误信息发送到指定的Email 地址
ifempty                         即便是空文件也转储,这个是 logrotate 的缺省选项。
notifempty                      若是是空文件的话,不转储
mail address                    把转储的日志文件发送到指定的E-mail 地址
nomail                          转储时不发送日志文件
olddir directory                转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir                        转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript             在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行
postrotate/endscript            在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行
daily                           指定转储周期为天天
weekly                          指定转储周期为每周
monthly                         指定转储周期为每个月
rotate count                    指定日志文件删除以前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate   不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size                       当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)

样例一

在第一个样例中,咱们将建立一个10MB的日志文件/var/log/log-file。咱们将展现怎样使用logrotate来管理该日志文件。ui

咱们从建立一个日志文件开始吧,而后在其中填入一个10MB的随机比特流数据。spa

# touch /var/log/log-file

# head -c 10M < /dev/urandom > /var/log/log-file

因为如今日志文件已经准备好,咱们将配置logrotate来轮循该日志文件。让咱们为该文件建立一个配置文件。命令行

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    monthly
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

样例二

在本例中,咱们只想要轮循一个日志文件,然而日志文件大小能够增加到50MB。日志

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    size=50M
    rotate 5
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

 样例三

咱们想要让旧日志文件以建立日期命名,这能够经过添加dateext常熟实现。

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    monthly
    rotate 5
    dateext
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

这将让归档文件在它们的文件名中包含日期信息。

排障

这里提供了一些logrotate设置的排障提示。

1. 手动运行logrotate

logrotate能够在任什么时候候从命令行手动调用。

要调用为/etc/lograte.d/下配置的全部日志调用logrotate

# logrotate /etc/logrotate.conf

要为某个特定的配置调用logrotate:

# logrotate /etc/logrotate.d/log-file

2. 演练

排障过程当中的最佳选择是使用‘-d’选项以预演方式运行logrotate。要进行验证,不用实际轮循任何日志文件,能够模拟演练日志轮循并显示其输出。

# logrotate -d /etc/logrotate.d/log-file

正如咱们从上面的输出结果能够看到的,logrotate判断该轮循是没必要要的。若是文件的时间小于一天,这就会发生了。

3. 强制轮循

即便轮循条件没有知足,咱们也能够经过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。

# logrotate -vf /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-file
reading config info for /var/log/log-file
 
Handling 1 logs
 
rotating pattern: /var/log/log-file  forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/log-file
  log needs rotating
rotating log /var/log/log-file, log->rotateCount is 5
dateext suffix '-20140916'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/log-file.5.gz to /var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),
old log /var/log/log-file.5.gz does not exist
renaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4),
old log /var/log/log-file.4.gz does not exist
. . .
renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),
old log /var/log/log-file.0.gz does not exist
log /var/log/log-file.6.gz doesn't exist -- won't try to dispose of it
renaming /var/log/log-file to /var/log/log-file.1
creating new /var/log/log-file mode = 0644 uid = 0 gid = 0
running postrotate script
compressing log with: /bin/gzip

4. Logrotate的记录日志

logrotate自身的日志一般存放于/var/lib/logrotate/status目录。若是处于排障目的,咱们想要logrotate记录到任何指定的文件,咱们能够指定像下面这样从命令行指定。

# logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file

5. Logrotate定时任务

logrotate须要的cron任务应该在安装时就自动建立了