How to Backup MongoDB to Google Drive with Bash

Introduction

Google Drive is a free cloud storage service that allows users to save files and directories online. It allows users to share all stored documents, files, and photos with other users. It can be accessed from multiple remote devices such as mobiles, tablets, laptops, and PCs.

SnapShooter is invaluable and powerful backup software used to backup your data in a safe and secure location. If comes with a user-friendly and web-based control panel to manage and schedule all backup jobs.

This post will show you how to backup the MongoDB database to Google Drive manually and using SnapShooter.

Backup MongoDB Database to Google Drive Manually

In this section, we will show you how to backup the MongoDB database to Google Drive using gdrive utility.

Install and Configure Gdrive

You will need to install gdrive on the server from where you want to backup MongoDB databases to Google Drive. Gdrive is a command-line utility used for interacting with Google Drive. With gdrive, you can manage, upload, download, delete and share files on Google Drive via the command line.

First, install the required dependencies with the following command:

apt-get install musl gnupg2 wget -y

Next, download the latest version of gdrive from the GitHub using the following command:

wget https://github.com/prasmussen/gdrive/releases/download/2.1.1/gdrive_2.1.1_linux_amd64.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf gdrive_2.1.1_linux_amd64.tar.gz

Next, install the gdrive to the system path using the following command:

sudo install gdrive /usr/bin/gdrive

Next, you will need to configure gdrive to access Google Drive. You can do it by running the following command:

gdrive about

You will get the following URL to authenticate with your Google account:

Authentication needed
Go to the following url in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state

Now, open your web browser and access the above URL. You will get the following page:

Click on the Allow button. You should see the Authorization Code on the following page:

Copy the authorization code from the above page and paste it into your command-line interface:

Enter verification code: 4/1AX4XfsfdsgdgdstRgB_BJsdgdhdhhDmb0TxwbVwbTM49D8aiaeG4nFlz6ILQgzUO18

Once gdrive is connected with your Google Drive, you will get the following output:

User: Hitesh Jethva, hitjethva75@gmail.com
Used: 550.7 KB
Free: 16.1 GB
Total: 16.1 GB
Max upload size: 5.2 TB

You can verify the gdrive version using the following command:

gdrive version

You will get the following output:

gdrive: 2.1.1
Golang: go1.11.13
OS/Arch: linux/amd64

Use Gdrive to Backup MongoDB Database to Google Drive

At this point, gdrive is configured to manage Google Drive. You can list all available files on your Google Drive using the following command:

gdrive list

You will get the following output:

Id Name Type Size Created
1CUc3AmOVY9V4cJkNNE0ZVvdb-JOtZsnJ Article dir 2022-02-08 12:49:10

Next, create a MongoDB database dump for the admin database using the following command:

mkdir backup
mongodump --host localhost --port 27017 --db admin --authenticationDatabase admin --username admin --password password --out backup

Next, compress the backup directory using the tar command:

tar -cvzf backup.tar.gz backup

Next, upload the backup.tar.gz dump file to the Google Drive using the following command:

gdrive upload backup.tar.gz

You will get the following output:

Uploading backup.tar.gz
Uploaded 1Btotct2BPWSHQeDB0pBMJ2U7iprrzDmV at 352.4 KB/s, total 352.4 KB

Next, run the following command to verify the uploaded file:

gdrive list

You will get the following output:

Id Name Type Size Created
1Btotct2BPWSHQeDB0pBMJ2U7iprrzDmV backup.tar.gz bin 352.4 KB 2022-04-13 06:57:22
1CUc3AmOVY9V4cJkNNE0ZVvdb-JOtZsnJ Article dir 2022-02-08 12:49:10

You can also access your Google Drive via a web browser to see your uploaded backup file as shown below:

Scheduled MongoDB (Single Databases) Backups SnapShooter

Backup a single MongoDB database to your external storage

Learn more about MongoDB (Single Databases) Backups

Get started for free
No credit card required. Cancel anytime!


Was this page helpful?

Thank you for helping us improve!