-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #786 from LibraryOfCongress/update-db-scripts
one step closer to fully automating data dump / restore
- Loading branch information
Showing
3 changed files
with
74 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,29 @@ | ||
#!/bin/bash | ||
|
||
# For AMI: sudo yum -y install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ami201503-96-9.6-2.noarch.rpm | ||
# sudo yum -y install postgresql96 | ||
set -eu -o pipefail | ||
|
||
pg_dump -Fc --clean --create --no-owner --no-acl -U concordia -h $POSTGRESQL_HOST concordia -f concordia.dmp | ||
ENV_NAME=prod | ||
|
||
# aws cloudformation create-stack --region us-east-1 --stack-name $ENV_NAME-bastionhosts --template-url https://s3.amazonaws.com/crowd-deployment/infrastructure/bastion-hosts.yaml --parameters ParameterKey=EnvironmentName,ParameterValue=$ENV_NAME ParameterKey=KeyPairName,ParameterValue=rstorey@loc.gov --disable-rollback | ||
# aws cloudformation delete-stack --region us-east-1 --stack-name $ENV_NAME-bastionhosts | ||
|
||
if [ $ENV_NAME != "prod" ]; then | ||
echo "This script should only be run in the production environment." | ||
exit 1 | ||
fi | ||
|
||
TODAY=$(date +%Y%m%d) | ||
POSTGRESQL_PW="$(aws secretsmanager get-secret-value --secret-id crowd/${ENV_NAME}/DB/MasterUserPassword | python3 -c 'import json,sys;Secret=json.load(sys.stdin);SecretString=json.loads(Secret["SecretString"]);print(SecretString["password"])')" | ||
# TODO: look up the RDS endpoint for this environment | ||
POSTGRESQL_HOST=${POSTGRESQL_HOST:-localhost} | ||
DUMP_FILE=concordia.dmp | ||
|
||
echo "${POSTGRESQL_HOST}:5432:*:concordia:${POSTGRESQL_PW}" > ~/.pgpass | ||
chmod 600 ~/.pgpass | ||
|
||
pg_dump -Fc --clean --create --no-owner --no-acl -U concordia -h "${POSTGRESQL_HOST}" concordia -f "${DUMP_FILE}" | ||
|
||
if [ -s $DUMP_FILE ]; then | ||
aws s3 cp "${DUMP_FILE}" "s3://crowd-deployment/database-dumps/concordia.${TODAY}.dmp" | ||
aws s3 cp "${DUMP_FILE}" s3://crowd-deployment/database-dumps/concordia.latest.dmp | ||
fi |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
|
||
#!/bin/bash | ||
|
||
# For AMI: sudo yum -y install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ami201503-96-9.6-2.noarch.rpm | ||
# sudo yum -y install postgresql96 | ||
set -eu -o pipefail | ||
|
||
ENV_NAME=dev | ||
|
||
# aws cloudformation create-stack --region us-east-1 --stack-name $ENV_NAME-bastionhosts --template-url https://s3.amazonaws.com/crowd-deployment/infrastructure/bastion-hosts.yaml --parameters ParameterKey=EnvironmentName,ParameterValue=$ENV_NAME ParameterKey=KeyPairName,ParameterValue=rstorey@loc.gov --disable-rollback | ||
# aws cloudformation delete-stack --region us-east-1 --stack-name $ENV_NAME-bastionhosts | ||
if [ $ENV_NAME = "prod" ]; then | ||
echo "This script should not be run in the production environment." | ||
exit 1 | ||
fi | ||
|
||
# Before running the restore, you'll have to stop the ECS task to close any open connections. | ||
# Then, run the following to drop the database. | ||
# psql -U concordia -h $POSTGRESQL_HOST | ||
# \c postgres | ||
# drop database concordia; | ||
POSTGRESQL_PW="$(aws secretsmanager get-secret-value --secret-id crowd/${ENV_NAME}/DB/MasterUserPassword | python3 -c 'import json,sys;Secret=json.load(sys.stdin);SecretString=json.loads(Secret["SecretString"]);print(SecretString["password"])')" | ||
# TODO: look up the RDS endpoint for this environment | ||
POSTGRESQL_HOST=${POSTGRESQL_HOST:-localhost} | ||
DUMP_FILE=/concordia.dmp | ||
|
||
export POSTGRESQL_HOST=${POSTGRESQL_HOST:-localhost} | ||
export DUMP_FILE=concordia.dmp | ||
echo "${POSTGRESQL_HOST}:5432:*:concordia:${POSTGRESQL_PW}" > ~/.pgpass | ||
chmod 600 ~/.pgpass | ||
|
||
pg_restore --create --clean -U concordia -h "${POSTGRESQL_HOST}" -Fc --dbname=postgres --no-owner --no-acl $DUMP_FILE | ||
psql -U concordia -h "$POSTGRESQL_HOST" -d postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datname='concordia';" | ||
psql -U concordia -h "$POSTGRESQL_HOST" -d postgres -c "drop database concordia;" | ||
pg_restore --create --clean -U concordia -h "${POSTGRESQL_HOST}" -Fc --dbname=postgres --no-owner --no-acl "${DUMP_FILE}" |