Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created October 21, 2022 21:31
Show Gist options
  • Save devinsays/b8622f376c7e4d73fa074478d7a83095 to your computer and use it in GitHub Desktop.
Save devinsays/b8622f376c7e4d73fa074478d7a83095 to your computer and use it in GitHub Desktop.
Bash script for syncing WP Engine production to local
sshenv=yoursite
replace=('yoursite.com' 'yoursite.local')
#!/bin/bash -e
# Syncs production to local
# Run chmod +x sync/sync.sh to make it executable.
# Run ./sync/sync.sh -d To get a fresh copy of database.
# Run ./sync/sync.sh -o To overwrite the database with the copy stored locally in db.sql.
# Run ./sync/sync.sh -u To download media files locally.
# Default false for flags
db=false
uploads=false
overwrite=false
# Checks for flags
while getopts duo option
do
case "${option}"
in
d) db=true;;
u) uploads=true;;
o) overwrite=true;;
esac
done
# Sets variable for message display
env_message=false
# Parses out additional flags
has_param() {
local term="$1"
shift
for arg; do
if [[ $arg == "$term" ]]; then
return 0
fi
done
return 1
}
function wpe_get_database() {
echo "Getting database from WP Engine..."
env_message=true
ssh -t $sshenv@$sshenv.ssh.wpengine.net bash -c "cd /sites/$sshenv/; wp db export /sites/$sshenv/db.sql"
rsync -rvz --progress $sshenv@$sshenv.ssh.wpengine.net:/sites/$sshenv/db.sql .
}
function overwrite_database() {
echo "Overwriting local database, running search and replace..."
env_message=true
wp db import db.sql --skip-themes --skip-plugins --skip-packages
wp search-replace ${replace[0]} ${replace[1]} --all-tables
}
function cleanup() {
echo "Disabling emails...."
wp plugin activate disable-emails
}
function wpe_get_uploads() {
echo "Getting media library uploads..."
env_message=true
rsync -rvz --progress $sshenv@$sshenv.ssh.wpengine.net:/sites/$sshenv/wp-content/uploads wp-content
}
function wpe_remove_db_export() {
echo "Deleting db.sql from server..."
ssh -t $sshenv@$sshenv.ssh.wpengine.net bash -c "cd /sites/$sshenv/ && rm -f /sites/$sshenv/db.sql"
}
function run_scripts() {
if [[ $db = true ]]; then
wpe_get_database
overwrite_database
wpe_remove_db_export
cleanup
fi
if [[ $uploads = true ]]; then
wpe_get_uploads
fi
# Overwrites the database with the version on mysql.sql at root
if [[ $overwrite = true ]]; then
overwrite_database
fi
}
srcdir="$(dirname "$0")"
source "$srcdir/sync-config.sh"
run_scripts
if [[ $env_message = false ]]; then
printf "$(tput setaf 3)$(tput bold)Warning:$(tput setaf 0)$(tput sgr0) Sync failed.\n"
else
printf "$(tput setaf 2)$(tput bold)Success:$(tput setaf 0)$(tput sgr0) ${sshenv} environment synced.\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment