jpbgomes / laravel-database-backup
A simple Laravel package to backup MySQL databases and send them via email.
Package info
github.com/jpbgomes/laravel-database-backup
pkg:composer/jpbgomes/laravel-database-backup
Requires
- php: >=8.0
- illuminate/support: >=9.0
- symfony/process: ^6.0|^7.0
README
A simple Laravel package to back up your MySQL or MariaDB database and send the backup file via email, now with optional ZIP compression, password protection, and extra file/folder inclusion.
⚠️ Only MySQL/MariaDB are supported currently, since the package relies on
mysqldump.
🚀 Installation
Require the package via Composer (from Packagist):
composer require jpbgomes/laravel-database-backup
Laravel will auto-discover the service provider. If not, add it manually to config/app.php:
'providers' => [ Jpbgomes\DatabaseBackup\BackupServiceProvider::class, ]
Publish the config file:
php artisan vendor:publish --provider="Jpbgomes\\DatabaseBackup\\BackupServiceProvider" --tag=config
⚙️ Configuration
Edit config/backup.php after publishing:
return [ 'path' => storage_path('app/backups'), 'recipients' => array_filter(array_map('trim', explode(',', env('BACKUP_EMAIL', 'admin@example.com')))), 'zip_password' => env('BACKUP_ZIP_PASSWORD', null), 'include' => [ // '.env', // 'storage/app/public', ], 'keep_local' => false, ];
Set your backup recipients and optional ZIP password in .env:
BACKUP_EMAIL=jpbgomesbusiness@gmail.com,arroz@gmail.com BACKUP_ZIP_PASSWORD=
Mailer Configuration
You must configure Laravel’s mailer to send the backups. A free recommended option is Gmail, but you must enable 2-Factor Authentication and create an App Password.
Example .env:
MAIL_MAILER=smtp MAIL_SCHEME=null MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD='your_app_password' MAIL_FROM_ADDRESS="your_email@gmail.com" MAIL_FROM_NAME="${APP_NAME}"
📝 Usage
Run the backup command:
php artisan backup:database
This will:
-
Create a
.sqldump usingmysqldump. -
Generate a
.zipfile containing the SQL dump. -
Optionally:
- Protect the zip with a password.
- Include extra folders/files in the zip.
-
Send the ZIP file to all configured recipients.
-
Optionally remove the local ZIP file.
⚙️ Advanced Configuration
Multiple Recipients
BACKUP_EMAIL=email1@gmail.com,email2@gmail.com
Password Protect ZIP
BACKUP_ZIP_PASSWORD=YourStrongPassword
If empty, the ZIP will not be encrypted.
Include Additional Files/Folders
Edit config/backup.php:
'include' => [ '.env', 'storage/app/public', ],
Paths are relative to base_path().
⏰ Automating Daily Backups
You can automate backups using crontab. Example to run every day at 4 AM:
0 4 * * * sudo php /path_to/your_project/artisan backup:database >> /dev/null 2>&1
Adjust the path to your Laravel project’s
artisanfile.
🛡 Security Notes & Caveats
mysqldumpmust be installed and accessible to the PHP process user.- DB credentials may appear briefly in process listings.
- For large databases, the backup is compressed in a ZIP.
- SQL file is never sent directly, reducing exposure.
- Password-protected ZIP uses AES-256 encryption.
🤝 Contributing & Improvements
Suggestions for improvements:
- Support for other database types (Postgres, SQLite).
- Remote storage integrations (S3, Google Cloud, etc.).
- Unit and integration tests for the backup command.
- CI/CD pipeline for testing and releases.
📜 License
MIT — free to use, modify, and share.