-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6fdc73
commit 234134e
Showing
1 changed file
with
63 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/bin/bash | ||
|
||
set -u # crash on missing env | ||
set -e # stop on any error | ||
|
||
ENVIRONMENT=$1 | ||
if [ "$ENVIRONMENT" == "" ]; then | ||
echo "Usage: $0 <ENV>" | ||
exit 1 | ||
fi | ||
DB="rove_nutrition_$ENVIRONMENT" | ||
|
||
# Get the directory where the script is located | ||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" | ||
|
||
# Ensure the script runs in api | ||
API_DIR="$SCRIPT_DIR/../../services/api" | ||
|
||
# Save the current directory and change to the target directory | ||
pushd "$API_DIR" > /dev/null || { echo "Failed to change directory to $API_DIR"; exit 1; } | ||
|
||
if [ ! -f "./scripts/anonymize-database.js" ]; then | ||
echo "Expecting ./scripts/anonymize-database.js" | ||
exit 1 | ||
fi | ||
|
||
bedrock cloud authorize $ENVIRONMENT | ||
API_CLI_POD=`bedrock cloud status $ENVIRONMENT 2> /dev/null | grep api-cli-deployment | grep -i running | awk '{print $1}'` | ||
echo "Using API_CLI pod $API_CLI_POD and database $DB" | ||
|
||
function exec_remote() { | ||
kubectl exec -it $API_CLI_POD -- /bin/bash -c "$*" | ||
} | ||
|
||
rm -f dump.tar.gz | ||
rm -rf dump | ||
echo "Creating export on CLI pod" | ||
exec_remote "rm -rf /export; mkdir -p /export" | ||
|
||
exec_remote "cd /export; mongodump --host=\"mongo:27017\" -d $DB; tar cfzv dump.tar.gz dump" | ||
exec_remote "md5sum /export/dump.tar.gz" | ||
echo "Transfering dump file" | ||
#kubectl cp $API_CLI_POD:/export/dump.tar.gz dump.tar.gz | ||
# Ghetto hack because the above is not stable for large files (EOF during transfer. Due to Kubernetes shortcomings) | ||
# Note if you run into md5 mismatch issues increase the sleep below... | ||
kubectl exec $API_CLI_POD -- bash -c 'cat /export/dump.tar.gz && sleep 20' > dump.tar.gz | ||
md5 dump.tar.gz | ||
echo "Cleaning up CLI pod" | ||
exec_remote "rm -rf /export" | ||
tar xfzv dump.tar.gz | ||
echo "Restoring dump locally" | ||
cd dump; mongorestore --drop $DB -d $DB; cd .. | ||
echo "Cleaning up locally" | ||
rm -rf dump | ||
rm -f dump.tar.gz | ||
|
||
if [ "$ENVIRONMENT" == "production" ]; then | ||
echo "Anonymizing data" | ||
MONGO_URI=mongodb://localhost/$DB node scripts/anonymize-database.js | ||
fi | ||
|
||
# Return to the original directory | ||
popd > /dev/null |