Backup and Restore MongoDB database (.gz) with and without username / password authentication using C# .NET
Links
GitHub Repository — https://github.com/joemoceri/database-toolkit
Intro
We’ll be using .NET 6 and MongoDB 5.0.5. See the GitHub repository below for the latest with comments and more.
Backup
In order to backup a MongoDB database, we need to use mongodump, a command line utility for backing up a MongoDB database. We can use the following C# .NET method and appsettings.json to get it done.
The three properties in appsettings.json we need to set when using authentication are
"MongoDBUser": "user","MongoDBPassword": "password","MongoDBAuthenticationDatabase": "admin"
The method signature itself looks like this
public void BackupDatabase(string databaseName, string localDatabasePath, bool withAuthentication = false)
Which accepts a database name and the path where it will be saved to, and optionally you can use authentication.
Without Authentication
The final command will look like this without authentication using mongodump. -d specifies which database to backup, — gzip tells mongodump to use gzip, and — archive specifies where to save the backup.
mongodump -d database --gzip --archive=backup.gz
With Authentication
When using authentication with mongodump the final command will look like this. This one is very similar to above, with the additions of — username and — authenticationDatabase.
mongodump -d database --gzip --archive=backup.gz --username user --authenticationDatabase admin
When using authentication, make sure your mongod.cfg file has the following values under #security: set.

The default location to mongod.cfg on Windows is the following (replace 5.0 with your version).
C:\Program Files\MongoDB\Server\5.0\bin
In this situation we’re using “admin” as our authentication database. Make sure to add a user that matches the values in the appsettings.json file above using the following commands, starting with mongo on the command line.
First, connect using mongo (disable authentication first by omitting or commenting out the values under #security: above)
mongo
use the admin database with the following command.
use admin
Then, with the following command add a user, specifying your username, password, and roles. Restart the service when you’re done.
db.createUser(
{
user: "admin",
pwd: "password",
roles: [
{
role: "userAdminAnyDatabase",
db: "admin"
},
"readWriteAnyDatabase"
]
}
)
mongodump will ask for the password when running the mongodb-backup-with-auth.bat file, so we make sure to handle it with the following C# code.
if (withAuthentication){ arguments += $" {options.Value.MongoDBUser} {options.Value.MongoDBAuthenticationDatabase}"; startInfo.RedirectStandardInput = true;}// ...
// Execute the command and when it asks, if we're using authentication, give it the passwordif (withAuthentication){ process.StandardInput.WriteLine(options.Value.MongoDBPassword);
}
Restore
In order to restore a MongoDB database, we need to use mongorestore, a command line utility for restoring a MongoDB database. We can use the following C# .NET method and the appsettings.json file from above to get it done.
The method signature looks like this
public void RestoreDatabase(string localDatabasePath, bool withAuthentication = false)
which accepts a path to the .gz backup file we’re restoring, and optionally with authentication using the same methods described above.
Without Authentication
The final command will look like this without authentication using mongorestore. — drop tells mongorestore to drop the database before restoring, — gzip tells mongorestore to use gzip, and — archive specifies the local .gz database file to restore. The name of the database we’re restoring (that will consequently be dropped) is derived from the .gz file.
mongorestore --drop --gzip --archive=backup.gz
With Authentication
The final command will look like this with authentication using mongorestore. This command is very similar to the one above, with the addition of — username specifying which user and — authenticationDatabase which tells mongorestore which authentication database to use. Make sure these values match with the authentication instructions under Backup above.
mongorestore --drop --gzip --archive=backup.gz --username user --authenticationDatabase admin