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 amongod
process running.
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/
.
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 "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
Change the permissions:
chmod +x dropbox_uploader.sh
Launch the setup process:
./dropbox_uploader.sh
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/dropbox_uploader.sh 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 mongodb_upload.sh
:
#!/usr/bin/env bash
#Get current date
NOW="$(date +'%d-%m-%Y_%H-%M')"
# Settings:
# Path to a temporary directory
DIR=~/mongodb_dump/
# Path to the target dropbox directory
TARGET_DIR=/
# Path do dropbox_uploader.sh file
UPLOADER_SCRIPT=/root/scripts/dropbox_uploader.sh
# Name of the database
DB_NAME=books
function mongodb_dump
{
# Name of the compressed file
FILE="${DIR}${DB_NAME}_${NOW}.tar.gz"
# 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
$UPLOADER_SCRIPT upload $FILE $TARGET_DIR
# Remove the file
rm $FILE
}
mongodb_dump
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.
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>/mongo_upload.sh
Save the file and check the list of your cron tasks: crontab -l
More about crontab: http://v1.corenominal.org/howto-setup-a-crontab-file/
To restore the data uncompress the file and run:
mongorestore --drop -d <database-name> <directory-of-dumped-backup>
If your database contains sensitive data, you should consider to encrypt the dump file with gpg or openssl as an additional step, before uploading it to some "cloud" space.