Steps by step guide to take a backup of mongo db and push it to google drive using Ubuntu command line

in #hive-16352129 days ago

For the past few months I have been looking for some automated options to setup a backup service that would take a backup of my mongo databases and upload it to google drive. I was initially thinking of coding a solution using Nodejs by refering to some articles I found online. Till the part where we take the backup of mongo db it was all looking good. After that using googleapi to push the file to google drive was not very interesting.

I spent nearly 3 days exploring this and trying to automate this but then I gave up. Today I had a dire need again to backup my mongo databases because of a recent issue. I was about to encounter data loss, so I wanted to have a quick solution as soon as possible.

image.png

I started here where @forykw shared a way to take push hive-engine snapshots to google drive. With an intention to understand that, I started with the article and then started searching the internet to keep it simple and smooth. The below solution that I finally arrived at is not going to be an automated solution but it is simple and helped me.

Most of the articles I found online talks about using Gnome or other graphical user interfaces to install Google Drive on Ubuntu. I wanted a solution that would help me do it with command prompt.

Step 1: Installing google drive on Ubuntu

If we have to upload the backup files to google drive, there are two options available. Either to use googleapis and upload the file with the help of a script or to have drive installed in our ubuntu machine and use a few commands to backup the files. I took the second option. Installing Google Drive on Ubuntu is pretty straight forward by using the following commands.

sudo snap install drive

The above command will install all the necessary packages requried.

drive init ~/gdrive

After the installation is done, we can use the above command to initialize and mount the google drive on our local. If we run the above command, it will give us a link though which we can login to Google via our browser to get the authorization code.

image.png

After this is done, we have successfully connected our google account. There are a few help commands available to perform various operations. The complete list of help commands can be found by using the command drive -help. drive pull and drive push are the two commands that can be used to push the files and pull the files.

image.png

If the image is too small to read, you can right click and open it in new tab to view it bigger

So, it was pretty simple to install Google Drive on Ubuntu using command line interface and make use of the push and pull commands. Now let's check how we can backup mongo databases.

Step2: Backup mongo databases

If we have mongo installed in the server, we can use the commands mongodump and mongorestore to create a backup and restore a backup. If we have enabled accessing the mongo database instance from another server, we will also be able to take a backup from other severs as well.

This gives an advantage where if we take a backup from a different server than the original server, that itself will act as a secondary backup location to keep the data safe. If the main database server crashes, we will be able to restore it from the secondary server location. In additon to that, we are planning to push the backup to google drive as well.

In my case, I have created a location in my secondary server for backups with the help of the below commands.

mkdir backups
cd backups

I maintain all my backups in the above location and I have also mounted this directly location in Google drive as well.

mongodump --host=<ip address> --port=27017 -d=mydatabase --gzip --archive=/home/ubuntu/backups/mydatabase -u="adminuser" -p="mystrongpassword" --authenticationDatabase=admin

Above is a sample mongodump command that has arguments to connect with a host IP, port, username and password. If we had not enabled the mongo instance open to the internet, we will have to go through a ssh tunnel to access the instance and take a backup. To do that, we will have to write some extra code. I will cover that in a separate article. Right now the assumption is that the database server is password protected and is accessible from a different server in the internet.

The above command will take a backup of the specified database and put it in the backups folder we created above.

Step 3: Push the files to google drive

After the backup files are created inside the backups folder we created above. We can use the command drive push -files command to push the backup files to Google Drive.

I then logged in to the Google drive and I was able to see the files reflecting there.

Though this procedure is a manual task, I find it more comfortable than coding everything in a nodejs script. I guess now I after doing the first time configuration of connecting Google Drive with the backup location, I can have a simple script that can take a backup of the databases with the connection string provided and push it to Google drive as soon as and when there are changes. That would be my next step.



Posted with STEMGeeks

Sort:  

Congratulations @brethayden! You received a personal badge!

Happy Hive Birthday! You are on the Hive blockchain for 1 year!

You can view your badges on your board and compare yourself to others in the Ranking

Congratulations @brethayden! You received a personal badge!

Happy Hive Birthday! You are on the Hive blockchain for 2 years!

You can view your badges on your board and compare yourself to others in the Ranking

Congratulations @brethayden! You received a personal badge!

Happy Hive Birthday! You are on the Hive blockchain for 3 years!

You can view your badges on your board and compare yourself to others in the Ranking

Check out our last posts:

Our Hive Power Delegations to the October PUM Winners
Feedback from the November Hive Power Up Day
Hive Power Up Month Challenge - October 2024 Winners List