Okay, I wrote an article here how to backup your gMail Inbox, and here are some more tips that will speed up the process because with large INBOX files the time increase exponentially.
1. Enable your gMail to get all INBOX messages
If you’d like to create full back to your gMail Inbox you need to go to the POP section under settings and select “Enable POP for all mail”

2. Create your file structure
To keep everything organized create a directory structure where you’ll store INBOX file parts and single message files as well.

3. Break your large INBOX file into smaller (size) files
To speed up the process I found that with files between 60-80MB it is the fastest, it takes couple of minutes to go through each file.
Here is the PHP function that does that.
<?php /** script global options */ ini_set("max_execution_time", "99999"); ini_set("memory_limit", "-1"); /** default & config values & script init */ $file_name = "INBOX"; $limit_lines = "1000000"; $total_lines = count(file($file_name)); break_inbox_file(0, $file_name, $limit_lines, $total_lines, 1); print memory_get_peak_usage(); function break_inbox_file($start, $file, $limit, $total, $part) { $line = 0; $message_num = 0; $content_part = null; $fih = new SplFileObject($file); $fih->seek($start); while(!$fih->eof()) { if ($fih->current() != "") $content_part .= $fih->current(); /** there is limit for 2044 files open per connection so don't let to have more then 2000 message in one part file */ if (preg_match("/From kobra/", $fih->current(), $matches)) { $message_num++; } /** exits at the end of the file but don't forget to write the last part of content */ if ($fih->key() == $total) { $file_tmp = "G:/parts/" . $file . "_" . $part . ".tmp"; $foh = fopen($file_tmp, "w"); fwrite($foh, $content_part); fclose($foh); return 1; } if ($line >= $limit || $message_num > 1999 || mb_strlen($content_part) > 74372611) { /** continue adding new content until we don't get to a news message start with the marker */ while (!preg_match("/From kobra/", $fih->current(), $matches)) { if ($fih->current() != "") $content_part .= $fih->current(); $line++; $fih->next(); } /** extract the content from the array into string & write it into file */ $file_tmp = "G:/parts/" . $file . "_" . $part . ".tmp"; $foh = fopen($file_tmp, "w"); fwrite($foh, $content_part); fclose($foh); unset($content_part); /** recursion starts here pass all default options + setup new start line and next file name index */ return break_inbox_file($start + $line, $file, $limit, $total, ($part+=1)); } $line++; $fih->next(); } } ?>
4. Statistics & Performance
For 1GB Inbox file (about 10,000 messages) it took me about 5 minutes to separate the INBOX file into smaller (size) parts, about 20 minutes to separate into single message files & another 10 minutes to pull out all the attachments using munpack.



