MongoDB Backup

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 backup utility is called mongodump. You can get this along with mongod and mongo utility.

The following command take a backup of all the databases.

$ mongodump


Backing up Single Database:

It might be possible to you have multiple applications are running on the same sever. You might need to back up each database individually, rather than all at once. You can do this by adding -d database name to the mongodump command.

Example:

$ mongodump - d Test


You may need backup of certain important collections/tables of a database on a certain interval. You can achieve this by adding some additional parameter -d database name and -c collection name.

Example:

$ mongodump -d Test -c users


The above command will create backup of the users collection of the Test database.

You can create backup of remote mongod instance using host, port, username and password.

Example: 

$mongodump --host Server IP or host name --port 3017 --username user --password pass --out dir name with file name


You may need to create and archieve the backup files and store the backup file on a specified directory.

Example:

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

[string[]] $MONGO_DBS = "all" #If need to keep backup of all files, replace the value with "all"
$BACKUP_TMP = "E:\Database\backup\tmp"
$BACKUP_DEST = "E:\Database\backup\dump"
$MONGODUMP_BIN = "E:\Database\mongo\mongodb\bin\mongodump"
$MONGO_HO = "127.0.0.1" #replace with your server ip
$MONGO_PORT = "27017"  #port number

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

$BACKUPFILE_DATE = Get-Date -format yyyyMMddHHmmss

# _do_backup <Database name>
function _do_backup($1) {
    $UNIQ_DIR = "$BACKUP_TMP\$1-$BACKUPFILE_DATE"
    #echo "`$1 = $1"
    #echo $UNIQ_DIR
    

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

    echo "dumping Mongo Database $1"
      
   
   if("all" -eq $1){
         #echo "$MONGODUMP_BIN --host $MONGO_HOST:$MONGO_PORT --out $UNIQ_DIR/dump"
         $backupCommand = "$MONGODUMP_BIN --host $($MONGO_HO):$MONGO_PORT --out $UNIQ_DIR"
         Invoke-Expression $backupCommand   
   }
   else {
        #echo "$MONGODUMP_BIN --host $MONGO_HO:$MONGO_PORT -d $1 --out $UNIQ_DIR\dump"
        $backupCommand = "$MONGODUMP_BIN -d $1 --host $($MONGO_HO):$MONGO_PORT --out $UNIQ_DIR"
        #echo $backupCommand
        Invoke-Expression $backupCommand
   }
   
 
   $KEY = "database-$1-$BACKUPFILE_DATE.zip"
   $zipFileName = "$zipPath\$KEY" #Zip file name with complete path
   $zipDirectory = "$UNIQ_DIR"    # Directory to create zip
   echo "Archiving Mongo database to $zipFileName"
   Write-Zip $zipDirectory $zipFileName

   # Move files to destination location
   $DEST_DIR = "$BACKUP_DEST"

   If(-Not (Test-Path $DEST_DIR)){
        New-Item -path $DEST_DIR -ItemType Directory
   }
   
   Move-Item -LiteralPath $zipFileName -Destination $DEST_DIR

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

# check to see if individual databases have been specified, otherwise backup the whole server to "all"
echo $MONGO_DBS.length

if ( 0 -eq "$MONGO_DBS.length" ){
    $MONGO_DB = "all"
    _do_backup $MONGO_DB
    
}
else{
    for ($i = 0; $i -lt $MONGO_DBS.length; $i++){
        _do_backup $MONGO_DBS[$i]
    }
}    
 

Description:

$MONGO_DBS: String array, can contain all the database names you want to take backup.

$BACKUP_TMP: Temporary backup directory to keep the created backups temporarily.

$BACKUP_DEST: The required directory, you want to keep all the backup files.

$MONGODUMP_BIN: The paths to mongoDB dump binary file.

$MONGO_HO: 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 from a schedule process to have backup on regular intervals.

Conclusion:

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

Reference:

http://docs.mongodb.org/

Book: The Definitive Guide to MongoDB [Apress]

Discussion
4 X 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 3 years ago 2413 Views
MongoDB Restore
about 3 years ago 1707 Views
MongoDB Data Model
about 4 years ago 1302 Views
MongoDB Read
about 4 years ago 1573 Views
Mongo DB crud operation
about 4 years ago 1864 Views