In this tutorial you will learn about the Laravel 8 Backup Store On Google Drive Example and its application with practical example.
In this Laravel 8 Backup Store On Google Drive Example I will show you how to take laravel site backup in google drive. In this tutorial I will also show you how to integrate google drive in laravel 8 to store backup on google drive. In this example we will learn Google Drive integration in Laravel 8. In this example we will be using spatie/laravel-backup package to take backup in google drive.
Laravel 8 Backup Store On Google Drive Example
In this step by step Laravel 8 Backup Store On Google Drive Example I will demonstrate you how to integrate google drive in laravel site to take site backup. Please follow instructions given below:
- Step 1 – Setup Google Drive Account
- Step 2 – Install Laravel 8 App
- Step 3 – Connecting App to Database
- Step 4 – Install spatie/laravel-backup
- Step 5 – Setup Google Drive as Filesystem in Laravel
- Step 6 – Configure Google Drive Details
- Step 7 – Execute Backup Command
Step 1 – Setup Google Drive Account
Before integrating Google Drive in laravel 8. that you have google client id and secret id, refresh token and folder id. Please follow the instruction given below. You can create Google App in Google Developer Console by following the steps given below.
Step 1 – Visit the link given below and create a new project:
1 |
https://console.developers.google.com/ |
Step 2 – Visit “Library” of google search console and search for “Google Drive API“:
Step 3 – Then enable the Google Drive API.
Step 4 – Now, Visit “Credentials” and click on the tab “OAuth Consent Screen”. Here you will get options to select user type, select “External” option, and click the “CREATE” button.
Step 5 – Please provide 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 6 – Now, on OAuth consent screen, you will see that the App’s Publishing status is testing. Click the Publish App button and confirm.
Step 7 – Now, visit Credentials and click the button that says “Create Credentials” and select “OAuth Client ID“.
Step 8 – For “Application type” choose “Web Application” and give it a name. You also need to provide your “Authorized redirect URIs”.
Step 9 – Now visit the below link:
1 |
https://developers.google.com/oauthplayground |
In the top right corner, click the settings icon, check “Use your own OAuth credentials” and paste your Client ID and Client Secret.
Step 10 – 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 11 – You also need to check “Auto-refresh the token before it expires” and click “Exchange authorization code for tokens“.
Step 12 – Now, complete this step and click on step 1 again and you should see your refresh token.
Step 13 – Now you need 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.
Step 2 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 3 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara8blog DB_USERNAME=root DB_PASSWORD= |
Step 4 – Install spatie/laravel-backup
In this step run the following command to install spatie/laravel-backup package in laravel 8:
1 |
composer require spatie/laravel-backup |
Now, run the following command to publish this installed package:
1 |
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" |
Now, you need to add google
in the disk option in the config/backup.php
.
1 2 3 4 |
'disks' => [ 'google', 'local', ], |
Step 5 – Setup Google Drive as Filesystem in Laravel
Now, you need to run the following command to install a Filesystem adapter for Google drive:
1 |
composer require nao-pon/flysystem-google-drive:~1.1 |
1 |
php artisan make:provider GoogleDriveServiceProvider |
Now, inside the boot()
method add the Google driver for the Laravel filesystem:
app/Providers/GoogleDriveServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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); }); } |
Now, you need to register the service provider toproviders
array of config/app.php
.
1 2 3 4 |
'providers' => [ // ... App\Providers\GoogleDriveServiceProvider::class, ]; |
Step 6 – Configure Google Drive Details
In this step, you need to configure Google drive app with this laravel app. Go to the config directory and open filesystem.php file and add the client id, secret and callback url:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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'), ], // ... ], // ... ]; |
You also you need to update .env
file of laravel 8 app. Please add the following Google drive credentials:
1 2 3 4 |
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 – Execute Backup Command
In this step we are ready to take backup run the following command to check the backup file is created or not:
1 |
php artisan backup:run |