Logo Mark

Backup Destinations

Google Drive

A guide on how to set up Google Drive as a cloud destination for Craft Remote Backup


Overview

Google Drive is one of the more difficult destinations to configure. You need to configure a new Google Cloud Console project (which may require setting up an account and adding payment details), then enable the "Drive API", then configure credentials and finally go through the OAuth flow to allow Remote Backup access to your Google Drive.

Setup

To use Google Drive, first make sure to install the official PHP SDK google/apiclient:^2.0:

composer require google/apiclient:"^2"

Setup Google Drive

Go the developer documentation for details on how to get access to the Drive API.

Follow this link in the Google Documentation to create a new project in the Google Cloud Console. Briefly, the steps are:

  1. Open the Google Cloud Console
  2. Click the top-left hamburger menu and go to "IAM & Admin > Create a Project."
  3. Click "Create".
  4. Give it a name. You can ignore the other fields.

Next enable the "Drive API".

  1. Click the top-left hamburger menu again and this time go to "APIs & Services > Library".
  2. Click "Enabled APIs & Services" in the left-hand menu, then "Enable APIs and Services" button up-top.
  3. Search for "Drive API" then click "Google Drive API"
  4. Click "Enable"

Next you need to configure credentials.

Click "Credentials" and then "Create Credentials"

Select "OAuth Client ID":

  • At this point, you might be asked to "configure consent screen"
    • Add an "App name", "User support email" and "Developer contact information"
    • You can skip the "Scopes" and "Summary" pages
    • Go back to the "Credentials" screen once created
  • Select "Web application":

Make sure to add the redirect URL. This should look like:

https://craft-4-plugins-test.ddev.site/admin/remote-backup/google-drive/auth-redirect

Where craft-4-plugins-test.ddev.site matches the hostname of the server you are backing up from. For example, if you want to use this in production, this should be domain to your production site.

Make sure to download the JSON file. It should look something like this:

Configure Remote Backup

We now need to add our credentials to Remote Backup. The easiest way to do this is to open the credentials.json that you downloaded from the previous step and add the values your project's .env file:

GOOGLE_DRIVE_CLIENT_ID="..."
GOOGLE_DRIVE_CLIENT_SECRET="..."
GOOGLE_DRIVE_REDIRECT_URL="https://craft-4-plugins-test.ddev.site/admin/remote-backup/google-drive/auth-redirect"
GOOGLE_DRIVE_PROJECT_NAME="..."
  • GOOGLE_DRIVE_CLIENT_ID is the client_id from the credentials.json
  • GOOGLE_DRIVE_CLIENT_SECRET is the client_secret
  • GOOGLE_DRIVE_CLIENT_SECRET is the project_id
  • GOOGLE_DRIVE_REDIRECT_URL is the URL that Google will redirect back to after OAuth is complete. The only change you should make here is the port. I'm using MAMP which runs all my local projects on 8888. The host and path must match the above example.

Now open the Remote Backup setting page and connect these environment variables:

See the "Folder Configuration" section below for more on the last setting.

Authenticate via OAuth

Click the "Authenticate with Google Drive" button down the bottom of the settings page. This will bring you to an another auth page. Hit "authenticate":

This will take you to Google to start the OAuth flow.

You should be returned to the /admin/remote-backup/google-drive/auth-redirect path which should display a success message:

In the background, a new token file has been saved to storage/remote-backup/google-drive-remote-backup-token.json which will be used to connect to Google Drive.

Folder Configuration

You can configure the folder within your Google Drive to send backups using the "Google Drive Folder ID" setting.

This can be:

  • a folder within your personal Google Drive
  • a team drive
  • a folder within a team drive

To configure this, you need to get the ID of the folder you want to use. The easiest way to do this is to log in Google Drive and navigate to the folder you want to use. Then grab the ID from the URL bar:


Troubleshooting

If you are encountering errors, first check your credentials. After that, have a look in storage/logs/web.log for errors. Usually these issues are related to credentials or folder paths. If you're still having trouble, see the troubleshooting section.

Previous
Backblaze B2