Automatically backup Google Drive folders to Cloud Storage

Stéphane Giron
3 min readJul 8, 2022

--

Few days ago, with the launch of French region ‘europe-west9’ in GCP, I made an apps script to backup a Drive folder to Cloud Storage. It is a cool script and works nicely but after some exchange, we can make it better.

So here I come back with onleebackup an open source code to backup multiple Google Drive folders to cloud storage with synchronisation.

Setup onleebackup

We assume you have completed the first installation and the creation of the Cloud Storage Bucket as detailed in the previous article (link).

#1 Enable Cloud Storage versioning

It is not mandatory but with this you will keep a version history of the backed up files. Go to your bucket and go to the tab “Protection”, click on “Object Versionning Off’. Follow the steps to activate the option.

Activate versioning in Google Cloud Storage

#2 Create Service Account

It is not mandatory and if you want to run the script with your credentials you can skip step #2 and #3. With Service Account you can have a centralized bucket and instead of working with user access to the bucket you can use the Service Account.

To create a service account go to : service account page, take care to be on the good project.

Once created click on the Service Account line. Go to ‘Keys’ tab and create a new key and select JSON. Save the file.

Service Account create JSON Key

#3 Last step: add rights to the Bucket for the Service Account

Go to the “Permissions” tab of your bucket and click “Add” to add a user and select these roles :

Start backuping your Drive folders to Cloud Storage

Get your copy of onleebackup

First get your copy of the script : https://onleebackup.ez34.net/copy

Click the “copy” icon at the top right to get your version of the script.

Setup onleebackup

There is just 4 parameters to define :

const FOLDER_ID = [‘FOLDER_ID_1’,’FOLDER_ID_2'];
const BUCKET_NAME = ‘bucket-name’;
const FORCE_UPLOAD = false; // Change to true to reupload all the folder.
const USE_SA = false; // You can use a service account to manage access to bucket

Bucket name is the one define in GCP Console for your project and FOLDER_ID is an array of folders ID.

If you want to force upload of all folders, set FORCE_UPLOAD to true. Could be useful if you want to reupload everything or if you change the bucket and the folder was already sunced.

USE_SA will have to be set to true if you want to use a service account.

Service account setup

Open the JSON file generated above and fill the varaible with your information on the page “Service_Account.gs”.

Run onleebackup

Run a first time the script to validate the scope, select in the dropdown the function “onleebackup” and click “Run”.

Once validated click “Run” again.

Schedule onleebackup for daily backup

To daily backup your files from Google Drive folders to Cloud Storage, select the function “createTrigger” in the dropdown and click “Run”.

Your backup wil lbe done each night between 5am and 6am.

What’s next ?

onleebackup is open source, so you can access the GitHub repository on this link : https://onleebackup.ez34.net/

If you have an issue or suggestion you can log an issue in the GitHub repository.

--

--