Skip to content

Instantly share code, notes, and snippets.

@sumitsk20
Forked from caraboides/backup-mongodb-to-s3.sh
Created January 11, 2019 07:08
Show Gist options
  • Save sumitsk20/12ab4dd6f1ef964d9fb2c17a3231e1d5 to your computer and use it in GitHub Desktop.
Save sumitsk20/12ab4dd6f1ef964d9fb2c17a3231e1d5 to your computer and use it in GitHub Desktop.
Simple script to backup MongoDB to S3, without waste diskspace for temp files. And a way to restore from the latest snapshot.
#!/bin/sh
set -e
HOST=localhost
DB=test-entd-products
COL=asimproducts
S3PATH="s3://mongodb-backups-test1-entd/$DB/$COL/"
S3BACKUP=$S3PATH`date +"%Y%m%d_%H%M%S"`.dump.gz
S3LATEST=$S3PATH"latest".dump.gz
/usr/bin/aws s3 mb $S3PATH
/usr/bin/mongodump -h $HOST -d $DB -c $COL -o - | gzip -9 | aws s3 cp - $S3BACKUP
aws s3 cp $S3BACKUP $S3LATEST
# Restore
echo -n "Restore: "
echo -n "aws s3 cp $S3LATEST - |gzip -d | mongorestore --host $HOST --db $DB -c $COL - "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment