-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_db.sh
executable file
·49 lines (39 loc) · 1.23 KB
/
load_db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# This script loads a dumpfile into the production database.
# Can only be run within the host machine (on AWS).
# Abort if any command within fails
set -e
# Check for .env.prod file and exit with error if not found
if [ ! -f .env.prod ]; then
echo "ERROR: no .env.prod file found. Exiting."
exit 1
fi
# Check for argument provided, set it to $FILENAME (or use default)
if [ -z "$1" ]; then
filename="dump.sql"
else
filename="$1"
fi
# Load environment variables from .env.prod
set -a
source .env.prod
set +a
# Check for running backend container (to obtain db credentials)
container_id=$(docker ps | grep "vaultmaster-backend" | awk '{print $1}')
# If not found, exit
if [ -z "$container_id" ]; then
echo "ERROR: backend container not running."
echo "Exiting..."
exit 1
fi
# Obtain current DB credentials
db_cred=$(docker exec "$container_id" python3 db_auth.py)
# load dumpfile
PGPASSWORD="$db_cred" psql "host=$POSTGRES_HOST port=5432 dbname=vaultmaster user=$POSTGRES_USER sslmode=$PG_SSLMODE sslrootcert=$PG_SSLROOT" -f "$filename"
# Check for success
if [ $? -ne 0 ]; then
echo "ERROR: Unable to load dumpfile."
exit 1
else
echo "Dumpfile loaded to production! Have a wonderful day."
fi