|
| 1 | +#!/bin/bash |
| 2 | +version="0.1.0" |
| 3 | +echo "Connecting to DB....(connect-db.sh v$version)" |
| 4 | + |
| 5 | +if [ "$1" == "-h" ]; then |
| 6 | + echo "Usage: $(basename $0)" |
| 7 | + exit 0 |
| 8 | +fi |
| 9 | +if [ $# -gt 0 ]; then |
| 10 | + mydbname=$1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [ $# -gt 1 ]; then |
| 14 | + mycollection=$2 |
| 15 | +fi |
| 16 | +[ -z "$RG_HOME" ] && RG_HOME='/opt/deploy/sp2' |
| 17 | +echo "RG_HOME=$RG_HOME" |
| 18 | +[ -z "$RG_SRC" ] && RG_SRC='/home/ubuntu' |
| 19 | +echo "RG_SRC=$RG_SRC" |
| 20 | +myinput=$(cat "$RG_HOME/config/mongo-config.json") |
| 21 | +if [ -z "$myinput" ]; then |
| 22 | + echo "Could not find DB details file. Exiting" |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +mydbuser=$(jq -r '.db_auth_config.username' <<<"${myinput}") |
| 27 | +mydbuserpwd=$(jq -r '.db_auth_config.password' <<<"${myinput}") |
| 28 | + |
| 29 | +if [ -z "$mydbuser" ] || [ -z "$mydbuserpwd" ]; then |
| 30 | + echo "Could not find DB details. Exiting" |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +if [ ! -f "$RG_HOME/docker-compose.yml" ]; then |
| 35 | + echo "docker-compose.yml does not exist. Exiting" |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | +mydocdburl=$(grep DB_HOST "$RG_HOME/docker-compose.yml" | head -1 | sed -e "s/.*DB_HOST=//") |
| 39 | +if [ -z "$mydocdburl" ]; then |
| 40 | + echo "Could not find DB URL. Exiting" |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | +if [ "$mycollection" == "ALL" ]; then |
| 44 | + echo "Importing all 3 collections into $mydocdburl" |
| 45 | + mongoimport --host "$mydocdburl:27017" --ssl \ |
| 46 | + --sslCAFile "$RG_HOME/config/rds-combined-ca-bundle.pem" \ |
| 47 | + --username "$mydbuser" --password "$mydbuserpwd" \ |
| 48 | + --db "${mydbname}" --collection=studies \ |
| 49 | + "$RG_SRC/dump/studies.json" |
| 50 | + mongoimport --host "$mydocdburl:27017" --ssl \ |
| 51 | + --sslCAFile "$RG_HOME/config/rds-combined-ca-bundle.pem" \ |
| 52 | + --username "$mydbuser" --password "$mydbuserpwd" \ |
| 53 | + --db "${mydbname}" --collection=studies \ |
| 54 | + "$RG_SRC/dump/standardcatalogitems.json" |
| 55 | + mongoimport --host "$mydocdburl:27017" --ssl \ |
| 56 | + --sslCAFile "$RG_HOME/config/rds-combined-ca-bundle.pem" \ |
| 57 | + --username "$mydbuser" --password "$mydbuserpwd" \ |
| 58 | + --db "${mydbname}" --collection=studies \ |
| 59 | + "$RG_SRC/dump/configs.json" |
| 60 | +else |
| 61 | + # trunk-ignore(shellcheck/SC2060) |
| 62 | + mycollection=$(echo "$mycollection" | tr [:upper:] [:lower:]) |
| 63 | + if [ "$mycollection" != "studies" ] && [ "$mycollection" != "standardcatalogitems" ] && [ "$mycollection" != "configs" ]; then |
| 64 | + echo "Unknown collection $mycollection" && exit 1 |
| 65 | + fi |
| 66 | + mongoimport --host "$mydocdburl:27017" --ssl \ |
| 67 | + --sslCAFile "$RG_HOME/config/rds-combined-ca-bundle.pem" \ |
| 68 | + --username "$mydbuser" --password "$mydbuserpwd" \ |
| 69 | + --db "${mydbname}" --collection=studies \ |
| 70 | + "$RG_SRC/dump/$mycollection.json" |
| 71 | +fi |
0 commit comments