How to Set Up Daily Laravel Backups on Google Drive for Free

How to Set Up Daily Laravel Backups on Google Drive for Free

Posted By

kamlesh paul

on

Dec 10, 2024

When it comes to Laravel backups, not every project needs to rely on premium storage services like Amazon S3. For developers seeking a cost-effective solution, Laravel backups on Google Drive for free provide an excellent alternative. Google Drive offers secure storage at no cost, making it ideal for daily backups in smaller Laravel projects. In this guide, we’ll show you step-by-step how to set up daily Laravel backups on Google Drive using two powerful packages: Spatie Laravel Backup for generating backups and Laravel Google Drive Storage to connect Laravel to Google Drive.

Table of Contents

Step 1: Install Spatie Laravel Backup

The Spatie Laravel Backup package helps in creating and managing backups for Laravel projects.

  1. Run the following command to install it via Composer:
composer require spatie/laravel-backup
  1. Publish the package configuration:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
  1. Configure the config/backup.php file to set backup preferences like the files to include and the backup frequency.

Step 2: Install and Configure Laravel Google Drive Storage

This package allows Laravel to store files on Google Drive by defining it as a filesystem disk.

  1. Install the package:
composer require yaza/laravel-google-drive-storage
  1. Add your credentials in the .env file:
.env
FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER=
  1. Update config/filesystems.php by adding a new disk configuration:
filesystems.php
'disks' => [
    'google' => [
      'driver' => 'google',
      'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
      'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
      'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
      'folder' => env('GOOGLE_DRIVE_FOLDER'),
    ]
]

Setup Google Keys

Step 3: Configure Spatie Backup to Use Google Drive

Now, set up the backup configuration to use Google Drive as the storage location.

  1. Open config/backup.php.
  2. Update the disk setting under backup.destination to point to the Google Drive disk:
backup.php
   'destination' => [
      /*
       * The disk names on which the backups will be stored.
      */
 
       'disks' => [
           'google',
       ],
   ],
  1. Update the disk setting under monitor_backups.disks to point to the Google Drive disk:
backup.php
'monitor_backups' => [
    [
        'name' => env('APP_NAME', 'laravel-backup'),
        'disks' => ['google'],
        
    ],

Step 4: Schedule the Backup

To automate backups, schedule the backup command in Laravel’s scheduler.

  1. Open app/routes/console.php.
  2. Add the following to the schedule method:
console.php
Schedule::command('backup:clean')->daily()->at('01:00');
Schedule::command('backup:run --only-db')->daily()->at('01:30');
  1. Ensure your server’s cron job is set up to run Laravel’s scheduler every minute:
   * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

Step 5: Test the Backup

Manually run the backup command to ensure everything is working:

php artisan backup:run

Check Google Drive to confirm that the backup has been successfully uploaded.

Share this article

99 views