Created
April 13, 2021 08:18
-
-
Save Galaxy83/7ea15d3fe91e53d5500acb36bc6677ca to your computer and use it in GitHub Desktop.
zsh / bash script copy production Postgres database to local and disk OR copy only specific table like oauth OR reset local from last pull
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
replace_with_remote_url:replace_with_remote_port:replace_with_remote_tablename:replace_with_remote_username:replace_with_remote_password |
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
# Add this to the end of .zshrc file or other sourced file. | |
# Add the content of .pgpass to ~/.pgpass | |
# Must create ~/remote_dumps folder before first run. | |
cpd(){ | |
if [[ "$1" == "oauth" ]]; then | |
pg_dump -c -Fp -h replace_with_db_url -w -U replace_with_remote_username -t oauths replace_with_remote_tablename | psql -h localhost -U replace_with_local_username replace_with_local_tablename | |
else | |
if [[ "$1" == "res" ]]; then | |
echo "restoring from ~/remote_dumps/$(ls -t1 ~/remote_dumps | head -n 1)" | |
else | |
file=~/remote_dumps/remote_dump-$(date +%Y-%m-%d-%H-%M).tar | |
touch "$file" | |
chmod 777 $file | |
pg_dump -c -v -Fp -h replace_with_remote_url -w -U replace_with_remote_username -Ft replace_with_remote_tablename > $file | |
echo "dumped to: $file" | |
fi | |
pg_restore -c -v -h localhost -U replace_with_local_username -d replace_with_local_tablename ~/remote_dumps/$(ls -t1 ~/db | head -n 1) | |
fi | |
} | |
# usage: | |
# cpd -> will dump remote db to local file and restore the local db from the dump file. | |
# cpd res -> will restore local db from the last dump file. | |
# cpd oauth -> will restore a specific table from remote directly to local. In this case the oauth table. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment