How do I restore a single collection for a specific point-in-time?

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:
  1. Start a temporary, standalone mongod instance:
    $ mkdir tempdb/
    $ mongod --port 28000 --dbpath tempdb/ --logpath tempdb/mongod.log --fork
    
  2. 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/
    
  3. Run mongodump on the temporary mongod instance to dump the foo.bar collection to the collectionDump directory:
    $ mongodump --port 28000 --db foo --collection bar --out collectionDump
    
  4. 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
    
  5. Stop the temporary mongod and remove its data directory:
    $ mongo --port 28000 --eval "db.adminCommand('shutdown')"
    $ rm -rf tempdb/

Comments