Skip to content

Instantly share code, notes, and snippets.

@tachang
Created December 19, 2017 18:13
Show Gist options
  • Save tachang/7b6fe3e2e585cc754459fc3d8aea773f to your computer and use it in GitHub Desktop.
Save tachang/7b6fe3e2e585cc754459fc3d8aea773f to your computer and use it in GitHub Desktop.
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