How to download and backup your GTalk/GMail chat logs
Published on 8th October 2010
Updated on 19th September 2011
Why?
I use GTalk a lot. I have stored thousands and thousands of conversations for about 5 years. And it's all there, in GMail, without being able to backup it locally, unlike normal e-mails.
Why would I want to backup my chats? Because I don't want to lose them, and because GMail is awfully slow when having more than a particular number of e-mails, and I think that includes chats. Also they show up in every search when I try to find an e-mail in particular.
Invalid solutions
Labeling the chats and downloading them via IMAP: it looks like it worked (by accident?) in the early times of IMAP in GMail. Doesn't work anymore.
Forwarding the chats to yourself: you have to do this one by one. It's not a very good method.
Creationg a rule that forward the chats to yourself: it doesn't work with already existent chats, and I'm not sure it'd work with the new ones.
Using a script to parse the Gmail website: if you have thousands and thousands of chats, like me, you will get blocked from your own account in a few hours. And it is a violation of the TOS, so you could lose your account permanently.
The solution
There is a function called Offline Gmail which allows to use Google Gears to download your e-mail to your browser so you can use while offline or when your connection is not reliable. You can choose to let Gmail decide which e-mails download, or you can specify a label.
But... what if we marked our chats with a label...


(Note that I used 'logs' because 'chats' is a reserved internal label)
... and then configured Offline Gmail to download all the mails with that label?


Then a nice SQLITE database would appear in our Google Gears directory!
If you open that SQLITE database with the right tool, you will see that the content is in plain text. You can extract your chats from there. In order to do this, I wrote a little ugly PHP script that would parse the database and spit out nicely formed logs in folders. Here it is:
<?php
set_time_limit(0);
// ### EDIT THIS !!! ###
define(OUTPUT_FOLDER, 'C:/output-folder');
define(DB_FILE, 'C:/path/to/your/gears/directory/<username@gmail.com>-GoogleMail#database');
$db = new SQLite3(DB_FILE);
if(!file_exists(OUTPUT_FOLDER))
if(!mkdir(OUTPUT_FOLDER))
die('Unable to create directory. Check permissions.');
$sql =
"SELECT
c.c1Body, c.c3FromAddressEmail, m.DateMs
FROM
Messages m, MessagesFT_content c
WHERE
m.MessageId = c.rowid
AND m.ReplyByChatThreadId LIKE 'replybychat%'";
$res = $db->query($sql);
while($row = $res->fetchArray(SQLITE3_ASSOC)) {
$timestamp = date('Y-m-d His', (int) ($row['DateMs']/1000));
$path = OUTPUT_FOLDER . '/' . $row['c3FromAddressEmail'];
if(!file_exists($path))
if(!mkdir($path))
die('Unable to create directory. Check permissions.');
if(!file_put_contents($path . '/' . $timestamp . '.htm', $row['c1Body']))
die('Unable to write file. Check permissions.');
}
I think you need the Sqlite3 PHP extension to run this script, so install and configure the right packages or whatever your system needs. I used XAMPP on Windows because it's what I had at hand. But I am sure that this link will help you ;)