Last active
December 29, 2020 13:50
-
-
Save krasnuydyx/8ee094f363376913ed4c1816c500ba57 to your computer and use it in GitHub Desktop.
Environment sync (Magento Cloud)
This file contains 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
#!/usr/bin/env bash | |
PROD_ENV="[email protected]" | |
STAGE_ENV="[email protected]" | |
INT_ENV="tx3iahfl3v2uk-integration-5ojmyuq--mymagento@ssh.us-3.magento.cloud" | |
TIMEFORMAT=%R | |
db_data_reg="cat app/etc/env.php | grep -A 10 \"'db'\"" | |
try() { | |
"$@" || { echo "command failed, with exit code $?"; exit 1; } | |
} | |
function execute_ssh { | |
ssh -A -t -q -o "StrictHostKeyChecking=no" -o "ServerAliveCountMax=60" -o "ServerAliveInterval=10" "$1" "$2" | |
} | |
function run { | |
echo "Preparing db variables" | |
s_db_data=$(execute_ssh "${SOURCE_ENV}" "${db_data_reg}") | |
s_db_host=$(echo "$s_db_data" | tr "," "\n" | grep host | cut -d "'" -f 4) | |
s_db_name=$(echo "$s_db_data" | tr "," "\n" | grep dbname | cut -d "'" -f 4) | |
s_db_user=$(echo "$s_db_data" | tr "," "\n" | grep user | cut -d "'" -f 4) | |
s_db_pass=$(echo "$s_db_data" | tr "," "\n" | grep pass | cut -d "'" -f 4) | |
d_db_data=$(execute_ssh "${DST_ENV}" "${db_data_reg}") | |
d_db_host=$(echo "$d_db_data" | tr "," "\n" | grep host | cut -d "'" -f 4) | |
d_db_name=$(echo "$d_db_data" | tr "," "\n" | grep dbname | cut -d "'" -f 4) | |
d_db_user=$(echo "$d_db_data" | tr "," "\n" | grep user | cut -d "'" -f 4) | |
d_db_pass=$(echo "$d_db_data" | tr "," "\n" | grep pass | cut -d "'" -f 4) | |
db_dump_core="mysqldump -h "$d_db_host" --user="$d_db_user" --password="$d_db_pass" --databases "$d_db_name" --tables core_config_data > /tmp/"${d_db_name}_core.sql"" | |
s_do_dump="mysqldump -h "$s_db_host" --user="$s_db_user" --password="$s_db_pass" --single-transaction --triggers --ignore-table="$s_db_name".customer_address_entity --ignore-table="$s_db_name".customer_address_entity_datetime --ignore-table="$s_db_name".customer_address_entity_decimal --ignore-table="$s_db_name".customer_address_entity_int --ignore-table="$s_db_name".customer_address_entity_text --ignore-table="$s_db_name".customer_address_entity_varchar --ignore-table="$s_db_name".customer_entity --ignore-table="$s_db_name".customer_entity_datetime --ignore-table="$s_db_name".customer_entity_decimal --ignore-table="$s_db_name".customer_entity_int --ignore-table="$s_db_name".customer_entity_text --ignore-table="$s_db_name".customer_entity_varchar "$s_db_name" > /tmp/"$s_db_name".sql" | |
s_do_sed="sed -i \"s/DEFINER[ ]*=[ ]*[^*]*\*/\*/g\" /tmp/"$s_db_name".sql" | |
d_do_rest="mysql -h "$d_db_host" --user="$d_db_user" --password="$d_db_pass" -D $d_db_name -e \"set session foreign_key_checks=0; source /tmp/${s_db_name}.sql;\"" | |
d_do_rest_core="mysql -h "$d_db_host" --user="$d_db_user" --password="$d_db_pass" $d_db_name < /tmp/${d_db_name}_core.sql" | |
echo "Starting database dump" | |
try execute_ssh "${SOURCE_ENV}" "rm -rf /tmp/$s_db_name.sql" | |
time try execute_ssh "${SOURCE_ENV}" "${s_do_dump}" | |
try execute_ssh "${SOURCE_ENV}" "${s_do_sed}" | |
#try execute_ssh "${SOURCE_ENV}" "sed -i \"1i\/\*\!40101 set session foreign_key_checks=0 \*\/;\" /tmp/"$s_db_name".sql" | |
#try execute_ssh "${SOURCE_ENV}" "sed -i \"1iset session foreign_key_checks=0;\" /tmp/"$s_db_name".sql" | |
echo ""; echo "Copying database" | |
try execute_ssh "${DST_ENV}" "rm -rf /tmp/$d_db_name.sql" | |
try execute_ssh "${SOURCE_ENV}" "rsync -e \"ssh -q -o \"StrictHostKeyChecking=no\"\" --progress /tmp/"$s_db_name".sql "${DST_ENV}":/tmp/" | |
echo ""; echo "Backup core_config_data, restore db and core on destination env" | |
try execute_ssh "${DST_ENV}" "${db_dump_core}" | |
time try execute_ssh "${DST_ENV}" "$d_do_rest" | |
try execute_ssh "${DST_ENV}" "$d_do_rest_core" | |
echo ""; echo "Syncing media" | |
try execute_ssh "${SOURCE_ENV}" "rsync -e \"ssh -q -o \"StrictHostKeyChecking=no\"\" -ru pub/media/ "$DST_ENV":pub/media/" | |
echo ""; echo "Flushing cache" | |
try execute_ssh "${DST_ENV}" "php bin/magento cache:flush" | |
} | |
case "$1" in | |
integration) | |
SOURCE_ENV="$STAGE_ENV" | |
DST_ENV="$INT_ENV" | |
run | |
;; | |
stage) | |
SOURCE_ENV="$PROD_ENV" | |
DST_ENV="$STAGE_ENV" | |
run | |
;; | |
custom) | |
if [ "$1" -a "$2" ]; then | |
SOURCE_ENV="$1" | |
DST_ENV="$2" | |
run | |
else | |
echo "" | |
echo $"Usage $0 custom source_url destanation_url" | |
echo $"Example usage: $0 custom ${STAGE_ENV} ${INT_ENV}" | |
echo "" | |
exit 1 | |
fi | |
;; | |
*) | |
echo "" | |
echo "This script will sync integration (from stage) or stage (from prod) or you can choose custom option" | |
echo $"Usage: $0 {integration|stage|custom}" | |
echo "" | |
exit 1 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment