Updated March 8, 2023
Introduction to Mongodb backup and restore
Mongo DB backup and restore is the process of migrating and transferring all the data from the source database to the target database which indeed is going to be a Mongo DB database. While migrating and transferring the data from the source it is very necessary to consider a factor that is whether the source database is online or offline. By the term online here we mean that is there any of the updates or changes taking place in the source database when we are trying to migrate or not. If the source database is changing and evolving, we will have to do online migration else we can go for offline migration. In this topic, we are going to learn about Mongodb backup and restore.
In case of data failure or unavoidable circumstances that lead to data loss, if we have backup then we can restore it and get back to the original state with previous data.
In both cases of migration, we need to follow two steps of creating a backup and restoring the same. In this article, we will study the offline migration of the dataset in Mongo DB. We will discuss each of the steps of backup creation till restoration in detail.
Steps for Migration
The process of migration in the offline database is very simple and straightforward. It involves the two steps mentioned below –
- Existing data is used and a zip file is created for consideration of backup.
- A new database or target database is selected and the dumping of the backup also called as the restoration process is carried out.
Now, we will look at each of these steps in detail.
Backup creation
For creating a backup zip file, there is a utility program that exists in Mongo DB which is mongodump. We can use this command for creating a zip file of the existing source database. The syntax of the mongodump command is as shown below –
Mongodump –host=”name of host : port number” – username = “name of the user” –password “associated password for the user” – authenticationDatabase “admin” --db = “name of source database” –collection = “Name of the collection” –query = “json” –forceTableScan -v –gzip –out ./dump
In the above syntax, the terminologies used here are described one by one –
–host: This is the address of the host that includes the name of the host and the port of the socket where the connection needs to be established. By default, the value of the hostname and port is considered as localhost:27017. In case, if you are trying to specify a string of connections then you can make the use of uri option shown below –
Uri = “mongodb:// name of user : associated password @ host address[: port number] …”
–name of the user: This will help you to specify the user’s name which will have the access to the database and which will be authenticated by the Mongo DB database whose we are creating a backup.
— password: This option helps you to specify the password associated with the specified user name which will be considered for authentication purposes to verify if the user is a valid user or not.
–authenticationDatabase: The database which is authenticated and where the specified user with the name of the user is created. If we will not specify the authentication database option here or if the export target database is not specified then the dump command will assume that there is the presence of the user’s credentials inside the admin database.
–db: This option helps in specifying the source database which needs to be considered for exporting or creating a dump file. When the name of the database is not mentioned here then all the databases which are present in the current instance are considered for dump file creation. We can also specify the database name directly by using the connection string containing URI.
–collection: This helps in specifying the name of the collection whose backup is to be taken. By default, if you do not specify this option and its value then all the collections of the database are dumped into the dump zip file.
–query: This is the JSON document containing a query that can be specified to restrict and limit the documents to be dumped in the zip file. It is necessary to enclose a query in single quotes ‘{…}’ to make sure that it doesn’t get miscommunicated with the other environmental parameters.
–forceTableScan: Usually, the dump file is created by scanning all the documents and saving them which occur inside the index of _id field. When we specify this option all the data is directly scanned to the data store.
–gzip: The output of the command is compressed due to this option. The individual files are also compressed and the files have the suffix mentioned as .gz.
–out: The directory or folder where the dump file is written in BSON files is specified here. By default, the directory where the output files are stored is inside the current directory inside which a new directory with dump name is created.
Restoration
The restoration can be done by using the following command –
Mongorestore --uri = “ mongodb:// name of user : associated password @ name of host : port address ? AuthSource = admin” –drop –noIndexRestore
The terminologies have the same description as mentioned for backup creation.
Example of Mongodb backup and restore
We have a database whose name is EDUCBA having Articles collection in it. To create a backup zip file which will be referred to as a dump file, we can fire the following command –
sudo mongodump --db EDUCBA --out /var/backups/educbaBackups/`Date +"%m-%d-%y"`
The output of the execution of the above command is as shown in the below image –
A zip file will be created which can be used as a dump file for restoration of this data in any of the other target databases. Now, let us look at the command used for restoring this data which is as shown below –
sudo mongorestore --db EDUCBA --drop /var/backups/educbaBackups/01-20-16/EDUCBA/
The output of the execution of the above command is as shown below –
Conclusion – Mongodb backup and restore
We can migrate the data in Mongo DB by exporting it to create a dump file and then importing the same zip file created from exporting the backup to restore the data in the target database.
Recommended Articles
This is a guide to Mongodb backup and restore. Here we discuss the offline migration of the dataset in Mongo DB and the steps of backup creation. You may also have a look at the following articles to learn more –