DEV Community


Posted on

Setting up a MongoDB backup on Google Drive

TL;DR: Script is on this gist

I have an app deployed on Digital Ocean that uses the stack Node.js + MongoDB, and when I first started setting this up (3~4 years ago) I had almost no idea about CI, CD, and managing "big-apps-stuff", so, for most of these problems, I've had to implement a solution from scratch, like a Github push listener that deploys my application on the cloud (more on that later).

Dealing with real data, one thing I had to worry about was backups, and outsourcing that to Digital Ocean was a bit expensive, so I had to project a solution.

These were my requirements:

  • It had to store the backups outside my cloud provider
  • It had to be run in a daily basis
  • A nice-have was to have the data compressed

After a bit of research, it was not hard to setup that solution. MongoDB already comes with a handy mongodump CLI that is easy configurable and also already supports gzip compression. After my first tests, my database backups generated by the tool were quite small. Its documentation is here.

The first solution I looked for regarding storage was Google Drive, since I had plenty of quota available on my account, and I found this great gdrive tool (install it here). One thing to notice here is that, for server-to-server applications like this where there's no human involved on the auth, you need to setup a Service Account, but it's not hard.

For running it on a daily basis, I've configured a simple cronjob. If you don't know how to, just read here

Before showing you my final shell script that glued all of this together, I must tell that I've also configured my solution to always have the last backup available on my cloud machine, just for redundancy.

So, here it is:

Feel free to reach me :)

Top comments (2)

djnitehawk profile image

i used to do a similar thing. was taking hourly dumps of my db. then one day my dedicated server just wouldn't boot up and i lost an hours worth of data. ever since then i use replication instead of dumps. i have a $2.50 p/m vps on vultr for the secondary. never looking back :-)

hafizjunaid971 profile image

please share complete step i will apply this line of code but no backup please help me in this regard