DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile Backup

  • submit to reddit
        This ruby script dumps all your bookmarks into a sqlite database. Requires sqlite3-ruby installed via rubygems. Don't forget to change the user and pass.
#!/usr/bin/env ruby
require 'rexml/document'
require 'net/http'
require 'net/https'
require 'rubygems'
require 'sqlite3'

# change credentials!
user = 'username'
pass = 'password'

# User-Agent: required for api
agent = ' backup v0.2'
schema = <<EOF
create table bookmarks (
    hash char(32) primary key,
    url varchar(1024),
    title varchar(1024),
    note varchar(2048),
    time timestamp
create table tags (hash char(32), tag varchar(1024));
create index ix_tags_hash on tags (hash);
create index ix_tags_tag on tags (tag);
insert_url = 'insert into bookmarks (hash, url, title, note, time) ' +
             'values (?, ?, ?, ?, ?);'
insert_tag = 'insert into tags (hash, tag) values (?, ?);'

http ='', 443)
http.use_ssl = true
xml = http.start { |http|
    req ='/v1/posts/all', {'User-Agent' => agent})
    req.basic_auth(user, pass)

db_name = ARGV[0] ||"%Y-%m-%d.db") { |db|
    db.prepare(insert_url) { |url_stmt|
        db.prepare(insert_tag) { |tag_stmt|
  'posts/post') { |el|
                    el.attributes['href'], el.attributes['description'],
                    el.attributes['extended'], el.attributes['time'])
                el.attributes['tag'].split(' ').each { |tag|
                    tag_stmt.execute(el.attributes['hash'], tag)