Laravel 8 Backup Store On Google Drive - Yudhy Network

Share on:

Yudhy Network - Laravel 8 store backup on google drive. In this tutorial, you will learn how to take backup and store on google drive in laravel 8 app.

Laravel 8 store backup on google drive example. In this tutorial, you will learn how to integrate google drive apis in laravel 8 app and store backup on google drive.

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.

Step 1 – Visit Google Developer Console. And create a new project as following in below picture:

Step 2 – At that point visit to β€œLibrary” of google search console and search for β€œGoogle Drive APIβ€œ:

Step 3 – Then enable the Google Drive API.

Step 4 – Then, 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.

Recommended:- Laravel 8 Google Recaptcha V3 Example

Step 5 – Now fill the form with 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 – 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 7 – Now, Visit back to Credentials, 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.

Enter your β€œAuthorized redirect URIs”, your production URL (https://tutsmake.com) β€” or create a separate production key later

Also add https://developers.google.com/oauthplayground temporarily, because you will need to use that in the next step.

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

Recommended:- Laravel 8 Google Chart Tutorial Example

Step 9 – Now head to 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 – When you get to step 10, 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.

Recommended:- Laravel 8 Socialite Google Login Example Tutorial

Step 13 – 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 IDCLIENT SECRETREFRESH 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.

Laravel 8 Store Backup On Google Drive Tutorial

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Install spatie/laravel-backup
  • Step 4 – Setup Google Drive as Filesystem in Laravel
  • Step 5 – Configure Google Drive Details
  • Step 6 – Execute Backup Command
  • Step 7 – Conclusion

Step 1 – Install Laravel 8 App

In this step, open your terminal and navigate to local web server directory. Then type the following command on terminal to download laravel 8 app:

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

Step 2 – Connecting App to Database

In this step, navigate to root directory of download laravel app. And open .env file. Then configure database details like following:

 DB_CONNECTION=mysql 
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
Recommended:- Laravel 8 Google Autocomplete Address Tutorial

Step 3 – Install spatie/laravel-backup

In this step, execute the following command on terminal to install spatie/laravel-backup package in laravel 8 app:

composer require spatie/laravel-backup

Then execute the following command on terminal to publish this installed package:

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

Note that, 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.

'disks' => [
    'google',                
    'local',             
],

Step 4 – Setup Google Drive as Filesystem in Laravel

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);
    });
}
Recommended:- Laravel 8 Dynamic Google Pie Charts Example

After this, register the service provider by adding the following line in the providers array of 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:

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'),
        ],
        
        // ...
        
    ],
    
    // ...
];
Recommended:- Laravel 8 Joins Example Tutorial

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:

GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=null

Step 6 – Execute Backup Command

In this step, open your terminal and execute the following command to check the backup file is created or not:

php artisan backup:run

Step 7 – Conclusion

Laravel 8 store backup on google drive example tutorial, you have learned how to store laravel 8 app backup on google drive using spatie/

Recommended Laravel Posts

Recommended:- Laravel 8 Push Notification to Android and IOS Tutorial
Recommended:- Laravel 8 Send Email with PDF Attachment Tutorial
Recommended:- Laravel 8 Ajax CRUD Using Datatable Tutorial
Recommended:- Laravel 8 Auto Load More Data On Page Scroll
Recommended:- Laravel Get Record Last Week, Last 3, 6 Month, 15 Days, 30 Days, Year
Recommended:- Laravel 8 Dynamic Dependent Dropdown using Ajax
Recommended:- Laravel Get Next / Previous Record and Url
Recommended:- Laravel 8 Livewire Load More OnScroll Tutorial Example
Recommended:- Laravel 8 Image Upload Tutorial
Recommended:- Laravel 8 Ajax Image Upload with Preview Tutorial
Recommended:- Laravel 8 Ajax Multiple Image Upload Tutorial
Recommended:- Laravel 8 Livewire File Upload Tutorial Example
Recommended:- Laravel 8 PHP Guzzle Http Client GET & POST Example
Recommended:- Laravel 8 Livewire Datatables Tutorial Example
Recommended:- Laravel 8 Rest API CRUD with Passport Auth Tutorial

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus