Skip to content

Instantly share code, notes, and snippets.

@mattbloomfield
Last active April 2, 2021 15:34
Show Gist options
  • Save mattbloomfield/91dad103676742833575fb887617e5a3 to your computer and use it in GitHub Desktop.
Save mattbloomfield/91dad103676742833575fb887617e5a3 to your computer and use it in GitHub Desktop.
Custom `sync` command to sync two remote dbs & files
# This command is for local-asset-storage strategies. If you are using AWS please use the `sync_remote_aws` job.
#!/bin/sh
## Custom `sync` command to sync two remote dbs & files
## Usage: fin sync_remote <source_environment> <destination_environment>
BLUE=\\x1B[34m
YELLOW=\\x1B[33m
GREEN=\\x1B[32m
RED=\\x1B[31m
RESET=\\x1b[0m
# Forces whole job to error on failure
set -e
TRANSACTION_ID=$(date +"%Y-%m-%d_%H-%M-%S")
if [ "$1" = "development" ] || [ "$1" = "master" ] || [ "$1" = "staging" ] || [ "$1" = "production" ]; then
SOURCE_ENV=$1
fi
if [ "$2" = "development" ] || [ "$2" = "master" ] || [ "$2" = "staging" ] || [ "$2" = "production" ]; then
DESTINATION_ENV=$2
fi
if [ "$1" = 'db' ] || [ "$2" = 'db' ] || [ "$3" = 'db' ] || [ "$4" = 'db' ]; then
PULL_DB="Yes"
fi
if [ "$1" = 'files' ] || [ "$2" = 'files' ] || [ "$3" = 'files' ] || [ "$4" = 'files' ]; then
SYNC_FILES="Yes"
fi
if [ "$1" = 'assets' ] || [ "$2" = 'assets' ] || [ "$3" = 'assets' ] || [ "$4" = 'assets' ]; then
SYNC_FILES="Yes"
fi
if [ -z "$SOURCE_ENV" ]
then
echo "${BLUE}Please choose an environment to pull from${RESET}"
select SOURCE_ENV in "master" "staging" "production"; do
case $SOURCE_ENV in
"master")
break
;;
"staging")
break
;;
"production")
break
;;
*)
echo "Invalid option $REPLY";;
esac
done
fi
if [ -z "$DESTINATION_ENV" ]
then
echo "${BLUE}Please choose an environment to push to${RESET}"
select DESTINATION_ENV in "master" "staging" "production"; do
case $DESTINATION_ENV in
"master")
break
;;
"staging")
break
;;
"production")
break
;;
*)
echo "Invalid option $REPLY";;
esac
done
fi
if [ -z "$PULL_DB" ]
then
echo "${BLUE}Do you want to sync the database? This will replace the ${DESTINATION_ENV} database with the ${SOURCE_ENV} database.${RESET}"
select PULL_DB in "Yes" "No"; do
case $PULL_DB in
"Yes")
echo "Noted."
break
;;
"No")
echo "Ok, your loss!"
break
;;
*)
echo "Invalid option $REPLY";;
esac
done
fi
if [ -z "$SYNC_FILES" ]
then
echo "${BLUE}Do you also want to pull assets from ${SOURCE_ENV}?${RESET}"
select SYNC_FILES in "Yes" "No"; do
case $SYNC_FILES in
"Yes")
echo "OK!"
break
;;
"No")
echo "Got it."
break
;;
*)
echo "Invalid option $REPLY";;
esac
done
fi
if [ $PULL_DB = 'Yes' ]; then
# SET VARS
SOURCE_SSH_URL=$(fin platform ssh --pipe --project b4mhz722nsjh4 -e $SOURCE_ENV)
if [ $SOURCE_ENV = 'master' ]; then
SOURCE_USER=user
SOURCE_PW=
SOURCE_HOST=127.0.0.1
SOURCE_DB_NAME=main
elif [ $SOURCE_ENV = 'staging' ]; then
SOURCE_USER=xxxx
SOURCE_PW=$DB_PW_STAGING
SOURCE_HOST=127.0.0.1
SOURCE_DB_NAME=xxxxx
elif [ $SOURCE_ENV = 'production' ]; then
SOURCE_USER=xxxxx
SOURCE_PW=$DB_PW_PROD
SOURCE_HOST=127.0.0.1
SOURCE_DB_NAME=xxxxxx
fi
DESTINATION_SSH_URL=$(fin platform ssh --pipe --project b4mhz722nsjh4 -e $DESTINATION_ENV)
if [ $DESTINATION_ENV = 'master' ]; then
DESTINATION_SSH_USER=xxxxx-master-7rqtwti--app
DESTINATION_SSH_HOST=ssh.us-2.platform.sh
DESTINATION_USER=user
DESTINATION_PW=
DESTINATION_HOST=database.internal
DESTINATION_DB_NAME=main
elif [ $DESTINATION_ENV = 'staging' ]; then
DESTINATION_SSH_USER=1.ent-xxxx-staging-xxxxx
DESTINATION_SSH_HOST=ssh.us-2.platform.sh
DESTINATION_USER=xxxxxx
DESTINATION_PW=$DB_PW_STAGING
DESTINATION_HOST=127.0.0.1
DESTINATION_DB_NAME=xxxxxx
elif [ $DESTINATION_ENV = 'production' ]; then
DESTINATION_SSH_USER=2.ent-xxxxx-production-xxxx
DESTINATION_SSH_HOST=ssh.us-2.platform.sh
DESTINATION_USER=xxxxxxx
DESTINATION_PW=$DB_PW_PROD
DESTINATION_HOST=127.0.0.1
DESTINATION_DB_NAME=xxxxx
fi
# Sync database via SSH
echo "${YELLOW}BACKING UP BOTH DATABASES${RESET}"
## Backup the destination just in case...
FILEPATH=/tmp/remote_${DESTINATION_ENV}_env_${TRANSACTION_ID}.sql
fin platform db:dump -e ${DESTINATION_ENV} --project $HOSTING_SITE -f $FILEPATH
## Backup the remote source database for import
FILENAME=remote_${SOURCE_ENV}_env_${TRANSACTION_ID}.sql
FILEPATH=/tmp/${FILENAME}
fin platform db:dump -e ${SOURCE_ENV} --project $HOSTING_SITE -f $FILEPATH
echo "${YELLOW}IMPORTING TO ${DESTINATION_ENV}... ${RESET}"
## Import
echo "ssh ${DESTINATION_SSH_USER}@${DESTINATION_SSH_HOST} mysql -u $DESTINATION_USER --password="$DESTINATION_PW" -h $DESTINATION_HOST $DESTINATION_DB_NAME < $FILEPATH"
fin exec "ssh ${DESTINATION_SSH_USER}@${DESTINATION_SSH_HOST} mysql -u $DESTINATION_USER --password="$DESTINATION_PW" -h $DESTINATION_HOST $DESTINATION_DB_NAME < $FILEPATH"
echo "${YELLOW}IMPORT COMPLETE! ${RESET}"
fi
if [ $SYNC_FILES = 'Yes' ]; then
echo "${YELLOW}PULLING FILES FROM ${SOURCE_ENV}... STORING LOCALLY${RESET}"
TEMP_PATH=/tmp/co_files_${TRANSACTION_ID}
echo "${YELLOW}LOCAL PATH: ${TEMP_PATH} ${RESET}"
mkdir $TEMP_PATH
mkdir $TEMP_PATH/uploads/
mkdir $TEMP_PATH/cpresources/
rsync -av ${SOURCE_SSH_URL}:web/uploads/* $TEMP_PATH/uploads/
rsync -av ${SOURCE_SSH_URL}:web/cpresources/* $TEMP_PATH/cpresources/
echo "${YELLOW}PUSHING FILES TO ${DESTINATION_ENV} FROM LOCAL STORE${RESET}"
rsync -av $TEMP_PATH/uploads/* ${DESTINATION_SSH_URL}:web/uploads/
rsync -av $TEMP_PATH/cpresources/* ${DESTINATION_SSH_URL}:web/cpresources/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment