Table of Contents

, , ,

Manipulate Google drive from CLI

We will use rclone tool to duplicate folders in this example on a shared Google drive.

First download the standalone binary e.g.

wget https://downloads.rclone.org/v1.66.0/rclone-v1.66.0-linux-amd64.zip
unzip rclone-v1.66.0-linux-amd64.zip

Using simple login

Use this method when you need to manipulate your own drive and you can authenticate via browser e.g. on your local machine. Simply follow instructions from here

Using Google service accounts

This is useful when you need to run the rclone tool from some other server which doesn't have web browser installed to authenticate and you have paid Google Workspace account and when you want to synchronise files onto machines that don't have actively logged-in users, for example build machines.

Instructions are adapted from here.

1. Create service account

2. Enable the Google Drive API

Go to https://console.developers.google.com/apis/api/drive.googleapis.com/overview and enable the API. (under Google Console > APIs & Services)

3. Allow API access to example.com Google Drive

Contact your Google Workspace admin and allow access for the service account's Client ID howto here

4. Create new config and test

After that is done follow steps 3 and 4 to make sure it works

Use Google OAuth account

This is another way to authorize the rclone. Just follow the steps here. Either way should work.

Some screenshots

Creating new OAuth client id 1 Creating new OAuth client id 2 Choose Internal if you have a Google Workspace account Selecting scopes Both auth methods active

Example commands

Show help:

./rclone -h

List existing remotes:

./rclone listremotes

Copy folder inside the my_remote using service account method. This reads the secrets from downloaded json;

./rclone -v --drive-impersonate me@gmail.com copy 'my_remote:folder1' 'my_remote:folder1_copy'

Copy folder inside the my_remote using Google OAuth account. The client's id's and secret is also obtained from the json you get from finishing the OAuth steps and can be used on cmd line.

./rclone -v --drive-impersonate me@gmail.com --drive-client-id 'xxxxxxx194eq.apps.googleusercontent.com' --drive-client-secret 'xxxx' copy 'my_remote:folder1' 'my_remote:folder1_copy'

Tested on

See also

References