Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - Users not restored using command mongorestore

I'm new to MongoDB and I'm testing some basic backup and restore operations in a single MongoDB instance.

All tests worked fine except for restoring users.

I first created a backup of a test database (db_test) using the following command:

$ mongodump -u adm_dump --authenticationDatabase admin -d db_test --dumpDbUsersAndRoles -o /opt/backup/mongodump_db_test

adm_dump is a user I created with "restore" and "backup" roles.

2 users are present in db_test database with "readWrite" and "read" roles respectively. So I was expecting these both users to be restored.

Then I restored the database into database db_test_2 (not existing):

$ mongorestore -u adm_dump --authenticationDatabase admin -d db_test_2 --restoreDbUsersAndRoles /opt/backup/mongodump_db_test/db_test

The new database db_test_2 is successfully created (all collections are restored) but users are not restored in db_test2.

Below the log tail:

...
2016-06-29T18:28:26.429+0200    restoring users from /opt/backup/mongodump_db_test/db_test/$admin.system.users.bson
2016-06-29T18:28:26.464+0200    roles file '/opt/backup/mongodump_db_test/db_test/$admin.system.roles.bson' is empty; skipping roles restoration
2016-06-29T18:28:26.464+0200    restoring roles from /opt/backup/mongodump_db_test/db_test/$admin.system.roles.bson
2016-06-29T18:28:26.468+0200    done

I also granted the "userAdmin" role to adm_dump and did operations several times with no success. Should I give another specific role to adm_dump user ?

I read the doc and googled on dumpDbUsersAndRoles / restoreDbUsersAndRoles options but found nothing relevant.

What did I miss to restore users from a dump backup ?

MongoDB version 3.2.1 on CentOS 6.6

like image 782
Francisco D. Avatar asked Jun 01 '26 13:06

Francisco D.


1 Answers

In case someone else is trying to do this...

  1. Don't specify a db or a collection in mongo dump.
  2. Log in as a user that has admin privileges (maybe adm_dump has those privileges?)
  3. Don't use --dumpDbUsersAndRoles, this only applies when you specify a db. See the docs.

Example:

$ mongodump -u adm_dump --authenticationDatabase admin --dumpDbUsersAndRoles -o /opt/backup/mongodump_db_test
like image 157
BruceJo Avatar answered Jun 04 '26 03:06

BruceJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!