Last active
April 21, 2016 13:24
-
-
Save kevinchappell/b882c02398d669741b94 to your computer and use it in GitHub Desktop.
Push a WordPress site from vagrant to remote server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Kevin Chappell http://kevin-chappell.com | |
# Description: Push a local vagrant dev site to production | |
# Version 0.1.2 | |
echo -e '\e[32m | |
_ ______ ____ __ | |
| | / / __ \ / __ \__ _______/ /_ | |
| | /| / / /_/ / / /_/ / / / / ___/ __ \ | |
| |/ |/ / ____/ / ____/ /_/ (__ ) / / / | |
|__/|__/_/ /_/ \__,_/____/_/ /_/\e[39m' | |
echo -e '\nUsage Requirements: VVV, Linux or Mac host machine and root access to 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 | |
read -p 'Local Site Name?: ' V_USERNAME | |
read -p 'Sub-Directory?: ' SUBDIR | |
read -p 'Local Domain: ' LOCAL_DOMAIN | |
read -p 'Remote Domain: ' REMOTE_DOMAIN | |
VAGRANT_SITE_DIR="/vagrant/www/$V_USERNAME" | |
HOST_VAGRANT_SITE_DIR=~/vagrant-local/www/$V_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=$V_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}/htdocs/" | |
# 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) | |
echo -e '\e[32mGetting the siteurl from site_options.\e[39m' | |
if [ ! $LOCAL_DOMAIN ]; then | |
LOCAL_DOMAIN=$(ssh vagrant@127.0.0.1 -p 2222 -o Compression=yes -o DSAAuthentication=yes -o LogLevel=FATAL -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) | |
fi | |
if [ ! $REMOTE_DOMAIN ]; then | |
REMOTE_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) | |
fi | |
LOCAL_DOMAIN_BASE=$( echo $LOCAL_DOMAIN | sed -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||') | |
REMOTE_DOMAIN_BASE=$( echo $REMOTE_DOMAIN | sed -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||') | |
# Set remote directory | |
if [ $SUBDIR ]; then | |
REMOTE_DIR="/home/${USERNAME}/public_html/${SUBDIR}" | |
else | |
REMOTE_DIR="/home/${USERNAME}/public_html" | |
fi | |
echo -e '\e[32mRSYNC local files to production \e[39m' | |
rsync -arvz --exclude 'cache' --exclude '.git' --exclude 'sftp-config.json' --chown=$USERNAME:$USERNAME "${HOST_VAGRANT_SITE_DIR}/htdocs/" root@${HOSTNAME}:${REMOTE_DIR}/ --delete | |
echo -e '\e[32mExport local database to remote \e[39m' | |
vagrant ssh -c "mysqldump -u root -proot ${DB_NAME} | gzip -c | ssh root@${HOSTNAME} 'cat > ${REMOTE_DIR}/${DB_NAME}.sql.gz'" | |
echo -e '\e[32mImport database on remote and ensure correct file permissions\e[39m' | |
ssh root@${HOSTNAME} -t -t <<EOF | |
cd ${REMOTE_DIR} | |
chown $USERNAME:nobody ./ | |
zcat ${DB_NAME}.sql.gz | mysql -u '${DB_USER}' -p'${DB_PASS}' ${DB_NAME} | |
rm -f ${DB_NAME}.sql.gz | |
rm -f ${DB_NAME}.sql | |
find ./ -type d -exec chmod 755 {} \; | |
find ./ -type f -exec chmod 644 {} \; | |
chmod 440 wp-config.php | |
wp search-replace '${LOCAL_DOMAIN_BASE}' '${REMOTE_DOMAIN_BASE}' --allow-root | |
service varnish restart | |
exit | |
EOF | |
echo -e '\e[32mPush Complete\e[39m' | |
cd $CUR_DIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment