Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active December 21, 2015 08:19
Show Gist options
  • Save westonruter/6277456 to your computer and use it in GitHub Desktop.
Save westonruter/6277456 to your computer and use it in GitHub Desktop.
Given a WordPress multisite instance, export the blog tables from one site (blog) on a server environment (e.g. staging) with the domains migrated to another server environment (e.g. production)
#!/bin/bash
# Export the tables from a multisite blog on one environment to another environment
# Author: Weston Ruter (@westonruter), X-Team (@x_team)
set -e
# Redirect all to stderr
exec 5>&1 1>&2
if [ $# != 2 ]; then
echo 'You must provide the src domain as the first argument (e.g. staging-www.example.com),'
echo 'And you must provide the dest domain as the first argument (e.g. www.example.com)'
echo "USAGE:"
echo "$0 staging.example.com www.example.com > production.sql"
exit 1
fi
src_domain=$1
dest_domain=$2
wp db export --url=$src_domain /tmp/$src_domain-backup.sql
echo 'Gathering DB credentials'
mysql_args=$(wp eval --url=$src_domain '
$args = array(
"--host=" . DB_HOST,
"--user=" . DB_USER,
"--password=" . DB_PASSWORD,
DB_NAME
);
global $wpdb;
foreach ( $wpdb->tables as $table ) {
$args[] = $wpdb->$table;
}
echo join( " ", $args );
')
echo 'Running search and replace'
wp search-replace --url=$src_domain --network $src_domain $dest_domain
echo 'Running mysqldump for blog tables'
# Restore stdout
exec 1>&5
mysqldump \
--no-create-db \
--default-character-set=utf8 \
--set-charset \
--extended-insert=FALSE \
--add-drop-table \
$mysql_args >&1
# Redirect all to stderr
exec 5>&1 1>&2
wp db import --url=$src_domain /tmp/$src_domain-backup.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment