How to backup and restore a Mongo Database
Introduction
MongoDB is a popular NoSQL database that is used for storing large amounts of data in a flexible and JSON-like format. The Backup and Restore in MongoDB is an important part to handle a database. This utility is important because the valuable data can be removed or crashed or corrupted from the server.
Prerequisite
- An SSH user access with sudo privileges
- Mongo DB credentials if it is authenticated
- The `mongodump` and `mongorestore` command line tools, which are included with the MongoDB installation
Implementation
Step 1: SSH the server and convert the user to the root
$ ssh user@IP $ sudo su |
Step 2: Backing Up a MongoDB Database
$ mongodump –host <hostname> –db <dbname> –out </backup directory path/> If the Mongo DB is running on the local host, the hostname is not required $ mongodump –db <dbname> –out <backup directory path> |
- –username and –password: The credentials to use to authenticate to the database
Step 3: Restoring a MongoDB Database
$ mongorestore –db <dbname> </backup directory path/> Use –drop option delete all data from the target database before restoring it $ mongorestore –db <dbname> –drop </backup directory path/> |