search by tags

for the user

adventures into the land of the command line

mongodb backups

– in version 3.2+ –

normally dumps would go by default, into a folder called ‘dump’, which contains all the data in different files:

$ mongodump --db planets

└── [4.3M]  planets
    ├── [2.1M]  earth.bson
    ├── [  87]  earth.metadata.json
    ├── [2.1M]  mars.bson
    ├── [  87]  mars.metadata.json
    └── [ 140]  system.indexes.bson

you can also choose a specific collection:

$ mongodump --db planets --collection mars

to dump a whole db into one file:

$ mongodump --db planets --archive=/srv/backup/mongodb/mars_not_compressed.archive

to dump whole db into one compressed file:

$ mongodump --db planets --gzip --archive=/srv/backup/mongodb/mars_compressed.archive

single collection:

$ mongodump --db planets --collection mars --archive=mars.archive

size comparison of dumped archives:

[email protected]:~# ls -lh /srv/backup/mongodb/
total 3.0G
-rw-r--r-- 1 root root 1.5G Apr 20 14:06 mars_compressed.archive
-rw-r--r-- 1 root root 1.6G Apr 20 14:14 mars_not_compressed.archive

to restore a non-compressed file:

$ mongorestore --archive=/srv/backup/mongodb/mars_not_compressed.archive

to restore a compressed file:

$ mongorestore --gzip --archive=/srv/backup/mongodb/mars_compressed.archive

to restore a single collection from an archived gzip of the entire db:

$ mongorestore --gzip --archive=/srv/backup/mongodb/planets_compressed.archive --db=planets --collection=mars

to restore the entire db from scratch from an archived gzip

$ mongorestore --drop --gzip --archive=/srv/backup/mongodb/planets_compressed.archive

To restore to a replica set

It’s a bit different here. don’t use mongorestore actually, you instead need to initialise the server and specify the dump as the db and also specify the replica set:

$ mongod --dbpath /path/to/db/dump/file --replSet my_groovy_replica_set
$ mongo
$ rs.initiate()

Then ensure that the data directories on the prospective replica set members are empty and one by one add a member to the replica set. The 'Initial Sync’ feature will copy data from the primary to the new member.

Using the oplog for point in time backups

When dumping, the dump will not include any writes that happen during the dump process, so there is potential for lost data in the dump. If your application relies on a high precision of data retention, you can use the oplog option when dumping, which will capture incoming write operations during the mongodump operation to ensure that the backups reflect a consistent data state.
This will create a file named oplog.bson as part of the mongodump output located in the top level of the output directory, which contain oplog entries that occur during the mongodump operation. It will only work against nodes that maintain an oplog, such as replication sets, but will not dump the collection named oplog

$ mongodump --oplog

To restore a db from a dump created with the oplog option, use the oplogReplay option. The option will tell mongo to replay the oplog entries from the oplog.bson file located in the top level of the dump directory, after mongo has restored the initial database dump

$ mongorestore --oplogReplay