Skip to content

Instantly share code, notes, and snippets.

@perfectwebtech
Forked from baniol/mongodb_backup.md
Created November 10, 2020 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perfectwebtech/25c3e6a7a586bc4d2fbe75c6a6484dd5 to your computer and use it in GitHub Desktop.
Save perfectwebtech/25c3e6a7a586bc4d2fbe75c6a6484dd5 to your computer and use it in GitHub Desktop.
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 "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.

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>/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/

Restoring a backup

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment