Skip to content

A simple script to automatically upload your database/media backups to google drive

Notifications You must be signed in to change notification settings

HesamKorki/backup-to-google-drive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backup to Google Drive

A simple script to automatically upload your database/media backups from your server directly to google drive

Register for Google Drive API

Follow these steps prior to implementing the code:

  1. Firstly, you need to create a project for this in your Google Cloud Platform

  2. Then, you should search for Google Drive API and enable it for your project

  3. Head to the API & Services tab and navigate to credentials. From there, try to create a credential: You want the Service Account since this API is going to get called only by server and not your end users

  4. Create a key from your service account detail page and download the credentials file in format of JSON and be careful not to lose nor expose it

  5. Rename the JSON file to service-credentials.json and put it in your working directory

Install Dependencies

Create a virtual environment for the project, I would recommend venv. Install the dependencies within the requirements.txt file. If you're on Windows, search for dealing with virtual environments in Windows but for Linux/MacOS this should work:

$ python3 -m venv env
$ source env/bin/activate
(env) $ pip install -r requirements.txt

Usage

you only need to execute the upload.py file:

(env) $ python update.py

This process can be concluded into 3 steps and it's OS independent:

  • Every file within the database/raw directory would be zipped to database directory and removed from raw directory

  • Each zip file within the database directory then would be uploaded to your Google service account Drive and moved to the uploaded directory with a name that represents the exact time of upload

  • Finally, the script would clean the uploaded directy, meaning, it would check if any files are uploaded more than 2 weeks ago. If so, it will delete those files to prevent the local storage from getting full

The best case of usage for this script is to run it on an automated schedule like a cronjob on your server. Also, you need another cronjob just before this to get a database backup from your database into the database/raw directory.

Configuration

You may configure this script by modifying the following in the config.py:

  • FOLDER_ID: You must set this value which is the folder id of the sharable folder that you created with your google drive account and shared it with the service account email address. You may extract the folder id from the URL of the folder.

  • CLEAN_TIME: It is the number of days before the uploaded directory files gets deleted and by default it is set to 14

About

A simple script to automatically upload your database/media backups to google drive

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages