Clone WordPress in Linux

Final product image
What You'll Be Creating

Duplicating WordPress Never Seems Easy

I often like to launch a new WordPress site based on an existing site, as a template. The configurations for themes, plugins and settings can be very useful to start with, as opposed to a clean installation where you have to repeat everything from the beginning.

In Building an App Image to Resell at Digital Ocean, I walked through the construction of an installable, pre-configured and pre-optimized WordPress droplet. Essentially, it's a Digital Ocean image that can launch a fully loaded WordPress site in minutes. But, more often, I want to add a WordPress site to one of my own pre-existing servers.

There are a number of ways to do this, but I often find they require a specific and detailed approach which I seem to have to relearn each time. I decided it was time to come up with a Linux shell script that would do everything in a few minutes for me. 

In this tutorial, I'll walk you through my research and resulting clone script for WordPress. I hope you like it—I thought it worked rather well when I was finished with it.

Before we get started, please remember, I do try to participate in the discussions below. If you have a question or topic suggestion, please post a comment below or contact me on Twitter @reifman. You can also email me directly. I expect a number of you will have better ideas and improvements for this script. I'd appreciate hearing from you.

Other Approaches to WordPress Migration

Often you can start a new website by migrating an existing one to a new server, essentially copying it, and building on the copy while leaving the source site intact. There are a number of approaches to this.

In Moving WordPress to a new Server Publishing with WordPress, I wrote about using the Duplicator plugin to do this—but I've found the process to be cumbersome. Refamiliarizing myself with Duplicator each time I need to move a site has also been difficult. 

Recently, I wrote about this in Backing Up and Restoring Your WordPress Site with CodeGuard for Envato Tuts+. It's a service that makes this process a bit easier. And, soon, How to Simplify Managing Multiple WordPress Sites will be released, describing a number of powerful advantages in using ManageWP. It has a cloning feature but it requires FTP—for security reasons, I avoid running FTP on my servers.

There's also 's two-part Envato Tuts+ series: Moving WordPress: An Introduction and Moving WordPress: Using Plugins to Move Your Site. And there's this tutorial at WPBeginner which uses BackupBuddy. Finally, WPClone doesn't require FTP but requires a clean WordPress install to build on.

You can learn a lot from all of these tutorials and services, but I wanted to see if I could create a command line script that cloned a WordPress site more quickly and easily, every time.

Planning the Script

To write this tutorial, I relied a lot on earlier works of others to jumpstart my knowledge of bash scripts and WordPress site manipulation. I've never considered myself an expert Linux system administrator. Ultimately, I decided to build my clone script on top of Brian Gallagher's WordPress Bash Install Script

Note: These are Debian-based setup scripts; other flavors of Linux such as RedHat and CentOS have different paths for Apache and different utilities.

Here's Gallagher's description of his base script:

Downloads latest WP version, updates wp-config with user supplied DB name, username and password, creates and CHMOD's uploads dir, copies all the files into the root dir you run the script from, then deletes itself!

There's a lot of well-organized script here to begin with, but I wanted to make something which could clone an active site. Let's review the architecture of a typical WordPress configuration.

The Initial Components of a WordPress Site

A typical WordPress installation has four primary components for cloning:

  1. Site's directory tree 
  2. Database 
  3. Web server configuration, e.g. Apache conf file
  4. Domain mapping

There are also information, access and security settings we'll need:

  • Server administration account and password
  • MySql server username and password
  • Site's source directory
  • Site's web server configuration file
  • Database name, username and password

Here's what we'll need to specify for the cloned site:

  • Cloned site's target directory
  • Cloned database name, username and password
  • Cloned sites' web server configuration file

What the Script Needs to Do

  • Acquire all the settings through configuration variables or user input.
  • Copy the site directory and restore it to a target directory.
  • Export the source database and import it to a target database.
  • Ensure the proper permissions on these directories.
  • Copy the server configuration file and update the domain and directory settings.
  • Reload the web server.

Manually, we'll have to update the DNS for the new target domain. I recommend creating DNS records before you begin so they are ready once your site is cloned. There's nothing like cloning a site and not being able to test the domain name because you're waiting for the DNS.

The Approach of the Cloning Script

Now, we're ready to walk through how the architecture of the script works. Again, I leveraged Gallagher's WordPress installation script to begin with, and you need that initial bash line at the top:

Preparing Your DNS Settings

Before you duplicate a site, you need to configure the DNS for the cloned site. You can read about DNS configuration for a new WordPress site here. I'm also excited about this Envato Tuts+ tutorial, An Introduction to Learning and Using DNS Records.

