HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Tips/Tricks/Mods (http://www.howtoforge.com/forums/forumdisplay.php?f=29)
-   -   Script to clean up old Trash, Junk and Spam mail. (http://www.howtoforge.com/forums/showthread.php?t=58300)

EckyBrazz 17th August 2012 18:00

Script to clean up old Trash, Junk and Spam mail.
 
Hi Folks,

Just found a script that I have adjusted to work with ISPConfig 3 to clean up the old trash mail ect.

Code:

#!/bin/bash
MAILDIRS=$(find /var/vmail/*/*/Maildir/ -maxdepth 0 -type d)
for basedir in $MAILDIRS; do
  for dir in .Trash .Junk .Spam .Low\ Priority; do
    for dir2 in cur new; do
      [ -e "$basedir/$dir/$dir2" ] && (
        echo "Processing $basedir/$dir/$dir2..."
        find "$basedir/$dir/$dir2/" -type f -mtime +30 -delete
      )
    done
  done
done

When migration from Cpanel to ISPconfig I found that my users have a lot of old mails in the trash. When I ran this script it saved me about 25GB of diskspace.

florix.net 21st March 2013 16:36

super ... just what I was looking for. I was having hard time and thought of writing one utility ...

:) Thanks mate

andypl 22nd March 2013 12:34

Scripts not working

find: `/var/vmail/*/*/Maildir/': No such file or directory

danhansen@denmark 22nd March 2013 20:24

Great idea - Scripts not working yet ...
 
Hi,

Sound really great!
This is exactly what's always missing, in any case of software making.. Order, clean-up functions etc. - any chance of getting it to work on ubuntu server 10.04/ispconfig 3 ?

Looking forward to hear from you guys :)

Kind Regards.
DanHansen@Denmark

Bashewa 16th September 2013 02:56

You can run this either from command line or via a cron

find /var/vmail/*/*/.Junk/new/ -mtime +30 -exec rm {} \;

Above will delete any file older than 30 days from all .Junk/new/ folders


Explanation

The first argument is the path to the files. This can be a path, a directory, or a wildcard ( For all email accounts ) as in the example above. I would recommend that you run the command without the exec rm to make sure you are getting the right results.
Only use rm as this will not remove any directory only files

The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +30, it will find files older than 30 days.

The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

mitsos 9th April 2014 17:34

I'm personally using:
Code:

#!/bin/sh
find /var/vmail/*/*/Maildir/.Trash/new/ -mtime +30 -type f -delete
find /var/vmail/*/*/Maildir/.Junk/new/ -mtime +90 -type f -delete
find /var/vmail/*/*/Maildir/.Junk\ E-mail/new/ -mtime +90 -type f -delete
exit

which is a file created in /etc/cron.daily/ and chmoded to 755

Works perfectly for me. Trash older than a month gets deleted, spam older than 3 months gets deleted.

Turbanator 14th April 2014 23:03

Here's another that we use and looks at timeframe to delete as well.

Code:

#!/bin/sh
# Time to wait before removing mails from the Junk folder (Default: 7 days) Set 0 to turn off.
junk_max_hours=$((24*7))
# Time to wait before removing mails from the Trash folder (Default: 30 days) Set 0 to turn off.
trash_max_hours=$((24*60))
for domain in /var/vmail/*
do
  if [ -d "$domain" ]
  then
    for user in $domain/*
    do
      if [ "$junk_max_hours" -gt "0" ]
      then
        if [ -d "$user/.Junk" ]
        then
          tmpreaper -m $junk_max_hours $user/.Junk/cur
        tmpreaper -m $junk_max_hours $user/.Junk/new
        fi
      fi
      if [ "$trash_max_hours" -gt "0" ]
      then
        if [ -d "$user/.Trash" ]
        then
          tmpreaper -m $trash_max_hours $user/.Trash/cur
        tmpreaper -m $trash_max_hours $user/.Trash/new
        fi
      fi
    done
  fi
done



All times are GMT +2. The time now is 12:41.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.