Google Drive - timmyomahony/craft-remote-backup GitHub Wiki

⚠️ Documentation has moved

The documentation for this plugin has moved to its own dedicated website: https://craft-plugins.timmyomahony.com/remote-backup. Please go there for up-to-date configuration guides.


Google Drive is one of the more difficult providers 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.

  1. Click "Credentials" and then "Create Credentials"

images/google-1.png

  1. Select "OAuth Client ID":

images/google-2.png

  1. 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
  1. Select "Web application":

images/google-3.png

images/google-4.png

  1. 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.

images/google-5.png

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

images/google-6.png

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:

images/Screenshot 2020-05-17 at 14.24.40.png

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":

images/Screenshot 2020-05-17 at 14.25.50.png

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

images/Screenshot 2020-05-17 at 14.28.39.png

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

images/Screenshot 2020-05-17 at 14.44.52.png

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.

images/Screenshot 2020-05-17 at 15.14.29.png

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:

images/Screenshot 2020-05-17 at 14.20.51.png

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, please leave an issue and we'll follow up asap.