Quick Review: Boxee Box
December 27, 2011 – 12:22 am | 3 Comments

Some of the technical issues with Boxee Box could have been fixed if the dev team was paying more attention to addressing the bugs rather than adding “features” of dubious value. In the final analysis, for the price and ease of use, Boxee Box is the best in its class and price range. You just need to be mindful of its limitations and buy it in hope of future improvements to its usability.

Read the full story »
Networking

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

Applications

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

Data

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

Monitoring

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 pmNo Comment
Duplicating WordPress Installation

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:

#$MYSQLDUMP $DBNAME | $MYSQL $DBNEW
/etc/init.d/mysql stop
$RSYNC "${DBHOME}/${DBNAME}/" "${DBHOME}/${DBNEW}/"
/etc/init.d/mysql start

to look like this:

$MYSQLDUMP $DBNAME | $MYSQL $DBNEW
#/etc/init.d/mysql stop
#$RSYNC "${DBHOME}/${DBNAME}/" "${DBHOME}/${DBNEW}/"
#/etc/init.d/mysql start

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

#!/bin/bash
 
DATE=$(date +'%Y-%m-%d_%H-%M-%S')
WORKDIR="/srv/www/htdocs/krazyworks/solobox"
 
if [ ${1} ]
then
	SOURCE="${1}"
else
	echo "Must specify source directory. Exiting..."
	exit 1
fi
 
if [ ! -d "${WORKDIR}/${SOURCE}" ]
then
	echo "Source ${WORKDIR}/${SOURCE} not found. Exiting..."
	exit 1
fi
 
if [ ${2} ]
then
	TARGET="${2}"
else
	echo "Must specify target directory. Exiting..."
	exit 1
fi
 
configure() {
	HTTPUSER="wwwrun"
	HTTPGROUP="www"
	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}"
	DBHOME="/var/lib/mysql"
	RSYNC="/usr/local/bin/rsync -avu"
}
 
db_copy() {
	echo "Copying database $DBNAME to $DBNEW"
	echo "CREATE DATABASE $DBNEW;" | $MYSQL
	echo "GRANT ALL PRIVILEGES ON ${DBNEW}.* to ${DBUSER}@'%' IDENTIFIED BY '$DBPASS' WITH GRANT OPTION ;" |\
	$MYSQL
	#$MYSQLDUMP $DBNAME | $MYSQL $DBNEW
	/etc/init.d/mysql stop
	$RSYNC "${DBHOME}/${DBNAME}/" "${DBHOME}/${DBNEW}/"
	/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
	SHOW TABLES ;
	quit
	EOF )
 
	echo "SHOW TABLES;" | $MYSQL $DBNEW | grep -v "Tables_in_$DBNEW" | while read TABLE
	do
		echo "SHOW COLUMNS FROM $TABLE;" | $MYSQL $DBNEW | awk -F'\t' '{print $1}' |\
		grep -v "Field" | while read COLUMN
		do
			echo "update $TABLE set ${COLUMN} = replace(${COLUMN}, '/${SOURCE}', '/${TARGET}');" |\
			$MYSQL $DBNEW
		done
	done
}
 
site_copy() {
	echo "Copying site from ${SOURCE} to ${TARGET}"
	mkdir "${WORKDIR}/${TARGET}"
	$RSYNC "${WORKDIR}/${SOURCE}/" "${WORKDIR}/${TARGET}/"
	chown -R ${HTTPUSER}:${HTTPGROUP} "${WORKDIR}/${TARGET}"
}
 
configure
db_copy
site_copy
modify_wp_conf

Popularity: 5% [?]

Related posts:

  1. WordPress Quick Upgrade for Linux
  2. MySQL Global Search and Replace Script
  3. Passing MySQL Commands from Shell Script
  4. Restart Apache, MySQL When Low on Memory
  5. Generating complex SQL queries with shell scripts
  6. Exporting spreadsheet data to MySQL
  7. Apache MySQL PHP Solaris 8 Installation
  8. Creating print.css for WordPress
  9. Database operations with SQL and Korn shell
  10. Improving WordPress Performance

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.