Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created January 20, 2025 20:04
Show Gist options
  • Save devinsays/0e5d74f9ed4aa4cc549bdfff5c250491 to your computer and use it in GitHub Desktop.
Save devinsays/0e5d74f9ed4aa4cc549bdfff5c250491 to your computer and use it in GitHub Desktop.
Sync - Partial DB sync
#!/bin/bash -e
# Default false for flags
db=false
uploads=false
overwrite=false
# Checks for flags
while getopts dupo 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
}
# These are the tables that we sync from WP Engine.
tables=(
wp_qftw_wc_product_meta_lookup
wp_qftw_woocommerce_shipping_table_rates
wp_qftw_woocommerce_shipping_zone_locations
wp_qftw_woocommerce_shipping_zone_methods
wp_qftw_woocommerce_shipping_zone_shipping_methods
wp_qftw_woocommerce_shipping_zones
wp_qftw_woocommerce_tax_rate_locations
wp_qftw_woocommerce_tax_rates
wp_qftw_gf_form
wp_qftw_gf_form_meta
wp_qftw_gf_form_revisions
wp_qftw_options
wp_qftw_posts
wp_qftw_postmeta
wp_qftw_terms
wp_qftw_term_relationships
)
function wpe_get_database() {
echo "Getting a partial database export from WP Engine..."
env_message=true
tables_string=$(printf "%s," "${tables[@]}")
ssh -t $sshenv@$sshenv.ssh.wpengine.net bash -c "cd /sites/$sshenv/; wp db export /sites/$sshenv/partial.sql --tables=${tables_string}"
echo "Export complete. Downloading..."
rsync -rvz --progress $sshenv@$sshenv.ssh.wpengine.net:/sites/$sshenv/partial.sql .
}
function overwrite_database_partial() {
echo "Running a partial database sync..."
env_message=true
for table in ${tables[@]}; do
wp db query "TRUNCATE $table"
done
wp db import partial.sql --skip-themes --skip-plugins --skip-packages
}
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]} --skip-tables=wp_qftw_users
wp search-replace ${replace2[0]} ${replace2[1]} --skip-tables=wp_qftw_users
}
function cleanup() {
echo "Running cleanup process..."
echo "Query: Deleting payment_retry posts..."
wp db query "DELETE FROM wp_qftw_posts WHERE post_type = 'payment_retry'"
echo "Query: Deleting payment_retry postmeta..."
meta=(
"'_used_by'"
"'_rule_email_template_customer'"
"'_rule_retry_after_interval'"
"'_rule_email_template_admin'"
"'_rule_status_to_apply_to_order'"
"'_rule_status_to_apply_to_subscription'"
)
# Join the array elements with ', ' for SQL syntax
meta_string=$(printf "%s, " "${meta[@]}")
# Remove the trailing comma and space
meta_string="${meta_string%, }"
wp db query "DELETE FROM wp_qftw_postmeta WHERE meta_key IN (${meta_string})"
tables_list="${tables[*]}"
wp search-replace ${replace[0]} ${replace[1]} $tables_list
wp search-replace ${replace2[0]} ${replace2[1]} $tables_list
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/2025 wp-content/uploads
# If you need to rsync everything, uncomment the line below.
# rsync -rvz --progress $sshenv@$sshenv.ssh.wpengine.net:/sites/$sshenv/wp-content/uploads wp-content
}
function wpe_remove_db_export() {
echo "Deleting partial.sql from server..."
ssh -t $sshenv@$sshenv.ssh.wpengine.net bash -c "cd /sites/$sshenv/ && rm -f /sites/$sshenv/partial.sql"
}
function run_scripts() {
if [[ $db = true ]]; then
wpe_get_database
overwrite_database_partial
cleanup
wpe_remove_db_export
fi
if [[ $uploads = true ]]; then
wpe_get_uploads
fi
if [[ $plugins = true ]]; then
wpe_get_plugins
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