Skip to content

Instantly share code, notes, and snippets.

@goldenapples
Created September 21, 2012 17:55
Show Gist options
  • Save goldenapples/3762944 to your computer and use it in GitHub Desktop.
Save goldenapples/3762944 to your computer and use it in GitHub Desktop.
BASH script for deploying WordPress content changes from staging to production
#!/bin/bash
# A BASH script for deploying WordPress content changes
# from staging to production. Note: this works for me because both my
# staging and production share the same plugins folder, and I have permissions
# on all the appropriate directories to allow the user running the script to
# perform all the actions in here. May need tweaking for other setups.
STAGING_DIR="/var/www/staging-wpsite"
PRODUCTION_DIR="/var/www/wpsite"
STAGING_DB="staging_wpsite"
PRODUCTION_DB="wpsite"
# Get list of currently installed plugins from production site
cd $PRODUCTION_DIR
production_plugin_status=`tempfile 2>/dev/null` || tempfile=/tmp/temp$$
wp plugin status --porcelain > $production_plugin_status
# Dump the staging database to temp file; import into production site
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/temp$$
mysqldump -u root $STAGING_DB > $tempfile
mysql -u root $PRODUCTION_DB < $tempfile
# Get list of installed plugins after copying the database over
staging_plugin_status=`tempfile 2>/dev/null` || tempfile=/tmp/temp$$
wp plugin status --porcelain > $staging_plugin_status
echo 'Syncing uploads folders...'
rsync -a $STAGING_DIR/wp-content/uploads/* $PRODUCTION_DIR/wp-content/uploads/
# Compare before/after status of installed plugins; activate or deactivate as necessary
STATUS_DIFF=`paste $production_plugin_status $staging_plugin_status | grep -v 'Installed plugins' | grep -v 'Legend:' | awk '$1 != $3'`
if [ -n "$STATUS_DIFF" ]; then
echo $STATUS_DIFF | while read line; do
PLUGIN_SLUG=`echo $line | awk '{print $2}'`
PLUGIN_STATUS=`echo $line | awk '{print $1}'`
if grep -q 'A' <<<$PLUGIN_STATUS; then
echo "$PLUGIN_SLUG should be active."
wp plugin activate `echo ${PLUGIN_SLUG}`
else
echo "$PLUGIN_SLUG should not be active."
wp plugin deactivate ${PLUGIN_SLUG}
fi
done
fi
echo 'Flushing rewrite rules...'
wp rewrite flush
echo 'Rebuilding sitemap...'
wp google-sitemap rebuild
echo 'Updating WADL file for console...'
wp wadl update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment