#!/bin/sh ###################################################################### # # Move the new email from transit to user back-up mailbox # ###################################################################### max=90 # 90 % full min=5 # 5% of the capacity disk=/dev/ad0s1g # the disk holding the mail back-up directories # If the /holding disk is more than $max% full, it will remove oldest files # for a total size of $min% of the total disk size. # So when the disk reaches 90%, cleaning is made to go back to 85%. This avoids # too frequent cleaning. # On the present machine (PIII 500, 128 MB ram, 26 GB of holding disk) it takes # less than 4 minutes to remove 5% of old emails. space=`/bin/df -t ufs -k | /usr/bin/grep $disk | /usr/bin/sed -e 's/ */ /g' \ | /usr/bin/cut -d' ' -f2-2` min=`echo $space\*1024\*$min/100|bc` echo Starting move_in while [ 1 ]; do # check the disk space space=`/bin/df -t ufs | /usr/bin/grep $disk | /usr/bin/sed -e 's/ */ /g' \ | /usr/bin/cut -d' ' -f5-5 | /usr/bin/sed -e 's/%//'` if [ $space -gt $max ] ; then # need to remove some old stuff. Find the older messages received # from any user mailbox and add them to match the $min size /usr/bin/find /home -mindepth 5 -ls | /usr/bin/grep /Maildir/cur/ | /usr/bin/sort -t/ -n +6 | /usr/bin/awk '{sum+=$7; if (sum < '$min') print $11;}'|xargs /bin/rm $i # delete empty users # we can remove the directory, there is very little risk that # we will have to recreate it just after to move in new email # at the current rate (early 2006), cleaning should occur once a year # so empty directory will be account that have been dead for more # than a year, that is: deleted accounts for i in `/bin/ls /home/`; do for j in `/bin/ls /home/$i/`; do list=`/bin/ls /home/$i/$j/Maildir/cur/` if [ -z "$list" ] ; then /bin/rm -rf /home/$i/$j fi done done fi # now copy new emails # loop on the user name for i in `/bin/ls /transit/mail-temp/save/`; do list=`/bin/ls /transit/mail-temp/save/$i/new/` homedir=`/usr/bin/getent passwd $i | /usr/bin/cut -d ":" -f6-6` gid=`/usr/bin/getent passwd $i | /usr/bin/cut -d ":" -f4-4` uid=`/usr/bin/getent passwd $i | /usr/bin/cut -d ":" -f3-3` inhome=`echo $homedir | /usr/bin/egrep "^/home/"` # proceed to copy only if the user is in /home # because we cannot recreate the homedir for root if [ ! -z "$inhome" ] ; then # proceed to the copy only if there are incoming emails if [ ! -z "$list" ] ; then # create the directory if needed if [ ! -d $homedir/Maildir/cur ] ; then if [ ! -d $homedir/Maildir ] ; then if [ ! -d $homedir ] ; then /bin/mkdir $homedir /bin/chmod -R 0700 $homedir /usr/sbin/chown -R $uid:$gid $homedir fi /bin/mkdir $homedir/Maildir /bin/chmod -R 0700 $homedir/Maildir /usr/sbin/chown -R $uid:$gid $homedir/Maildir fi /bin/mkdir $homedir/Maildir/cur $homedir/Maildir/new $homedir/Maildir/tmp /bin/chmod -R 0700 $homedir/Maildir /usr/sbin/chown -R $uid:$gid $homedir/Maildir fi # loop on the email of the user for j in `/bin/ls /transit/mail-temp/save/$i/new`; do # echo moving /transit/mail-temp/save/$i/new/$j /bin/mv /transit/mail-temp/save/$i/new/$j $homedir/Maildir/cur/$j /usr/sbin/chown $uid:$gid $homedir/Maildir/cur/$j done fi else # users that are not in /home if [ ! -z "$list" ] ; then # echo need to empty /transit/mail-temp/save/$i/new/ for j in `/bin/ls /transit/mail-temp/save/$i/new`; do /bin/rm /transit/mail-temp/save/$i/new/$j done fi fi done /bin/sleep 300 done