Production and Development Environments using WordPress
Several web hosting companies offer a one-click install (and one-click updates) of WordPress. While this is very convenient, I would not recommend using it for a production (LIVE) website with any kind of significant, around-the-clock traffic. Reasons? Among other things that could break, there is always the chance that one of your plugins is not compatible with the latest build - either by displaying some kind of ugly error after the upgrade or just silently ceasing to work properly.
My proposition is to set up a separate, development (DEV), instance of your website, under a separate URL, using its own database, and of course, separate file structure. In this environment, you can use the one-click install, try new plugins, perform and test theme modifications, etc. without the risk of breaking anything on your LIVE website.
The task this article wrestles with is trying to create an easy way to a) deploy your changes from DEV to LIVE environment, and b) to restore the LIVE environment to DEV to have the most current version for messing with. The scripts I show help me with semi-automating these tasks.
So to set up this DEV environment, you'll need…
- A dedicated subdomain. To make it easy to remember, you can set up dev.yoururl.com to be your DEV environment for your www.yoururl.com.
- A dedicated database
SCRIPT 1: Restores LIVE database (or its previously created dump) to DEV database.
#!/bin/bash
# Directory to be used for temp files created by this script
BTEMP_DIR= # e.g. /home/andrej/.restore_temp
# If you are restoring from a previously created database dump
BACKUP_DIR= # e.g. /mnt/hda7/db_dump
# Your database access credentials
BMYSQL_HOST= # e.g. localhost or mysql.yoururl.com
BMYSQL_USER= # e.g. andrej
BMYSQL_PWD= # your password
# Source Database
BMYSQL_DBNAME= # e.g. wordpressLIVE
# Target Database
BMYSQL_DBTARGET= # e.g. wordpressDEV
# Script (.sql) to run after the database is restored
BPOST_RESTORE= # e.g. /home/andrej/.scripts/post_live_to_dev.sql
# This script mentioned above can contain these two useful statements
# and possibly other
# UPDATE wp_posts
# SET post_content = REPLACE(post_content,'www.yoururl.com','dev.yoururl.com');
# UPDATE wp_options
# SET option_value = REPLACE(option_value,'www.yoururl.com','dev.yoururl.com');
# Make sure output directory exists.
if [ ! -d $BTEMP_DIR ]; then
mkdir -p $BTEMP_DIR
fi
If you are restoring from a previously generated backup as described in my post about nightly MySQL backups, add the following:
# gunzip and write contents of the db backup to a temporary sql_to_run.sql
cat $BACKUP_DIR/$BMYSQL_DBNAME.gz.0 | gunzip > $BTEMP_DIR/sql_to_run.sql 2>/dev/null
otherwise add the following:
# Dump (sounds scary, but really means export) database to a temp file
mysqldump --host=$BMYSQL_HOST --user=$BMYSQL_USER --pass=$BMYSQL_PWD --add-drop-table $BMYSQL_DBNAME | gzip > $BTEMP_DIR/$BMYSQL_DBNAME
# gunzip and write contents of the temp file to a temporary sql_to_run.sql file
cat $BTEMP_DIR/$BMYSQL_DBNAME | gunzip > $BTEMP_DIR/sql_to_run.sql 2>/dev/null
…and now everybody continue here:
# read from $BPOST_RESTORE file mentioned above and add to the end of
# the temporary sql_to_run.sql file
less $BPOST_RESTORE >> $BTEMP_DIR/sql_to_run.sql
# run the generated sql_to_run.sql file
mysql --host=$BMYSQL_HOST --user=$BMYSQL_USER --pass=$BMYSQL_PWD $BMYSQL_DBTARGET < $BTEMP_DIR/sql_to_run.sq
OK, the DEV database should be successfully restored to what the LIVE looks like.
SCRIPT 2: Make an identical copy of the LIVE file structure to DEV:
If you are restoring from a file backup described in my previous post, add the following:
#!/bin/bash
# your backup file
SOURCE_FILE= # e.g. /mnt7/backup/www.yoururl.com.tgz.0
# your DEV folder
TARGET_DIR= # e.g. /var/www/dev.yoururl.com
# Clean everything in the DEV folder
rm $TARGET_DIR/* -R >/dev/null 2>&1
# Restore files from the backup file
tar xvzf $SOURCE_FILE -C $TARGET_DIR
If there is no file backup, just create a copy using rsync.
#!/bin/bash
# Path to your LIVE file structure
SOURCE_DIR = # e.g. /var/www/www.yoururl.com
# Path to your DEV file structure
TARGET_DIR = # e.g. /var/www/dev.yoururl.com
# create a mirror of LIVE on DEV (deleting DEV files)
rsync -a --delete --exclude "wp-config.php" --exclude "/cache/wp-cache-" $SOURCE_DIR/ $TARGET_DIR
IMPORTANT: After you restore your WordPress file system, make sure to edit your wp-config.php to reflect the correct database (DEV or LIVE). You may also have to regenerate permalinks in WordPress' interface under Options, Permalinks.
Messing with the above 2 scripts, you can run this both directions - DEV to LIVE and LIVE to DEV.
Comments and corrections are welcome.
Leave a Comment