Press "Enter" to skip to content

Accessing Google Drive from Linux CLI

bist 0

Working with Linux and Docker offers many benefits, but uploading persistent Docker volume backups to Google Drive can be a challenging task.

Installing gdrive on Linux using the tool provided at https://github.com/glotlabs/gdrive is a simple process that can greatly enhance your ability to interact with Google Drive. Before we dive into the installation process, it is important to note that you must first set up the Google Drive API.

Google has made it really easy to create api credentials for own use, just follow these 50 steps……:

  1. Go to Google Cloud Console
  2. Create a new project (or select an existing) from the menu 
  3. Search for drive api in the search bar and select Google drive api under the marketplace section 
  4. Click to enable Google Drive API button 
  5. Click on the Credentials menu item
  6. Click on the Configure Consent Screen button 
  7. Select External user type (Internal is only available for workspace subscribers) 
  8. Click on the Create button
  9. Fill out the fields App nameUser support emailDeveloper contact information with your information; you will need to put the Project ID into the app name (keep the other fields empty) 
  10. Click the Save and continue button. If you get An error saving your app has occurred try changing the project name to something unique
  11. Click the Add or remove scopes button
  12. Search for google drive api
  13. Select the scopes .../auth/drive and .../auth/drive.metadata.readonly 
  14. Click the Update button
  15. Click the Save and continue button 
  16. Click the Add users button
  17. Add the email of the user you will use with gdrive 
  18. Click the Add button until the sidebar disappears
  19. Click the Save and continue button 
  20. Click on the Credentials menu item again
  21. Click on the Create credentials button in the top bar and select OAuth client ID 
  22. Select application type Desktop app and give a name 
  23. Click on the Create button
  24. You should be presented with a Cliend Id and Client Secret 
  25. Click on OAuth consent screen
  26. Click on Publish app (to prevent the token from expiring after 7 days) 
  27. Click Confirm in the dialog

Thats it!

Install binary

  • Download the latest binary from the release section
  • Unpack and put the binary somewhere in your PATH (i.e. /usr/local/bin on linux and macos)
  • Note that the binary is not code signed and will cause a warning on windows and macos when running. This will be fixed later, but for now you can find a workaround via you favorite search engine.

Add google account to gdrive

  • Run gdrive account add
  • This will prompt you for your google Client ID and Client Secret
  • Next you will be presented with an url
  • Follow the url and give approval for gdrive to access your Drive
  • You will be redirected to http://localhost:8085 (gdrive starts a temporary web server) which completes the setup
  • Gdrive is now ready to use!

Using gdrive on a remote server

Part of the flow for adding an account to gdrive requires your web browser to access localhost:8085 on the machine that runs gdrive. This makes it tricky to set up accounts on remote servers. The suggested workaround is to add the account on your local machine and import it on the remote server:

  1. [local] Run gdrive account add
  2. [local] Run gdrive account export <ACCOUNT_NAME>
  3. [local] Copy the exported archive to the remote server
  4. [remote] Run gdrive account import <ARCHIVE_PATH>

Credentials

Gdrive saves your account credentials and tokens under $HOME/.config/gdrive3/. You don’t usually need to use these files directly, but if someone gets access to them, they will also be able to access your Google Drive. Keep them safe.

Gdrive on virtual machines in the cloud

There are some issues communicating with the Drive API from certain cloud providers. For example on an AWS instance the api returns a lot of 429 Too Many Requests / 503 Service Unavailable / 502 Bad Gateway errors while uploading. While the same file uploads without any errors from a Linode instance. Gdrive has retry logic built in for these errors, but it can slow down the upload significantly. To check if you are affected by these errors you can run the upload command with these flags: --print-chunk-errors --print-chunk-info.

Leave a Reply

Your email address will not be published. Required fields are marked *