Tech Tac

Tech Tac

Automatically Backup a MongoDB Database to DropBox using Github action

Automatically Backup a MongoDB Database to DropBox using Github action

Abu Sadat Md. Sayem's photo
Abu Sadat Md. Sayem
·Aug 7, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

A backup of a database is a way of protecting and restoring the data. We will see how we can take a backup from MongoDB and store the backup to dropbox. First of all, we shall create a file named in /scripts directory. Then we shall write our shell script to take backup and upload it to DropBox.

#!/usr/bin/env bash

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

# Path to a temporary directory

# Path to the target dropbox directory

# Name of the database

# Name of the compressed file

function mongodb_dump
  # Dump the database
  mongodump --uri $DB_URI -o $DIR

  if [ $? -eq 0 ]
    echo "✅ MongoDB dump successful"
    echo "🔴 MongoDB dump failed"
    exit 1

  # Compress
  tar -zcvf $FILE $DIR

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

mongodb_dump # mongodb_dump function call 

readonly TOKEN=$2
readonly DIR=
BASENAME=$(basename $FILE)
if [ -f "$FILE" ]; then
# upload file to dropbox
HTTP_CODE=$(curl -X POST -sL -w "%{http_code}" --output /dev/null \
--header "Authorization: Bearer $TOKEN" \
--header "Dropbox-API-Arg: {\"path\": \"$DIR/$BASENAME\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @$FILE)
echo $CMD
echo "Response code => $HTTP_CODE"

if [ $HTTP_CODE != "200" ]; then
  echo "🔴 Backup failed"
  exit 1
  echo "✅ Backup successful"
  exit 0

Now it’s time to set up GitHub action. We have to create a .gihub/workflows directory in our repository, to set up our GitHub action to automatically backup our database we have to create a .yml file as well. We have created backup.yml


# Name of the action
name: MongoDB Backup

# Controls when the action will run. 
  # Triggers the workflow on cron schedule
    - cron: '0 0 * * *' # at UTC 00:00
  # Allows you to run this workflow manually from the Actions tab

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
  # This workflow contains a single job called "backup"
    name: 'MongoDB Backup'
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      uses: actions/checkout@v2

    - shell: bash
      # Populates env variables from github secrets
        MONGO_URI: ${{ secrets.MONGO_URI }}
      # Runs a set of commands using the runners shell
      run: |
        sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
        mkdir swiftex_backup
        sudo chmod +x ./scripts/
        ./scripts/ $MONGO_URI $DROPBOX_TOKEN

To upload the backup file to DropBox we need an API access token. After going to this URL click the Create apps button

1_vFJAk6G3nQC6uoKl9v6-iA.png Then check Scoped access, App folder, and input your app name.

1_hn7ln2g-gzuXW4phX-q9EQ.png After creating the app add permissions and don’t forget to click submit button.

1_xMb7qAhEb4NE9LsVPLZxaA.png Before generating a token change Access token expiration is Short-lived to No expiration.


Copy this access token and MongoDB URI, then add it to GitHub secrets.{username}/{repository}/settings/secrets/actions


Yay! We have done our setup. After pushing to the GitHub default branch, If everything is fine we should get the backup on UTC 00:00 or by running this workflow manually from the Actions tab.


My Github Repository:

Share this