A client running a postfix server called me the other day wanting to configure log rotation for Postfix on Linux. This was a Redhat box and I didn’t have much experience configuring log rotation. After my trip to Coyoacan in Mexico City I was very relaxed and started putting things together. Below is what I found.

To configure log rotation for Postfix on a Linux server, you can create a custom logrotate configuration file specifically for Postfix log files. Postfix logs are usually located at /var/log/maillog, /var/log/mail.log or something like that. You can create a logrotate configuration file at /etc/logrotate.d/postfix Here’s a sample file:

/var/log/maillog
{
daily
rotate 14
compress
dateext
missingok
notifempty
copytruncate
}

Explanation log rotation

  • rotate 14: Keep up to 14 rotated log files.
  • daily: Rotate the log files daily.
  • missingok: Do not generate an error if the log file is missing.
  • notifempty: Do not rotate the log file if it’s empty.
  • delaycompress: Compress the rotated log files one day after rotation.
  • compress: Compress the rotated log files using gzip.
  • postrotate/endscript: Execute a custom command after log rotation. This one is very cool! I didn’t use it on this example. Good for running scripts before or after.

Save the Configuration File: Save the changes to the “postfix” configuration file and exit the text editor.

Test Log Rotation: Test the log rotation configuration using the logrotate command with the -d or --debug option to simulate rotation without actually rotating files:

sudo logrotate -d /etc/logrotate.d/postfix

You can add -v option for verbose output. Use it! Verify that log rotation works as expected and that no errors occur.

Automatic Rotation: Logrotate is typically run as a cron job. It will automatically rotate log files based on the schedule defined in the configuration file.

As usual, you can contact me if you have any questions or like to collaborate. I’m working on fixing my online shop for now. Stay tuned.

Leave a comment

Your email address will not be published. Required fields are marked *