copying and renaming mailboxes to clean up directories

Thorsten Büker mailinglists at
Wed May 2 09:11:03 EDT 2007

Dear all,

For quite a few month I ran a backup-scenario set up according to [1]
including synchronisation of the mailboxes using "rsync -vaR". (In the
meantime I migrated to the usage of LVM snapshots.)

As restoring a backup became necessary due to filesystem corruption I
fetched the mailboxes from the r'synched folders as well as the mailbox 
databases and further necessary stuff. The restore went smoothly.

But wondering about the heavily increased amount of partition in use, I
noticed that my call of rsync lacked of the "--delete" attribute. In 
consequence the restored mailbox directories include not only the 
messages, which should be in the inbox, but all files beeing in the 
directories during the backup cronjobs. (My mailbox sizes ~700 MB 
instead of ~100 MB.)

With regard to backup performance I'd like to clean up the relevant
directories by ensuring uniformity of the inbox according to the Cyrus
mailbox database and the files in the mailbox directory.

In the wiki I found Andrew's collection of scripts [2] and had a try. My 
intention is/was to firstly manually select mailbox directories, which 
contain much too much unneeded files -- mainly mailboxes used via imap. 
After temporarily stopping mail delivery and mailbox access, each chosen 
mailbox might be renamed and its contents copied to a new mailbox of its 
initial name. After starting mail delivery and access again, the 
mailboxes and directories containing wrong fails might be deleted at all.

But, as usual, the idea doesn't work yet. A first try to copy the
existing mailbox "thorsten" to "thorsten_neu" leads to the following:


/usr/local/sbin/cyrus/ thorsten thorsten_neu
Successfully created inbox for thorsten_neu
Successfully set quota for thorsten_neu to 102400
Copying folder user.thorsten to user.thorsten_neu
Copying folder user.thorsten.Archiv to user.thorsten_neu.Archiv
Copying folder user.thorsten.Drafts to user.thorsten_neu.Drafts
Copying folder user.thorsten.Mailinglisten to
Error copying messages to user.thorsten_neu.Mailinglisten
Not connected at /usr/local/sbin/cyrus/ line 104
Error sending '24 SELECT user.thorsten.Mailinglisten.Announces' to IMAP:
  at /usr/local/sbin/cyrus/ line 104
Copying folder user.thorsten.Mailinglisten.Announces to
Not connected at /usr/local/sbin/cyrus/ line 129
Error sending '25 STATUS user.thorsten.Mailinglisten (MESSAGES)' to
IMAP:  at /usr/local/sbin/cyrus/ line 129
Use of uninitialized value in numeric eq (==) at
/usr/local/sbin/cyrus/ line 129.

[.. various errors at lines 104 and 129 ..]

Not connected at /usr/local/sbin/cyrus/ line 152
Error sending '84 LOGOUT' to IMAP:  at
/usr/local/sbin/cyrus/ line 152


Please note, the additional output on the folder name results from
uncommenting line 92 of the script. The relevant sections look like
below [6]. Evaluating, why the connection to Cyrus gets lost while 
looping through the folders, a discovered "word too long" in 

Well, in this case (my personal inbox) erasing various mails inside 
"user.thorsten.Mailinglisten" (~2200 mails) helped to make copying work. 
But regarding customer's inboxes this might not be first choice ;-)

In google I found [3], suggesting to recompile imapd. Alas, is there any 
way to modify the script instead of recompiling imapd to permit a larger 
value? Indeed I'd prefer to keep sarge's "2.1.18-1+sarge2".

Furthermore I tried to rename a mailbox via cyradm in advance, but this 
leads to the following:

~~~~> renm user.thorsten user.thorsten_orig
renamemailbox: Operation is not supported on mailbox


The amount of results by google is rather poor, honestly spoken one 
result [4]. The presented workaround [5] contains quite a lot of work 
dealing manually with each subfolder. Is there any other way to rename a 

Or, going even further, is there any more clever attempt than the one 
above to solve the problem? Thanks in advance!

kind regards,






[6] ~~~~
# Open connection for Mail::IMAPClient
my $imap1 = Mail::IMAPClient->new(Server => $prefs{'cyrushost'},
                                 User => $prefs{'cyrususer'},
                                 Password => $prefs{'cyruspass'},
                                 Uid => 1,
                                 Peek => 1,
                                 Buffer => 4096,
                                 Fast_io => 1,
                                 ) or die("Cannot connect to imap server
- $!");

my $search = $oldmailbox . ".*";
my $targetf;
my @folders;
foreach my $f (&getfolderlist($imap1, $oldmailbox)) {
print "$f\n"; # line 92

         # Strip quotes
         $f =~ s/"//g;

         # Add acl on source so we can read it
         $rv = $imap->set_acl($f, "cyrus", "lrswipcda"); # line 104
         if ($rv != 0) {
                 print "Error setting acl on $f: " . $imap->{'Error'} .

         # Add acl on source so we can read it
         $rv = $imap->set_acl($f, "cyrus", "lrswipcda");
         if ($rv != 0) {
         # Get target folder name
         if ($f =~ /^user\.$oldusername(.*)$/) {
                 $targetf = "user.$newusername$1";

         # Push all the folder names into an array so we can remove
         # the "cyrus" acls later
         push @folders, $f, $targetf;

         print "Copying folder $f to $targetf\n";

         # Create folder on target side if it does not exist
         if (! $imap->list($targetf)) {
                 # Created folders inherit acl of parent, so no need to
add cyrus acl
                 $rv = $imap->create($targetf);
                 if ($rv != 0) {
                         print "Error creating target folder $targetf: "
. $imap->{'Error'} . "\n";

         # Now copy the selected folder
         # Skip folders with zero messages or the copy will appear to error
         next if ($imap1->message_count == 0); # line 129


More information about the Info-cyrus mailing list