Laravel — Backup database to your Google Drive

Dennis Smink
Sep 17, 2018 · 5 min read

Backups are important, period. You will be needing these once your server or server provider suddenly dies. In this tutorial I will cover how to setup Google Drive backup, with Spatie’s backup package, Googles Flysystem and Laravel.

Image for post
Image for post


  • Laravel installation


We’ll start with installing Spatie’s backup package:

$ composer require spatie/laravel-backup

If you have auto-discover of or an older Laravel version, register the service provider your self in the config/app.php:

'providers' => [

Publish the config file:

$ php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"

Schedule it in commands kernel:

// Backups (to Google Drive)
$schedule->command('backup:run --only-db')->dailyAt('01:35');

Open up the app/backup.php and change the driver to use google’s driver:

'disks' => [

Install the Flysystem adapter for Google Drive:

$ composer require nao-pon/flysystem-google-drive:~1.1

Create a new service provider that provides the filesystem driver:

$ php artisan make:provider GoogleDriveServiceProvider

Then inside the boot() method add the google driver for the filesystem:

\Storage::extend('google', function ($app, $config) {
$client = new \Google_Client();
$service = new \Google_Service_Drive($client);
$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);
return new \League\Flysystem\Filesystem($adapter);

Register the GoogleDriveServiceProvider provider inside your config/app.php file.

Add the storage disk to your config/filesystems.php

return [

// ...

'disks' => [

// ...

'google' => [
'driver' => 'google',
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),

// ...


// ...

Update your .env file:

Get Google credentials

Getting these variables will require us to create an application in Google, this is not that hard.

Go to the google console:

We will first have to get a Client ID and client secret.

Start by creating a new project at the dropdown at the top:

Image for post
Image for post

After you enter a name, it takes a few seconds before the project is successfully created on the server.

Make sure you have the project selected at the top.

Then go to Library and click on “Drive API” under “Google Apps APIs”:

Image for post
Image for post

And then enable the Google Drive API.

Then, go to “Credentials” and click on the tab “OAuth Consent Screen”. Fill in a “Product name shown to users” and Save it. Don’t worry about the other fields, you can leave these blank as they are not required for this tutorial.

Image for post
Image for post

Then go back to Credentials, click the button that says “Create Credentials” and select “OAuth Client ID”.

Image for post
Image for post

Choose “Web Application” and give it a name.

Enter your “Authorized redirect URIs”, preferably your test URL ( and your production URL ( — or create a separate production key later.

Also add temporarily, because you will need to use that in the next step.

Image for post
Image for post

Click Create and take note of your Client ID and Client Secret.

Now head to

Make sure you added this URL to your Authorized redirect URIs in the previous step.

In the top right corner, click the settings icon, check “Use your own OAuth credentials” and paste your Client ID and Client Secret.

Image for post
Image for post

In step 1 on the left, scroll to “Drive API v3”, expand it and check each of the scopes.

Image for post
Image for post

Click “Authorize APIs” and allow access to your account when prompted.

When you get to step 2, check “Auto-refresh the token before it expires” and click “Exchange authorization code for tokens”.

Image for post
Image for post

When you get to step 3, click on step 2 again and you should see your refresh token.

Image for post
Image for post

Add this refresh token to your .env file under: GOOGLE_DRIVE_REFRESH_TOKEN

Getting the folder ID in Google Drive where to store too

If you want to store files in your Google Drive root directory, then the folder ID can be null. Else go into your Drive and create a folder.

Because Google Drive allows for duplicate names, it identifies each file and folder with a unique ID. If you open your folder, you will see the Folder ID in the URL.

Image for post
Image for post

You can add this URL in your .env file under the GOOGLE_DRIVE_FOLDER_ID variable. As said before, if you do not enter a ID it will store to the root.

Now lets check to see if it actually works, run this command to see if it worked:

backup:run --only-db

Thats it, you should now have automatic backups to your Google Drive in combination with the backup package from Spatie, and the Flysystem driver from Google. This will look like this:

Image for post
Image for post

Resolving possible errors

If you encounter an error “File not found” you might want to set the name to a empty string inside the backup configuration file.


'name' => env('GOOGLE_DRIVE_FOLDER_ID', ''),

Note: please bare in mind that I am a Dutch developer, English is not my native language so this article could contain any grammatical errors.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store