Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts that loads a database from S3
#!/bin/bash
# This script looks for a database container and reloads the database
# inside the container
DB_CONTAINER_ID=`docker ps --filter="ancestor=postgres:9.6" -q`
read -p "Enter Amazon Access Key: " AMAZON_ACCESS_KEY
read -p "Enter Amazon Secret Key: " AMAZON_SECRET_KEY
s3cmd --access_key=$AMAZON_ACCESS_KEY --secret_key=$AMAZON_SECRET_KEY --force get s3://mybackup/db_backup/example.sql.gz .
gunzip example.sql.gz
if [ -z "$DB_CONTAINER_ID" ]
then
echo "Unable to find database container ID. Is the Docker container running?"
exit 1
else
echo "Using DB container ID: $DB_CONTAINER_ID"
fi
docker exec -it $DB_CONTAINER_ID psql -U postgres example -c "SELECT pg_terminate_backend(pg_stat_activity.pid)\
FROM pg_stat_activity WHERE datname = current_database()\
AND pid <> pg_backend_pid();"
docker exec -i $DB_CONTAINER_ID psql -U postgres -c "drop database example;"
docker exec -i $DB_CONTAINER_ID psql -U postgres -c "create database example;"
docker exec -i $DB_CONTAINER_ID psql -U postgres example < example.sql
rm -f example.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.