Custom Mail Log Rotation Script



Thanks to google, I was able to find a command to create files as filename_DATE.

Usually, all logs are automatically rotated if logrotate is installed. However, I’m creating a custom script to rotate mail logs. Thought I'd share. ^_^

Paste your text here.# vim /root/rotate

##### this script will be used by crond to rotate mail log #####
#!/bin/sh

/etc/init.d/postfix stop
/etc/init.d/dovecot stop

cat /var/log/maillog > "/var/log/maillog_`date '+%d-%m-%Y'`"
echo > /var/log/maillog

gzip "/var/log/maillog_`date '+%d-%m-%Y'`"


/etc/init.d/postfix start
/etc/init.d/dovecot start

##### end of script #####


# chmod 744 /root/rotate


CRON

# crontab -e

#### run the script every 10 days ####

00 00 */10 * * /root/rotate

:wq

/etc/init.d/crond restart

Comments