MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

How to take backup of MongoDB users and roles without data?

Anyone knows how to take backup of mongodb users and roles without data ?

Hey @Ajithkumar_S

The roles are stored on system.roles in the admin db and users are the system.users collection. Therefore you can take backups of just those collections if you want.

https://docs.mongodb.com/manual/reference/system-roles-collection/index.html

https://docs.mongodb.com/manual/reference/system-users-collection/index.html

1 Like

Hi Natac, I have already tried this.

I can able to take backup.

But while importing to admin database its shows some error.

Pasted the error below,

[root@ajith
/]# /mongo_ak/mongodb4_0/bin/mongorestore --port=27018 --db admin --dir=/dump/admin/system.users.bson
2020-03-02T12:31:46.664+0000 checking for collection data in /dump/admin/system.users.bson
2020-03-02T12:31:46.664+0000 assuming users in the dump directory are from <= 2.4 (auth version 1)
2020-03-02T12:31:46.665+0000 Failed: the users and roles collections in the dump have an incompatible auth version with target server: cannot restore users of auth version 1 to a server of auth version 5
2020-03-02T12:31:46.665+0000 0 document(s) restored successfully. 0 document(s) failed to restore.
[root@casdendra02 /]#

Are the versions of Mongod the same?
This error maybe outside the level of my knowledge… sorry.

Yes @Natac13. Both the MongoDB versions are same.

We don’t have any direct method to dump users and roles with out data.

But I have find a alternative method. To do this.

Sorry for the typo,

Yes @Natac13. Both the MongoDB versions are same.

We don’t have any direct method to (dump and restore) users and roles with out data.

But I have find a alternative method. To do this.

The admin is a special collection, I don’t think you can just dump/restore like that (more knowledgeable poster required)

That said mongorestore has a flag for restoring db users and roles.

Are you making a seed database? Once the users are created drop collections and then dump that whole mongodb. Then restore that with the previously mentioned flag.

1 Like

Was the source database upgraded at some point from a MongoDB version lower then 2.6 on it’s way to MongoDB 4.0? Is it possible that the authSchemaUpgrade was never performed on the source during an upgrade?

https://docs.mongodb.com/manual/release-notes/2.6-upgrade-authorization/index.html

HI @Michael_Grayson,

Here, I didn’t upgrade anything.

I have just installed the 4.0 binary package and tried the test case.

Dear @chris,

I know the collections inside the admin database are special collections.

But my requirement is, I need to backup the users and roles for the particular database without data and the database size is 3.4 TB.

I have tried all the methods available in mongodb but nothing is help for me.

After that I made some changes in the dump file after that its works.

My question is, any possible ways that anyone know to dump the users and roles directly from MongoDB ?

without the data. Is so important.

This might be really naive but can’t you achieve what you want with https://docs.mongodb.com/manual/reference/method/db.getUsers/?

HI @steevej,
Using db.getUsers(), we can get all the users list, yes we can take this user backup but how will restore.

You probably can write a simple .js loop over all documents that calls https://docs.mongodb.com/manual/reference/method/db.createUser/.

Note that you do not get any password with db.getUsers() so you will have to generate new password.

Yes @steevej,

You are correct, that won’t help for backup and restore. It will help to take the users list only.

But I have some other methods to backup and restore users separately.

If you wanted I will explain the steps.

Otherwise we don’t have any option to take backup & restore, users and roles database wise(without data).

I do not need that. And I do not follow anymore.

In one post

and now

Looks like you just need to dump admin database in its entirety.

system.versions contains the schema version which is the error when your are restoring.