- Laravel -

Backup Laravel Application on Google Drive

|
4 min read
Backup Laravel Application on Google Drive

While working on a Laravel application, my client requested me to add a feature to backup the application files and database. At first what I did was backup the files and database into the server itself, but that was not a good idea because if the server crashes, the backups will be lost. So, I looked for a solution to backup the application files and database onto some cloud storage.

Sp, in this article, we will learn how to backup a Laravel application on Google Drive on a scheduled basis using spatie/laravel-backup and masbug/flysystem-google-drive-ext.

Step 1: Install and Configure spatie/laravel-backup

We will use spatie/laravel-backup to backup the application files and database. So, let's install it using Composer.

Now, let's publish the configuration file using the following command.

This command will publish the config/backup.php file in our Laravel application.

Step 2: Install and Configure masbug/flysystem-google-drive-ext

We will use masbug/flysystem-google-drive-ext to backup the application files and database onto Google Drive. So, let's install it using Composer.

To get the Google Drive credentials, please follow the Google Docs link. In addition you can alse check the masbug/flysystem-google-drive-ext repository for more information.

Once you have the Google Drive credentials. We will add them to our .env file and associate them into the config/filesystems.php file.

Finally, we will add the Google Drive credentials into the config/filesystems.php file within the 'disks' array.

How add newly created disk to the backup.php file within backup > destination array.

With this package, we need to update the backup > name value to the id of the folder we created in the Google Drive. You can find the id of the folder by clicking on the folder name in the Google Drive. The id is the last part of the URL.

For example, if the folder URL is https://drive.google.com/drive/folders/1Xsr-xxxxxxxxxxxxxxxxxxxxxxxxxx, then the id is 1Xsr-xxxxxxxxxxxxxxxxxxxxxxxxxx.

Now with the Google Drive credentials configured, we will create a new service provider class using the following command.

This command will create a new GoogleDriveServiceProvider class in the app/Providers directory.

Open the app/Providers/GoogleDriveServiceProvider.php file and add the following code.

Step 3: Schedule the Backup

We will utilize Laravel's built-in scheduler to schedule the backup. So within the app/Console/Kernel.php file, we will add the following code inside the schedule method.

With the above code, we will schedule the backup clean and run commands to run daily at 2:10 and 2:45 pm respectively. You can change the time according to your preference. You can also test the backup by running the following command.

This command will run the backup and create a new backup file in the Google Drive and local storage as defined in the config/backup.php file.

Conclusion

With the above steps, we have successfully configured the Laravel application to backup the application files and database onto Google Drive. You can also schedule the backup to run daily at a specific time. I hope this article has been helpful to you. If you have any questions or feedback, please do not hesitate to reach out to me. Your feedback is valuable to me, so if you'd like to see more content like this in the future, please don't hesitate to let me know.

Keep coding, keep exploring, and keep inspiring. 🐼

Written By Anuz Pandey

Anuz Pandey is a multi-disciplinary designer and developer with a passion for creating visually stunning and user-friendly digital experiences. He has been working in the industry for over 5 years. He is currently working as a freelance designer and developer and is available for hire.

Tags

  • Laravel
  • PHP
  • Google Drive
  • spatie/laravel-backup
  • backup
  • database-backup
  • file-backup