3

I need to be able to restore a single database, even a single collection from a backup. Since mongodump --oplog only applies to a full instance (Replica Set), I made the following procedure to filter only the entries from the db that I want to restore from the oplog.bson generated by the --oplog option. Could someone tell me if this is correct or if I'm missing something?

  1. First I restore only the db (the db is test in this example) that I need

    mongorestore -d test dump/test

  2. Restore into a random collection the oplog.bson file generated by the --oplog option

    mongorestore -d oplog -c oplog dump/oplog.bson

  3. Dump from the restored oplog.bson collection only the documents that refer to the db that I'm restoring

    mongodump -d oplog -c oplog -q "{ns:/^test[.]/}" -o oplog

  4. Restore with the --oplogReplay option using the last dump with filtered operations

    mongorestore --oplogReplay oplog/oplog

  5. Finally I drop the temporary oplog collection.

    mongo --eval "db.getSisterDB('oplog').dropDatabase()"

Thanks in advance!

peporro
  • 83
  • 7

1 Answers1

0

Basically, that's one way to do it.. Or you can do extra export after dumping specific database.

mongoexport -d local -c oplog.rs --query="{ns:/^test[.]/}" -o oplog.dump

2017-10-10T11:51:50.780+0300    connected to: localhost
2017-10-10T11:51:51.737+0300    local.oplog.rs  0
2017-10-10T11:51:51.938+0300    local.oplog.rs  3
2017-10-10T11:51:51.938+0300    exported 3 records

And you have now direct json dump file what you can read back

JJussi
  • 1,540
  • 12
  • 12
  • Thanks for the reply. If my method works, I think i'll stick with it, since I end up with a much smaller portion of the oplog. Taking into account that oplog.rs is usually a big collection and without indexes, so doing the export for each database would take significantly more – peporro Oct 10 '17 at 13:44
  • 1
    Actually, you can export whole oplog.rs (without --query) and use `grep` command at mongoimport to import only wanted lines.. ;-) `egrep '"ns":"test.*' oplog2.dump | mongoimport -d X -c x` – JJussi Oct 11 '17 at 08:36