CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
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

| 3 min read

Last updated on : Dec 10, 2024

ServerBackup
380 views

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
  • Step 2: Install and Configure Laravel Google Drive Storage
  • Setup Google Keys
  • Step 3: Configure Spatie Backup to Use Google Drive
  • Step 4: Schedule the Backup
  • Step 5: Test the Backup

#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

  • Getting your Client ID and Secret
  • Getting your Refresh Token

#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.

Related Posts

  • How to Customize FrankenPHPHow to Customize FrankenPHP
  • How to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP PackageHow to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP Package
  • How to setup Laravel Queues on cPanel: A Step-by-Step GuideHow to setup Laravel Queues on cPanel: A Step-by-Step Guide
  • How to Protect Your Linux Server from Brute Force SSH AttacksHow to Protect Your Linux Server from Brute Force SSH Attacks
  • How to Manage Multiple GitHub Accounts on the Same ComputerHow to Manage Multiple GitHub Accounts on the Same Computer

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

380 views