Created
May 10, 2017 18:44
-
-
Save csaden/d6ba009e8b1e6c43863c15580a7e6e7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# Uses `pg_dump` to export data from a remote database to the local database and | |
# `psql` to load it into a local one. | |
# | |
# Expects environment variables: | |
# DB_HOST | |
# DB_NAME | |
# DB_PORT | |
# DB_USER | |
# REMOTE_DB_HOST | |
# REMOTE_DB_NAME | |
# REMOTE_DB_PORT | |
# REMOTE_DB_USER | |
# REMOTE_DB_PASS | |
# | |
# Recommended usage is to use an env runner, like Foreman, e.g. | |
# $ foreman run -e .env bin/sync | |
set -e | |
: "${DB_HOST?Need to set DB_HOST}" | |
: "${DB_NAME?Need to set DB_NAME}" | |
: "${DB_PORT?Need to set DB_PORT}" | |
: "${DB_USER?Need to set DB_USER}" | |
: "${REMOTE_DB_HOST?Need to set REMOTE_DB_HOST}" | |
: "${REMOTE_DB_NAME?Need to set REMOTE_DB_NAME}" | |
: "${REMOTE_DB_PASS?Need to set REMOTE_DB_USER}" | |
: "${REMOTE_DB_PORT?Need to set REMOTE_DB_PORT}" | |
: "${REMOTE_DB_USER?Need to set REMOTE_DB_USER}" | |
echo "== Downloading database dump ==" | |
PGCLUSTER=9.5/main PGPASSWORD=$REMOTE_DB_PASS pg_dump -h $REMOTE_DB_HOST -U $REMOTE_DB_USER -F c -b -v -f "tmp/db.dump" $REMOTE_DB_NAME | |
echo "== Importing database dump ==" | |
pg_restore -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -v "tmp/db.dump" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment