Backup Laravel 9 App to Google Drive

Digamber March 01, 2023
backup-laravel-9-app-to-google-drive

Backup Laravel 9 App to Google Drive tutorial will teach you how to save Laravel 9 application folder backup in Google Drive cloud storage using the Laravel Spatie backup package.

Google Drive is an online synchronization service that helps store important files, images, and documents in the cloud.

Ideally, to access Google Drive, you install the Drive app and access it using a Gmail account. It shows sync status and grants file and document access from any device.

To store Laravel backup in Google, we will create Laravel as a Google Drive Filesystem. In order to take Laravel application backup in Google Drive, we will learn how to get client id, secret, refresh token, and folder id.

To implement Google drive in Laravel 9 app, we will install the spatie laravel backup library. The Laravel backup package helps you set up Google drive as Filesystem in Laravel. Resulting in securing not just your important files but a whole laravel project.

Build Laravel App

The first requirement is to have Composer in our system. It is an application-level dependency manager exclusively built for PHP-based web app development. It will allow us to build a new Laravel project and make sure to invoke the given command.

composer create-project laravel/laravel --prefer-dist laravel-google-drive-example

Connect to Database

Now, we are going to connect laravel to the MySQL database. Look for the .env file; this file helps to establish a database connection using database credentials.

DB_CONNECTION='mysql'
DB_HOST='127.0.0.1'
DB_PORT=3306
DB_DATABASE='db_name'
DB_USERNAME='root'
DB_PASSWORD='your password'

If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Get Google Client Id, Secret, Refresh Token and Folder Id

In order to add Google drive in Laravel, we need to have Google Authentication API, Google Drive Service enabled in the developer console, Refresh token and Google drive folder id.

Install Spatie Laravel Backup Package

This step brings us to do the most important thing that is to install the laravel-backup library. Run the suggested command to add the package.

composer require spatie/laravel-backup

The above command added the library in laravel; now time comes to publish the package using the below command.

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

After we ran the publish command, a new configuration file is generated. You now have to open the config/backup.php file and add the google property in the disks array.

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

Configure Google Drive Filesystem

Furthermore, we will show you how to configure Google Drive as Filesystem in the Laravel project. Therefore, you must evoke the given command and order the CLI to install the packages.

composer require nao-pon/flysystem-google-drive:~1.1
php artisan make:provider GoogleDriveServiceProvider

Head over to the app/Providers/GoogleDriveServiceProvider.php file, and register the Google driver within the boot function.

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);
    });
}

Next up, move on to config/app.php file then add the given class in the providers property.

'providers' => [
    App\Providers\GoogleDriveServiceProvider::class,
];

Register Google Drive Credentials

We are now going to accomplish the essential task; which is to register Google drive storage in Laravel environment.

Ensure that you include your Google client id, client secret, refresh token and Google drive folder id:
Update Google API details in .env file.

GOOGLE_DRIVE_CLIENT_ID=xxx.xxx.xxxxxx.xxx
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=null

Moreover, you also have insert the given code into the config/filesystem.php file.

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'),
        ],
    ], 
];

Save Laravel Backup in Google Drive

Eventually, the time has come to take the Laravel 9 application backup in Google Drive storage. Here is the command to help you save the Laravel backup on Google cloud storage.

php artisan backup:run

Conclusion

We unveiled the step-by-step process of taking a full backup of the Laravel application in Google drive cloud storage using a third-party laravel/spatie library.

 

also view other related posts,