How To Backup and Restore MongoDB administrator commands
MongoDB is a cross-platform document-aligned database. Categorized as a NoSQL database, MongoDB relinquishes the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the amalgamation of data in unquestionable types of applications easier and faster. Released under a merger of the GNU Affero General Public License and the Apache License, MongoDB is free and open-source software.Let we start MongoDB And Restore MongoDB.First is Mongo no needed username and password sometimes.
First of stop the mongod instance.
Now, start the mongod instance using the below given command:
The above given mongodump directory will create a dump sub-directory under the current directory. As you see below given, it has taken a backup of both mongodevdb and mongoproddb databases.
If you look inside any one of the database backup directory, you’ll get that it contains all the objects from the database that was backed-up.
2. Take Backup without Shutting down Mongod Instance.
The below given example will connect to a running mongod instance, and it will take backup of a specific database. First of all, make sure mongod is up and running.
In the Next step, go to the backup directory, and run the mongodump command, and enter the database name, username and password parameters as shown below:
Under the /backup directory. Now where you run the mongodump command, it will create a dump directory as given below. The dump directory will have a sub-directory for the database that was just backed-up.
If the mongo instance has multiple database, run the mongodump command couple of time as given below to backup both the databases.
If you don’t want to frame the password in the mongodump command line, you can also enter the password interactively.
3. Now continue with the Backup to define a specific Collection as following given:
Also, if you are trying to run mongodump when the mongoDB instance is not up and running, you’ll get the following shown error message.
4. Now continue with the Backup to define a specific Location as following given:
In all the above examples, mongodump created a dump directory under the current directory from where the command was run. Instead, if you want to backup mongoDB to a specific location, specify the –out parameter as given below.The following example takes a backup of howtoistart collection and stores it under /dbbackup directory.
In this case, under the /dbbackup directory, mongodump command will create a sub-directory for the database that it getting backed-up and all the collections will be backed-up under that sub-directory as shown below.
5. Now continue with Backup of a Remote Mongodb Instance as given below:
In all the previous example we run the mongodump command from the same server where the mongo database instance was running.
However, you can also associate to a mongodb instance running on a different server, and take a backup of that.
In the following shown example, the mongodump command is run on a server called “localhost”, but it connects to the mongodb instance running on 192.168.1.200 and takes the backup of that instance and stores it in the localhost.
II. Restore Mongo Database. How to restore MongoDB
After taking the backup of a MongoDB database using mongodump, you can reinstate it using mongorestore command. In case of a disaster where you lost your mongoDB database, you can run this command to restore the database. Or, you can just use this command to reinstate the database on a different server for testing purpose.
1. Restore All Database without Mongod Instance
If the backupyou have taken is without mongod instance, you can use this method to restore the same backup without running the mongod instance.
First of all, stop the mongod:
Next, navigate to the directory where the backup is located, and execute the restore command as executed below.
Note: In the above given command, the last parameter “dump” is the directory name where the backup is stored. In this example, since we did a “cd /backup”, before performing the mongorestore, and specified “dump” as the directory name, it will take the backup from /backup/dump directory, and reinstate it.
2. Now Restore a specific Database without Mongod Instance:
If you’ve backedup several mongodb database and like to reinstate only a specific database (instead of all the database), you can state the database that you like to restore using the –db argument as given below. The following example will reinstate only the mongodevdb.
3. Now drop the old Database before Restoring:
In the above two examples, mongorestore will perform a combination if it sees that the database earlier exists. If you don’t understand how the merge works, the above two re-installations will give you unexpected outcomes. As you see given below, it is giving a warning message for every collection that it is trying to reinstate, but is already present in the destination database.
If you want a clean restore, use the –drop option. If a collection that prevail in the backup also exist in the target database, mongorestore command will now drop that collection, and reinstate the one from the backup. In this example, as you see following given, it is dropping the objects before reinstating it.
4. Now restore/reinstate to a Remote Database:
In all the previous examples, we run the mongorestore command from the same server where the mongo database instance was running.
However you can also reinstate a mongo backup to a mongodb instance running on a different server.For example, the mongorestore command is run on a server called “localhost”, but it reinstates the mongo database backup located on the localhost to the mongodb instance running on 192.168.1.200 server
Refrence : http://docs.mongodb.org/manual/tutorial/backup-with-mongodump
How To Backup and Restore MongoDB administrator commands