doss / laravel-db-backup
Back up your Laravel database to Google Drive with `php artisan db:backup`.
Requires
- php: ^8.1
- ext-zip: *
- google/apiclient: ^2.15
- illuminate/console: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- symfony/process: ^6.0|^7.0|^8.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Back up your Laravel database to Google Drive:
php artisan db:backup
What it does:
- Dumps the database into a temporary folder:
storage/app/db-backups/classquiz_2026-10-05-02-00/classquiz_2026-10-05-02-00.dump - Zips that folder:
storage/app/db-backups/classquiz_2026-10-05-02-00.zip - Uploads the zip to your Google Drive folder
- Deletes the local zip once the upload has succeeded
Supported databases: PostgreSQL (pg_dump), MySQL / MariaDB (mysqldump), SQLite.
Connection details come from your existing config/database.php.
Installation
composer require doss/laravel-db-backup
The service provider is auto-discovered.
Configuration
Two settings are needed, in .env:
DB_BACKUP_GOOGLE_FOLDER_ID=your-folder-id # from https://drive.google.com/drive/folders/<FOLDER_ID> DB_BACKUP_GOOGLE_CREDENTIALS=client_secret.json # OAuth client secret or service account key
Relative paths are resolved from the project root. If DB_BACKUP_GOOGLE_CREDENTIALS is not set, the file is read
from storage/app/google/credentials.json. The package detects which kind of JSON file it is.
To change the dump path, name prefix, date format, timeout or pg_dump / mysqldump binaries, publish the config:
php artisan vendor:publish --tag=db-backup-config
Google Drive setup
In Google Cloud Console, enable the Google Drive API, then pick one of the following.
OAuth client secret (personal Google account, recommended)
-
Create an OAuth client of type Desktop app and download its JSON (
client_secret.json). -
Set
DB_BACKUP_GOOGLE_CREDENTIALSto that file. -
Run once:
php artisan db-backup:google-auth
Open the link, allow access, and paste the code (or the full redirect URL). The token is saved to
storage/app/google/token.json(DB_BACKUP_GOOGLE_TOKEN) and refreshed automatically after that.
Service account (Google Workspace Shared Drive)
- Create a service account and download its JSON key.
- Set
DB_BACKUP_GOOGLE_CREDENTIALSto that file. - Add the service account e-mail (
...@...iam.gserviceaccount.com) to the Shared Drive as Content manager.
Service accounts have no storage quota of their own, so they cannot upload to a personal "My Drive" folder (
storageQuotaExceeded). Use OAuth for a personal account.
Keep the credentials and token files out of git.
Usage
php artisan db:backup # dump, zip, upload, delete the local zip php artisan db:backup --keep-local # same, but keep the local zip php artisan db:backup --no-upload # only create the local zip php artisan db:backup --connection=mysql # back up another connection
If the upload fails, the error is logged, the command exits with a non-zero code, and the local zip is kept.
Scheduling
In routes/console.php (Laravel 11+):
use Illuminate\Support\Facades\Schedule; Schedule::command('db:backup')->dailyAt('02:00')->withoutOverlapping();
or in app/Console/Kernel.php (Laravel 10):
$schedule->command('db:backup')->dailyAt('02:00')->withoutOverlapping();
Make sure the Laravel scheduler cron is running:
* * * * * cd /path/to/app && php artisan schedule:run >> /dev/null 2>&1
Requirements
- PHP 8.1+ with the
zipextension, Laravel 10–13 pg_dumpormysqldumpinstalled on the server (matching your DB server version)
License
MIT