All good now:
# cat /etc/init.d/archive-system-mail
#! /bin/sh
### BEGIN INIT INFO
# Provides: mailarchive
# Required-Start: $local_fs $remote_fs $syslog
# Required-Stop: $local_fs $remote_fs $syslog
# X-Start-Before: exim4 dovecot
# X-Stop-After: exim4 dovecot
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Save exim4 system_filter archive
# Description: This file should be ran at boot time before either
# exim4 or dovecot is started. It saves mail that is
# created via rules within the /etc/system_filter and
# it's related log file.
### END INIT INFO
# Author: Andrew McGlashan <andrew.mcglashan@affinityvision.com.au>
# set -x
#
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="Save exim4 system_filter archive"
NAME=archive-system-mail
DAEMON=/usr/local/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
case "$1" in
start)
# Run the required script
$DAEMON
;;
stop|status|restart|force-reload)
exit 0
;;
*)
echo "Usage: $SCRIPTNAME {start}" >&2
exit 3
;;
esac
:
And the script itself:
# cat /usr/local/sbin/archive-system-mail
#! /bin/sh
do_cleanup()
{
# Cleanup
echo -e "\a\a\aPerforming cleanup"
exit $1
}
do_error_exit()
{
# Display error message and exit
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
do_cleanup 1
}
do_get_archivedir()
{
for VER in a b c d e f g h i j k l m n o p q r s t u v w x y z
do
ARCHIVE_DIR=/backup/mail/system_filter_all_mail/archive/${DATEX}${VER}
[ ! -d ${ARCHIVE_DIR} ] && return 0
done
return 1
}
# main
trap "do_cleanup 99" HUP INT TERM
# Global Variables
ARCHIVE_DIR=
DATEX=$(/bin/date +%Y%m%d)
PROGNAME=$(/usr/bin/basename $0)
[ -r /backup/mail/system_filter_all_mail/log--lots ] || do_error_exit
"Missing or unreadable SOURCE log--lots file"
(
# Exit if the we cannot get a suitable archive directory
do_get_archivedir || do_error_exit "Cannot get archive dir"
[ ! -d ${ARCHIVE_DIR} ] && /bin/mkdir ${ARCHIVE_DIR}
[ -d ${ARCHIVE_DIR} ] || do_error_exit "The required archive
directory wasn't created as expected"
(
# Exit if the we cannot cd in to the archive directory
cd ${ARCHIVE_DIR} || do_error_exit "Cannot cd in to the
archive directory"
/bin/mv /backup/mail/system_filter_all_mail/log--lots .
[ -r log--lots ] || do_error_exit "Missing or unreadable
archive copy of log--lots file"
/bin/mv /backup/mail/system_filter_all_mail/Maildir .
[ -d Maildir ] || do_error_exit "Missing archive of
Maildir folder"
# Fix log--lots file to have only ONE line per record, in cases
where system_filter wrote multi-lines
TMP_LOG_LOTS=${ARCHIVE_DIR}/log--lots.$$
/usr/bin/touch -r log--lots $TMP_LOG_LOTS
echo '' >> log--lots
sed -i -e ':a' -e 'N' -e '$!ba' -e 's/\n[\t\ *]/\ /g' log--lots
sed -i -e '/^$/d' log--lots
/usr/bin/touch -r $TMP_LOG_LOTS log--lots
/bin/ls -ladrt /backup/mail/system_filter_all_mail/log--lots
${ARCHIVE_DIR}/*
)
) 2>&1 | /usr/bin/tee /var/log/${PROGNAME}--$(date +%Y%m%d%H%M%S).log
Cheers
AndrewM
Attachment:
signature.asc
Description: OpenPGP digital signature