procmail
[Top] [All Lists]

limiting the size of a backup folder

1998-06-27 12:50:28
Stathy Touloumis asked,

| Is there any way to backup mail say to a single file but prevent it from
| backing up a certain number of files?

You mean, so that it never contains more than N messages, and if an N+1st
message comes in, the oldest message is removed, and it always has copies of
the N most recent messages?

There's a recipe in the procmailex(5) man page, but it is for a backup
directory where each message is kept as a separate file, and only the N
newest are left undeleted.

It can be done with a single flat folder as well, and there should be answers
in the list archives, because the question has come up before.  One major
question to decide is whether you want incoming messages preprended to the
top of the backup folder while old ones scroll off the bottom or you want new
ones appended to the bottom of the backup folder while old ones scroll off
the top; the method is considerably different for the two.

Maybe I can draw up something here, but this will be untested:

 TO_KEEP = 50 # change to the number of messages you want to keep backed up
 BACKUP = $MAILDIR/backup # name of backup file, change to your preference

 ADD_AT = top     # place to add incoming mail to the backup file
#ADD_AT = bottom  # select one, comment out the other

 # clone procmail for backup copy, lock backup file throughout,
 # and make parent wait [locking at this level also serializes clones]
 :0cw:$BACKUP$LOCKEXT
 * ADD_AT ?? ^^top^^
 {
  :0frw # append current contents of backup file to message
  | cat - $BACKUP

  :0r # rewrite backup file (clone has global lock in effect)
  | formail -${TO_KEEP}s > $BACKUP
 }
 :0Ecw:$BACKUP$LOCKEXT
 * ADD_AT ?? ^^bottom^^
 {
  :0c # append copy to backup file (clone has global lock in effect)
  $BACKUP

  FROM_X_STRING=`formail -I"From x" -zx"From " -s < $BACKUP` # includes new msg

  :0fir # lop off oldest message(s) if there is overflow
  * 1^1 FROM_X_STRING ?? x
  * $ -$TO_KEEP^0
  | formail +$= -s < $BACKUP

  :0Ar # rewrite only if truncation is needed
  | cat > $BACKUP

  :0h # if the backup file had room without being truncated, lose copy
  /dev/null
 }

... and recipes continue for delivery of incoming messages.

And for you jokers (you know who you are), inserting new messages into the
middle of the backup file while old ones scroll off alternating ends is not
an option here; you want that, you code it yourself.