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 Rachel McCollin'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:
- Site's directory tree
- Database
- Web server configuration, e.g. Apache conf file
- 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:
#!/bin/bash -e # Clone a WordPress site via Bash script clear echo "===================================================" echo "Clone WordPress Script" echo "==================================================="
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:
chmod +x clonewp.sh
Then, once it's complete, you can run it like this:
sudo bash clonewp.sh
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:
# Set Default Settings (helpful for testing) default_mysql_user=$"root-admin" default_mysql_pass=$"super-strong-password" default_source_domain=$"gardening.io" default_target_domain=$"cycling.io" default_source_directory=$"/var/www/gardening" default_target_directory=$"/var/www/cycling" default_apache_directory=$"/etc/apache2/sites-available" default_source_conf=$"gardening.conf" default_target_conf=$"cycling.conf" default_source_dbname=$"gardening" default_source_dbuser=$"user_for_garden" default_source_dbpass=$"pwd_garden" default_target_dbname=$"cycling" default_target_dbuser=$"user_for_cycling" default_target_dbpass=$"pwd_cycling" NOW=$(date +"%Y-%m-%d-%H%M")
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:
# Request Source Settings read -p "Source Domain (e.g. "$default_source_domain"): " source_domain source_domain=${source_domain:-$default_source_domain} echo $source_domain read -p "Source Directory (no trailing slash e.g. "$default_source_directory"): " source_directory source_directory=${source_directory:-$default_source_directory} echo $source_directory read -p "Source Database Name (e.g. "$default_source_dbname"): " source_dbname source_dbname=${source_dbname:-$default_source_dbname} echo $source_dbname read -p "Source Database User (e.g. "$default_source_dbuser"): " source_dbuser source_dbuser=${source_dbuser:-$default_source_dbuser} echo $source_dbuser read -p "Source Database Pass (e.g. "$default_source_dbpass"): " source_dbpass source_dbpass=${source_dbpass:-$default_source_dbpass} echo $source_dbpass # Request Source Settings read -p "Source Conf File (e.g. "$default_source_conf"): " source_conf source_conf=${source_conf:-$default_source_conf} echo $source_conf # Request Target Settings read -p "Target Domain (e.g. "$default_target_domain"): " target_domain target_domain=${target_domain:-$default_target_domain} echo $target_domain read -p "Target Directory (no trailing slash e.g. "$default_target_directory"): " target_directory target_directory=${target_directory:-$default_target_directory} echo $target_directory read -p "Target Database Name (e.g. "$default_target_dbname"): " target_dbname target_dbname=${target_dbname:-$default_target_dbname} echo $target_dbname read -p "Target Database User (e.g. "$default_target_dbuser"): " target_dbuser target_dbuser=${target_dbuser:-$default_target_dbuser} echo $target_dbuser read -p "Target Database Pass (e.g. "$default_target_dbpass"): " target_dbpass target_dbpass=${target_dbpass:-$default_target_dbpass} echo $target_dbpass read -p "Target Conf File (e.g. "$default_target_conf"): " target_conf target_conf=${target_conf:-$default_target_conf} echo $target_conf
Once we've collected all the settings from the user, we ask if they wish to begin:
echo "Clone now? (y/n)" read -e run if [ "$run" == n ] ; then exit else echo "===================================================" echo "WordPress Cloning is Beginning" echo "==================================================="
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:
#backup source_directory cd $source_directory # add -v option to these if you want to see verbose file listings tar -czf source_clone_$NOW.tar.gz . #unzip clone in target directory mkdir -p $target_directory tar -xzf source_clone_$NOW.tar.gz -C $target_directory #remove tarball of source rm source_clone_$NOW.tar.gz cd $target_directory
We also run the standard file permissions for WordPress to make sure everything is set up properly and securely:
# Reset Directory Permissions find $target_directory -type d -exec chmod 755 {} \; find $target_directory -type f -exec chmod 644 {} \;
Update the WP-Config File
Next, we use perl to search and replace the source database authentication with the destination database information:
#set database details with perl find and replace perl -pi -e "s/$source_dbname/$target_dbname/g" wp-config.php perl -pi -e "s/$source_dbuser/$target_dbuser/g" wp-config.php perl -pi -e "s/$source_dbpass/$target_dbpass/g" wp-config.php echo "define('RELOCATE',true);" | tee -a wp-config.php #echo "define('WP_HOME','http://$target_domain');" | tee -a wp-config.php #echo "define('WP_SITEURL','http://$target_domain');" | tee -a wp-config.php echo "================================" echo "Directory duplicated" echo "================================"
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:
# Begin Database Duplication # Export the database mysqldump -u$mysql_user -p$mysql_pass $source_dbname > $target_directory/clone_$NOW.sql # Create the target database and permissions mysql -u$mysql_user -p$mysql_pass -e "create database $target_dbname; GRANT ALL PRIVILEGES ON $target_dbname.* TO '$target_dbuser'@'localhost' IDENTIFIED BY '$target_dbpass'" # Import the source database into the target mysql -u$mysql_user -p$mysql_pass $target_dbname < $target_directory/clone_$NOW.sql echo "================================" echo "Database duplicated" echo "================================"
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:
#Activate Web Configuration cp $default_apache_directory/$source_conf $default_apache_directory/$target_conf #set database details with perl find and replace perl -pi -e "s/$source_domain/$target_domain/g" $default_apache_directory/$target_conf perl -pi -e "s|${source_directory}|${target_directory}|g" $default_apache_directory/$target_conf a2ensite $target_conf service apache2 reload echo "================================" echo "Web configuration added" echo "================================" echo "Clone is complete." echo "Test at http://"$target_domain echo "================================" fi
And, that's it. Here's what a run-through of the script looks like in real life:
=================================================== Clone WordPress Script =================================================== MySQL Master Username (e.g. root-admin): harry_potter harry_potter MySQL Master Password (e.g. super-strong-password): voldemoort~jenny7! voldemoort~jenny7! Source Domain (e.g. gardening.io): gardening.io Source Directory (no trailing slash e.g. /var/www/gardening): /var/www/gardening Source Database Name (e.g. gardening): database_gardening database_gardening Source Database User (e.g. user_for_garden): hermione hermione Source Database Pass (e.g. pwd_garden): !987654321abcdefgh# !987654321abcdefgh# Source Conf File (e.g. gardening.conf): gardening.conf gardening.conf Target Domain (e.g. cycling.io): cycling.io Target Directory (no trailing slash e.g. /var/www/cycling): /var/www/cycling /var/www/cycling Target Database Name (e.g. cycling): database_cycling database_cycling Target Database User (e.g. user_for_cycling): hedwig hedwig Target Database Pass (e.g. pwd_cycling): pwd_for_cycling_not_hogwartz Target Conf File (e.g. cycling.conf): 0007b-cycling.conf 0007b-cycling.conf Clone now? (y/n) y =================================================== WordPress Cloning is Beginning =================================================== tar: .: file changed as we read it define('RELOCATE',true); ================================ Directory duplicated ================================ ================================ Database duplicated ================================ Enabling site 0007b-cycling. To activate the new configuration, you need to run: service apache2 reload * Reloading web server apache2 * ================================ Web configuration added ================================ Clone is complete. Test at http://cycling.io ================================
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:
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:
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:
#!/bin/bash -e # Clone a WordPress site via Bash script clear echo "===================================================" echo "Clone WordPress Script" echo "===================================================" # Set Default Settings (helpful for testing) default_mysql_user=$"root-admin" default_mysql_pass=$"super-strong-password" default_source_domain=$"gardening.io" default_target_domain=$"cycling.io" default_source_directory=$"/var/www/gardening" default_target_directory=$"/var/www/cycling" default_apache_directory=$"/etc/apache2/sites-available" default_source_conf=$"gardening.conf" default_target_conf=$"cycling.conf" default_source_dbname=$"gardening" default_source_dbuser=$"user_for_garden" default_source_dbpass=$"pwd_garden" default_target_dbname=$"cycling" default_target_dbuser=$"user_for_cycling" default_target_dbpass=$"pwd_cycling" NOW=$(date +"%Y-%m-%d-%H%M") #Request MySQL Admin read -p "MySQL Master Username (e.g. "$default_mysql_user"): " mysql_user mysql_user=${mysql_user:-$default_mysql_user} echo $mysql_user read -p "MySQL Master Password (e.g. "$default_mysql_pass"): " mysql_pass mysql_pass=${mysql_pass:-$default_mysql_pass} echo $mysql_pass # Request Source Settings read -p "Source Domain (e.g. "$default_source_domain"): " source_domain source_domain=${source_domain:-$default_source_domain} echo $source_domain read -p "Source Directory (no trailing slash e.g. "$default_source_directory"): " source_directory source_directory=${source_directory:-$default_source_directory} echo $source_directory read -p "Source Database Name (e.g. "$default_source_dbname"): " source_dbname source_dbname=${source_dbname:-$default_source_dbname} echo $source_dbname read -p "Source Database User (e.g. "$default_source_dbuser"): " source_dbuser source_dbuser=${source_dbuser:-$default_source_dbuser} echo $source_dbuser read -p "Source Database Pass (e.g. "$default_source_dbpass"): " source_dbpass source_dbpass=${source_dbpass:-$default_source_dbpass} echo $source_dbpass # Request Source Settings read -p "Source Conf File (e.g. "$default_source_conf"): " source_conf source_conf=${source_conf:-$default_source_conf} echo $source_conf # Request Target Settings read -p "Target Domain (e.g. "$default_target_domain"): " target_domain target_domain=${target_domain:-$default_target_domain} echo $target_domain read -p "Target Directory (no trailing slash e.g. "$default_target_directory"): " target_directory target_directory=${target_directory:-$default_target_directory} echo $target_directory read -p "Target Database Name (e.g. "$default_target_dbname"): " target_dbname target_dbname=${target_dbname:-$default_target_dbname} echo $target_dbname read -p "Target Database User (e.g. "$default_target_dbuser"): " target_dbuser target_dbuser=${target_dbuser:-$default_target_dbuser} echo $target_dbuser read -p "Target Database Pass (e.g. "$default_target_dbpass"): " target_dbpass target_dbpass=${target_dbpass:-$default_target_dbpass} echo $target_dbpass read -p "Target Conf File (e.g. "$default_target_conf"): " target_conf target_conf=${target_conf:-$default_target_conf} echo $target_conf echo "Clone now? (y/n)" read -e run if [ "$run" == n ] ; then exit else echo "===================================================" echo "WordPress Cloning is Beginning" echo "===================================================" #backup source_directory cd $source_directory # add -v option to these if you want to see verbose file listings tar -czf source_clone_$NOW.tar.gz . #unzip clone in target directory mkdir -p $target_directory tar -xzf source_clone_$NOW.tar.gz -C $target_directory #remove tarball of source rm source_clone_$NOW.tar.gz cd $target_directory # Reset Directory Permissions find $target_directory -type d -exec chmod 755 {} \; find $target_directory -type f -exec chmod 644 {} \; #set database details with perl find and replace perl -pi -e "s/$source_dbname/$target_dbname/g" wp-config.php perl -pi -e "s/$source_dbuser/$target_dbuser/g" wp-config.php perl -pi -e "s/$source_dbpass/$target_dbpass/g" wp-config.php echo "define('RELOCATE',true);" | tee -a wp-config.php #echo "define('WP_HOME','http://$target_domain');" | tee -a wp-config.php #echo "define('WP_SITEURL','http://$target_domain');" | tee -a wp-config.php echo "================================" echo "Directory duplicated" echo "================================" # Begin Database Duplication # Export the database mysqldump -u$mysql_user -p$mysql_pass $source_dbname > $target_directory/clone_$NOW.sql # Create the target database and permissions mysql -u$mysql_user -p$mysql_pass -e "create database $target_dbname; GRANT ALL PRIVILEGES ON $target_dbname.* TO '$target_dbuser'@'localhost' IDENTIFIED BY '$target_dbpass'" # Import the source database into the target mysql -u$mysql_user -p$mysql_pass $target_dbname < $target_directory/clone_$NOW.sql echo "================================" echo "Database duplicated" echo "================================" #Activate Web Configuration cp $default_apache_directory/$source_conf $default_apache_directory/$target_conf #set database details with perl find and replace perl -pi -e "s/$source_domain/$target_domain/g" $default_apache_directory/$target_conf perl -pi -e "s|${source_directory}|${target_directory}|g" $default_apache_directory/$target_conf a2ensite $target_conf service apache2 reload echo "================================" echo "Web configuration added" echo "================================" echo "Clone is complete." echo "Test at http://"$target_domain echo "================================" fi
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:
sudo rm -ifr /var/www/clone sudo a2dissite clone.conf sudo service apache2 reload sudo rm /etc/apache2/sites-available/clone.conf mysql -u root -p -e "drop database clone;"
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:
/**#@+ * Authentication Unique Keys and Salts. * * Change these to different unique phrases! * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service} * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again. * * @since 2.6.0 */ define('AUTH_KEY', '+9%S?YVnr%5Vr!Et4J,@9/Z^.kT_Lu~5SGwr9=|Y &D-ARSWf$mF#J_3U:/iE>-R'); define('SECURE_AUTH_KEY', 'e3Wr7%Aa7H1,f<SR[Sp&g.kJw,.)bR-9jz{uU&[R{[J]ITK8q>:!5@y:Q;c01dL '); define('LOGGED_IN_KEY', '1I%pW%UyjRMqy__Da)siA)+V]Ur$9uXPmxv|eBjM~-m&-<WEy&+XXb43uh8&aP+U'); define('NONCE_KEY', 'A9]+PFgvxYa^<B}_.F?9A,!&i-.b6E.I?&?U*)X.Vh+fq`SfE[XJG+MG|pg;y%Ah'); define('AUTH_SALT', 'gT (4]L{mm!|>9kC<%59rB7sbe1)jW0GCnfupJT+8z-z#%o@b|[QH=i@h|-/t!9S'); define('SECURE_AUTH_SALT', 'ON8K<,WSy8+F ~XaQpCwC8(a/{HksMh<T)QLD]s[-:yv+fx8!`<!*~mgB32X:w5k'); define('LOGGED_IN_SALT', 'vHJ%{=X6$ue>ZIo|%|cisp1R}9cJ< Rz-J;H|:O2A7$+*aGXMH!+KvD+tZ/I*U5$'); define('NONCE_SALT', '[ytQ;C)BvgU!#>a,,g|)~EKBQUig7Uv.-8?q%lmFte,P>,]f#.}i`Wx8S+_S@&.('); /**#@-*/
You can just visit https://api.wordpress.org/secret-key/1.1/salt/ and cut and paste them into your wp-config.php file:
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:
#set WP salts perl -i -pe' BEGIN { @chars = ("a" .. "z", "A" .. "Z", 0 .. 9); push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|"; sub salt { join "", map $chars[ rand @chars ], 1 .. 64 } } s/put your unique phrase here/salt()/ge ' wp-config.php
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).
Comments