PHPackages                             artryazanov/laravel-mysql-drive-backup - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. artryazanov/laravel-mysql-drive-backup

ActiveLibrary[Database &amp; ORM](/categories/database)

artryazanov/laravel-mysql-drive-backup
======================================

Laravel 12 package providing Artisan commands to back up MySQL to Google Drive using OAuth2.

v1.0.0(1mo ago)225[1 PRs](https://github.com/artryazanov/laravel-mysql-drive-backup/pulls)MITPHPPHP ^8.2CI passing

Since Aug 11Pushed 1mo agoCompare

[ Source](https://github.com/artryazanov/laravel-mysql-drive-backup)[ Packagist](https://packagist.org/packages/artryazanov/laravel-mysql-drive-backup)[ RSS](/packages/artryazanov-laravel-mysql-drive-backup/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (13)Versions (12)Used By (0)

☁️ Laravel MySQL Drive Backup (OAuth2)
======================================

[](#️-laravel-mysql-drive-backup-oauth2)

[![Latest Version on Packagist](https://camo.githubusercontent.com/733c48a4249f77235565117c18dbf554097343a3d084c45ad580ea8b17543a16/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/artryazanov/laravel-mysql-drive-backup)[![Tests](https://camo.githubusercontent.com/521f7742376bc5ac16109abab7310ee3a18f0315428d8b2afa9e45b92f1f9fe2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/artryazanov/laravel-mysql-drive-backup/actions/workflows/tests.yml)[![Lint](https://camo.githubusercontent.com/18e71de3514dd468108c0f42d319ed28d5f31a5fa0754c37f512ede453890318/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702f6c696e742e796d6c3f6272616e63683d6d61696e266c6162656c3d6c696e74267374796c653d666c61742d737175617265)](https://github.com/artryazanov/laravel-mysql-drive-backup/actions/workflows/lint.yml)[![Codecov](https://camo.githubusercontent.com/2d551930e7f66acb4bd10248f8d9a711ac13a36c79521d98563b7f8f8986a244/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/artryazanov/laravel-mysql-drive-backup)[![Total Downloads](https://camo.githubusercontent.com/ec1b11d3be2804ec2db069b673b2bfea9f915f8f0a9754573c4591a9dc912bda/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/artryazanov/laravel-mysql-drive-backup)[![License](https://camo.githubusercontent.com/1a69b1a9cad4cefa42bd5d5f56178d6d4b994fdd7f6e164cee8543f3b043e98e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6172747279617a616e6f762f6c61726176656c2d6d7973716c2d64726976652d6261636b75702e7376673f7374796c653d666c61742d737175617265)](https://github.com/artryazanov/laravel-mysql-drive-backup/blob/main/LICENSE)

Laravel 10-12 package to back up a MySQL database and upload the dump to Google Drive using OAuth2 (user consent). Provides Artisan commands for authorization, creating backups, and restoring them from Drive (supports .sql, .gz, .zip and wildcard masks). Suitable for manual runs and for scheduling via Laravel Scheduler.

📋 Requirements
--------------

[](#-requirements)

- PHP 8.2+
- Laravel 10.x–12.x
- MySQL client utilities installed and available in PATH:
    - mysqldump (for backup)
    - mysql (for restore)
- PHP extensions: zip, zlib
- Google OAuth2 credentials and configured redirect URI

📦 Installation
--------------

[](#-installation)

You can install the package via composer:

```
composer require artryazanov/laravel-mysql-drive-backup
```

Laravel auto-discovers the service provider. No manual registration is needed.

⚙️ Configuration
----------------

[](#️-configuration)

Publish the configuration to customize defaults:

```
php artisan vendor:publish --tag=config
```

Configuration file: `config/drivebackup.php`

- client\_id: Google OAuth2 Client ID (env GOOGLE\_DRIVE\_CLIENT\_ID)
- client\_secret: Google OAuth2 Client Secret (env GOOGLE\_DRIVE\_CLIENT\_SECRET)
- redirect\_uri: Redirect URI registered in Google Cloud Console (env GOOGLE\_DRIVE\_REDIRECT\_URI, default )
- token\_file: Path to the token JSON file (env GOOGLE\_DRIVE\_TOKEN\_PATH, default storage/app/google\_drive\_token.json)
- drive\_backup\_folder\_id: Optional Drive folder ID where backups are stored (env GOOGLE\_DRIVE\_BACKUP\_FOLDER\_ID)
- backup\_file\_name: File name to use on Google Drive (env DB\_BACKUP\_NAME, default mysql\_backup\_{timestamp}.sql). You can include {timestamp} placeholder which is replaced as YYYYMMDD-HHMMSS.
- temp\_file\_path: Local temporary dump path (env DB\_BACKUP\_TEMP\_PATH, default storage/app/mysql\_backup\_{timestamp}.sql). You can include {timestamp} placeholder which is replaced as YYYYMMDD-HHMMSS.
- restore\_temp\_dir: Temporary directory for downloaded archives during restore (env DB\_RESTORE\_TEMP\_DIR, default storage/app/drive-restore-temp)
- compress: When true (default), gzip-compress the .sql dump before upload (env DB\_BACKUP\_COMPRESS, default true). If enabled and backup\_file\_name does not end with .gz, the uploaded name will have .gz appended.
- exclude\_tables: Array of table names to exclude from backup. Set via env DB\_BACKUP\_EXCLUDE\_TABLES as a comma-separated list (e.g., "jobs,failed\_jobs,sessions"). For each listed table, mysqldump will receive --ignore-table="{database}.{table}".

🔐 Google Drive OAuth2 Authorization
-----------------------------------

[](#-google-drive-oauth2-authorization)

### 1. Obtaining Google Drive API Credentials

[](#1-obtaining-google-drive-api-credentials)

To use this package, you need to create an OAuth2 application in the Google Cloud Console and obtain a Client ID and Client Secret.

1. Go to the [Google Cloud Console](https://console.cloud.google.com/).
2. Create a new project or select an existing one.
3. **Enable the Google Drive API**:
    - Navigate to **APIs &amp; Services** &gt; **Library**.
    - Search for **Google Drive API** and click **Enable**.
4. **Configure the OAuth Consent Screen**:
    - Navigate to **APIs &amp; Services** &gt; **OAuth consent screen**.
    - Choose **External** (or Internal if using a Google Workspace) and click **Create**.
    - Fill in the required primary fields (App name, User support email, Developer contact information) and click Save and Continue through the steps.
    - *Note: If your application publishing status is "Testing", you MUST add the Google account you will use for backups to the **Test users** list. Otherwise, authorization will fail.*
5. **Create Credentials**:
    - Navigate to **APIs &amp; Services** &gt; **Credentials**.
    - Click **Create Credentials** &gt; **OAuth client ID**.
    - Select **Web application** as the Application type.
    - Name your OAuth client (e.g., "Laravel Drive Backup").
    - Under **Authorized redirect URIs**, add your application's callback URL. This must exactly match your `GOOGLE_DRIVE_REDIRECT_URI` value (e.g., `http://localhost:8000/google/drive/callback` for local development or `https://your-domain.com/google/drive/callback` for production).
    - Click **Create**.
6. A dialog will appear with your **Client ID** and **Client Secret**.

### 2. Environment Configuration

[](#2-environment-configuration)

Add the obtained credentials and redirect URI to your `.env` file:

```
GOOGLE_DRIVE_CLIENT_ID=your-client-id...
GOOGLE_DRIVE_CLIENT_SECRET=your-client-secret...
GOOGLE_DRIVE_REDIRECT_URI=http://localhost:8000/google/drive/callback
```

### 3. Application Setup

[](#3-application-setup)

Publish config and clear cache if needed:

```
php artisan vendor:publish --tag=config
php artisan cache:clear && php artisan config:clear
```

4. Run the authorization command:

```
php artisan backup:authorize-drive
```

Open the link, approve access; Google will redirect to your redirect\_uri (/google/drive/callback). The package callback stores the token automatically. If you prefer to enter the code manually, confirm the prompt in the console and paste the value of the `code` parameter from the redirected URL.

💻 Usage
-------

[](#-usage)

Run the backup command:

```
php artisan backup:mysql-to-drive
```

Restore a backup from Google Drive:

```
php artisan backup:restore-mysql "backup-*.sql.gz"
```

You may restrict tables during restore:

```
php artisan backup:restore-mysql "backup-*.zip" --only=users,orders
php artisan backup:restore-mysql "nightly-*.sql" --except=log_*
```

### Restore options and behavior

[](#restore-options-and-behavior)

- mask: File name or wildcard mask to search on Google Drive. The latest file by modifiedTime matching the mask is selected. Examples: `backup-*.sql`, `*.sql.gz`, `*.zip`.
- Supported formats: `.sql`, `.gz` (gzip of a single .sql), `.zip` (one or more .sql files inside).
- --only: Comma-separated table names or wildcard masks to include (e.g., `users,orders`, `log_*`).
- --except: Comma-separated table names or wildcard masks to exclude.
- --keep-temp: Keep downloaded/extracted temporary files and skip cleanup at the end.

Cleanup by default: After a successful restore, the command removes only artifacts created during the current run in the restore temp directory (the directory itself remains). For example:

- When restoring a `.gz` dump: removes the downloaded `.gz`, the extracted `.sql`, and the filtered variant if created.
- When restoring a `.sql` dump: removes the downloaded `.sql` and its filtered variant if created.
- For `.zip`: extracted .sql files are intentionally preserved because their names cannot be reliably inferred from the archive name. Cleanup removes the downloaded `.zip` and conservative `filtered-*` variants only; manage extracted files yourself if needed. Use `--keep-temp` to skip any cleanup entirely.

Remove outdated backups from Google Drive according to the retention policy:

```
php artisan backup:cleanup-drive
```

Backup command behaviour:

1. The package creates a MySQL dump of the default connection (must be MySQL).
2. The dump file is uploaded to Google Drive using OAuth2.
3. On success, the local dump file is removed.

💾 Large dumps and memory usage
------------------------------

[](#-large-dumps-and-memory-usage)

This package handles large backup files efficiently by streaming data instead of loading it into memory:

- Downloads from Google Drive are streamed to disk in chunks.
- .gz archives are decompressed with gzread() in chunks.
- .zip archives are extracted via ZipArchive streams and written in chunks.
- SQL filtering is performed line-by-line; allowed table blocks are written directly to the output file without buffering entire tables in memory.
- MySQL import uses passthru, avoiding accumulation of child process output in PHP memory.

This design keeps peak RAM usage low even for multi‑gigabyte dumps. Ensure there is enough free disk space in restore\_temp\_dir for temporary files during restore.

⏰ Scheduling
------------

[](#-scheduling)

Add this to your `app/Console/Kernel.php` schedule method:

```
$schedule->command('backup:mysql-to-drive')->dailyAt('03:00');
$schedule->command('backup:cleanup-drive')->dailyAt('04:00');
```

🛠️ Troubleshooting
------------------

[](#️-troubleshooting)

- mysqldump not found: Ensure `mysqldump` is installed and available in PATH on the server.
- The default database connection is not MySQL: Configure Laravel DB to use a MySQL connection as default.
- Token file not found: Run `php artisan backup:authorize-drive` first.

🛡️ Security
-----------

[](#️-security)

Protect your token file and client secret. Do not commit secrets to version control.

📄 License
---------

[](#-license)

This package is released under the MIT License. See [LICENSE](LICENSE) for details.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance91

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~75 days

Total

4

Last Release

50d ago

Major Versions

v0.1.2 → v1.0.02026-03-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/6357ca96399697b30326b317cf64cb85eb01005fb296cf50176d4008cf448135?d=identicon)[artryazanov](/maintainers/artryazanov)

---

Top Contributors

[![artryazanov](https://avatars.githubusercontent.com/u/4519328?v=4)](https://github.com/artryazanov "artryazanov (38 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

ai-assistedartisanautomationbackupdatabase-backupgoogle-drivegoogle-drive-apilaravellaravel-packagemysqlmysqldumpoauth2phprestore

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/artryazanov-laravel-mysql-drive-backup/health.svg)

```
[![Health](https://phpackages.com/badges/artryazanov-laravel-mysql-drive-backup/health.svg)](https://phpackages.com/packages/artryazanov-laravel-mysql-drive-backup)
```

###  Alternatives

[spatie/laravel-backup

A Laravel package to backup your application

6.0k21.8M191](/packages/spatie-laravel-backup)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[ryangjchandler/orbit

A flat-file database driver for Eloquent.

922256.2k5](/packages/ryangjchandler-orbit)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[cybercog/laravel-clickhouse

ClickHouse migrations for Laravel

163166.8k](/packages/cybercog-laravel-clickhouse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
