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 sync.sh 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
DIR=./backup

# Path to the target dropbox directory
TARGET_DIR=/

# Name of the database
DB_URI=$1
DB_NAME=test

# Name of the compressed file
FILE="${DB_NAME}_${NOW}.tar.gz"

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

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

  # 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
CMD="upload $DIR/$BASENAME"
HTTP_CODE=$(curl -X POST -sL -w "%{http_code}" --output /dev/null https://content.dropboxapi.com/2/files/upload \
--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)
fi
echo $CMD
echo "Response code => $HTTP_CODE"

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

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

.gihub/workflows/backup.yml

# Name of the action
name: MongoDB Backup

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

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "backup"
  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
    steps:
      # 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
      env:
        MONGO_URI: ${{ secrets.MONGO_URI }}
        DROPBOX_TOKEN: ${{ secrets.DROPBOX_ACCESS_TOKEN }}
      # 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/sync.sh
        ./scripts/sync.sh $MONGO_URI $DROPBOX_TOKEN

To upload the backup file to DropBox we need an API access token. After going to this URL dropbox.com/lp/developers 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.

1_YyLY9TVhKzTwSG4vPDTN7A.png

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

1_s3wHwkDgruFvpdufELQxjg.png

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.

1_PVWRX5n2-IrsHaidPWsn-g.png

My Github Repository: https://github.com/caffeines/mongo-backup

 
Share this