Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » MySQL, Scripts, WordPress

Duplicating WordPress Installation

Submitted by on June 25, 2009 – 2:51 pm 5 Comments

Whenever you upgrade your WordPress installation or do development work, it is always a good idea to be working on a copy of your main site and not on the real thing. Copying WordPress installation is not exactly difficult, but it is a bit tedious, which is the reason why many Webmasters apply changes directly to the production site and hope for the best. As a Sysadmin I understand the value of this approach. I also understand that eventually it will cost you weeks of work or even your job.

I say, when in doubt – write a script. Here’s one that will create a near-mirror image of your WordPress installation – plugins, themes, galleries and all. The script will duplicate your database and make the necessary changes to the copy to reflect the changed URL. Look through this script carefully: depending on your system configuration, there are a few things you may need to change. Always back up your site and your database before running any scripts (especially my scripts). Pay particular attention to the “configure()” section. Once your site is copied, there is one plugin that you may need to reconfigure: WP-DBManager. If you have it, just open the options page and re-enter the required paths and other settings.

The basic syntax is simple: wp-copy . You will then be prompted for database login and password, as well as for the name of the new database. The and are just folder names in your server home – not paths. Here’s an example: say, the absolute path to your site root is /srv/www/htdocs/original_site. You will run the script like so: “wp-copy original_site copy_site“. This will copy your site to /srv/www/htdocs/copy_site.

Another point to make: there are two ways to copy a MySQL database. One method is to use mysqldump | mysql construct. This does not always work and you may encounter some SQL errors. The more direct approach is to temporarily shut down the database and duplicate the entire database directory. This is what this script does. If you wish to try mysqldump method first, change this section:

/etc/init.d/mysql stop
/etc/init.d/mysql start

to look like this:

#/etc/init.d/mysql stop
#/etc/init.d/mysql start

And here is the entire script. Save is as /sbin/wp-copy and chmod 700.


DATE=$(date +'%Y-%m-%d_%H-%M-%S')

if [  ]
	echo "Must specify source directory. Exiting..."
	exit 1

if [ ! -d "${WORKDIR}/${SOURCE}" ]
	echo "Source ${WORKDIR}/${SOURCE} not found. Exiting..."
	exit 1

if [  ]
	echo "Must specify target directory. Exiting..."
	exit 1

configure() {
	DBNAME=$(grep DB_NAME "${WORKDIR}/${SOURCE}/wp-config.php" | awk -F"'" '{print $4}')
	echo -n "Enter database username: " ; read DBUSER
	echo -n "Enter $DBUSER password: " ; stty -echo ; read DBPASS ; stty echo ; echo ""
	echo -n "Enter new database name: " ; read DBNEW
	MYSQL="/usr/bin/mysql -u${DBUSER} -p${DBPASS}"
	MYSQLDUMP="/usr/bin/mysqldump -u${DBUSER} -p${DBPASS}"
	RSYNC="/usr/local/bin/rsync -avu"

db_copy() {
	echo "Copying database $DBNAME to $DBNEW"
	/etc/init.d/mysql stop
	/etc/init.d/mysql start

modify_wp_conf() {
	echo "Modifying new database"
	cat "${WORKDIR}/${TARGET}/wp-config.php" | 
	sed "s/define('DB_NAME', '$DBNAME')/define('DB_NAME', '$DBNEW')/g" > /tmp/wp-copy.tmp
	mv /tmp/wp-copy.tmp "${WORKDIR}/${TARGET}/wp-config.php"
	options_table=$($MYSQL $DBNEW << "	EOF" | grep "_options$" | tail -1
	EOF )

	echo "SHOW TABLES;" | $MYSQL $DBNEW | grep -v "Tables_in_$DBNEW" | while read TABLE
		echo "SHOW COLUMNS FROM $TABLE;" | $MYSQL $DBNEW | awk -F't' '{print $1}' |
		grep -v "Field" | while read COLUMN
			echo "update $TABLE set ${COLUMN} = replace(${COLUMN}, '/${SOURCE}', '/${TARGET}');" |

site_copy() {
	echo "Copying site from ${SOURCE} to ${TARGET}"
	mkdir "${WORKDIR}/${TARGET}"

Print Friendly, PDF & Email


  • JackReynolds says:

    I want to start a small website that I can put my writing up for review on. I need a service that’s free, has an option to change to a different URL when I need to, and have a shop app (or something similar like PayPal). First GOOD answer gets 10 points AND best answer. Please provide a link to the website.

    Additional Details
    I also need it to have a sub-domain that’s free, like
    but with an option to change to a registered domain name later on for free.

  • Milk84 says:

    I ran the install and everything seemed to go well. But when I login I get these error messages

    Warning: ini_set() has been disabled for security reasons in /www/ on line 272

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 353

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 365

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 737

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 738

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 739

    Warning: Cannot modify header information – headers already sent by (output started at /www/ in /www/ on line 934

    I added ?> to the wp-config.php like the tutorial suggested but still no go. Whats goin on here?

  • Jason says:

    Would I be able to have multiple sites under one WordPress 3.0 installation and have each of those sites share the same content among all of them. I know you can run multiple sites under one installation but I am not completely certain about the ability to share content (ie. post and media, etc.) among all of the individual sites

  • Maggie says:

    Hi i am very new to wordpress.
    My question is:
    Is it possible to add new sites to a single database. I will put it in this way.
    I have a WordPress database XYZ and it have all the tables needed for a single site.
    I want to add a new site in this without a new table added.
    Can i insert a new set of records to the existing table for the second site? If so how i differentiate between the records.

  • Scorch Delta-62 says:

    I was just wondering what are the licence terms on those types of wordpress themes.
    Under what licence are they distributed? Can they be used for commercial purposes? Basically, my question is – if a theme is free, what am I allowed and not allowed to do with it?

    I would like to know as much as possible


Leave a Reply

%d bloggers like this: