This project supports automatic database and S3-compatible storage backups to Google Drive using the Google Drive API.
The backup process includes:
- MySQL database dump
- Files stored on S3-compatible storage (such as AWS S3 or Cloudflare R2)
- Archive creation
- Upload to Google Drive
Install the Google API PHP Client:
composer require google/apiclient:^2.15Make sure your Laravel application has a configured S3 disk:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_BUCKET=
AWS_ENDPOINT=For Cloudflare R2, configure the S3-compatible endpoint:
AWS_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com- Go to Google Cloud Console.
- Create a new project (for example:
hsc-stack-backups). - Open APIs & Services → Library.
- Search for Google Drive API.
- Click Enable.
- Navigate to:
APIs & Services → OAuth consent screen
- Choose:
External
- Fill in the required information:
- App name
- User support email
- Developer contact email
- Save and continue.
Add:
https://www.googleapis.com/auth/drive.file
This allows the application to create and manage files created by the backup application without granting access to the entire Google Drive.
While the application is in Testing mode:
- Open OAuth consent screen → Test users
- Add the Google account that will store backups.
Publishing the application is not required for personal backup usage.
- Open:
APIs & Services → Credentials
- Click:
Create Credentials → OAuth client ID
- Select:
Application type: Desktop app
- Give it a name:
Example:
hsc-stack-backup-cli
- Create the credentials.
Copy:
- Client ID
- Client Secret
Add these values to your .env file:
GOOGLE_DRIVE_CLIENT_ID=
GOOGLE_DRIVE_CLIENT_SECRET=
GOOGLE_DRIVE_REFRESH_TOKEN=
GOOGLE_DRIVE_FOLDER_NAME=backupThe configuration is loaded from:
'google_drive' => [
'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folder_name' => env('GOOGLE_DRIVE_FOLDER_NAME', 'backup'),
],Run:
php artisan backup:drive-authorizeThe command will:
- Generate a Google authorization URL.
- Ask you to open it in your browser.
- Sign in with the Google account added as a test user.
- Grant access.
- Copy the authorization code.
- Paste the code back into the terminal.
The command will output a refresh token:
GOOGLE_DRIVE_REFRESH_TOKEN=1//xxxxxxxxxxxxxxxx
Add this value to .env.
The authorization process only needs to be completed once. The refresh token is reused automatically for future scheduled backups.
Create and upload a backup:
php artisan backup:driveThe command performs these steps:
1. Dump MySQL database
2. Download files from S3 storage temporarily
3. Create S3 archive
4. Combine database + storage archive
5. Upload final archive to Google Drive
6. Remove temporary local backup files
If the configured Google Drive folder does not exist, it will be created automatically.
After the refresh token is configured, backups can run automatically using Laravel Scheduler or cron.
Example scheduler:
Schedule::command('backup:drive')
->daily();Example cron entry:
0 0 * * * cd /path/to/project && php artisan schedule:runThe backup command does not directly backup storage/app/public.
Instead, it backs up the configured S3 disk:
S3 Bucket
|
├── images
├── documents
├── PDFs
└── other uploaded files
This keeps the backup process aligned with the application's production storage system.
For S3-compatible providers such as Cloudflare R2, ensure the Laravel S3 filesystem configuration is correct before running backups.