laravel-db-backup maintained by doss
Laravel DB Backup
Back up your Laravel database to Google Drive:
php artisan db:backup
What it does:
- Dumps the database into a folder:
db-backups/classquiz_2026-10-05-02-00/classquiz_2026-10-05-02-00.dump - Zips that folder:
db-backups/classquiz_2026-10-05-02-00.zip - Uploads the zip to Google Drive
- With
--delete-local, 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. Publish the config file:
php artisan vendor:publish --tag=db-backup-config
Configuration
.env
DB_BACKUP_GOOGLE_FOLDER_ID=your-folder-id # from https://drive.google.com/drive/folders/<FOLDER_ID>
DB_BACKUP_GOOGLE_AUTH=service_account # or oauth
DB_BACKUP_GOOGLE_SERVICE_ACCOUNT=/path/to/credentials.json
config/db-backup.php
Everything else is set directly in the config file:
'dump' => [
'path' => storage_path('app/db-backups'), // where the folder and zip are created
'filename_format' => 'Y-m-d-H-i', // date format used in the name
'prefix' => env('APP_NAME', 'laravel'), // name prefix, e.g. "classquiz"
'delete_local' => false, // true = always delete the local zip after upload
'timeout' => 3600, // max seconds for the dump command
'binaries' => [
'pg_dump' => 'pg_dump', // full path if not on PATH, e.g. /usr/bin/pg_dump
'mysqldump' => 'mysqldump',
],
// ...
],
Google Drive setup
Service account (default)
- In Google Cloud Console, enable the Google Drive API.
- Create a service account and download its JSON key.
- Set
DB_BACKUP_GOOGLE_SERVICE_ACCOUNTto the path of that file. - Share the Drive folder with the service account e-mail (
...@...iam.gserviceaccount.com) as Editor.
On personal Gmail accounts, service accounts have no storage quota of their own. If uploads fail with
storageQuotaExceeded, use a Shared Drive or switch to OAuth.
OAuth (your own Google account)
-
Create an OAuth client of type Desktop app and download
client_secret.json. -
Put it at
storage/app/google/client_secret.json(or changeoauth_client_jsonin the config). -
Set
DB_BACKUP_GOOGLE_AUTH=oauth. -
Run once:
php artisan db-backup:google-authOpen the link, allow access, and paste the code (or the full redirect URL). The token is saved to
storage/app/google/token.jsonand refreshed automatically after that.
Usage
php artisan db:backup # dump, zip, upload (local zip is kept)
php artisan db:backup --delete-local # same, then delete the local zip after upload
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,
even with --delete-local.
Scheduling
In routes/console.php (Laravel 11+):
use Illuminate\Support\Facades\Schedule;
Schedule::command('db:backup --delete-local')->dailyAt('02:00')->withoutOverlapping();
or in app/Console/Kernel.php (Laravel 10):
$schedule->command('db:backup --delete-local')->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