
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.
The Spatie Laravel Backup package helps in creating and managing backups for Laravel projects.
composer require spatie/laravel-backupphp artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"config/backup.php file to set backup preferences like the files to include and the backup frequency.This package allows Laravel to store files on Google Drive by defining it as a filesystem disk.
composer require yaza/laravel-google-drive-storage.env file:FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER=config/filesystems.php by adding a new disk configuration:'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'),
]
]Now, set up the backup configuration to use Google Drive as the storage location.
config/backup.php.backup.destination to point to the Google Drive disk: 'destination' => [
/*
* The disk names on which the backups will be stored.
*/
'disks' => [
'google',
],
],monitor_backups.disks to point to the Google Drive disk:'monitor_backups' => [
[
'name' => env('APP_NAME', 'laravel-backup'),
'disks' => ['google'],
],To automate backups, schedule the backup command in Laravel’s scheduler.
app/routes/console.php.Schedule::command('backup:clean')->daily()->at('01:00');
Schedule::command('backup:run --only-db')->daily()->at('01:30'); * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1Manually run the backup command to ensure everything is working:
php artisan backup:runCheck Google Drive to confirm that the backup has been successfully uploaded.