Automate your Laravel database backups to Google Drive with cron jobs.
This Laravel project provides a complete solution to:
β
Create a database backup (SQL dump)
β
Compress it into a ZIP file
β
Upload it automatically to Google Drive
β
Clean up local storage after upload
- Automatic database backup
- ZIP compression for reduced file size
- Google Drive API integration
- Secure upload using OAuth2 refresh token
- Auto delete local backup after upload
- PHP >= 7.4 / 8+
- Laravel Framework
- Google Cloud Account
- Google Drive API enabled
git clone https://github.com/xaizikhan/database_backup_to_google_drive_laravel.git
cd database_backup_to_google_drive_laravelcomposer installYour controller uses some external packages that need to be installed:
- Google API Client (for Google Drive integration):
composer require google/apiclient:^2.0- HTTP Client (for HTTP requests, usually built-in in Laravel >=7, otherwise install Guzzle):
composer require guzzlehttp/guzzleAdd the following variables:
DB_DATABASE=your_database_name
GOOGLE_DRIVE_CLIENT_NAME=your_app_name
GOOGLE_DRIVE_CLIENT_ID=your_client_id
GOOGLE_DRIVE_CLIENT_SECRET=your_client_secret
GOOGLE_DRIVE_CLIENT_API_KEY=your_api_key
GOOGLE_DRIVE_FOLDER_ID=your_google_drive_folder_id
GOOGLE_DRIVE_REFRESH_TOKEN=your_refresh_token'google' => [
'driver' => 'google',
'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID'),
],- Go to Google Cloud Console
- Create a new project
Enable:
- Google Drive API
-
OAuth 2.0 Client ID
-
Get:
- Client ID
- Client Secret
- Use Postman or OAuth playground
- Save refresh token in
.env
app/
βββ Http/
βββ Controllers/
βββ DatabaseBackupController.php
storage/
βββ app/
β βββ backup/ (SQL files)
β βββ zip/ (ZIP files)
-
Fetch all database tables
-
Generate SQL dump file
-
Save file in:
storage/app/backup/ -
Compress SQL file into ZIP
-
Move ZIP to:
storage/app/zip/ -
Upload ZIP file to Google Drive
-
Delete local files after upload
use App\Http\Controllers\DatabaseBackupController;
Route::get('/take-database-backup', [DatabaseBackupController::class, 'takeBackUp']);http://your-domain.com/take-database-backup
- Main function
- Calls backup + upload process
- Loops through all tables
- Generates SQL dump
- Saves file locally
- Converts
.sqlfile into.zip - Reduces file size
- Uploads ZIP file to Google Drive
- Deletes local files after success
- Generates access token using refresh token
- Never commit
.envfile - Keep Google credentials secure
- Restrict API usage
Add this in app/Console/Kernel.php:
$schedule->call(function () {
app(\App\Http\Controllers\DatabaseBackupController::class)->takeBackUp();
})->daily();php artisan schedule:run- Add email notification after backup
- Add logs for backup history
- Store multiple backups
- Add restore functionality
- Add UI dashboard
If you need help, feel free to contact.
Developed and maintained by Faizan Raza Powered by Contriver Mate π
This package is open-sourced software licensed under the MIT license.