User Tools

Site Tools


wiki:manipulate_google_drive_cli

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
  • To create a service account and obtain its credentials, go to the Google Developer Console.
  • You must have a project - create one if you don't.
  • Then go to “IAM & admin” → “Service Accounts”.
  • Use the “Create Service Account” button. Fill in “Service account name” and “Service account ID” with something that identifies your client.
  • Select “Create And Continue”. Step 2 and 3 are optional.
  • These credentials are what rclone will use for authentication. If you ever need to remove access, press the “Delete service account key” button.
  • Click on created service account and create new json key which you will store for later in a local file e.g. rclone-sac.json

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

  • rclone-v1.66.0
  • Google services 2024-05

See also

References

wiki/manipulate_google_drive_cli.txt · Last modified: 2024/06/13 16:25 by antisa

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki