-
-
Save artbikes/f8a8e6c04e913588790ae2f5b19d8436 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.
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/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 - " |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ver 3.2+ onwards there has been compression feature introduced which can be used as following to make this work for entire server or specific db
/usr/bin/mongodump -h $HOST -d $DB --gzip --archive | aws s3 cp - $S3BACKUP
and your restore script would be
aws s3 cp $S3LATEST - | mongorestore --host $HOST --archive --gzip