This recipe teaches you how to back up a MongoDB database using the mongodump
utility and restore it using mongorestore
.
This recipe requires a running MongoDB server and access to a user account with membership in the userAdmin
role.
Follow these steps to back up a MongoDB database:
Connect to MongoDB as a user with membership in the
userAdmin
role:mongo --username admin --password "" admin
Create an account with membership in the
backup
andrestore
roles to be used for creating and restoring backups:db.createUser({ user: "backupusr", pwd: "B@CK&4th", roles: [ { role: "backup", db: "admin" }, { role: "restore", db: "admin" } ] })
Use
mongodump
on the command-line to export a MongoDB database:mongodump --authenticationDatabase admin --username backupusr --password "" --db packt
To restore a database from the backup made by
mongodump
, use themongorestore
program:mongorestore...