Skip to content

Instantly share code, notes, and snippets.

@jcamblan
Created February 16, 2022 09:21
Show Gist options
  • Save jcamblan/350643bfc7c7d6eda675a7d165531f03 to your computer and use it in GitHub Desktop.
Save jcamblan/350643bfc7c7d6eda675a7d165531f03 to your computer and use it in GitHub Desktop.
Download Scalingo PG dump & pg_restore it locally
#!/usr/bin/env bash
set -e # stop on first error
STACK=$1
DUMP=./tmp/${STACK}.pgsql
LOCAL_DB_NAME=$2
if [[ $# -eq 0 ]] ; then
echo 'Usage: `sh sync.sh SCALING0_STACK_NAME LOCAL_DB_NAME`'
exit 0
fi
scalingo --app $STACK db-tunnel SCALINGO_POSTGRESQL_URL &
tunnel_pid=$!
while ! nc -z localhost 10000; do
sleep 1
done
DB_URL=$(scalingo --app "${STACK}" env | grep -v DATABASE_URL | grep SCALINGO_POSTGRESQL_URL | cut -d '=' -f2-)
SCALINGO_DB_URL=$(echo "${DB_URL}" | gsed -e 's/@[^/]\+/@127.0.0.1:10000/')
pg_dump --clean --if-exists --format c --dbname $SCALINGO_DB_URL --no-owner --no-privileges --no-comments --exclude-schema 'information_schema' --exclude-schema '^pg_*' --file ${DUMP}
kill $tunnel_pid
wait
bundle exec rails db:drop db:create
pg_restore --clean --if-exists --no-owner --no-privileges -j 8 -d ${LOCAL_DB_NAME} ${DUMP}
bundle exec rails db:migrate
echo 🚀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment