Thursday, March 11, 2010

Import / restore Firefox bookmarks via command-line

Have you ever required to maintain a single centralize source of Firefox bookmarks, and push it to your users using during login?

At first, I thought that bookmark sharing problem can be easily solved using Xmarks, which is an awesome bookmarks synchronization add on, but I discovered that it does not support "read only mode" - if one of the users adds or removes bookmarks locally, sooner or later the change will be synced with the rest of the users. I couldn't find any other add ons that perform that kind of task either.

Surprisingly, I found that the hardcore way to do it is quite easy.

As some of you might know, Firefox 3 (and above) manages its bookmarks in a sqlite database. The bookmarks of the user are stored in the %appdata%\Mozilla\Firefox\Profiles\xxxx.user\places.sqlite file (Windows OS). So, I'll explain how to create a bookmark template from the .sqlite file, and how to import it back to the user from the command line. You should have sqlite for that.
  1. Customize your bookmarks as usual (via Firefox)
  2. Close Firefox (to prevent sqlite locking issues)
  3. Go to the profile directory 
  4. Open places.sqlite via sqlite 
  5. Backup your bookmarks. FILENAME is the file your bookmarks will be saved to
    sqlite> .backup FILENAME
  6. Quit sqlite
    sqlite> .quit
Great! Now you have your bookmarks template file.

Importing it back:
  1. Close Firefox (if it is opened)
  2. Go to the profile directory
  3. Open places.sqlite using sqlite
  4. Restore your bookmarks
    sqlite> .restore FILENAME
  5. Quit sqlite
    sqlite> .quit
That's it. You can open Firefox and see that all the bookmarks were imported. I tested it on Windows environment, but I don't think it should be much different on Linux.
Since it was done from command line, it can be easily implemented in your favorite scripting language.

Please note that not only the bookmarks are copied, but the browser history as well. I suggest to clear the browser's history before performing the export.

5 comments:

  1. Thanks babe. You nailed it.

    ReplyDelete
  2. this seems not to work with FF10.0.2ESR

    ReplyDelete
  3. does not work with latest firefox uses a different format for sqlite database file

    ReplyDelete
  4. Thanks baby girl it's not what i wanted

    ReplyDelete
  5. Optimizing can be a real hassle, but using the right tools can speed the process along. Here is where Firefox and some of its plugins come into action! ublock

    ReplyDelete