Apacheのログでlogrotateを設定してみる

元々こんな設定が入ってる状態

o /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

# 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 -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}

# system-specific logs may be also be configured here.

こんなのつくってみました

o /etc/logrotate.d/apache2

/path/logs/access_log /path/logs/error_log {
daily
rotate 90
missingok
compress
sharedscripts
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}

ちぇっくしてみる

-d はdryrun 試行するというオプション

# logrotate -d /etc/logrotate.d/apache2

reading config file /etc/logrotate.d/apache2
reading config info for /path/logs/access_log /path/logs/error_log

Handling 1 logs

rotating pattern: /path/access_log /path/logs/error_log  after 1 days (90 rotations)
empty log files are rotated, old logs are removed
considering log /path/logs/access_log
log does not need rotating
considering log /path/logs/error_log
log does not need rotating
not running postrotate script, since no logs were rotated

という感じ。
強制的にlogrotateの設定を反映する為には -f オプションをつければよいとのことなので
dovecotのログを試しにローテートしてみました。

o /etc/logrotate.d/dovecot

/var/log/dovecot.log {
weekly
rotate 4
missingok
compress
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
endscript
}

# logrotate -fv /etc/logrotate.d/dovecot

reading config file /etc/logrotate.d/dovecot
reading config info for /var/log/dovecot.log

Handling 1 logs

rotating pattern: /var/log/dovecot.log forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/dovecot.log
log needs rotating
rotating log /var/log/dovecot.log, log->rotateCount is 4
renaming /var/log/dovecot.log.4.gz to /var/log/dovecot.log.5.gz (rotatecount 4, logstart 1, i 4),
old log /var/log/dovecot.log.4.gz does not exist
renaming /var/log/dovecot.log.3.gz to /var/log/dovecot.log.4.gz (rotatecount 4, logstart 1, i 3),
old log /var/log/dovecot.log.3.gz does not exist
renaming /var/log/dovecot.log.2.gz to /var/log/dovecot.log.3.gz (rotatecount 4, logstart 1, i 2),
old log /var/log/dovecot.log.2.gz does not exist
renaming /var/log/dovecot.log.1.gz to /var/log/dovecot.log.2.gz (rotatecount 4, logstart 1, i 1),
old log /var/log/dovecot.log.1.gz does not exist
renaming /var/log/dovecot.log.0.gz to /var/log/dovecot.log.1.gz (rotatecount 4, logstart 1, i 0),
old log /var/log/dovecot.log.0.gz does not exist
log /var/log/dovecot.log.5.gz doesn't exist -- won't try to dispose of it
renaming /var/log/dovecot.log to /var/log/dovecot.log.1
running postrotate script
compressing log with: /bin/gzip

こんな結果が出力されて、ログファイルも無事にローテートされてました。
ぐーぐる先生に質問してみたんだけど案外みつからなくて、
ログのローテートってあんまりやらないのかな。。