Basically, you need to create an A record or CNAME that routes your desired clone URL to the server we're duplicating on.

Setting Permissions

On my server, I'm creating a bash script called clonewp.sh. It will need executable permissions:

Then, once it's complete, you can run it like this:

I recommend running the script as sudo so you don't run into file permission problems.

Setting Defaults

For testing purposes, I created the ability to pre-load the script with default settings. It helped me running through tests repeatedly without having to type everything over and over. I also thought it might be useful for people who want to later modify the script or use it in other ways.

Here are all the default settings:

I know it seems like a lot, but I found it useful to have a master MySQL user and password for database backups, database creation and imports. Yet it was also useful to have the site-specific database user and passwords for setting target database privileges and searching and replacing in the wp-config.php file. It makes the ultimate cloning process very seamless.

I used the NOW timestamp to make sure the archives we create are unique.

Requesting the Settings

The following code shows the default to the user and allows them to accept it (pressing return) or replacing it:

Once we've collected all the settings from the user, we ask if they wish to begin:

Copying the Directory Tree

Now things move along a bit faster. We create tarballs of the source site, make a target directory and extract the tarball there:

We also run the standard file permissions for WordPress to make sure everything is set up properly and securely:

Update the WP-Config File

Next, we use perl to search and replace the source database authentication with the destination database information:

I also add the RELOCATE setting to the end of the file. If you like, you can replace this with static WP_HOME and WP_SITEURL settings.

Copy the Database

Next, we dump the database, create a new database with permissions the user provided, and then import the database to it:

Again, I found it best to user the master MySQL authentication for these activities while configuring the database settings based on the source site and single-site clone settings.

Copy the Web Server Configuration

Finally, we're ready to wrap things up and press the launch button. It's rare I see these kinds of scripts manage the extra step for web server configuration. So, I wanted to do that too.

I copied the source site's Apache .conf file over to a new .conf file for the clone. I used perl for a string replace for the domains and the directory paths. Then, I activated the site with Apache and reloaded the web server:

And, that's it. Here's what a run-through of the script looks like in real life:

On my small WordPress sites, duplication took only 30 to 90 seconds!

Fine Print

There are a couple more things you'll need to know.

Direct Login Path at First

First, to log in to your cloned site, you'll need to use the wp-login.php path rather than wp-admin which redirects to the source site URL, e.g. http://clone.io/wp-login.php as shown below:

Clone WordPress Use the wp-login php path

Changing Over Domains

Since WordPress hardcodes much of your source domain in the database, I've found that using the RELOCATE setting in wp-config.php makes it easy to update this through General > Settings. You just save the form with the new destination URL:

Clone WordPress General Domain Settings

Once you've saved the cloned target URL, you can remove the RELOCATE setting from wp-config.php manually.

However, a colleague suggests that you may want to use a tool such as InterconnectIT's Search and Replace for WordPress Databases. It's also been documented at Envato Tuts+ in Migrating WordPress Across Hosts, Servers and URLs.  

The Final Script

Here's the final script for wpclone.sh—feel free to change the defaults:

If you have suggestions and customizations, please let me know. Post your thoughts below in the comments.

Cleanup for Extended Testing

The following lines might be helpful to you for deleting and undoing test sites that you clone. You can customize it to your needs:

Changing Security Keys in Wp-Config.php

You can also better secure your new WordPress site by manually replacing the authentication keys and salts within the destination site's wp-config.php:

You can just visit https://api.wordpress.org/secret-key/1.1/salt/ and cut and paste them into your wp-config.php file:

WordPress Authentication Key and Salt Generator

Now if you're a Linux script purist, I'll let you update Gallagher's WordPress Bash Install Script. His script copied over the default WordPress wp-config.php so he'd have predictable source strings to replace with keys his script generated:

I never wrote up a regex to replace the key values in dynamic pre-existing wp-config.php files of our source sites. If you decide to, please share it in the comments and thanks in advance.

Questions?

I very much enjoyed getting this script working. Or, I should at least say I enjoyed running it when I was done. I wished I'd created it a long time ago as it's incredibly effective and efficient. I could clone small WordPress sites and have them running on my server in about 60 seconds. None of the other plugins or duplication options are as seamless.

If you have questions, please post them below. Or, you can contact me on Twitter @reifman or email me directly. Please check out my Envato Tuts+ instructor page to see other tutorials I've written, such as my startup series (Building Your Startup With PHP).

Related Links

Tags:

Comments

Related Articles