Last active
April 2, 2021 15:32
-
-
Save mattbloomfield/4a1ab88a13bdf98860753caf1a0500b5 to your computer and use it in GitHub Desktop.
Custom `sync` command to sync two remote dbs and files
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
# This command is for AWS S3 asset-storage strategies. If you are using local files please use the `sync_remote_local` job. | |
#!/bin/sh | |
## Custom `sync` command to sync two remote dbs and files | |
## Usage: fin sync_remote <source_environment> <destination_environment> <"db"> <"files"|"assets"> | |
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 | |
PULL_FILES="Yes" | |
fi | |
if [ "$1" = 'assets' ] || [ "$2" = 'assets' ] || [ "$3" = 'assets' ] || [ "$4" = 'assets' ]; then | |
PULL_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 "$PULL_FILES" ] | |
then | |
echo "${BLUE}Do you also want to sync assets from ${SOURCE_ENV} to ${DESTINATION_ENV}?${RESET}" | |
select PULL_FILES in "Yes" "No"; do | |
case $PULL_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 | |
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=xxxxxx | |
SOURCE_PW=$DB_PW_STAGING | |
SOURCE_HOST=127.0.0.1 | |
SOURCE_DB_NAME=xxxxx | |
elif [ $SOURCE_ENV = 'production' ]; then | |
SOURCE_USER=xxxx | |
SOURCE_PW=$DB_PW_PROD | |
SOURCE_HOST=127.0.0.1 | |
SOURCE_DB_NAME=xxxx | |
fi | |
if [ $DESTINATION_ENV = 'master' ]; then | |
DESTINATION_SSH_USER=xxxxx-master-xxxx--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-xxxxx-staging-xxxx | |
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=xxxxx | |
elif [ $DESTINATION_ENV = 'production' ]; then | |
DESTINATION_SSH_USER=2.ent-xxxxx-production-xxxx | |
DESTINATION_SSH_HOST=ssh.us-2.platform.sh | |
DESTINATION_USER=xxxxx | |
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 [ $PULL_FILES = 'Yes' ]; then | |
echo "${YELLOW}SYNCING FILES FROM ${SOURCE_ENV} TO ${DESTINATION_ENV} S3${RESET}" | |
# Sync files to local s3 | |
SOURCE_BUCKET='s3://my-site-assets/' | |
if [ $SOURCE_ENV = 'development' ]; then | |
SOURCE_BUCKET='s3://my-site-assets-development/' | |
elif [ $SOURCE_ENV = 'master' ]; then | |
SOURCE_BUCKET='s3://my-site-assets-development/' | |
elif [ $SOURCE_ENV = 'staging' ]; then | |
SOURCE_BUCKET='s3://my-site-assets-staging/' | |
elif [ $SOURCE_ENV = 'production' ]; then | |
SOURCE_BUCKET='s3://my-site-assets/' | |
fi | |
DESTINATION_BUCKET='s3://my-site-assets/' | |
if [ $DESTINATION_ENV = 'development' ]; then | |
DESTINATION_BUCKET='s3://my-site-assets-development/' | |
elif [ $DESTINATION_ENV = 'master' ]; then | |
DESTINATION_BUCKET='s3://my-site-assets-development/' | |
elif [ $DESTINATION_ENV = 'staging' ]; then | |
DESTINATION_BUCKET='s3://my-site-assets-staging/' | |
elif [ $DESTINATION_ENV = 'production' ]; then | |
DESTINATION_BUCKET='s3://my-site-assets/' | |
fi | |
fin exec "aws s3 sync --acl public-read ${SOURCE_BUCKET} ${DESTINATION_BUCKET}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment