Created
March 22, 2017 22:01
-
-
Save MikeyYeahYeah/e540085ae8a425e02b80eb60aed3f106 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/bash | |
set -e | |
# Blame: Michael Rosabal (mrosabal@circleci.com) | |
#------------------------------------- | |
# Preflight Checks | |
#------------------------------------- | |
echo "Preflight Checks..." | |
if [ -d /backup/mongo ]; then | |
rm -rf /backup/mongo/* | |
else | |
mkdir -p /backup/mongo | |
fi | |
if [ -d /backup/postgres ]; then | |
rm -rf /backup/postgres/* | |
else | |
mkdir -p /backup/postgres | |
fi | |
#------------------------------------- | |
# Get Database Container IDs | |
#------------------------------------- | |
echo "Getting Container IDs..." | |
MONGO_DOCKER_ID=$(docker ps | grep mongo | awk '{ print $1 }') | |
POSTRGRES_DOCKER_ID=$(docker ps | grep postgres | awk '{ print $1 }') | |
#------------------------------------- | |
# Get Path to Secrets | |
#------------------------------------- | |
PATH_TO_SECRETS=$(find / -name *.circlerc* | tail -1) | |
#------------------------------------- | |
# Get Creds for Mongo and Postgress | |
#------------------------------------- | |
MONGO_PW=$(cat $PATH_TO_SECRETS | grep CIRCLE_SECRETS_MONGODB_MAIN_URI | awk -F '[(:|@)]' '{{ print $3 }}') | |
#------------------------------------- | |
# Backup MongoDB | |
#------------------------------------- | |
echo "Backing Up Mongo (This may take a while)..." | |
MONGODUMP_CMD="mongodump -u circle -p $MONGO_PW --out /data/db/mongo_dump_raw" | |
MONGO_ZIP="tar czvf /data/db/mongodump_$(date +"%Y_%m_%d_%H_%M").tar.gz -C /data/db mongo_dump_raw" | |
docker exec $MONGO_DOCKER_ID $MONGODUMP_CMD | |
docker exec $MONGO_DOCKER_ID $MONGO_ZIP | |
mv /data/circle/mongo/mongodump_* /backup/mongo/ | |
#------------------------------------- | |
# Backup Postgres | |
#------------------------------------- | |
echo "Backing Up Postgresql (This may take a while)..." | |
POSTGRES_CMD="pg_dump -U circle -Fc circle > /var/lib/postgresql/data/pgdump_$(date +"%Y_%m_%d_%H_%M").sql" | |
docker exec $POSTRGRES_DOCKER_ID bash -c "$POSTGRES_CMD" | |
mv /data/circle/postgres/pgdump_* /backup/postgres/ | |
#------------------------------------- | |
# Completed Message | |
#------------------------------------- | |
echo "=====================================================" | |
echo " Backup process is complete. You can find the files " | |
echo " in the /backup directory" | |
echo "=====================================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment