Laravel 8 Backup Store On Google Drive

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

27224

17-12-2020


Laravel 8 Backup Store On Google Drive

Hello Friends,

Now let's see example of how to project backup store on google drive in laravel 8 application. We will talk about how to integrate google drive apis in laravel 8 app and store backup on google drive.

In this tutorial, We will learn laravel 8 store backup on google drive example. Google drive integration is easy in laravel 8 app. And this is because of the spatie/laravel-backup package. If you do not know how to google drive get client id and secret of google developer console, then you can create Google App in Google Developer Console by following the steps given below.

Here i will give you full example for laravel 8 store backup on google drive. So let's follow bellow example step by step.

Step 1 : Create Project


In this step, you can visit Google Developer Console. And create a new project as following in below picture:

Step 2 : Enable Google Drive API

In this step, you can visit to “Library” of google search console and search for “Google Drive API“:

Step 3 : OAuth Consent Screen

Now, Visit “OAuth Consent Screen“. Here you will get options to select user type, select “External” option, and click the “CREATE” button.

Step 4 : Fill The Information

In this step, You can fill the following details like “App Name“, “User support email” and “Email” in the Developer contact information and click Save And Continue button. For other forms “Scopes“, “Test Users” and “Summary” click on Save And Continue button and go to the dashboard. Don’t worry about the other fields.

Step 5 : Fill The Information

Now. Click On the OAuth consent screen, you will see that the App’s Publishing status is testing. Here we need to publish the App. So, click the Publish App button and confirm.

Step 6 : Create Credentials

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

Step 7 : Fill the Credentials Detail

In this step, you can fill the bellow details like application type and name. Let's fill the inforamtion.

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

Step 8 : Authorize Google Drive

Now click here https://developers.google.com/oauthplayground fill the bellow inforamtion.

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

Step 9 : Google Drive Api v3

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

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

Step 10 : Google Drive Api v3

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

Step 11 : Google Drive Api v3

Now, complete this step and click on step 1 again and you should see your refresh token.

Step 12 : Create Folder Google Drive

Now you need to folder ID that is optional. If you set folder ID to null, in that case, it is going to store backup files in the root path of Google Drive.

You can get the folder ID from the URL, as shown in the below image.

Now, you have got Google Client ID, CLIENT SECRET, REFRESH TOKEN, and FOLDER ID. Please save it in any text file. Because you need to update these values in the .env file of your laravel 8 app.

Step 1: Install Laravel Project

First, you need to download the laravel fresh setup. Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog

Step 2: Setup Database

After successfully install laravel 8 Application, Go to your project .env file and set up database credential and move next step :

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=here your database name

DB_USERNAME=here database username

DB_PASSWORD=here database password

Step 3 : Install spatie/laravel-backup

In this step, we need to install Install spatie/laravel-backup laravel package in our application. so let's open terminal and run bellow command:

composer require spatie/laravel-backup

Successfully install package then execute the following command on terminal to publish this installed package:

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

It will publish the configuration file in config/backup.php. Now, configure your backup according to your requirement.

Now, you need to add google in the disk option in the config/backup.php.

config/backup.php

'backup' => [

'name' => '',

],

'disks' => [

'google',

'local',

],

Step 4 : Setup Google Drive as Filesystem

In this step, you need to execute the following command on terminal to install a Filesystem adapter for Google drive. So, run the following command in your terminal:

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

php artisan make:provider GoogleDriveServiceProvider

Then, inside the boot() method add the Google driver for the Laravel filesystem:

app/Providers/GoogleDriveServiceProvider.php

public function boot()

{

\Storage::extend('google', function ($app, $config) {

$client = new \Google_Client();

$client->setClientId($config['clientId']);

$client->setClientSecret($config['clientSecret']);

$client->refreshToken($config['refreshToken']);

$service = new \Google_Service_Drive($client);

$adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $config['folderId']);

return new \League\Flysystem\Filesystem($adapter);

});

}

After this, register the service provider by adding the following line in the providers array of config/app.php.

config/app.php

'providers' => [

// ...

App\Providers\GoogleDriveServiceProvider::class,

];

Step 5 : Configure Google Drive Details

In this step, configure Google drive app with this laravel app. So, open your laravel 8 project in any text editor. Then navigate the config directory and open filesystem.php file and add the client id, secret and callback url:

config/filesystem.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'),

],

// ...

],

];

And also you need to update .env file of laravel 8 app. In this environment file you need to add the following Google drive credentials:

Step 6 : Setup env

In this step you can add client id and secret and token and folder id in env file so let's open env file and put the bellow code.

.env

GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com

GOOGLE_DRIVE_CLIENT_SECRET=xxx

GOOGLE_DRIVE_REFRESH_TOKEN=xxx

GOOGLE_DRIVE_FOLDER_ID=null

Step 7 : Run Backup Command

In this step, you can open terminal and run bellow command:

php artisan backup:run

I hope It will help you....


Recommended Posts