Automate Google Drive Backups with Python
Automating a day-to-day task with Python

Many times we find ourselves in a position where we have to take daily backup of a folder in cloud, this article will help you to achieve the same. Without using any fancy-schmancy software we are going to implement this in simple Python. So what will this program do? It will-
- Trigger daily at 12:00 am and create a backup(zip) of a folder which is to be uploaded.
- Rename the zip with a timestamp.
- Upload this zip file to the google drive of a user.
Flow of this article:
- Prerequisites
- Setup the google drive API
- Start coding
Prerequisites
- You will need to have python3 installed in your computer, if it is not then install it from here.
- A good IDE such as VSCode, Pycharm or any one of your choice.
- Internet connection.
These are the basic requirements for creating and executing the program.
Setup the Google drive API
There are 2 ways to get the google drive API client secret. First is where you log on to GCP console, create a new project, search for Drive API, enable it and add OAuth ID to the current project. Thankfully there is a way to avoid this long process and get straight to the client secret, let’s see how!
- Visit the Drive API Quickstart guide from here.
- Now you will get a screen like this, log in to your google account if you haven’t.

3. Now click on Enable the Drive API button, after doing so you must get something like this

Enter a project name that suits you, here I have entered GDrive-backup. After entering the name click on NEXT.
4. On the next screen, select Web Server from drop down and enter https://localhost:8080/ as the redirection URL. Click on CREATE.

5. With this, we are done setting up our google drive API and now to complete this process click on DOWNLOAD CLIENT CONFIGURATION button to download the client secrets JSON file.

Rename this file to client_secrets.json and move it to the working directory of this project.
Coding
Now that every thing is ready from our Python environment to the Google Drive API key, we are now ready to write a program to generate daily backups on the cloud.
To contact the drive API we are going to use the PyDrive library and to perform daily execution we will use schedule library. So go ahead and install them by running the following line on your terminal.
pip3 install PyDrive schedule
After doing this open up a new file in the project directory and name it to automate_backup.py this is going to be our driver file.
First, we will write a function that creates a zip file of our backup folder and save it with a custom name. All zips will be store in archive folder, this also helps in maintaining a local backup. If zip creation fails it will return False.
Secondly, we will write a function to start authentication with google drive API. As this function is called you default browser will open and ask for user permissions to access your drive contents. Make sure that the client_secrets.json file is in same directory as our Python file and keep the json file a secret make sure it does not leak online.
Next, it is time to write a function that will take a file name and path of the zip file and upload it to google drive.
The only thing remaining now is to put all these functions together and watch it in action. For this we will create a controller function that will be called as soon as our Python file is executed.
And finish it of by setting up a scheduler that will call controller() everyday at 12:00 am.
For testing purposes, you can remove the scheduler part and just run controller to see if backup is taking place.
And that’s it! You have implemented a daily backup system and need not worry about backing up manually anymore:)
Resources
Further reading
Thank you for reading and don’t forget to leave your thoughts about this article in comments✌