Syncing local content with development / staging sites

by GSTAR   Last Updated July 09, 2017 18:08 PM

I come from a Laravel background which means I am used to using database migrations and seedings to keep content on dev / staging sites in sync with my local environment.

I'm starting my first project in WordPress and was wondering how to go about doing the same. Essentially I'm building the site on my local environment and through the use of plugins such as Advanced Custom Fields I will create the content for the site. I need an easy was of ensuring the dev / staging sites have the same content as my local environment. I only want to sync content such as posts, pages, custom post types, and any associated media.

Deployment of code itself is not an issue as the code is kept under version control and I can create a webhook to auto deploy the code.

I have read up on plugins such as WP Migrate DB but I'm not sure this is the tool for the job.

Can anyone advise the best way to accomplish this?



Answers 3


Overview

That's the one main caveat of WordPress development, that even a seasoned WordPress developer still wonders what's the perfect automation for syncing database content from local environment to live server.

You can't just upload your database dump from your local development environment to the server and be happy with the results.

WordPress actually stores the resulting URLs in the database.

So let's say your local development URL is http://localhost/awesome. Your posts as it stored in the database has also stored your development URL(i.e. http://localhost/awesome) in every post. The URL is also stored in the wp_options table. So when you import your local database dump to the server you need to replace the existing URL to the server URL in order for the post links to work.

A not so elegant solution:

One of the solutions that I personally use is Search Replace DB by interconnect/it.

To use the script, download the zip file from below, extract the folder called search-replace-db-master (assuming you are using the 3.1.0 version), renaming it to something secret of your choosing, upload it via FTP, SFTP or SCP to your web server’s public facing directory, then navigate to that folder in your browser. The script will automatically attempt to find and populate the database field but you must check that the details are correct and that it is for the database you wish to carry out a search/replace operation on.

A typical WP install with this script would have the folders as follows:

  • /your-secret-search-replace-folder
  • /wp-admin
  • /wp-content
  • /wp-includes

NOTE: Please read the Installation and Usage of the tool in their website, and make sure the details are correct upon proceeding with the replacement.

How does it work?

Your:

Development URL: http://localhost/awesome
Live Server URL: http://awesome.com

You provide the development URL to the Replace Textbox and the live server URL to the With Textbox. Make sure to have a backup of your database, then hit the button dry run or live run to perform the action.

Conclusion

Does it work all the time? Well, not so quick, it actually depends on your requirements, but so far in all my WordPress projects and its varying requirements it works as expected.

There's a lots of solutions out there and one of them is mentioned in your question(i.e. WP Migrate DB). You just got to bare the tedious workflow of WordPress development, as great as CMS WordPress has been throughout its existence there's still this one main caveat that lets us want to be freed.

Den Isahac
Den Isahac
July 09, 2017 18:42 PM

WP Migrate DB is indeed the established pro tool.

There is also ServerPress' Desktop Server and Flywheel hosting's Local app (formerly Pressmatic). Both of these Windows and Mac apps generate a custom local development environment that can be shared on a network or over the network and pushed to staging or live sites on (some) hosts. The latter feature is not really mature in either app, especially the newer Local.

Finally there is VersionPress (currently Beta 4) -- a plugin and (in the works) a hosted service.

Dan Knauss
Dan Knauss
July 11, 2017 22:04 PM

For our site, we've written a Perl script that copies the database then uses wp-cli commands to tweak certain settings that are local to a given site. The script also fires off a UNIX line command to sync up the uploads directories.

We use that when we need to copy the entire database, which gets plugin settings in addition to all content.

However, we also have a content staging site for our users, and we use RAMP to migrate individual content items (or batches of them) from the content staging site to the live site. This will bring all dependencies with a given post/page such as media attachments, taxonomy terms, users, etc. RAMP is geared toward the end user who is updating and creating content. It does require some customization to work with sites that have been customized using something like ACF.

Our typical workflow is to use RAMP to go between content staging and live, and then to use our copy script to refresh our dev and test sites with content at the content staging site.

Finally, since you're already using version control, you may wish to have a look at VersionPress which allows you to version the database. I've not used it but have been keeping an eye on development for a while and it's intriguing.

spacegrrl
spacegrrl
July 11, 2017 23:26 PM

Related Questions



Strange characters added to the database

Updated October 07, 2016 09:03 AM

Migrate a Non Wordpress database to wordpress

Updated April 06, 2017 16:08 PM

Migration issue

Updated May 19, 2017 12:08 PM

Organizing WP Pages based on old website database

Updated June 01, 2017 18:08 PM