Tuesday, August 20, 2019

MongoDB Restore:

MOngorestore utility is used to restore the data backed up using mongodump utility.
Inorder to run mongorestore utility, user should have restore role in the database.

If you have a mongo instance running with no data and you want to restore the all of the data.

mongorestore /home/mongod/backup/dump

This will restore everything , assuming you to connect to local server, default port
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

To restore to nameserver

mongorestore --host hostname --port 27017 /home/mongod/backup/dump


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If you want to replace documents with the documents in the backup, then use an --drop parameter.
this will drop target collection before it restore.

[mongod@sikki4u1c dump]$ mongorestore -u mongo-root -p passw0rd --drop /home/mongod/backup/dump
2019-08-19T11:22:36.620+0000    preparing collections to restore from
2019-08-19T11:22:36.626+0000    reading metadata for movie.mycol from /home/mongod/backup/dump/movie/mycol.metadata.json
2019-08-19T11:22:36.643+0000    restoring movie.mycol from /home/mongod/backup/dump/movie/mycol.bson
2019-08-19T11:22:36.656+0000    reading metadata for demo.peeps from /home/mongod/backup/dump/demo/peeps.metadata.json
2019-08-19T11:22:36.658+0000    no indexes to restore
2019-08-19T11:22:36.658+0000    finished restoring movie.mycol (3 documents)
2019-08-19T11:22:36.671+0000    restoring demo.peeps from /home/mongod/backup/dump/demo/peeps.bson
2019-08-19T11:22:36.676+0000    reading metadata for movie.movie from /home/mongod/backup/dump/movie/movie.metadata.json
2019-08-19T11:22:36.677+0000    no indexes to restore
2019-08-19T11:22:36.677+0000    finished restoring demo.peeps (1 document)
2019-08-19T11:22:36.691+0000    restoring movie.movie from /home/mongod/backup/dump/movie/movie.bson
2019-08-19T11:22:36.694+0000    no indexes to restore
2019-08-19T11:22:36.694+0000    finished restoring movie.movie (1 document)
2019-08-19T11:22:36.694+0000    restoring users from /home/mongod/backup/dump/admin/system.users.bson
2019-08-19T11:22:36.717+0000    done
[mongod@sikki4u1c dump]$

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Restore only one collection:

note: --drop with drop the collection if exists and restore it


[mongod@sikki4u1c movie]$ mongorestore -u mongo-root -p passw0rd --drop --collection mycol --db movie /home/mongod/backup/dump/movie/mycol.bson --authenticationDatabase admin
2019-08-20T11:41:57.583+0000    checking for collection data in /home/mongod/backup/dump/movie/mycol.bson
2019-08-20T11:41:57.586+0000    reading metadata for movie.mycol from /home/mongod/backup/dump/movie/mycol.metadata.json
2019-08-20T11:41:57.595+0000    restoring movie.mycol from /home/mongod/backup/dump/movie/mycol.bson
2019-08-20T11:41:57.657+0000    no indexes to restore
2019-08-20T11:41:57.657+0000    finished restoring movie.mycol (3 documents)
2019-08-20T11:41:57.657+0000    done
[mongod@sikki4u1c movie]$


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Restore all collection to new database.

 mongorestore -u mongo-root -p passw0rd  --db restored_movie /home/mongod/backup/dump/movie/ --authenticationDatabase admin

 2019-08-20T11:44:16.072+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-08-20T11:44:16.073+0000    building a list of collections to restore from /home/mongod/backup/dump/movie dir
2019-08-20T11:44:16.074+0000    reading metadata for restored_movie.mycol from /home/mongod/backup/dump/movie/mycol.metadata.json
2019-08-20T11:44:16.084+0000    restoring restored_movie.mycol from /home/mongod/backup/dump/movie/mycol.bson
2019-08-20T11:44:16.086+0000    no indexes to restore
2019-08-20T11:44:16.086+0000    finished restoring restored_movie.mycol (3 documents)
2019-08-20T11:44:16.089+0000    reading metadata for restored_movie.movie from /home/mongod/backup/dump/movie/movie.metadata.json
2019-08-20T11:44:16.102+0000    restoring restored_movie.movie from /home/mongod/backup/dump/movie/movie.bson
2019-08-20T11:44:16.105+0000    no indexes to restore
2019-08-20T11:44:16.105+0000    finished restoring restored_movie.movie (1 document)
2019-08-20T11:44:16.105+0000    done
[mongod@sikki4u1c movie]$  mongo -u mongo-root -p passw0rd
MongoDB shell version v3.6.13
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ec1042f6-cfd4-4ba5-97fe-ef16218a7c8d") }
MongoDB server version: 3.6.13
Server has startup warnings:
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten]
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten]
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-08-20T05:49:51.204+0000 I CONTROL  [initandlisten]
> show dbs
admin           0.000GB
config          0.000GB
demo            0.000GB
local           0.000GB
movie           0.000GB
restored_movie  0.000GB
> use restored_movie
switched to db restored_movie
> show collections
movie
mycol
>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
In case of replicaset , you can use --oplog option to restore

mongorestore -u mongo-root -p passw0rd  --oplogReplay /home/mongod/backup/dump --authenticationDatabase admin


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home