Backup and recovery is two most valuable parts in database in case of system failure or unexpected system crash or database replacement.
In MongoDB, data can be import and exported using commands.
Lets move on MongoDB command:
Before starting import and export you need to start mongo using terminal and open a new terminal and navigate to that folder where you want to backup your database.
Open terminal and navigate to that directory where your MongoDB is installed and after that, type below commands.
Backup for one database in MongoDB, execute the below commands.
mongodump --host localhost --port 27017 --db db_name
Where db_name is your database name.
Restore for one database, execute below commands.
mongorestore --host localhost --port 27017 --db new_db_name dump/db_name
Where db_name is your old database name and new_db_name is your new database name.
Backup for total database, execute below command.
mongodump --host localhost --port 27017
Restore for total database, execute below command
mongorestore --host localhost --port 27017 dump