Skip to content

Instantly share code, notes, and snippets.

@mattbloomfield
Last active April 2, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbloomfield/92103e409a5d6f6ea8105be641d3ae6b to your computer and use it in GitHub Desktop.
Save mattbloomfield/92103e409a5d6f6ea8105be641d3ae6b to your computer and use it in GitHub Desktop.
Custom `pull` command to sync your local db with a remote env
#!/bin/sh
# This command is used for when assets are stored locally. For AWS syncing, see sync_aws.sh
## Custom `pull` command to sync your local db with a remote env.
## NOTE THAT THIS WILL REPLACE YOUR EXISTING DATABASE
## Usage: fin sync
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
SYNC_FILES="Yes"
fi
if [ "$1" = 'assets' ] || [ "$2" = 'assets' ] || [ "$3" = 'assets' ] || [ "$4" = 'assets' ]; then
SYNC_FILES="Yes"
fi
if [ -z "$REMOTE_ENV" ]
then
echo "${BLUE}Please choose an environment to pull from${RESET}"
select REMOTE_ENV in "development" "staging" "production"; do
case $REMOTE_ENV in
"development")
echo "Development not yet functional. Please try staging or prod"
;;
"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 "$SYNC_FILES" ]
then
echo "${BLUE}Do you want to pull the files? This will replace your local files with ${REMOTE_ENV}${RESET} files"
select SYNC_FILES in "Yes" "No"; do
case $SYNC_FILES in
"Yes")
echo "Noted."
break
;;
"No")
echo "Ok, your loss!"
break
;;
*)
echo "Invalid option $REPLY";;
esac
done
fi
if [ $PULL_DB = 'Yes' ]; then
echo "${YELLOW}DUMPING LOCAL DB TO /tmp/${MYSQL_DATABASE}_local.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 --gzip --project xxxxx -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 [ $SYNC_FILES = 'Yes' ]; then
fin sync_files $REMOTE_ENV
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment