COMMANDDUMP – Cloning a WordPress website for a Sandbox, Upgrade or Overhaul
Over the years, we have had clients ask us to create an exact copy of their current website (files, database and all) in a sandbox environment that would not affect their existing website. This typically involves setting up a temporary domain and hosting environment, and a new MySQL database, however they need them to be populated with an exact copy.
The needs they have varies:
- often it is to just be able to test a change within a disposable Sandbox,
- sometimes, they may want to do some sort of an upgrade, but they do not have a dedicated development or test environment,
- and commonly it is to start some sort of a site overhaul using the existing site’s pages, blog entries and design. In this case they will often migrate this site to their production site in the future
While a copy and paste seems like the simply way to do this, there is much more that must occur. This list below describes a list of all of the ones we have found so far
- Copy all of the files from the OLD WordPress root, to the NEW WordPress root
- Copy the entire database from Database A to Database B
- Update the NEW WordPress install to connect to Database B
- Update the Database B install wp_options to have the NEW url (if you skip this step, attempting to login to the NEW WordPress install will redirect you to the OLD WordPress install)
- Update all posts, pages and other entries which have absolute links to the OLD WordPress install to have absolute links to the NEW WordPress install. (if you do not change this, you may end up with embedded images and links which point back to the OLD WordPress install, sometimes this can be difficult to realize because the file structure is identical)
Once we realized this was going to be a common request and that we often need to do this from one directory on a server to another, we wanted to automate this process. We created a quick and dirty script which accomplishes all of the tasks of cloning the database and files, and then updating the contents of the database to the new location.
If you would like help with this process please contact us, Matraex would be happy to help you clone your WordPress website.
If you need a company to Manage your WordPress security and updates on a monthly basis please let us know here.
The script relies on some basic commands which should already be installed on your system, but you may want to confirm first
- sed
- mysql
- mysqldump
The script is one that you will run from the command line when you are within the existing WordPress website. You will run the command with parameters about the new WordPress website (The new directory, the new url, the new MySQL connection information.
The script does a couple of basic checks to make sure that the directory you are cloning to, does not already have a WordPress installation, and that the MySQL database is available but does not already have a WordPress install in the ‘default’ location.
It also uses the wp-config.php of the current WordPress installation to get connection information to the existing WP database and get the current URL.
If everything checks out the script
- copies all files from the old directory to the new directory
- dumps the existing database, manipulates a file to replace the old url with the new url
- imports the file into the new mysql database.
- updates the new directory wp-config.php to use the new MySQL connection information
File: wordpress_clone.sh
#!/bin/bash echo echo Usage: $0 1-NEW_DIR 2-NEW_URL 3-NEW_DB_HOST 4-NEW_DB_NAME 5-NEW_DB_USER 6-NEW_DB_PASSWORD if [ "$1" == "" ] || [ "$2" == "" ] || [ "$3" == "" ] || [ "$4" == "" ] || [ "$5" == "" ] || [ "$6" == "" ]; then echo echo "Invalid Parameters; please review usage"; echo "Exiting" echo exit fi NEW_DIR=$1 NEW_URL=$2 #type the url address that the new WordPress website is located at NEW_DB_HOST=$3 #TYPE the name of the database server for the NEW WordPress Install NEW_DB_NAME=$4 # Type the name of the NEW WordPress Database you want to connect to NEW_DB_USER=$5 #TYPE the username to connect to the NEW WordPress Database NEW_DB_PASSWORD=$6 #TYPE the password to connect to the NEW WordPress Database #this script assumes that you entered perfect information, it does not do any checking to confirm that any of the information you entered is valid before proceeding ORIG_DIR=`pwd` OLD_DIR=$ORIG_DIR #load all of the DB_variables from the old database into memory so we can dump it if [ ! -e wp-config.php ]; then echo echo "The current directory is not an existing WordPress installation" echo "Exiting" echo exit fi if [ ! -d $NEW_DIR ]; then echo echo "The new directory $NEW_DIR does not exist" echo "Exiting" echo exit fi cd $OLD_DIR source <(grep "^define('DB" wp-config.php |awk -F"'" '{print $2"=\""$4"\""}') EXISTING_NEW_DB=` mysql -u $NEW_DB_USER --password=$NEW_DB_PASSWORD -N --execute='select now()' -h $NEW_DB_HOST $NEW_DB_NAME 2>/dev/null` if [ "" == "$EXISTING_NEW_DB" ]; then echo echo "New Database Connection Failed; A new blank database must be available in order to continue" echo "Exiting" echo exit fi EXISTING_NEW_URL=` mysql -u $NEW_DB_USER --password=$NEW_DB_PASSWORD -N --execute='select option_value from wp_options where option_id=1' -h $NEW_DB_HOST $NEW_DB_NAME 2>/dev/null` if [ "" != "$EXISTING_NEW_URL" ]; then echo echo "There is already a WordPress database located at $NEW_DB_NAME: using '$EXISTING_NEW_URL'" echo "Exiting" echo exit fi OLD_URL=` mysql -u $DB_USER --password=$DB_PASSWORD -N --execute='select option_value from wp_options where option_id=1' -h $DB_HOST $DB_NAME` if [ "" == "$OLD_URL" ]; then echo echo "The database configuration in wp-config.php for the current WP install does not have a valid connection to the database $DB_NAME $DB_USER:$DB_PASSWORD@$DB_HOST" echo "Exiting" echo exit fi echo "from:$OLD_URL" echo "to :$NEW_URL" cp -ar $OLD_DIR/. $NEW_DIR/. TMPFILE=$(mktemp /tmp/`basename $0`.XXXXXXXXX) echo "Dumping Database " mysqldump -h $DB_HOST --extended-insert=FALSE -c -u $DB_USER --password=$DB_PASSWORD $DB_NAME >$TMPFILE echo Temp DB File:$TMPFILE sed -e"s|$OLD_URL|$NEW_URL|g" -i $TMPFILE cat $TMPFILE | mysql -u $NEW_DB_USER --password=$NEW_DB_PASSWORD $NEW_DB_NAME rm $TMPFILE cd $ORIG_DIR cd $NEW_DIR sed -e"s/define('DB_USER', '[A-Za-Z0-9]*/define('DB_USER', '$NEW_DB_USER/" -i wp-config.php sed -e"s/define('DB_PASSWORD', '[A-Za-Z0-9]*/define('DB_PASSWORD', '$NEW_DB_PASSWORD/" -i wp-config.php sed -e"s/define('DB_HOST', '[A-Za-Z0-9\.]*/define('DB_HOST', '$NEW_DB_HOST/" -i wp-config.php sed -e"s/define('DB_NAME', '[A-Za-Z0-9]*/define('DB_NAME', '$NEW_DB_NAME/" -i wp-config.php echo "Wrote DB Changes to $NEW_DIR/wp-config.php"