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:


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

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

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

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

    domainofthenetwork2: 
      substitute: 
        an_other_substitution: 
          old: "find-this-other-string" 
          new: "replace-with-this-other-string" 
        more_substitution: 
          old: "you-can-ask-replacement" 
          new: "of-as-many-strings-as-you-want" 
      tables: 
        - 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: , ,