Skip to content

Commit

Permalink
Database script update (#1256)
Browse files Browse the repository at this point in the history
* update the scripts to include a help option and allow the changing of hosts and filenames

* update readme to reflect script changes

* Changes to readme with extra instruction and formatting

* update scripts to use printf for better portability

* update scripts to use printf for better portability

---------

Co-authored-by: A. K <[email protected]>
  • Loading branch information
will-byrne and soggy-mushroom authored Sep 30, 2024
1 parent 0f633ad commit b1327ef
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ dist
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# database dumps
*.dump
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ and to lint:
yarn lint
```

# Clone staging database
# Clone cluster databases

To clone the staging database (as long as you have k8s access) run `scripts/clone-staging-db.sh` to make a dump of the database.
start the application with a `docker compose up --wait`
when the containers have all started run `scripts/use-staging-db.sh`
To clone one of the cluster databases (as long as you have k8s access) run `scripts/clone-cluster-db.sh` to make a dump of the database.

Use `-o` or `--output` (defaults to `./database.dump`) to change the name of the dump and `-r` or `--remote` to change which remote to use (only accepts `staging` or `prod` and defalts to `staging`).

Start the application with a `docker compose up --wait`.

When the containers have all started run `scripts/use-cluster-db.sh`, use `-i` or `--input` (defaults to `./database.dump`) to change the name of the dump to be used.

To see the options on either script run them with `-h`

To check that the db has been imported correctly go to `localhost:8081` and click through to /epp folder.
54 changes: 54 additions & 0 deletions scripts/clone-cluster-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# /bin/sh

R="\033[0;31m"
G="\033[0;32m"

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
case $1 in
-o|--output)
OUTPUTPATH="$2"
shift # past argument
shift # past value
;;
-r|--remote)
if [ "$REMOTE" != "staging" ] && [ "$REMOTE" != "prod" ]; then
printf "Error: Invalid value for remote. Allowed values are 'staging' or 'prod'."
exit 1
fi
REMOTE="$2"
shift # past argument
shift # past value
;;
-h|--help)
printf "This script will clone the mongodb data on either staging or prod so it can be used locally.\nkubectl will need to be setup and authenticated to access the cluster\n\n"
printf "options:\n"
printf "${G}-o --output The output path for the database dump including the filename (default: ./database.dump)\n"
printf "${G}-r --remote Which remote to use, must be either 'staging' or 'prod' (default: staging)\n"
exit 0
;;
-*|--*)
printf -e "${R}Unknown option $1"
exit 1
;;
esac
done

OUTPUTPATH="${OUTPUTPATH:-./database.dump}"
REMOTE="${REMOTE:-staging}"

# port forward to cluster
kubectl port-forward -n epp--${REMOTE} service/epp-database-psmdb-db-replicaset 27017:27017 &
process_pid=$!

# get creds for db
credentials=$(kubectl get secret epp-database-psmdb-db-secrets -o yaml -n epp--${REMOTE})
password=$(echo "$credentials" | grep 'MONGODB_DATABASE_ADMIN_PASSWORD' | cut -d ':' -f 2 | xargs | base64 -d | cut -d '%' -f 1)
username=$(echo "$credentials" | grep 'MONGODB_DATABASE_ADMIN_USER' | cut -d ':' -f 2 | xargs | base64 -d | cut -d '%' -f 1)

# clone db to local file
mongodump --host 127.0.0.1 --port 27017 --username $username --password $password --authenticationDatabase admin --db epp --collection versioned_articles --out ${OUTPUTPATH}

# kill port forwarding
kill $process_pid
16 changes: 0 additions & 16 deletions scripts/clone-staging-db.sh

This file was deleted.

36 changes: 36 additions & 0 deletions scripts/use-cluster-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# /bin/sh

R="\033[0;31m"
G="\033[0;32m"

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
case $1 in
-i|--input)
INPUTPATH="$2"
shift # past argument
shift # past value
;;
-h|--help)
printf "This script will restore a mongodb data dump into a local mongodb docker container."
printf
printf "options:"
printf -e "${G}-i --input The input path for the database dump including the filename (default: ./database.dump)"
exit 0
;;

-*|--*)
printf -e "${R}Unknown option $1"
exit 1
;;
esac
done

INPUTPATH="${INPUTPATH:-./database.dump}"

# push local file to docker container
mongo_container_id=$(docker ps -f 'name=mongodb' -q)
docker cp ${INPUTPATH} $mongo_container_id:/tmp/database.dump

docker compose exec mongodb mongorestore --uri="mongodb://admin:testtest@localhost:27017" --authenticationDatabase=admin --drop --nsInclude=epp.versioned_articles /tmp/database.dump
7 changes: 0 additions & 7 deletions scripts/use-staging-db.sh

This file was deleted.

0 comments on commit b1327ef

Please sign in to comment.