Sunday, April 16, 2006

Lame Mail Rotating Script

Yesterday nite when chit-chatting in #myoss, fellow in the channel - filex asking whether it is possible to rotate the email or maybe deleting the email every month, the quicky for sure is using cron job, however you are better to write a simple shell script to help yourself administrating the email. Here's the script -

#!/bin/sh

# Simple yet useful mail rotating script
# Written by geek00L [ 20060416 ]
# Revision
# None

maildir=/var/log/mail
backdir=/backup/mail

# Backup all the user's mail

cd $maildir

for i in `ls -1 $maildir`;
do
tar cvzf $i.tar.gz $i
mv $i.tar.gz $backdir
done

# HouseKeeping - Delete those files that haven't been accessed or modified for 30 days

find $maildir -type f \( -atime +30 -o -mtime +30 \) | xargs rm -rf

# EOF (:])

Remember to tweak maildir and backdir path. The find command will locate any 'inactive' mail and deleting it. Then just simply add this monthly cronjob will do.

30 1 1 * * root /pathto/mail-rotate.sh

I know this script is pretty lame, however hopefully that helps :P

Cheers :]

4 comments:

Anonymous said...

You're very helpful dude :)

C.S.Lee said...

filex,

Cheers, dude :]

Joel Esler said...

Geek, instead of piping find to xargs, why don't you use the "-exec" arguement within find? -exec 'rm -rf'

C.S.Lee said...

hey joel,

I read somewhere xargs is more capable of controlling buffer and low overhead so I just think using it would be better and faster. Actually I used to use -exec and combine with grep to locate the files I want.

Cheers :)