Skip to content

Instantly share code, notes, and snippets.

@mattbloomfield
Created April 2, 2021 15:01
Show Gist options
  • Save mattbloomfield/e20c86edd61a745723343e123667f3a5 to your computer and use it in GitHub Desktop.
Save mattbloomfield/e20c86edd61a745723343e123667f3a5 to your computer and use it in GitHub Desktop.
Custom `sync` command to sync your local db with a remote env.
#!/bin/sh
# This command is useful for when you are storing assets in S3. If storing locally, please use the `sync_local.sh` job.
## Custom `sync` command to sync your local db with a remote env.
## NOTE THAT THIS WILL REPLACE YOUR EXISTING DATABASE
## Usage: fin sync <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
REMOTE_ENV=$1
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 "$REMOTE_ENV" ]
then
echo "${BLUE}Please choose an environment to pull from${RESET}"
select REMOTE_ENV in "master" "staging" "production"; do
case $REMOTE_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 pull the database? This will replace your local database with ${REMOTE_ENV}${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 pull assets from ${REMOTE_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
echo "${YELLOW}DUMPING LOCAL DB TO /tmp/${MYSQL_DATABASE}_local_${TRANSACTION_ID}.sql${RESET}"
# Dump existing local database (for safety)
fin db dump /tmp/${MYSQL_DATABASE}_local_${TRANSACTION_ID}.sql
echo "${YELLOW}DROPPING LOCAL DB${RESET}"
# Delete local database if exists
fin db cli "DROP DATABASE ${MYSQL_DATABASE}"
echo "${YELLOW}CREATING LOCAL DB${RESET}"
# Create new local database
fin db create $MYSQL_DATABASE
echo "${YELLOW}DUMPING REMOTE DB FROM ${REMOTE_ENV}${RESET}"
# Dump the remote database and save locally
fin platform db:dump --project $HOSTING_SITE --gzip -e $REMOTE_ENV -f /tmp/${MYSQL_DATABASE}_remote_${REMOTE_ENV}_${TRANSACTION_ID}.sql.gz
echo "${YELLOW}IMPORTING REMOTE DB${RESET}"
# Import locally saved database into container
fin exec "zcat < /tmp/${MYSQL_DATABASE}_remote_${REMOTE_ENV}_${TRANSACTION_ID}.sql.gz | mysql -u root -p$MYSQL_ROOT_PASSWORD -h db $MYSQL_DATABASE"
fi
if [ $PULL_FILES = 'Yes' ]; then
echo "${YELLOW}SYNCING FILES FROM ${REMOTE_ENV} TO LOCAL S3${RESET}"
# Sync files to local s3
REMOTE_BUCKET='s3://my-site-assets/'
if [ $REMOTE_ENV = 'development' ]; then
REMOTE_BUCKET='s3://my-site-assets-development/'
elif [ $REMOTE_ENV = 'master' ]; then
REMOTE_BUCKET='s3://my-site-assets-development/'
elif [ $REMOTE_ENV = 'staging' ]; then
REMOTE_BUCKET='s3://my-site-assets-staging/'
elif [ $REMOTE_ENV = 'production' ]; then
REMOTE_BUCKET='s3://my-site-assets/'
fi
fin exec "aws s3 sync --acl public-read ${REMOTE_BUCKET} s3://my-site-assets-local/${DEV_USER}/"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment