Created
September 11, 2019 13:13
-
-
Save moki9/f291bb8019b9de445127b749bab8cf9c to your computer and use it in GitHub Desktop.
Dump/Restore from Postgres in Docker with gzip
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
# Dump | |
#!/usr/bin/env bash | |
container=`docker ps | awk '/database/ { print $1 }'` | |
echo "Dumping data from ${container}" | |
sudo docker exec -t -u postgres "${container}" pg_dumpall -c -U postgres -l databasename | gzip > dump_`date +%d-%m-%Y"_"%H_%M_%S`.tar.gz | |
echo "Done!" | |
# Restore | |
#!/usr/bin/env bash | |
if [ ! -z "$1" ]; then | |
container=`docker ps | awk '/database/ { print $1 }'` | |
echo "Loading ${1} on ${container}" | |
echo "select pg_terminate_backend(pid) from pg_stat_activity where datname='databasename';" | docker exec -i "${container}" psql -U postgres | |
gzip -d -c "${1}" | cat | docker exec -i "${container}" psql -U postgres | |
echo "Done!" | |
else | |
echo "Usage: ./loaddb <latest.tar.gz>" | |
fi | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment