Archivemail

De wikiGite

Archive les mails au format mailbox ou maildir.

Install :

apt-get install archivemail

Script à mettre dans un répertoire accessible aux utilisateurs :

#!/bin/bash
# Archivemail backup mbox or Maildir to mbox gzipped files.
# To read/search a backuped mail, use mutt -f [path_to_archive_file.gz].
# Then forward it with mutt to a valid email address.
# The variable BACKUPDIR gives the path of backuped files.

MAILDIR=$HOME/Maildir
BACKUP=$HOME/archivemails
LOG=/opt/tools/log
LOGFILE=$BACKUP/archivemail.log
BACKUPDIR=$BACKUP/arch$(date +%d%m%y)

echo $(date) >> $LOGFILE

# $1 is the retention policy. If not set, the default of archimail is 180 days.
if [ ! -z $1 ]
then
	RP="-d $1"
fi

# Archivemail don't work on symbolic links. Retreive real directory path.
if [ -h $MAILDIR ]
then
	MAILDIR=$(ls -l /var/www/web/Maildir | awk 'BEGIN{FS="-> "}{print $2}')
fi

if [ ! -d $BACKUPDIR ]
then
	mkdir -p $BACKUPDIR
	if [ $? -ne 0 ]
	then
		echo "Creation repertoire $BACKUPDIR impossible !" >> $LOGFILE
		exit 1
	fi
fi

# "For" doesn't handle directory names with space. Use "while" loop.
find $MAILDIR -type d | (while read mfile
do
	if [ "$(echo $mfile | grep -i spam)" != "" -o "$(echo $mfile | grep -i junk)" != "" ]
	then
# If spams, we force delete instead of archive
		archivemail -d 60 --delete "$mfile" >> $LOGFILE 2>&1
	else
		archivemail $RP -o $BACKUPDIR "$mfile" >> $LOGFILE 2>&1
	fi
done)
echo "Fin de traitement : $(date)">> $LOGFILE

Planifier ensuite par utilisateur dans /etc/cron.d/archivemail-<nom_utilisateur> :

* * * * 0       user1    /opt/tools/archivemail.sh 360

Pour lire les mails archiver, utiliser mutt en ligne de commande :

mutt -f [path_to_archive_file.gz]