Skip to content

Instantly share code, notes, and snippets.

@DrizzlyOwl
Created September 1, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrizzlyOwl/434ccc9c8300a2f2c9e1192a1d5e8e96 to your computer and use it in GitHub Desktop.
Save DrizzlyOwl/434ccc9c8300a2f2c9e1192a1d5e8e96 to your computer and use it in GitHub Desktop.
Onyx Managed WordPress deployment tool
#! /bin/bash
#######################################################################
## USAGE ##
#######################################################################
## To use these helper functions you first need to make the target ##
## environment available by defining the SSH Port number and the ##
## branch you wish to deploy from. ##
## Substitute 12345 with your port. ##
## ##
## $ export SSH_PORT=12345 ##
## $ export BRANCH=your-branch-here ##
## ##
## (optional) define your rollback commit number ##
## $ export COMMIT=your-commit-hash ##
## ##
## IMPORTANT: Please note that all of these functions MUST be used ##
## in the project root folder NOT the theme folder ##
## ##
## Now to get access to the functions you need to load them into ##
## your terminal. ##
## ##
## $ source ./helper.sh ##
## ##
## Once this has been done you can call the functions straight from ##
## your terminal. ##
## ##
## e.g. ##
## $ pull_db ##
## $ pull_uploads ##
## $ push_db ##
## $ push_uploads ##
## $ deploy ##
## $ rollback ##
## ##
#######################################################################
alias pull_db = function () {
(
echo "Exporting the database on the live environment and zipping it"
ssh webuser@sites-01.sites.onyx.io -p $SSH_PORT -t '
mkdir -p ~/db_backups/ &&
(cd /var/www/html/ && wp db export ~/db_backups/YOURSITEHERE.co.uk.sql) &&
cd ~/db_backups/ && tar -czvf YOURSITEHERE.co.uk.sql.tar.gz YOURSITEHERE.co.uk.sql &&
rm ~/db_backups/YOURSITEHERE.co.uk.sql'
echo
) &&
(
echo "Downloading the zipped export from the server and unzipping it"
scp -P $SSH_PORT webuser@sites-01.sites.onyx.io:~/db_backups/YOURSITEHERE.co.uk.sql.tar.gz . &&
tar -xzvf YOURSITEHERE.co.uk.sql.tar.gz
echo
) &&
(
echo "Import the unzipped database into the local site"
wp db import YOURSITEHERE.co.uk.sql && rm YOURSITEHERE.co.uk.sql YOURSITEHERE.co.uk.sql.tar.gz
echo
) &&
echo "Done"
}
alias pull_uploads = function () {
echo "Pulling remote wp-content/uploads/ into local directory"
rsync -avuz -e "ssh -p $SSH_PORT" webuser@sites-01.sites.onyx.io:/var/www/html/wp-content/uploads/ ./wp-content/uploads/
echo
echo "Done"
}
alias push_db = function () {
(
echo "Creating a local database export..."
mkdir -p ~/db_backups && wp db export $_/YOURSITEHERE.co.uk.sql
echo
) &&
(
echo "Zipping up the local export..."
cd ~/db_backups/ && tar -czvf YOURSITEHERE.co.uk.sql.tar.gz YOURSITEHERE.co.uk.sql
echo
) &&
(
echo "Uploading the export to the server..."
cd ~/db_backups/ && scp -P $SSH_PORT ./YOURSITEHERE.co.uk.sql.tar.gz webuser@sites-01.sites.onyx.io:~/
echo
) &&
(
echo "Unzipping the export and importing it into the site..."
ssh webuser@sites-01.sites.onyx.io -p $SSH_PORT -t '
(
tar -xzvf YOURSITEHERE.co.uk.sql.tar.gz &&
cd /var/www/html/ &&
wp db import ~/YOURSITEHERE.co.uk.sql
) &&
(
rm ~/YOURSITEHERE.co.uk.sql.tar.gz ~/YOURSITEHERE.co.uk.sql
)' &&
rm ~/db_backups/YOURSITEHERE.co.uk.sql.tar.gz ~/db_backups/YOURSITEHERE.co.uk.sql
rmdir ~/db_backups/
echo
) &&
echo "Done"
}
alias push_uploads = function () {
echo "Pushing local wp-content/uploads/ folder onto remote host..."
rsync -avuz -e "ssh -p $SSH_PORT" ./wp-content/uploads/ webuser@sites-01.sites.onyx.io:/var/www/html/wp-content/uploads/
echo
echo "Done"
}
alias deploy = function () {
echo "Checking out $BRANCH on the remote..."
ssh -A webuser@sites-01.sites.onyx.io -p $SSH_PORT -t '
export BRANCH='"'$BRANCH'"';
(
cd /var/www/html/wp-content/themes/YOURTHEMEHERE &&
git fetch --prune origin -q;
git checkout $BRANCH -q;
git pull origin $BRANCH;
git checkout --detach
)'
echo
echo "Done"
}
alias rollback = function () {
echo "Rolling back to commit $COMMIT"
ssh -A webuser@sites-01.sites.onyx.io -p $SSH_PORT -t '
export COMMIT='"'$COMMIT'"';
(
cd /var/www/html/wp-content/themes/YOURTHEMEHERE && git reset $COMMIT --hard
)'
echo
echo "Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment