Advertisement
Scroll to top
Read Time: 17 min
Final product imageFinal product imageFinal 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:

1
#!/bin/bash -e

2
# Clone a WordPress site via Bash script

3
clear
4
echo "==================================================="
5
echo "Clone WordPress Script"
6
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:

1
chmod +x clonewp.sh

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

1
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:

1
# Set Default Settings (helpful for testing)

2
default_mysql_user=$"root-admin"
3
default_mysql_pass=$"super-strong-password"
4
default_source_domain=$"gardening.io"
5
default_target_domain=$"cycling.io"
6
default_source_directory=$"/var/www/gardening"
7
default_target_directory=$"/var/www/cycling"
8
default_apache_directory=$"/etc/apache2/sites-available"
9
default_source_conf=$"gardening.conf"
10
default_target_conf=$"cycling.conf"
11
default_source_dbname=$"gardening"
12
default_source_dbuser=$"user_for_garden"
13
default_source_dbpass=$"pwd_garden"
14
default_target_dbname=$"cycling"
15
default_target_dbuser=$"user_for_cycling"
16
default_target_dbpass=$"pwd_cycling"
17
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:

1
# Request Source Settings

2
read -p "Source Domain (e.g. "$default_source_domain"): " source_domain
3
source_domain=${source_domain:-$default_source_domain}
4
echo $source_domain
5
read -p "Source Directory (no trailing slash e.g. "$default_source_directory"): " source_directory
6
source_directory=${source_directory:-$default_source_directory}
7
echo $source_directory
8
read -p "Source Database Name (e.g. "$default_source_dbname"): " source_dbname
9
source_dbname=${source_dbname:-$default_source_dbname}
10
echo $source_dbname
11
read -p "Source Database User (e.g. "$default_source_dbuser"): " source_dbuser
12
source_dbuser=${source_dbuser:-$default_source_dbuser}
13
echo $source_dbuser
14
read -p "Source Database Pass (e.g. "$default_source_dbpass"): " source_dbpass
15
source_dbpass=${source_dbpass:-$default_source_dbpass}
16
echo $source_dbpass
17
# Request Source Settings

18
read -p "Source Conf File (e.g. "$default_source_conf"): " source_conf
19
source_conf=${source_conf:-$default_source_conf}
20
echo $source_conf
21
# Request Target Settings

22
read -p "Target Domain (e.g. "$default_target_domain"): " target_domain
23
target_domain=${target_domain:-$default_target_domain}
24
echo $target_domain
25
read -p "Target Directory (no trailing slash e.g. "$default_target_directory"): " target_directory
26
target_directory=${target_directory:-$default_target_directory}
27
echo $target_directory
28
read -p "Target Database Name (e.g. "$default_target_dbname"): " target_dbname
29
target_dbname=${target_dbname:-$default_target_dbname}
30
echo $target_dbname
31
read -p "Target Database User (e.g. "$default_target_dbuser"): " target_dbuser
32
target_dbuser=${target_dbuser:-$default_target_dbuser}
33
echo $target_dbuser
34
read -p "Target Database Pass (e.g. "$default_target_dbpass"): " target_dbpass
35
target_dbpass=${target_dbpass:-$default_target_dbpass}
36
echo $target_dbpass
37
read -p "Target Conf File (e.g. "$default_target_conf"): " target_conf
38
target_conf=${target_conf:-$default_target_conf}
39
echo $target_conf

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

1
echo "Clone now? (y/n)"
2
read -e run
3
if [ "$run" == n ] ; then

4
exit

5
else

6
echo "==================================================="
7
echo "WordPress Cloning is Beginning"
8
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:

1
#backup source_directory

2
cd $source_directory
3
# add -v option to these if you want to see verbose file listings

4
tar -czf source_clone_$NOW.tar.gz .
5
#unzip clone in target directory

6
mkdir -p $target_directory
7
tar -xzf source_clone_$NOW.tar.gz -C $target_directory
8
#remove tarball of source

9
rm source_clone_$NOW.tar.gz
10
cd $target_directory

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

1
# Reset Directory Permissions

2
find $target_directory -type d -exec chmod 755 {} \;
3
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:

1
#set database details with perl find and replace

2
perl -pi -e "s/$source_dbname/$target_dbname/g" wp-config.php
3
perl -pi -e "s/$source_dbuser/$target_dbuser/g" wp-config.php
4
perl -pi -e "s/$source_dbpass/$target_dbpass/g" wp-config.php
5
echo "define('RELOCATE',true);" | tee -a wp-config.php
6
#echo "define('WP_HOME','http://$target_domain');" | tee -a wp-config.php

7
#echo "define('WP_SITEURL','http://$target_domain');" | tee -a wp-config.php

8
echo "================================"
9
echo "Directory duplicated"
10
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:

1
# Begin Database Duplication

2
# Export the database

3
mysqldump -u$mysql_user -p$mysql_pass $source_dbname > $target_directory/clone_$NOW.sql
4
# Create the target database and permissions

5
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'"
6
# Import the source database into the target

7
mysql -u$mysql_user -p$mysql_pass $target_dbname < $target_directory/clone_$NOW.sql
8
echo "================================"
9
echo "Database duplicated"
10
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:

1
#Activate Web Configuration

2
cp $default_apache_directory/$source_conf $default_apache_directory/$target_conf
3
#set database details with perl find and replace

4
perl -pi -e "s/$source_domain/$target_domain/g" $default_apache_directory/$target_conf
5
perl -pi -e "s|${source_directory}|${target_directory}|g" $default_apache_directory/$target_conf
6
a2ensite $target_conf
7
service apache2 reload
8
echo "================================"
9
echo "Web configuration added"
10
echo "================================"
11
echo "Clone is complete."
12
echo "Test at http://"$target_domain
13
echo "================================"
14
fi

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

1
===================================================
2
Clone WordPress Script
3
===================================================
4
MySQL Master Username (e.g. root-admin): harry_potter
5
harry_potter
6
MySQL Master Password (e.g. super-strong-password): voldemoort~jenny7!
7
voldemoort~jenny7!
8
Source Domain (e.g. gardening.io): 
9
gardening.io
10
Source Directory (no trailing slash e.g. /var/www/gardening): 
11
/var/www/gardening
12
Source Database Name (e.g. gardening): database_gardening
13
database_gardening
14
Source Database User (e.g. user_for_garden): hermione
15
hermione
16
Source Database Pass (e.g. pwd_garden): !987654321abcdefgh#
17
!987654321abcdefgh#
18
Source Conf File (e.g. gardening.conf): gardening.conf
19
gardening.conf
20
Target Domain (e.g. cycling.io): 
21
cycling.io
22
Target Directory (no trailing slash e.g. /var/www/cycling): /var/www/cycling
23
/var/www/cycling
24
Target Database Name (e.g. cycling): database_cycling
25
database_cycling
26
Target Database User (e.g. user_for_cycling): hedwig
27
hedwig
28
Target Database Pass (e.g. pwd_cycling): 
29
pwd_for_cycling_not_hogwartz
30
Target Conf File (e.g. cycling.conf): 0007b-cycling.conf               
31
0007b-cycling.conf
32
Clone now? (y/n)
33
y
34
===================================================
35
WordPress Cloning is Beginning
36
===================================================
37
tar: .: file changed as we read it
38
define('RELOCATE',true);
39
================================
40
Directory duplicated
41
================================
42
================================
43
Database duplicated
44
================================
45
Enabling site 0007b-cycling.
46
To activate the new configuration, you need to run:
47
  service apache2 reload
48
 * Reloading web server apache2                                                                                     * 
49
================================
50
Web configuration added
51
================================
52
Clone is complete.
53
Test at http://cycling.io
54
================================

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 pathClone WordPress Use the wp-login php pathClone 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 SettingsClone WordPress General Domain SettingsClone 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:

1
#!/bin/bash -e

2
# Clone a WordPress site via Bash script

3
clear
4
echo "==================================================="
5
echo "Clone WordPress Script"
6
echo "==================================================="
7
# Set Default Settings (helpful for testing)

8
default_mysql_user=$"root-admin"
9
default_mysql_pass=$"super-strong-password"
10
default_source_domain=$"gardening.io"
11
default_target_domain=$"cycling.io"
12
default_source_directory=$"/var/www/gardening"
13
default_target_directory=$"/var/www/cycling"
14
default_apache_directory=$"/etc/apache2/sites-available"
15
default_source_conf=$"gardening.conf"
16
default_target_conf=$"cycling.conf"
17
default_source_dbname=$"gardening"
18
default_source_dbuser=$"user_for_garden"
19
default_source_dbpass=$"pwd_garden"
20
default_target_dbname=$"cycling"
21
default_target_dbuser=$"user_for_cycling"
22
default_target_dbpass=$"pwd_cycling"
23
NOW=$(date +"%Y-%m-%d-%H%M")
24
25
#Request MySQL Admin

26
read -p "MySQL Master Username (e.g. "$default_mysql_user"): " mysql_user
27
mysql_user=${mysql_user:-$default_mysql_user}
28
echo $mysql_user
29
read -p "MySQL Master Password (e.g. "$default_mysql_pass"): " mysql_pass
30
mysql_pass=${mysql_pass:-$default_mysql_pass}
31
echo $mysql_pass
32
33
# Request Source Settings

34
read -p "Source Domain (e.g. "$default_source_domain"): " source_domain
35
source_domain=${source_domain:-$default_source_domain}
36
echo $source_domain
37
read -p "Source Directory (no trailing slash e.g. "$default_source_directory"): " source_directory
38
source_directory=${source_directory:-$default_source_directory}
39
echo $source_directory
40
read -p "Source Database Name (e.g. "$default_source_dbname"): " source_dbname
41
source_dbname=${source_dbname:-$default_source_dbname}
42
echo $source_dbname
43
read -p "Source Database User (e.g. "$default_source_dbuser"): " source_dbuser
44
source_dbuser=${source_dbuser:-$default_source_dbuser}
45
echo $source_dbuser
46
read -p "Source Database Pass (e.g. "$default_source_dbpass"): " source_dbpass
47
source_dbpass=${source_dbpass:-$default_source_dbpass}
48
echo $source_dbpass
49
# Request Source Settings

50
read -p "Source Conf File (e.g. "$default_source_conf"): " source_conf
51
source_conf=${source_conf:-$default_source_conf}
52
echo $source_conf
53
# Request Target Settings

54
read -p "Target Domain (e.g. "$default_target_domain"): " target_domain
55
target_domain=${target_domain:-$default_target_domain}
56
echo $target_domain
57
read -p "Target Directory (no trailing slash e.g. "$default_target_directory"): " target_directory
58
target_directory=${target_directory:-$default_target_directory}
59
echo $target_directory
60
read -p "Target Database Name (e.g. "$default_target_dbname"): " target_dbname
61
target_dbname=${target_dbname:-$default_target_dbname}
62
echo $target_dbname
63
read -p "Target Database User (e.g. "$default_target_dbuser"): " target_dbuser
64
target_dbuser=${target_dbuser:-$default_target_dbuser}
65
echo $target_dbuser
66
read -p "Target Database Pass (e.g. "$default_target_dbpass"): " target_dbpass
67
target_dbpass=${target_dbpass:-$default_target_dbpass}
68
echo $target_dbpass
69
read -p "Target Conf File (e.g. "$default_target_conf"): " target_conf
70
target_conf=${target_conf:-$default_target_conf}
71
echo $target_conf
72
echo "Clone now? (y/n)"
73
read -e run
74
if [ "$run" == n ] ; then

75
exit

76
else

77
echo "==================================================="
78
echo "WordPress Cloning is Beginning"
79
echo "==================================================="
80
#backup source_directory

81
cd $source_directory
82
# add -v option to these if you want to see verbose file listings

83
tar -czf source_clone_$NOW.tar.gz .
84
#unzip clone in target directory

85
mkdir -p $target_directory
86
tar -xzf source_clone_$NOW.tar.gz -C $target_directory
87
#remove tarball of source

88
rm source_clone_$NOW.tar.gz
89
cd $target_directory
90
# Reset Directory Permissions

91
find $target_directory -type d -exec chmod 755 {} \;
92
find $target_directory -type f -exec chmod 644 {} \;
93
#set database details with perl find and replace

94
perl -pi -e "s/$source_dbname/$target_dbname/g" wp-config.php
95
perl -pi -e "s/$source_dbuser/$target_dbuser/g" wp-config.php
96
perl -pi -e "s/$source_dbpass/$target_dbpass/g" wp-config.php
97
echo "define('RELOCATE',true);" | tee -a wp-config.php
98
#echo "define('WP_HOME','http://$target_domain');" | tee -a wp-config.php

99
#echo "define('WP_SITEURL','http://$target_domain');" | tee -a wp-config.php

100
echo "================================"
101
echo "Directory duplicated"
102
echo "================================"
103
# Begin Database Duplication

104
# Export the database

105
mysqldump -u$mysql_user -p$mysql_pass $source_dbname > $target_directory/clone_$NOW.sql
106
# Create the target database and permissions

107
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'"
108
# Import the source database into the target

109
mysql -u$mysql_user -p$mysql_pass $target_dbname < $target_directory/clone_$NOW.sql
110
echo "================================"
111
echo "Database duplicated"
112
echo "================================"
113
#Activate Web Configuration

114
cp $default_apache_directory/$source_conf $default_apache_directory/$target_conf
115
#set database details with perl find and replace

116
perl -pi -e "s/$source_domain/$target_domain/g" $default_apache_directory/$target_conf
117
perl -pi -e "s|${source_directory}|${target_directory}|g" $default_apache_directory/$target_conf
118
a2ensite $target_conf
119
service apache2 reload
120
echo "================================"
121
echo "Web configuration added"
122
echo "================================"
123
echo "Clone is complete."
124
echo "Test at http://"$target_domain
125
echo "================================"
126
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:

1
  sudo rm -ifr /var/www/clone
2
  sudo a2dissite clone.conf 
3
  sudo service apache2 reload
4
  sudo rm /etc/apache2/sites-available/clone.conf
5
  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:

1
/**#@+

2
 * Authentication Unique Keys and Salts.
3
 *
4
 * Change these to different unique phrases!
5
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key 
6
service}
7
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have 
8
to log in again.
9
 *
10
 * @since 2.6.0
11
 */
12
define('AUTH_KEY',         '+9%S?YVnr%5Vr!Et4J,@9/Z^.kT_Lu~5SGwr9=|Y &D-ARSWf$mF#J_3U:/iE>-R');
13
define('SECURE_AUTH_KEY',  'e3Wr7%Aa7H1,f<SR[Sp&g.kJw,.)bR-9jz{uU&[R{[J]ITK8q>:!5@y:Q;c01dL ');
14
define('LOGGED_IN_KEY',    '1I%pW%UyjRMqy__Da)siA)+V]Ur$9uXPmxv|eBjM~-m&-<WEy&+XXb43uh8&aP+U');
15
define('NONCE_KEY',        'A9]+PFgvxYa^<B}_.F?9A,!&i-.b6E.I?&?U*)X.Vh+fq`SfE[XJG+MG|pg;y%Ah');
16
define('AUTH_SALT',        'gT (4]L{mm!|>9kC<%59rB7sbe1)jW0GCnfupJT+8z-z#%o@b|[QH=i@h|-/t!9S');
17
define('SECURE_AUTH_SALT', 'ON8K<,WSy8+F ~XaQpCwC8(a/{HksMh<T)QLD]s[-:yv+fx8!`<!*~mgB32X:w5k');
18
define('LOGGED_IN_SALT',   'vHJ%{=X6$ue>ZIo|%|cisp1R}9cJ< Rz-J;H|:O2A7$+*aGXMH!+KvD+tZ/I*U5$');
19
define('NONCE_SALT',       '[ytQ;C)BvgU!#>a,,g|)~EKBQUig7Uv.-8?q%lmFte,P>,]f#.}i`Wx8S+_S@&.(');
20
/**#@-*/

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 GeneratorWordPress Authentication Key and Salt GeneratorWordPress 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:

1
#set WP salts

2
perl -i -pe'

3
  BEGIN {

4
    @chars = ("a" .. "z", "A" .. "Z", 0 .. 9);

5
    push @chars, split //, "!@#$%^&*()-_ []{}<>~\`+=,.;:/?|";

6
    sub salt { join "", map $chars[ rand @chars ], 1 .. 64 }

7
  }

8
  s/put your unique phrase here/salt()/ge

9
' 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).

Related Links

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.