You can back up and restore a point-in-time snapshot of an entire
mongod using the mongodump --oplog and mongorestore --oplogReplaycommands. 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
mongodinstance:$ mkdir tempdb/ $ mongod --port 28000 --dbpath tempdb/ --logpath tempdb/mongod.log --fork - Restore the full dump and replay the oplog on the temporary
mongodinstance. This provides a point-in-time restore for all the databases.$ mongorestore --port 28000 --oplogReplay dump/ - Run
mongodumpon the temporarymongodinstance to dump thefoo.barcollection to thecollectionDumpdirectory:$ 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
mongodand remove its data directory:$ mongo --port 28000 --eval "db.adminCommand('shutdown')" $ rm -rf tempdb/
Comments
Post a Comment