CentOSで構築されたメールサーバ(Postfix)のログ保存期間を変更したメモ
Mail logはデフォルト設定では
- syslog経由でログが記録される
- syslogは毎週日曜日にrotateされる
- syslogは4世代保存される
-rw——- 1 root root 978899 11月 8 09:53 /var/log/maillog
-rw——- 1 root root 1506470 11月 4 04:02 /var/log/maillog.1
-rw——- 1 root root 1593125 10月 28 04:02 /var/log/maillog.2
-rw——- 1 root root 1592808 10月 21 04:02 /var/log/maillog.3
-rw——- 1 root root 1683844 10月 14 04:02 /var/log/maillog.4
logrotateの設定ファイルは /etc/logrotate.confでデフォルトでは下記の内容になっています
rotate 4の部分が何世代管理するか設定する場所になります
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@host1 ~]# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs <font color=red>rotate 4</font> # 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. |
さて、ここでmail logだけrotate 4からrotate 12に変更したい場合 /etc/logrotate.confを変更するとすべてのログに影響してディスクを無駄に消費する可能性があります
そこで、syslogのlog rotate設定の /etc/logrotate.d/syslogを設定変更します
/etc/logrotate.d/syslogのデフォルトは下記の内容です
1 2 3 4 5 6 7 8 |
[root@host1 ~]# cat /etc/logrotate.d/syslog /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true endscript } |
ここで下記のように変更してもいいのですが、そうすると/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cronすべてのログが12世代保存されてしまいます
1 2 3 4 5 6 7 8 |
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { <font color=red>rotate 12</font> sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true endscript } |
maillogのみrotate 12にしたいので下記のように変更しました
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@host1 ~]# cat /etc/logrotate.d/syslog <font colot=red>/var/log/messages /var/log/secure /var/log/spooler /var/log/boot.log /var/log/cron</font> { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true endscript } <font color=red> /var/log/maillog { rotate 12 sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true endscript } </font> |
これでmaillogのみrotate 12になります
logrotateの動作は下記のコマンドで確認できます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@host1 ~]# logrotate -v /etc/logrotate.conf reading config file /etc/logrotate.conf including /etc/logrotate.d reading config file acpid (snip) rotating pattern: /var/log/messages /var/log/secure /var/log/spooler /var/log/boot.log /var/log/cron weekly (4 rotations) empty log files are rotated, old logs are removed considering log /var/log/messages log does not need rotating considering log /var/log/secure log does not need rotating considering log /var/log/spooler log does not need rotating considering log /var/log/boot.log log does not need rotating considering log /var/log/cron log does not need rotating not running postrotate script, since no logs were rotated <font color=red>rotating pattern: /var/log/maillog weekly (12 rotations)</font> empty log files are rotated, old logs are removed considering log /var/log/maillog log does not need rotating not running postrotate script, since no logs were rotated (snip) |