View Full Version : Auto prune old emails
sajo
10th November 2006, 00:20
I look over the forum and I do search but didnt find what I was looking for.
So I would like to do automatic monthly prune of old emails of my customers.
For example.
I would like to automaticaly delete mails that are older than 6 months and which are in junk, trash and inbox folders, not deleting mails in other folders.
Is that possible and how.
I am new to linux so I would need detailed how to.
I am using ubuntu server 6.10 installed by this Falko how to http://www.howtoforge.com/perfect_setup_ubuntu_6.10
Thanks in advance
falko
10th November 2006, 16:57
You can create a cron job that uses the find command: http://www.howtoforge.com/forums/showthread.php?t=7040&highlight=%2Fusr%2Fbin%2Ffind
sajo
10th November 2006, 22:48
Thanks Falko
So if I understand right then I make cron that will run once every month and the script should look like this
#!/bin/sh
for file in "$( /usr/bin/find /.junk -type f -mtime +90 )"
for file in "$( /usr/bin/find /.inbox -type f -mtime +90 )"
for file in "$( /usr/bin/find /.trash -type f -mtime +90 )"
do
rm -f $file
done
exit 0
Am I right?
This script will look over the whole system to find this folders (junk, inbox,trash)and in those folders that are found, will remove files that are older than 90 days?
falko
11th November 2006, 17:28
The script should look like this:
#!/bin/sh
for file in "$( /usr/bin/find /.junk -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /.inbox -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /.trash -type f -mtime +90 )"
do
rm -f $file
done
exit 0Also, are you sure that /.junk, /.inbox, and /.trash are the correct folders?
sajo
12th November 2006, 00:25
Also, are you sure that /.junk, /.inbox, and /.trash are the correct folders?
Not shure...
emails are in
/var/www/webXX/user/XXX/Maildir/cur There are already read emails
/var/www/webXX/user/XXX/Maildir/new There are unread emails
/var/www/webXX/user/XXX/Maildir/.Junk/cur There are junk-spam emails
/var/www/webXX/user/XXX/Maildir/.Trash/cur There are trash emails
/var/www/webXX/user/XXX/Maildir/.Sent/cur There are sent emails
So do yu think that kode shold look like this
#!/bin/sh
for file in "$( /usr/bin/find /Maildir/cur -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /Maildir/new -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /Maildir/.Junk/cur -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /Maildir/.Trash/cur -type f -mtime +90 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /Maildir/.Sent/cur -type f -mtime +90 )"
do
rm -f $file
done
exit 0
falko
12th November 2006, 16:27
You must use /var/www/webXX/user/XXX/Maildir/cur then instead of /Maildir/cur, and so on.
sajo
12th November 2006, 19:53
That would be difficult, because I have about 27 domains and each domain has about 15 emails, so I cant write that for ever user, because there can be new email user any minut and I will not be notified about new user and I would like to automate that prune system.
Isnt there a way that system will look for all subfolders by that name and search files in that folders that are older than XX days?
falko
13th November 2006, 16:12
You can try to use wildcards, e.g. /var/www/web*/user/*/Maildir/cur, but I haven't tested this.
sajo
13th November 2006, 20:15
OK
I try this tomorow. We will see then.
sajo
14th November 2006, 18:08
Okey
I try the script and wildcarts works :D
But there is other problem, I have luck that I tested on test box :o
Wel this is what happens.
When I run the scrip the script did search for folders and also the files but script delete all never files until date I put in in my case that was 28 of october so I have no new files but files older than 17days, stays in folders.
and that is the script I use
#!/bin/sh
for file in "$( /usr/bin/find /var/www/web*/user/*/Maildir/cur -type f -mtime +17 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /var/www/web*/user/*/Maildir/new -type f -mtime +17 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /var/www/web*/user/*/Maildir/.Junk/cur -type f -mtime +17 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /var/www/web*/user/*/Maildir/.Trash/cur -type f -mtime +17 )"
do
rm -f $file
done
for file in "$( /usr/bin/find /var/www/web*/user/*/Maildir/.Sent/cur -type f -mtime +17 )"
do
rm -f $file
done
exit 0
I tested on debian sarge, I belive on Ubuntu 6.10 is the same thing. Because Ubuntu 6.10 is in my production box.
falko
15th November 2006, 16:27
When I run the scrip the script did search for folders and also the files but script delete all never files until date I put in in my case that was 28 of october so I have no new files but files older than 17days, stays in folders.
Sorry, I don't understand the problem... :confused:
sajo
15th November 2006, 19:43
To tell shortly
Script delete new mails and not old, it work in reverse mode than I want to
falko
16th November 2006, 15:53
Then try
/usr/bin/find /var/www/web*/user/*/Maildir/cur -type f -mtime -17
instead of
/usr/bin/find /var/www/web*/user/*/Maildir/cur -type f -mtime +17
Also have a look at man find
sajo
19th November 2006, 11:32
I try - and + but, Its the same old files stays new were deleted. I did read man find, but dontknow how to help with that, I am not a programer so that is s little problem for me. But I saw there something about prune but didnt understand well. Could you help me.
sajo
19th November 2006, 20:12
OKey I did it :)
I use this script
#!/bin/sh
find /var/www/web*/user/*/Maildir/cur -type f -mtime +90 | xargs rm -f
find /var/www/web*/user/*/Maildir/new -type f -mtime +90 | xargs rm -f
find /var/www/web*/user/*/Maildir/.Junk/cur -type f -mtime +90 | xargs rm -f
find /var/www/web*/user/*/Maildir/.Junk/new -type f -mtime +90 | xargs rm -f
find /var/www/web*/user/*/Maildir/.Trash/cur -type f -mtime +90 | xargs rm -f
find /var/www/web*/user/*/Maildir/.Sent/cur -type f -mtime +90 | xargs rm -f
exit 0
And dont forget to make executable file ;) I name it mailprune.sh, then I make crontab that run once a month
crontab -e
* * 1 * * /path/to/yourscript
I wrote that if someone would need that also, and dont know how to do it.
Thanks for your help Falko.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.