« back to index

How to download and backup your GTalk/GMail chat logs

Published on 8th October 2010
Updated on 19th September 2011

AS OF SEPTEMBER 2011, THIS ARTICLE IS NOW OBSOLETE!
GMail now allows downloading chats via IMAP! Just use Thunderbird or whatever software you like to download them. Thanks to Edwin from Gmail Keeper for letting me now! He talks about it here.

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

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...

Creating the GMail filter 1

Creating the GMail filter 2

(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?

Configuring Offline Gmail

Synchronizing e-mails

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 ;)

Useful sources