procmail
[Top] [All Lists]

Re: Both backing up and archiving monthly email folders

2002-07-17 00:21:54
Jaswant asked:
Before I write any crontab, I'd like to ask how you suggest I monthly 
archive email to 'users' (a special group alias) monthly, perhaps 
using the power of procmail?

Hmmmm ... interesting ...
----------------------------------------------------------------------------
1. This simple procmail recipe should automatically archive ALL incoming
   email into a 12 automatically-created dated mail folders, stored anywhere
   you like, but, stored in your $MAILDIR by default, sorted by year, and
   then by month. All folders and files are created automatically by the 
   incoming mail messages themselves - there is no need for a crontab!

   .procmailrc:
    INCLUDERC=$PMDIR/rc.archive  # Create sorted monthly mail archives

   rc.archive
    # Automatically create a monthly email archive of all incoming mail.
    # One for every month of the year (e.g., archive_0201, archive_0202, etc.)
    MONTHLY_ARCHIVE=archive_`date +%y%m`
    :0
    $MONTHLY_ARCHIVE

   Note: There is no need to touch or mkdir anything; assuming your mail 
         directory exists (typically $HOME/Mail), procmail will take care
         of the automatic creation of the monthly mail folders as soon 
         as the first email of the month occurs.
----------------------------------------------------------------------------
2. For personal use, you'll likely desire the 'c' (continue) flag, 
   (so you actually can read email on your MUA even if you archive it), 
   e.g.:

   rc.archive
    # Creates 12/yr archives of the format 'archive_0201', 'archive_0202', etc.
    MONTHLY_ARCHIVE=archive_`date +%y%m`
    :0 c
    $MONTHLY_ARCHIVE

   And, if you do not want to archive all email, for example, to ignore
   any mail from bulk mailers, crontabs, mailer-daemon, etc., just add 
   the procmail FROM_DAEMON built-in macro, e.g.:

    # Create monthly archives of the format 'archive_0201', 'archive_0202', etc.
    MONTHLY_ARCHIVE=archive_`date +%y%m`
    :0 c
    * ! ^FROM_DAEMON
    $MONTHLY_ARCHIVE

   This says (roughly), as a mail message arrives:
    - Define a monthly archive (i.e., MONTHLY_ARCHIVE=archive_`date +%y%m`)
    - Begin a recipe  (i.e., :0)
    - Let all mail continue on for further filtering (i.e., c)
    - Don't bother archiving bulk email (i.e., * ! ^FROM_DAEMON)
    - Archive all mail into this monthly archive ($MONTHLY_ARCHIVE)
----------------------------------------------------------------------------
3. If you wanted, you could also save, say, the last 150 messages, 
   individually, in a DIRECTORY (as opposed to a mail folder as shown in
   number 1 and 2 above) where the number of messages in that directory
   are ALWAYS automatically pruned to (in this case, 150) as the 151st 
   message comes in:

   .procmailrc:
     INCLUDERC=$PMDIR/rc.backup # Back up last 150 incoming emails separately

   rc.backup
    # Backup the last 150 messages into $MAILDIR/backupdir/{msg*,msg*,msg*}
    :0 c
    backupdir

    :0 ic
    | cd backupdir && rm -f dummy `ls -t msg.* | sed -e 1,150d`           

   This says (roughly):
    - Begin a recipe  (i.e., :0);
    - Copy the mail onward to the next recipe (i.e., c);
    - There are no (*) conditions (therefore, all mail will be processed);
    - Save messages individually to $MAILDIR/backupdir/* (i.e., backupdir);
    - Then, begin another recipe (i.e., :0);
    - Ignore write errors if they occur (i.e., i);
    - Copy the mail onward to the next recipe (i.e., c);
    - Again, there are no (*) conditions (all mail will be processed);
    - cd backupdir & create msg.3YS1, msg.4YS1, msg.VYS1, msg.fYS1, etc.;
    - Remove (f=force=don't ask) the file named dummy (i.e., rm -f dummy);
    - Do a listing by date of all msg files (i.e., ls -t msg.*);
    - Delete all but the last 150 messages (i.e., sed -e 1,150d).
----------------------------------------------------------------------------
See also Jari's procmail tips page (with many helpful examples):
 http://hcs.harvard.edu/~thurston/ua/pm-tips.html (search for 'backup')
 ftp://cs.uta.fi/pub/ssjaaa/pm-tips.html 

Hope this helps others (improvements or corrections always welcome), 
jjg
_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

<Prev in Thread] Current Thread [Next in Thread>
  • Re: Both backing up and archiving monthly email folders, John Gianni <=