Flying memes

Migrating a single site of a wordpress multisite instance

Recently I had to deal with a WordPress multisite instance, the task was moving the contents of one of the sites hosted on the network to a local copy, update them and the re-move them to the production server.

Due the fact that many plugins were used, in particular WPML, using the native import/export features was impossible, so I developed a little Rake task to do the job. Essential was the discovery of PHP serialize by Elijah Miller, which basically lets you un-serialize and serialize objects using the same algorithm used to store data on some WordPress tables, such as wp_options.

The task is driven by a config file and do two things, first of all it dumps the database in a temporary instance, then it find and replace the required patterns, including those saved in a serialized database field; last it exports only the required tables.

Here’s an example of the config file:

  mysqldump: "path/to/bin/mysqldump" 
  mysql: "path/to/bin/mysql" 

      database: your-db-name 
      username: your-db-username 
      password: your-db-password 
      host: your-db-host 

      database: a-transit-database-name 
      username: your-db-username 
      password: your-db-password 
      host: your-db-host 

      substitute: a_substitution: 
        old: "find-this-string" 
        new: "replace-with-this-string" 
        - a_table_we_want_to_export 

          old: "find-this-other-string" 
          new: "replace-with-this-other-string" 
          old: "you-can-ask-replacement" 
          new: "of-as-many-strings-as-you-want" 
        - a_table_we_want_to_export2

Once created this config.yml file you can call

 rake util:dump domain=domainofthenetwork1 

and obtain a replace.sql file containing only the tables required by domainofthenetwork1 and with all the desired patterns replaced.

The Rakefile containing this task is, as always, available on Github.

Tags: , ,