How to automate wordpress sandbox setup on Linux

Maintaining a wordpress site can be pretty involving task specially when you have so many plugins with regular updates. Many time you have to experiment new plugins, remove old plugins, do performance tuning, etc. The best workflow is to maintain a sandbox wordpress environment and constantly synchronize it with the main production environment.

Here are the steps to automate the syncing process on Linux which I follow and so far these have been working great for me. I’m using Bash script on ubuntu linux instance in Amazon AWS EC2 environment.

  1.  Create a sandbox setup on a subdomain. e.g. sandbox.infoheap.com
  2. Assuming this sandbox is using sandbox_blog database and main database is prod_blog, you can have a script containing code similar to the following
    mysqldump -u root --password="whatever_is_password" -h localhost prod_blog | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    (make sure database sandbox_blog is already created)
    
    ## update blog name
    echo "update wp_options set option_value='sandbox InfoHeap' where option_name='blogname';" | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    
    # update domain
    echo "update wp_options set option_value='http://sandbox.infoheap.com' where option_name='siteurl';" | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    echo "update wp_options set option_value='http://sandbox.infoheap.com' where option_name='home';" | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    echo "update wp_options set option_value='sandbox InfoHeap' where option_name='blogname';" | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    
    ## update Yoast Google analytics (in case you are using it with subdomain tracking)
    echo 'update wp_options set option_value=replace(option_value, "s:12:\"infoheap.com\"", "s:20:\"sandbox.infoheap.com\"") where option_name = "Yoast_Google_Analytics";' | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    (The number after s: is string length. You may have to change old and new value after s according to your domain name string length. I accept that this is big hack and there must be more polished ways to do it using deserialization-serialization. I'll update it once I find a cleaner way.)
    
    ## Unset ping sites to avoid pinging http://rpc.pingomatic.com/, etc. when we update a blog.
    echo "update wp_options set option_value = '' where option_name = 'ping_sites';" | mysql -u root --password="whatever_is_password" -h localhost sandbox_blog
    
    # images 
    sudo -u www-data rsync -r [PROD_BLOG_HOME]/wp-content/uploads/* [SANDBOX_BLOG_HOME]/wp-content/uploads/

 

This takes care of most of the stuff. Some points to note:

  1. The image links still may point to production images in blogs. You can modify the script to do a search replace in wp_post table in case you really need that.
  2. There may be more plugins which may need modifications. But this script is a good starting point and you can add stuff to it.
  3. Also have a robots.txt in sandbox blog to block search engines from crawling you sandbox blog. You may want to allow Mediapartners-Google in case you want to test adsense ads on your sandbox setup.
    User-agent: *
    Disallow: /
    
    User-agent: Mediapartners-Google
    Disallow:
  4. Copying database will case sandbox to have same active plugin as production.  Make sure whatever plugin is present on production environment is present on sandbox as well. The extra plugins will become inactive after the sync. You can manually activate those if you need extra plugins.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments