Dump MongoDB Data
Use the mongodump command to create a backup of your MongoDB database. The whole contents of your server will be dumped into the dump directory with this command. There are numerous ways to control the amount of data or make backups of your remote server.
The basic syntax of mongodump command is as follows −
Use the mongodump command to create a backup of your MongoDB database.
The whole contents of your server will be dumped into the dump directory with this command. There are numerous ways to control the amount of data or make backups of your remote server.
The command will connect to the server running at 127.0.0.1 and port 27017 and back all data of the server to directory /bin/dump/. Following is the output of the command −
Following is a list of available options that can be used with the mongodump command.
|mongodump –host HOST_NAME –port PORT_NUMBER||This commmand will backup all databases of specified mongod instance.||mongodump –host tutorialspoint.com –port 27017|
|mongodump –dbpath DB_PATH –out BACKUP_DIRECTORY||This command will backup only specified database at specified path.||mongodump –dbpath /data/db/ –out /data/backup/|
|mongodump –collection COLLECTION –db DB_NAME||This command will backup only specified collection of specified database.||mongodump –collection mycol –db test|
To restore backup data MongoDB’s mongorestore command is used. This command restores all of the data from the backup directory.
The basic syntax of mongorestore command is −
Following is the output of the command −
How to create a backup of all databases
The command used to create a backup in MongoDB is “mongodump“. The following syntax will help you in this regard:
To create a backup of all the databases and associated collections, you must execute the following command in your Ubuntu terminal:
You may have noticed that the command is executed in the Ubuntu terminal. It is a universal command that can be executed on several operating systems and is used to create backups in MongoDB.
$ sudo mongodump
The collections and associated documents present on your MongoDB server are backed up.
How to backup a database in MongoDB
The syntax to create a backup of a database is provided below:
mongodump --db <database-name>
For instance, we want to create a backup for “linuxhint” database. To do so, we have executed the below-stated command in Ubuntu’s terminal:
$ sudo mongodump --db linuxhint
It can be observed from the output that, all the collections and documents associated with linuxhint database are successfully backed up after this execution.
How to backup a collection of a database
Sometimes the size of the database is very large and creating a backup for these databases consumes time as well as storage. In such conditions, the administrator will prefer to create a backup of important collections. The mongodump command also allows you to create a backup of a single collection only. To create a backup of a single collection; one must follow the syntax provided below:
mongodump –db <database-name> –collection <collection-name>
Furthermore, the command written below creates the backup of a collection “staff” and this collection belongs to “linuxhint” database:
> mongodump --db linuxhint --collection staff
As discussed earlier, the default dump address of the MongoDB database and collections is your home directory. However, you can change the backup address by using the “–out” option in “mongodump” command.
mongodump –db linuxhint –out </path/of/location>
For example, we have used the following command to get the backup of “linuxhint” database at our desired address:
> sudo mongodump –db linuxhint –out /adnan/linuxhintdb_backup/
How to restore backed up databases/collections in MongoDB
Once the backup is created, you must know the way to restore it. In this section, we will demonstrate the way to restore data that includes databases and collections.
The command used to restore the backup has the following syntax:
A single backup command creates backups for all databases. Similarly, a single restore command retrieves all the databases from the backup directory to your MongoDB server. The command stated below restores all the databases at once:
Database Management Systems are used to manage the data of an organization and MongoDB is one of the well-known database management systems. The backup phenomenon has a key role in DBMS’s and almost all database administrators update the backup directory regularly. In this descriptive post, we have provided the possible ways to create a backup in MongoDB. Additionally, we have also provided ways to restore the backed-up data. Wrapping up, this guide is equally important for all users of MongoDB. Lastly, it is recommended to create backups of your data, either you are working on databases or any operations related to data management.