10/5/2014 - 7:44 AM

MongoDB automatic backup

MongoDB automatic backup

Maintaining even a small mongodb application in production requires regular backups of remotely stored data. MongoDB gives you three ways to acomplish it. In this post I'm using monogodump command for creating a backup and mongorestore for recreating the data. The purpose of this writing is to provide a simple way of periodic database dumps from a remote server to a Dropbox cloud storage.

Remember that for using mongodump you have to have a mongod process running.

Dumping a database

Suppose that you want make a backup of your books database.

To create a dump use mongodump -d books -o <destination_folder> which will result in a book folder containing bson files with all collections. For backup purposes we will compress it all to one file: tar -zcvf books.tar.gz books/.

Dropbox uploader

To send the backup of the database to your Drobpox cloud storage install dropbox uploader script on the remote server:

First, download the script:

curl "" -o

Change the permissions:

chmod +x

Launch the setup process:


The script will guide you through all necessary steps to connect the remote machine with your Dropbox account. During the installation process you will be asked to navigate to your Dropbox web page, create an application and providing app key and app secret for the download script.

After a successfull installation you can try out the connection uploading the books:

/root/downloads/ upload books.tar.gz /

The ending slash means that the file will be uploaded to the root directory of your Dropbox application.

The complete script for creating an archive and uploading, let's name it

#!/usr/bin/env bash

#Get current date
NOW="$(date +'%d-%m-%Y_%H-%M')"

# Settings:

# Path to a temporary directory

# Path to the target dropbox directory

# Path do file

# Name of the database

function mongodb_dump
  # Name of the compressed file

  # Dump the database
  mongodump -d $DB_NAME -o $DIR

  # Compress
  tar -zcvf $FILE $DIR$DB_NAME

  # Remove the temporary database dump directory
  rm -fr $DB_NAME

  # Upload the file

  # Remove the file
  rm $FILE


After running the scritp navigate to your Dropbox Applications folder and look for a folder named after the application you created during the installation process. The books.tar.gz file should be there already.

Setting a cronjob

You can have the script executed periodically by seting a cron job. To edit the crontab file responsible for registering new cron tasks run: crontab -e. To perform an action at 01.45 am add this line: 45 01 * * * <path to the script>/ Save the file and check the list of your cron tasks: crontab -l More about crontab:

Restoring a backup

To restore the data uncompress the file and run: mongorestore --drop -d <database-name> <directory-of-dumped-backup>