Last active
September 23, 2021 17:41
-
-
Save eriktdesign/2b95501f372440674d58baea873954f1 to your computer and use it in GitHub Desktop.
Download a custom backup from WPEngine
This file contains hidden or 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 | |
# Get params | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-s|--server) | |
SERVER="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-i|--identity_file) | |
IDENTITY="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-u|--exclude_uploads) | |
EXCLUDE_UPLOADS="YES" | |
shift # past argument | |
;; | |
-x|--exclude) | |
EXCLUDE_STRING="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-o|--output_file) | |
OUTPUT_FILE="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-d|--db_only) | |
DB_ONLY=YES | |
shift # past argument | |
;; | |
--db_options) | |
DB_OPTIONS="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-p|--path) | |
SSH_PATH="$2" | |
shift # past argument | |
shift # past value | |
;; | |
"-?"|--help) | |
echo $" | |
usage: $0 [options...] <environment> | |
-s, --server SERVER Host server {wpe|ipdev|ipdev2} | |
Or custom user@host for SSH/SCP. | |
-p, --path PATH The path to WordPress. (optional) | |
Set automatically for WPE and IPDEV servers. | |
-i, --identity_file FILE Identity file to use for SSH connection. | |
-o, --output_file FILE Path to save completed backup. | |
-d, --db_only Export and download only the database. | |
--db_options String of options to pass to wp db export | |
-u, --exclude_uploads Excludes the wp-content/uploads directory. | |
-x, --exclude PATHS Space separated list of paths to exclude. | |
Uses zip command format. | |
environment The name of the target environment. | |
On WP Engine use environment name. | |
For IP Dev/2, use cpanel username. | |
Ignored if using custom user@host." | |
exit 0 | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" #restore positional parameters | |
# Get the environment name from the "unknown" | |
if [[ -n $1 ]]; then | |
ENVIRONMENT=("$1") | |
else | |
echo "No environment specified" | |
exit 1 # can't do anything if this isn't specified | |
fi | |
if [[ $IDENTITY ]]; then | |
IDENTITY_STRING="-i ${IDENTITY} " | |
else | |
IDENTITY_STRING="" | |
fi | |
if [[ -z "$OUTPUT_FILE" ]]; then | |
CURRENT_TIME=$(date "+%Y%m%d-%H%M%S") | |
if [[ -z "$DB_ONLY" ]]; then | |
OUTPUT_FILE="$ENVIRONMENT-backup-$CURRENT_TIME.zip" | |
else | |
OUTPUT_FILE="$ENVIRONMENT-mysql-$CURRENT_TIME.sql" | |
fi | |
fi | |
case $SERVER in | |
wpe) | |
SERVER_NICENAME="WP Engine" | |
SSH_URL="${ENVIRONMENT}@${ENVIRONMENT}.ssh.wpengine.net" | |
if [[ -z "$SSH_PATH" ]]; then SSH_PATH="sites/${ENVIRONMENT}/"; fi | |
;; | |
ipdev|dev) | |
SERVER_NICENAME="IP Dev" | |
SSH_URL="${ENVIRONMENT}@dev.inverseparadox.net" | |
if [[ -z "$SSH_PATH" ]]; then SSH_PATH="public_html/"; fi | |
;; | |
ipdev2|ipdev02|dev2) | |
SERVER_NICENAME="IP Dev 2" | |
SSH_URL="${ENVIRONMENT}@dev02.inverseparadox.net" | |
if [[ -z "$SSH_PATH" ]]; then SSH_PATH="public_html/"; fi | |
;; | |
*) | |
SERVER_NICENAME="${SERVER}" | |
SSH_URL="${SERVER}" | |
if [[ -z "$SSH_PATH" ]]; then SSH_PATH="public_html/"; fi | |
# echo "No server specified. Usage: $0 -s {wpe|ipdev|ipdev2}" | |
# exit 2 | |
;; | |
esac | |
# Excludes | |
if [[ -z "$EXCLUDE_STRING" ]]; then | |
# Default excludes | |
# TODO attempt to load excludes from a dotfile conf | |
EXCLUDES=( "wp-content/ai1wm-backups**" "wp-content/mu-plugins**" "wp-content/cache**" "wp-content/backups-dup-pro**" "wp-content/updraft**" "wp-content/themes/.git**" ) | |
EXCLUDE_STRING="" | |
for t in ${EXCLUDES[@]}; do | |
EXCLUDE_STRING="${EXCLUDE_STRING} \"${t}\"" | |
done | |
fi | |
if [[ ! -z "$EXCLUDE_UPLOADS" ]]; then | |
EXCLUDE_STRING="${EXCLUDE_STRING} \"wp-content/uploads**\"" | |
fi | |
# DEBUG | |
# echo "" | |
# echo "ENVIRONMENT: $ENVIRONMENT" | |
# echo "SERVER: $SERVER" | |
# echo "SERVER_NICENAME: $SERVER_NICENAME" | |
# echo "EXCLUDE_STRING: $EXCLUDE_STRING" | |
# echo "OUTPUT_FILE: $OUTPUT_FILE" | |
# echo "DB_ONLY: $DB_ONLY" | |
# exit | |
# Inform the user of our intentions | |
echo "" | |
echo "Connecting to ${ENVIRONMENT} on ${SERVER_NICENAME}" | |
echo "${SSH_URL}" | |
echo "" | |
if [[ -n "$DB_ONLY" ]]; then | |
# Connect to the server and backup the database only | |
ssh -T ${IDENTITY_STRING}${SSH_URL} << EOF | |
cd ${SSH_PATH} | |
echo "Exporting database to 'wp-content/mysql.sql'..." | |
wp db export wp-content/mysql.sql ${DB_OPTIONS} | |
EOF | |
if [ "$?" != 0 ] ; then | |
echo "Error connecting to ${SSH_URL}" | |
exit 1 | |
fi | |
echo "" | |
echo "Downloading ${SSH_URL}:${SSH_PATH}wp-content/mysql.sql" | |
scp ${IDENTITY_STRING}${SSH_URL}:${SSH_PATH}wp-content/mysql.sql ${OUTPUT_FILE} | |
if [ "$?" = 0 ] ; then | |
echo "Backup saved to '${OUTPUT_FILE}'" | |
else | |
echo "Error downloading from ${SSH_URL}" | |
exit 1 | |
fi | |
exit 0 | |
fi | |
# Connect to the server and backup the files | |
ssh -T ${IDENTITY_STRING}${SSH_URL} << EOF | |
cd ${SSH_PATH} | |
echo "Exporting database to 'wp-content/mysql.sql'..." | |
wp db export wp-content/mysql.sql ${DB_OPTIONS} | |
echo "" | |
echo "Compressing files..." | |
zip -r backup.zip wp-content/ --exclude ${EXCLUDE_STRING} | |
EOF | |
if [ "$?" != 0 ] ; then | |
echo "Error connecting to ${SSH_URL}" | |
exit 1 | |
fi | |
# Download the backup | |
echo "" | |
echo "Downloading ${SSH_URL}:${SSH_PATH}backup.zip" | |
scp ${IDENTITY_STRING}${SSH_URL}:${SSH_PATH}backup.zip ${OUTPUT_FILE} | |
if [ "$?" = 0 ] ; then | |
echo "Backup saved to '${OUTPUT_FILE}'" | |
else | |
echo "Error downloading from ${SSH_URL}" | |
exit 1 | |
fi | |
# Remove the backup zip we created | |
ssh -T ${IDENTITY_STRING}${SSH_URL} << EOF | |
cd ${SSH_PATH} | |
echo "" | |
echo "Removing 'backup.zip' from server..." | |
rm backup.zip | |
EOF | |
if [ "$?" != 0 ] ; then | |
echo "Error connecting to '${SSH_URL}'" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment