Search code examples
mongodbmongodumpmongorestore

MongoDB - dump and restore across different host, db with oplog


Is it possible to take a mongodump and mongorestore it to a different hosts, with different DB names, with oplog enabled?

From: mongodb://user:password@source-hostname:source-port/db1

To: mongodb://user:password@dest-hostname:dest-port/db5

When I do a mongodump with oplog on the source MongoDB, it takes a dump of the entire DB.

mongodump --oplog --host <source-hostname> -u <user> -p <password> --port <source-port> --authenticationDatabase admin

Now for the restore, I want to restore to a different hostname, and the db-name is also different. Is there way to restore the data to this db, with the oplogReplay?

mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay --db <db5> <path-to-dump>/dump

If I use oplogReplay, I am getting the following error

Can only replay oplog on full restore

I do not want to do a full restore, as it will create the db-name as db1, whereas I want to make use of db5. Also, there are already multiple DBs on this destination host and I do not want to bombard with another new database.

Any suggestions on this issue?


Solution

  • You can't use two options --oplogReplay and --db at the same time. If you don't want to restore the full DB, simply go to dump/ folder and delete files for DBs other than db5. Then retry mongorestore without --db:

    mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay <path-to-dump>/dump
    

    If this doesn't not work for you, you may need to import oplog collection to a temporary db, and manipulate it to remove all except records for db5.