You can back up and restore a point-in-time snapshot of an entire
mongod
using the mongodump --oplog
and mongorestore --oplogReplay
commands. However, the mongorestore --oplogReplay
command can only replay the oplog for a full restore. You cannot use the --oplogReplay
option with the --database
, --collection
, or --nsInclude
options to replay the oplog for a single database or collection. Attempting to do so results in an error: Can only replay oplog on full restore
.Workaround
You can restore a dump and replay the oplog for a single database or collection using a temporary
mongod
as an intermediary. For example, to restore the foo.bar
collection to a mongod
running on port 27017:- Start a temporary, standalone
mongod
instance:$ mkdir tempdb/ $ mongod --port 28000 --dbpath tempdb/ --logpath tempdb/mongod.log --fork
- Restore the full dump and replay the oplog on the temporary
mongod
instance. This provides a point-in-time restore for all the databases.$ mongorestore --port 28000 --oplogReplay dump/
- Run
mongodump
on the temporarymongod
instance to dump thefoo.bar
collection to thecollectionDump
directory:$ mongodump --port 28000 --db foo --collection bar --out collectionDump
- Restore the new dump generated in step 3 to the target
mongod
.MongoDB 3.4 and later:$ mongorestore --port 27017 --nsInclude foo.bar collectionDump/
MongoDB 3.2 and earlier:$ mongorestore --port 27017 --db foo --collection bar collectionDump/foo/bar.bson
- Stop the temporary
mongod
and remove its data directory:$ mongo --port 28000 --eval "db.adminCommand('shutdown')" $ rm -rf tempdb/
Comments
Post a Comment