Notes on rotating mail archive file made by synonym
As the root user, I ran
crontab -u root -e
to edit the crontab, then added these two lines:
# rotate the mail archive each night
55 23 * * * /usr/local/bin/rotate_archive 2>&1
Which tells the server to run the rotate_archive script every night at 23:55
(11:55pm).
The contents of rotate_archive are:
#!/bin/bash
# set some variables
export STORAGEBASE="/home/mailarchive"
export YEAR=$(date +%Y)
export MONTH=$(date +%m)
export FILENAME=$(date +%Y%m%d)
export FULLFILENAME=$STORAGEBASE/$YEAR/$MONTH/$FILENAME
# make a directory to store the files in, in case it doesn't already exist.
mkdir $STORAGEBASE/$YEAR > /dev/null 2>&1
mkdir $STORAGEBASE/$YEAR/$MONTH > /dev/null 2>&1
# rename the mailarchive inbox so it's quick, and new mail will just
# make a new folder and start a new one. Then move it to the
# other file system where we will keep it (may take longer to
# move from one filesystem to another than on same fs).
mv /var/spool/mail/mailarchive /var/spool/mail/$FILENAME
mv /var/spool/mail/$FILENAME $FULLFILENAME
chown mailarchive $FULLFILENAME
That script makes a directory named the current year in /home/mailarhive
for example /home/mailarchive/2009
and inside that directory makes a directory named the number of the month
for example /home/mailarchive/2009/10
and inside there makes a file named year+month+day
for example /home/mailarchive/2009/10/20091031
You end up with a file for each day, organized into moonth folders, inside of
year folders.