Skip to content

Instantly share code, notes, and snippets.

@bpb54321
Created December 8, 2017 20:44
Show Gist options
  • Save bpb54321/e5f30538f2b4643fbb428d8da274cb27 to your computer and use it in GitHub Desktop.
Save bpb54321/e5f30538f2b4643fbb428d8da274cb27 to your computer and use it in GitHub Desktop.
WordPress Deploy Bash Script
#!/usr/bin/env bash
# Variables
DB_FILENAME=
LOCAL_DIR=
LOCAL_UPLOADS_DIR=
SERVER_DIR=
SERVER_UPLOADS_DIR=
SERVER_THEME_DIR=
LOCAL_DB_PASS=
LOCAL_DB_USER=
LOCAL_DB=
LOCAL_URL=
SERVER_DB_PASS=
SERVER_DB_USER=
SERVER_DB=
SERVER_URL=
SERVER_USERNAME=
SERVER=
while getopts a: option; do
case $option in
a) action=$OPTARG;;
esac
done
if [ $action = 'push' ]
then
echo Dumping the localhost database
mysqldump -u "${LOCAL_DB_USER}" -p"${LOCAL_DB_PASS}" --socket '/Applications/MAMP/tmp/mysql/mysql.sock' "${LOCAL_DB}" > "${LOCAL_DIR}${DB_FILENAME}"
# Find and replace the local hostname with the remote hostname
# sed: stream editor
# --in-place: perform find and replace in-place (on the same file)
# .original: sed will make a backup of the original file with that extension appended. The extension can be anything.
# If you type '' (empty string) as the extension, sed does not make a backup of the modified file.
# s: substitute command (substitute old string with new string)
# g: Global command (substitute everywhere in the file you see the regular expression
# | delimiters of the regular expressions
echo Performing find and replace on the dumped database
sed -i.original "s|${LOCAL_URL}|${SERVER_URL}|g" "${LOCAL_DIR}${DB_FILENAME}"
echo Using rsync to transfer the db export file to the remote server
rsync -rv -e 'ssh' "${LOCAL_DIR}${DB_FILENAME}" "${SERVER_USERNAME}@${SERVER}:${SERVER_DIR}"
# Transfer all the files in uploads to the remote server
# A trailing / on a source name means "copy the contents of this directory". Without a trailing slash it means "copy the directory".
rsync -rv -e 'ssh' "${LOCAL_UPLOADS_DIR}" "${SERVER_USERNAME}@${SERVER}:${SERVER_UPLOADS_DIR}"
# Commands to perform on the server via SSH
COMMANDS="
# Import .sql file into database
mysql -u ${SERVER_DB_USER} -p'${SERVER_DB_PASS}' ${SERVER_DB} < ${SERVER_DIR}${DB_FILENAME};
# Navigate to the theme directory
cd ${SERVER_THEME_DIR};
# Pull code from Git repository
git pull origin master;
exit"
# SSH onto the Domains server (using an SSH key for authentication)
ssh "${SERVER_USERNAME}@${SERVER}" "${COMMANDS}"
else
# Commands to perform on the server via SSH
COMMANDS="
# Dump the server database
mysqldump -u ${SERVER_DB_USER} -p'${SERVER_DB_PASS}' ${SERVER_DB} > ${SERVER_DIR}${DB_FILENAME};
# Perform find and replace on the database dump
sed -i.original 's|${SERVER_URL}|${LOCAL_URL}|g' ${SERVER_DIR}${DB_FILENAME}
exit"
# SSH onto the Domains server and perform the commands (using an SSH key for authentication)
ssh "${SERVER_USERNAME}@${SERVER}" "${COMMANDS}"
# Use rsync to transfer all uploaded files from the server to the local uploads directory
# A trailing / on a source name means "copy the contents of this directory". Without a trailing slash it means "copy the directory".
rsync -rv -e 'ssh' "${SERVER_USERNAME}@${SERVER}:${SERVER_UPLOADS_DIR}" "${LOCAL_UPLOADS_DIR}"
# Use rsync to transfer the file from the Domains server to localhost
rsync -rv -e 'ssh' "${SERVER_USERNAME}@${SERVER}:${SERVER_DIR}${DB_FILENAME}" "${LOCAL_DIR}"
# Import .sql file into database
mysql -u "${LOCAL_DB_USER}" -p"${LOCAL_DB_PASS}" --socket '/Applications/MAMP/tmp/mysql/mysql.sock' "${LOCAL_DB}" < "./${DB_FILENAME}";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment