Back up the database with the Google Laravel driver

Tram Ho

In this article we will learn how to back up the database on google driver with project laravel

Requirements

Install project laravel

Google Drive hosting

Mysql

Basic knowledge of PHP setting up packages in laravel.

Installation

We will start with installing the package spatie / laravel-backup into the laravel project.

Register service provider in config / app.php file.

Publish the config file.

Schedule to run backupdate database custome without sending a notification email.

Change the configuration of the app / backup.php file to use google driver.

Install Flysystem to use Google Drive:

Then in the newly created GoogleDriveServiceProvider.php file in the boot() we will modify the following to add google driver to the filesystem.

Register the GoogleDriveServiceProvider provider in config / app.php

Add the google driver storage to our config / filesystems.php:

Update your .env file:

Get Google credentials

Go to https://console.developers.google.com/ and create our new project.

Next we Library and search google driver and enable google driver api.

Next we will create OAuth client ID

Next we will select Web Application and name it. and need to add Authorized redirect URIs to create a private production key later and then create.

Add temporary https://developers.google.com/oauthplayground , because you will need to use it in the next step.

Now popup show window will have Your Client ID and Your Client Secret . Next let’s go to the page: https://developers.google.com/oauthplayground . Make sure the URL to your Authorized redirect URIs in the previous step is correct for your site. In the right corner of the screen click the icon settings and check, Use your own OAuth credentials and you paste the Client ID and Client Secret just obtained in the previous step. And add authorized applications to your google driver.

Then press the Authorize APIs button and accept the access. Next we will go to step 2. and check Auto-refresh the token before it expires ”and click“ Exchange authorization code for tokens ”.

Now we have the Refresh token. Next, based on the information we have just received, we update the .env

Update .env config

To get the backup directory id we access the directory then get the ID then update to .env

We will now perform the database backup job.

Run bacth backup

If there are errors you will edit the config in config / backup.php as follows and then run again.

Next we will check the results on the directory where we backed up the results. My article is here to meet you again in the next article:

References:

https://console.developers.google.com/

https://developers.google.com/oauthplayground/

https://laravel.com/docs/7.x

https://medium.com/@dennissmink/laravel-backup-database-to-your-google-drive-f4728a2b74bd

Share the news now

Source : Viblo