Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 - "
@anilbhanushali

This comment has been minimized.

Copy link

commented Feb 1, 2017

Are you able to stream out entire db ( by not specifying collection option ) ?

@anilbhanushali

This comment has been minimized.

Copy link

commented Feb 1, 2017

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

@sreekanth1990

This comment has been minimized.

Copy link

commented May 6, 2018

Hi howTo solve error like " Invalid endpoint: https://s3.US East.amazonaws.com"

@sreekanth1990

This comment has been minimized.

Copy link

commented May 6, 2018

Hi,
How to solve error like Invalid endpoint: https://s3.US East.amazonaws.com

@jainmnsh

This comment has been minimized.

Copy link

commented Feb 12, 2019

backup and restore works great for me. I just have one question - I have many databases and was wondering if there is a way to backup all databases without defining a list of databases? or recommendation to backup separately for restore purposes? whats the best practice
use to backup 10-12 databases via one script?

@aftabnaveed

This comment has been minimized.

Copy link

commented Apr 25, 2019

@anilbhanushali thanks for your comment. Does it restore into the same db as exported or it creates it with a new name?

@innokentiyt

This comment has been minimized.

Copy link

commented Jul 31, 2019

How the script works with very large (~60-100 GB) collections dumps?

@caraboides

This comment has been minimized.

Copy link
Owner Author

commented Jul 31, 2019

@innokentiyt It works ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.