Export WordPress site from a multisite network the right way!

So today i’m gonna talk about one of the most frustrating things about the WP multisite installation: Exporting a site out of the realm.
full disclosure: I’m not a PHP programmer and i don’t find WP as ‘user-friendly’ as people say it is.

Great, so here is the story:
Lately i had to do this job and i found out that the normal export/import plugins just won’t cut.
3 very annoying things

  • WP don’t do relative paths. I understand why but i still don’t like it.
  • The plugin won’t export the users.
  • Multisite shares the users tables so it’s not easy to export just the site’s users.

So what can one do to accomplish this task without knowing PHP? BASH!!!
the solution requires shell access to the server and a DB user that can access schema DB.
Here is my script, step by step.

#first, some vars:

#now, we can initialize the script:

the script asks for the website ID and creates a directory where the script lays to hold the files.

#DB export website tables

the script exports all the tables except for the users tables into a script file and replace the table name suffix to fit a standalone installation.

# users & usermeta tables

now this is the tricky part,
the script takes out the users ID’s that belongs to this website and saves them to an array.
then it iterates over the array and create a ‘WHERE’ clause for each of the tables using their own syntax.
the final part is to export those 2 tables to SQL scripts and replace the table name suffix that is stored inside of wp_usermeta.meta_key column.

#plugins

the scripts creates an array of active plugins and makes a plugins directory inside the website directory it created before.
then it iterates over the array and copies the plugins into the new plugins dir.
one thing to note here: wordpress plugins are stored inside a directory*.
[*except for the hello dolly plugins that for some reason wordpress decided it needs to be in the root plugins directory, so i had to add a special treatment for this.]

#uploads

next is the upload folder. nothing new here just coping the website’s uploads folder as is.

#final

the final part is just to wrap it all and get it ready to be shipped to the new server.
I made it open as a question since there is one important thing that got left behind in the script, the website’s Theme.
the Theme part is not in the script since sometimes people uses Child Themes,
and I found out that just copying the needed folders is much more faster then trying to analyze the style.css file and look for the parent theme.
You can add this if you want and also share it with me will be nice.

So that’s it. now you have a folder with all of your website data ready to be imported on the new server.

the steps i took on the new server are those:
download and unzip the new generated files.
move plugins to plugins dir.
create uploads dir and move uploads into it.
import all DB scripts
chmod + chown on the wordpress dir
change hard coded link in theme folder ( ( if any ) )
for the upload folders do: find . -type f -print0 | xargs -0 sed -i ‘s,uploads/sites/[id]/,uploads/,g’ # replace [id] with website ID from multisite.
for the old url with new url do: find . -type f -print0 | xargs -0 sed -i ‘s,[oldurl],[newurl],g’

the DB is now in place but it still holds the full path urls from the old server.
the url’s are stored in serialized arrays so a simple replace command will just break the WPDB.
So since i’m not a PHP Programmer I had to outsource the help of a free code by a company named interconnect/it.
The product is called DATABASE SEARCH AND REPLACE SCRIPT IN PHP,
And I must say, except for the fact the it does the job, I just fell in love with the UI/UX. those guys really know how to make a product.

so how do you use it?
download search and replace and extract it to %wp-root-dir%/sar/
go to http://[NEW_URL]/sar/ and replace the domain name and the old uploads path ( like the replace on the theme folder ).
delete the %wp-root-dir%/sar/ directory so no one else can play around with your DB.

That’s it! all that is left now is to go to the /wp-admin/ section,
follow the update DB procedure and you are DONE!

full script right here:

Leave a Reply

Your email address will not be published. Required fields are marked *

*