-
-
Save rodde177/5fc08a956010db01325a89009cedfac2 to your computer and use it in GitHub Desktop.
Magento 2 script to push DB and `pub/media` changes from prod>stage
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 | |
# stop on errors | |
set -e | |
# turn on debugging if you're running into issues | |
#set -x | |
######################### | |
# Overview | |
######################### | |
# Magento 2 script to push DB and pub/media changes from prod>stage | |
# This script lives on the prod SSH user and pushes the DB and `pub/media` files to stage. | |
# Before using this script you should: | |
# [] Update the "Variables" section below | |
# [] Ensure https://github.com/netz98/n98-magerun2 is installed on the server and is aliased to `mr` | |
# [] Either remove or update the "Anonymize emails" section | |
# [] Add the script on the prod SSH user and test it thoroughly | |
######################### | |
# Variables | |
######################### | |
SOURCE_DB_NAME=example_prod | |
SOURCE_DB_HOST="localhost" | |
SOURCE_MAGENTO_ROOT="/var/www/prod/current/" | |
TIMESTAMP=$(date +"%F_%H-%M-%S") | |
DESTINATION_DB_NAME=example_stage | |
DESTINATION_DB_HOST="localhost" | |
DESTINATION_SSH_USER=www-stage | |
DESTINATION_SSH_HOST='stage.example.com' | |
DESTINATION_MAGENTO_ROOT="/var/www/stage/current/" | |
######################### | |
# Sync commands | |
######################### | |
if [ -z "$STY" ]; then | |
echo "You need to run this script within a screen so that in case your current session disconnects, the sync process doesn't break"; | |
exit; | |
fi | |
echo -e "\033[0;31m################# IMPORTANT ################# \n\ | |
Are you sure you want to update the stage DB and media directory with content from prod? \n\ | |
#############################################\033[0m" | |
read -p "Type 'y' or 'n' " -n 1 -r | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell | |
fi | |
echo "## Backing up stage database" | |
# Create dump of DB in the "current" directory. This will naturally get cleaned up by Capistrano once this release is purged | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " mr --root-dir=${DESTINATION_MAGENTO_ROOT} db:dump --strip=@stripped stage_prior_to_clone_from_prod_${TIMESTAMP} " | |
echo "## Dumping production database" | |
mr --root-dir=$SOURCE_MAGENTO_ROOT db:dump --stdout --strip=@stripped | pv | gzip > ~/tmp/${SOURCE_DB_NAME}_$TIMESTAMP.sql.gz | |
echo "## Importing data" | |
scp ~/tmp/${SOURCE_DB_NAME}_$TIMESTAMP.sql.gz $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST:~/tmp/${SOURCE_DB_NAME}_$TIMESTAMP.sql.gz | |
rm ~/tmp/${SOURCE_DB_NAME}_$TIMESTAMP.sql.gz | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " | |
${DESTINATION_MAGENTO_ROOT}/bin/magento maintenance:enable | |
${DESTINATION_MAGENTO_ROOT}/bin/magento cache:flush | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} db:import --compression=gzip ~/tmp/${SOURCE_DB_NAME}_${TIMESTAMP}.sql.gz | |
rm ~/tmp/${SOURCE_DB_NAME}_${TIMESTAMP}.sql.gz | |
" | |
echo "## Cleaning DB" | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE core_config_data SET \`value\` = REPLACE(\`value\`, \"https://www.\", \"https://stage.\") WHERE path LIKE \"web/%_url\"' | |
" | |
echo "## Import config settings" | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST "mr --root-dir=${DESTINATION_MAGENTO_ROOT} app:config:import" | |
echo "## Enabling stage configuration settings" | |
# Alternatively you can configure these settings in the `app/etc/env.php` file on stage | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} config:set design/head/demonotice 1 | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} config:set google/analytics/active 0 | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} config:set carriers/freeshipping/active 1 | |
mr --root-dir=${DESTINATION_MAGENTO_ROOT} config:set payment/checkmo/active 1 | |
" | |
######################### | |
# Anonymize emails - Either remove or generate content from https://gist.github.com/erikhansen/aa30947f2df263a95a054d51c5277692 | |
######################### | |
echo "## Anonymizing emails" | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`admin_user\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`customer_entity\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`customer_grid_flat\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`email_template\` SET \`template_sender_email\` = REPLACE(\`template_sender_email\`, SUBSTRING(\`template_sender_email\`, LOCATE(\"@\", \`template_sender_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`template_sender_email\`, LOCATE(\"@\", \`template_sender_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`template_sender_email\` NOT LIKE \"%@example.com\" AND \`template_sender_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`integration\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_giftregistry_person\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_invitation\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_rma\` SET \`customer_custom_email\` = REPLACE(\`customer_custom_email\`, SUBSTRING(\`customer_custom_email\`, LOCATE(\"@\", \`customer_custom_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_custom_email\`, LOCATE(\"@\", \`customer_custom_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_custom_email\` NOT LIKE \"%@example.com\" AND \`customer_custom_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_sales_creditmemo_grid_archive\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_sales_invoice_grid_archive\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_sales_order_grid_archive\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_sales_shipment_grid_archive\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_scheduled_operations\` SET \`email_receiver\` = REPLACE(\`email_receiver\`, SUBSTRING(\`email_receiver\`, LOCATE(\"@\", \`email_receiver\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email_receiver\`, LOCATE(\"@\", \`email_receiver\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email_receiver\` NOT LIKE \"%@example.com\" AND \`email_receiver\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_scheduled_operations\` SET \`email_sender\` = REPLACE(\`email_sender\`, SUBSTRING(\`email_sender\`, LOCATE(\"@\", \`email_sender\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email_sender\`, LOCATE(\"@\", \`email_sender\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email_sender\` NOT LIKE \"%@example.com\" AND \`email_sender\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_scheduled_operations\` SET \`email_template\` = REPLACE(\`email_template\`, SUBSTRING(\`email_template\`, LOCATE(\"@\", \`email_template\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email_template\`, LOCATE(\"@\", \`email_template\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email_template\` NOT LIKE \"%@example.com\" AND \`email_template\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_scheduled_operations\` SET \`email_copy\` = REPLACE(\`email_copy\`, SUBSTRING(\`email_copy\`, LOCATE(\"@\", \`email_copy\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email_copy\`, LOCATE(\"@\", \`email_copy\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email_copy\` NOT LIKE \"%@example.com\" AND \`email_copy\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`magento_scheduled_operations\` SET \`email_copy_method\` = REPLACE(\`email_copy_method\`, SUBSTRING(\`email_copy_method\`, LOCATE(\"@\", \`email_copy_method\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email_copy_method\`, LOCATE(\"@\", \`email_copy_method\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email_copy_method\` NOT LIKE \"%@example.com\" AND \`email_copy_method\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`newsletter_queue\` SET \`newsletter_sender_email\` = REPLACE(\`newsletter_sender_email\`, SUBSTRING(\`newsletter_sender_email\`, LOCATE(\"@\", \`newsletter_sender_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`newsletter_sender_email\`, LOCATE(\"@\", \`newsletter_sender_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`newsletter_sender_email\` NOT LIKE \"%@example.com\" AND \`newsletter_sender_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`newsletter_subscriber\` SET \`subscriber_email\` = REPLACE(\`subscriber_email\`, SUBSTRING(\`subscriber_email\`, LOCATE(\"@\", \`subscriber_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`subscriber_email\`, LOCATE(\"@\", \`subscriber_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`subscriber_email\` NOT LIKE \"%@example.com\" AND \`subscriber_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`newsletter_template\` SET \`template_sender_email\` = REPLACE(\`template_sender_email\`, SUBSTRING(\`template_sender_email\`, LOCATE(\"@\", \`template_sender_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`template_sender_email\`, LOCATE(\"@\", \`template_sender_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`template_sender_email\` NOT LIKE \"%@example.com\" AND \`template_sender_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`quote\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`quote_address\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_creditmemo_grid\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_invoice_grid\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_order\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_order_address\` SET \`email\` = REPLACE(\`email\`, SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`email\`, LOCATE(\"@\", \`email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`email\` NOT LIKE \"%@example.com\" AND \`email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_order_grid\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
mr -q --root-dir=${DESTINATION_MAGENTO_ROOT} db:query 'UPDATE \`sales_shipment_grid\` SET \`customer_email\` = REPLACE(\`customer_email\`, SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`)), CONCAT(\"+\", SUBSTRING(MD5(SUBSTRING(\`customer_email\`, LOCATE(\"@\", \`customer_email\`))) FROM 1 FOR 6), \"@example2.com\")) WHERE \`customer_email\` NOT LIKE \"%@example.com\" AND \`customer_email\` NOT LIKE \"%@example2.com\"' | |
" | |
echo "## Running upgrade scripts and re-indexing" | |
ssh -C $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST " | |
${DESTINATION_MAGENTO_ROOT}bin/magento -q setup:upgrade --keep-generated | |
${DESTINATION_MAGENTO_ROOT}bin/magento -q indexer:reindex | |
${DESTINATION_MAGENTO_ROOT}/bin/magento maintenance:disable | |
${DESTINATION_MAGENTO_ROOT}bin/magento -q cache:flush | |
" | |
echo "## Syncing Media" | |
rsync -avz --exclude=import --exclude=catalog/product/cache $SOURCE_MAGENTO_ROOT"pub/media/" $DESTINATION_SSH_USER@$DESTINATION_SSH_HOST:$DESTINATION_MAGENTO_ROOT"pub/media/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment