Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Import a remote site to your local vagrant
#!/bin/bash
# Author: Kevin Chappell http://kevin-chappell.com
# Description: Pull a production site into your local vagrant
# Version: 0.1.2
echo -e '\e[32m
_ ______ ____ ____
| | / / __ \ / __ \__ __/ / /
| | /| / / /_/ / / /_/ / / / / / /
| |/ |/ / ____/ / ____/ /_/ / / /
|__/|__/_/ /_/ \__,_/_/_/\e[39m'
echo -e '\nUsage Requirements: VVV, Linux or Mac host machine and RHEL 6 production server. \n'
# Get some variables
CUR_DIR=$(pwd)
# Vars from the user
read -p 'Server Hostname?: ' HOSTNAME
read -p 'Server Account Username?: ' USERNAME
VAGRANT_SITE_DIR="/vagrant/www/$USERNAME"
HOST_VAGRANT_SITE_DIR=~/vagrant-local/www/$USERNAME
if [ ! -d "$HOST_VAGRANT_SITE_DIR" ]; then
echo "No Vagrant site matches the supplied Username, please enter a local username to map the site to."
read -p 'Vagrant Username?: ' V_USERNAME
VAGRANT_SITE_DIR="/vagrant/www/$V_USERNAME"
HOST_VAGRANT_SITE_DIR=~/vagrant-local/www/$V_USERNAME
else
V_USERNAME=$USERNAME
fi
if [ ! -e $HOST_VAGRANT_SITE_DIR/htdocs/wp-config.php ]; then
echo "The site at : ${HOST_VAGRANT_SITE_DIR}"
echo "does not appear to be a valid WordPress install"
exit
fi
cd $HOST_VAGRANT_SITE_DIR
# Get useraccount the dev site is being pushed to.
REMOTE_PUBLIC_DIR="/home/${USERNAME}/public_html/"
# Get fresh wp-config so we are working with the right variables
scp root@${HOSTNAME}:${REMOTE_PUBLIC_DIR}/wp-config.php wp-config.php
# Vars from wp-config.php
DB_NAME=$(cat wp-config.php | grep DB_NAME | cut -d \' -f 4)
DB_USER=$(cat wp-config.php | grep DB_USER | cut -d \' -f 4)
DB_PASS=$(cat wp-config.php | grep DB_PASS | cut -d \' -f 4)
DB_PREFIX=$(grep '$table_prefix' "wp-config.php" | cut -d "'" -f 2)
# Query remote database for live domain
PRODUCTION_DOMAIN=$(ssh root@${HOSTNAME} -t "mysql $DB_NAME -u ${DB_USER} --password=\"${DB_PASS}\" --skip-column-names <<<'SELECT \`option_value\` FROM \`${DB_PREFIX}options\` WHERE \`option_name\` = \"siteurl\"'" | tr -d '[[:space:]]' 2>&1)
PRODUCTION_DOMAIN_BASE=$( echo $PRODUCTION_DOMAIN | sed -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||')
# Query local database for domain
DEV_DOMAIN=$(vagrant ssh -c "mysql $DB_NAME -u root --password=\"root\" --skip-column-names <<<'SELECT \`option_value\` FROM \`${DB_PREFIX}options\` WHERE \`option_name\` = \"siteurl\"'" | tr -d '[[:space:]]' 2>&1)
DEV_DOMAIN_BASE=$( echo $PRODUCTION_DOMAIN | sed -e 's/\.[^.]*$/.dev/g' -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||')
# If there is a problem query user
if [[ $DEV_DOMAIN == *"ERROR"* ]]
then
read -p "Dev Domain? (eg. ${DEV_DOMAIN_BASE}): " DEV_DOMAIN
DEV_DOMAIN_BASE=$( echo $DEV_DOMAIN | sed -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||')
fi
read -r -d '' SITE_CONF << SITECONFDOC
cd "${VAGRANT_SITE_DIR}/htdocs/"
ssh root@${HOSTNAME} 'mysqldump -u ${DB_USER} --password="${DB_PASS}" ${DB_NAME} | gzip > ${REMOTE_PUBLIC_DIR}/${DB_NAME}.sql.gz'
rsync -arvz --exclude 'cache' --exclude '.git' --exclude 'sftp-config.json' root@${HOSTNAME}:${REMOTE_PUBLIC_DIR} ./ --delete
mysql -u root -proot <<<"DROP DATABASE IF EXISTS $DB_NAME; CREATE DATABASE $DB_NAME;"
mysql $DB_NAME -u root -proot <<<"GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';"
zcat ${DB_NAME}.sql.gz | mysql -u '${DB_USER}' -p'${DB_PASS}' ${DB_NAME}
wp search-replace '${PRODUCTION_DOMAIN_BASE}' '${DEV_DOMAIN_BASE}' --allow-root
wp cache flush --allow-root
rm -f ${DB_NAME}.sql.gz
SITECONFDOC
vagrant ssh -c "$SITE_CONF"
# cleanup
echo -e '\e[32mCleaning up\e[39m'
ssh root@${HOSTNAME} -t -t <<EOF
cd ${REMOTE_PUBLIC_DIR}
rm -f ${DB_NAME}.sql.gz
exit
EOF
echo -e '\e[32mPull Complete\e[39m'
cd $CUR_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment