Skip to content

Instantly share code, notes, and snippets.

@RanKey1496
Created April 3, 2019 19:32
Show Gist options
  • Save RanKey1496/4e8c841c8f6c40f2a91af54713553489 to your computer and use it in GitHub Desktop.
Save RanKey1496/4e8c841c8f6c40f2a91af54713553489 to your computer and use it in GitHub Desktop.
Backup a dockerized MongoDB and upload to S3
#!/bin/bash
# Configuration
CONTAINER="mongo"
FILENAME="databasename-`date +%Y-%m-%d`"
MONGO_USER='user'
MONGO_PASSWORD='pass'
DATABASE_NAME='databasename'
S3_BUCKET_NAME='bucket'
S3_BUCKET_PATH='backups/database'
# Backup from Docker
docker exec -it $(docker ps -f name=$CONTAINER -q --no-trunc | head -n1) mongodump -d $DATABASE_NAME -u $MONGO_USER -p $MONGO_PASSWORD -o /tmp
# Copy to host
docker cp $(docker ps -f name=$CONTAINER -q --no-trunc | head -n1):/tmp/$DATABASE_NAME /tmp/$FILENAME
# Remove files from container
docker exec -it $(docker ps -f name=$CONTAINER -q --no-trunc | head -n1) rm -rf /tmp/$DATABASE_NAME
# Upload to Digital Ocean Spaces
# s3cmd should be configured before
s3cmd put /tmp/$FILENAME s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/ --recursive
# Remove files from host
rm -rf /tmp/$FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment