Migrating Drupal to Wordpress

October 10th, 2005

Here are some scripts along with instructions for migrating a drupal site to wordpress.

README.txt

These instructions are ‘implemented’ in code as a small python script called migrate-drupal-data.py which you can find below.

  1. Dump your drupal database. To find out how to do this refer to the manual for your db (for mysql from the command line you use mysqldump and for postgres it is pg_dump)

  2. Load the drupal dump into your wordpress db:

      mysql -u [username] -p [password] < [path-to-drupal-dump]
    
  3. Edit the convert-drupal-data.sql script replacing ‘weblog_’ with your wordpress prefix

  4. Run convert-drupal-data.sql script against your wordpress db:

      mysql -u [username] -p [password] < convert-drupal-data.sql
    

    WARNING: this will delete all existing posts, comments and categories from your wordpress db. If you don’t want to do that edit the script as indicated therein).

  5. Finished.

Scripts

convert-drupal-data.sql

-- taken from
-- http://vrypan.net/log/archives/2005/03/10/migrating-from-drupal-to-wordpress/
-- and then improved :-) 
-- these lines will result in the deletion of all existing posts, comments
-- and categories. Comments these out using '--' or /* ... */ if you don't
-- want that to happen 
DELETE FROM weblog_wp_categories ;
DELETE FROM weblog_wp_posts ;
DELETE FROM weblog_wp_post2cat ;
DELETE FROM weblog_wp_comments ;

/*
-- does not work (think it is to do with password issues)
-- also causes problems with auto-increment
delete from weblog_wp_users;

INSERT INTO weblog_wp_users
  (ID, user_login, user_pass, user_nickname, user_email, user_registered)
  SELECT
  uid, name, pass, name, mail, FROM_UNIXTIME(timestamp)
  FROM users;
*/

INSERT into weblog_wp_categories(
  cat_ID,cat_name, category_nicename, category_description, category_parent
  )
  SELECT term_data.tid, name, name, description, parent
  FROM term_data, term_hierarchy
  WHERE term_data.tid=term_hierarchy.tid;

INSERT INTO weblog_wp_posts(
  ID, post_author, post_date, post_content, post_title, post_excerpt,
  post_name, post_modified
  )
  SELECT nid, 1, FROM_UNIXTIME(created), body, title, teaser, concat('OLD',nid), FROM_UNIXTIME(changed)
  FROM node
  WHERE type='blog' OR type='page' OR type='story' OR type='forum' ;

INSERT INTO weblog_wp_post2cat (post_id,category_id)
  SELECT nid,tid
  FROM term_node ;

INSERT INTO weblog_wp_comments (
  comment_post_ID, comment_date, comment_content, comment_parent
  )
  SELECT nid, FROM_UNIXTIME(timestamp), concat(subject,' ', comment), thread
  FROM comments ;

DROP TABLE term_data;
DROP TABLE term_hierarchy;
DROP TABLE node;
DROP TABLE term_node;
DROP TABLE comments;
DROP TABLE users;

migrate-drupal-data.py

#!/usr/bin/env python
import os

# target wordpress db information
user = 'your_user_name'
db = 'your_db_name'

# replace this with the path to your dump from drupal
drupalDbDump = '~/tmp/drupal-db-dump.sql'

# path to drupal conversion sql script
templateConvertDrupalSql = 'convert-drupal-data.sql'

# tmp file used as an intermediary 
convertDrupalData = 'tmp_sql.sql'

# default wordpress prefix used in templateConvertDrupalSql script
# you shouldn't have to change this
templatePrefix = 'weblog_'

# your wordpress prefix
wpPrefix = ''

def prepareConvertSql():
    ff = file(templateConvertDrupalSql)
    tstr = ff.read()
    ff.close()
    tstr = tstr.replace(templatePrefix, wpPrefix)
    ff2 = file(convertDrupalData, 'w')
    ff2.write(tstr)
        ff2.close()

if __name__ == '__main__':
    sqlCmd = 'mysql -u %s -p %s < %s'
    cmdInsertDrupalData = sqlCmd % (user, db, drupalDbDump)
    cmdConvertDrupalData = sqlCmd % (user, db, convertDrupalData)

    # getData()
    prepareConvertSql()
    os.system(cmdInsertDrupalData)
    os.system(cmdConvertDrupalData)

8 Responses to “Migrating Drupal to Wordpress”

  1. Loaded Mouth | Happy Thoughts and Flowers » So where are all the old posts? Says:

    […] Their in a SQL database sitting on my harddrive where nobody can read them. But I’m trying to fix that, and this might help me. Of course, this means that I have to fuck around with database scripts and, since I’m pretty inept witth that task, it’s going to take me a few hours and a lot of screaming. Plus I’ll probably lose all my current Wordpress posts and user accounts. […]

  2. mik Says:

    for rgrp
    perhaps you could help me convert one drupal site to wordpress
    i am offering $50 no rush deadline
    please reply by email if intrested

  3. rgrp Says:

    mik: I’m sorry to say I’m not able to help you with your conversion. Have you tried using the scripts above?

  4. mik Says:

    Hi
    i have not tied, i have no idea how to start :-( perhaps you could give istructions simple ppl?
    such as what preparation before using scripts?
    where to put the scripts in what folder?
    where to put various sql files?
    how do i know if and where phyton is installed on my server?
    will this script work for drupal 4.4.2?
    thanks a lot for time and patience

  5. rgrp Says:

    mik: I have reworked the post to provided proper instructions for usage. Let me know if that helps. Regarding drupal 4.4: I used this script to convert a drupal 4.5 db without problem and I don’t think there were any schema changes between 4.4 and 4.5 that would affect the tables being used here so I think it should work fine. If you do encounter errors try taking a look at your drupal db dump and comparing table columns with those used by the convert-drupal-data.sql script.

  6. Slant Truth » My Adventures in Migrating From Drupal to Wordpress Says:

    […] I found a handful of sites that documented their migration from Drupal to Wordpress and I want to give them props for helping me get along my way: Tom Markiewicz, miscellaneous factZ, Vrypan|Net|Log, and Spindrop were all great resources. Props. […]

  7. Mac User Group Linz, Austria » Blog Archive » Todo Says:

    […] http://www.thefactz.org/ideas/archives/62 […]

  8. Alex’s Blog » Migrated to WordPress Says:

    […] The changeover was fairly easy to do, thanks largely to this post: Migrating Drupal to Wordpress. Since that post is almost two years old, the sql needed some tweaking to work with the 5.x series of Drupal. If you need the tweak’s I’ve made them available here: convert-drupal-wp.txt […]