MongoDB Restore

When deploying mongoDB on production environment, you should have a plan to keep backup of your data and also restore the same data when required. The mongoDB restore utility is called mongorestore. You can get this along with mongod and mongo utility.

The mongorestore program restore data from a backup files created by mongodump to a MongoDB database. mongorestore may create a new MongoDB database or add data to an existing database.

While restoring to an existing database, mongorestore won't update existing records. It means if it finds any _id matching on the target database, it never update that document.

  1. mongorestore recreate indexes captured by mongodump

  2. It inserts the data not update

  3. It doesn't wait for a response from the mongod

The following command will restore the database

$mongorestore --collection users --db local dump/users.bson #complete BSON file path


The above example read the data from the BSON file and restore it in the user collection of local database. In this case the MongoDB is running on local server having host as 127.0.0.1 on default port 27017.

In the following example, there is no need of active mongod instance attached to the directory.

$mongorestore --host hostName --port portNumber --username user --password pass backupFile

#!/bin/bash
###############################################
# Edit these to define source and destination

$MONGODUMP_BIN = "E:\Database\mongo\mongodb\bin\mongorestore"
$MONGODB_DATA = "E:\Database\mongo\mongodb\data"
$MONGO_BACKUP_PATH = "E:\Database\backup\dump\database-all-20141016044947.zip"
$UNZIP_EXE = "C:\Program Files\7-Zip\7z.exe"
$BACKUP_TMP = "E:\Database\backup\temp"
$MONGO_HOST = "127.0.0.1" #replace with your server ip
$MONGO_PORT = "27017"  #port number

###############################################

If(-Not (Test-Path $BACKUP_TMP)){
   New-Item -path $BACKUP_TMP -ItemType Directory
}

Get-ChildItem $MONGO_BACKUP_PATH | % {& $UNZIP_EXE "x" $_.fullname "-o$BACKUP_TMP"}


#$backupCommand = "$MONGODUMP_BIN --host $MONGO_HOST --port $MONGO_PORT $BACKUP_TMP"
$backupCommand = "$MONGODUMP_BIN --host $MONGO_HOST --port $MONGO_PORT --dbpath $MONGODB_DATA $BACKUP_TMP"
echo $backupCommand
Invoke-Expression $backupCommand

# Remove temp folder
Remove-Item $BACKUP_TMP -Force -Recurse


Descrption:

$MONGODUMP_BIN: The paths to mongoDB restore binary file.

$MONGODB_DATA: The paths to mongoDB restore database.

$MONGO_BACKUP_PATH : MongoDB backup file

$UNZIP_EXE: Unzip binary file path

$BACKUP_TMP: Temporary restore directory to keep the unzip files temporarily.

$MONGO_HOST: MongoDB server IP. The default local server IP is 127.0.0.1

$MONGO_PORT: The port number, mongoDB is running. The default port is 27017


You can create one power shell script and execute the same to restore the database from the archived BSON file.

Conclusion:

In this tutorial, I have explained how to restore backups of MongoDB. I will add more in my next articles.


Reference:

http://docs.mongodb.org/

Book: The Definitive Guide to MongoDB [Apress]


Discussion
Nine - 8 =
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Srikant Biswal

More from me...
Auto Increment in MongoDB
about 9 years ago 6018 Views
MongoDB Backup
about 10 years ago 3308 Views
MongoDB Data Model
about 10 years ago 3291 Views
MongoDB Read
about 10 years ago 3456 Views
Mongo DB crud operation
about 10 years ago 3729 Views