cyberwizard / dbsafeguard
A Laravel package for safeguarding your database with easy-to-use backup commands.
Requires
- php: ^8.0
- league/flysystem-aws-s3-v3: ^3.0
README
DBSafeGuard is a Laravel package that provides a convenient command-line interface to back up your database. It supports MySQL and PostgreSQL databases, and allows you to save backups locally or upload them directly to AWS S3.
What's New in 1.3.0
- Progress Bars: Added visual progress bars for both local database dumping and S3 uploading.
- Backup Retention: Automatically clean up old backups to save storage. Keep the latest backups based on the new
MAX_BACKUPSenvironment variable (defaults to 3).
What's New in 1.2.1
- Memory Optimization: Fixed a memory exhaustion issue when uploading large database backups to S3 by using file streams.
What's New in 1.1.0
- Laravel 12 Support: Fully compatible with Laravel 9.0 through 12.0+.
- PostgreSQL Support: Automatically detects your connection and supports
pg_dump. - S3 Uploads: Stream backups directly to AWS S3 using the new
--disk=s3flag.
Requirements
- PHP 8.0 or higher
- Laravel 9.0, 10.0, 11.0, or 12.0+
mysqldumporpg_dumpinstalled on your server (depending on your database)
Installation
Install the package via Composer:
composer require cyberwizard/dbsafeguard
If you are installing directly from the GitHub repository, add the following to your composer.json first:
"repositories": [ { "type": "vcs", "url": "https://github.com/cyberwizard-dev/DBSafeGuard" } ]
Then run composer require cyberwizard/dbsafeguard:dev-main.
Configuration
The package automatically uses your standard Laravel database configuration. Ensure the standard environment variables are set in your .env file:
DB_CONNECTION=mysql # or pgsql DB_HOST=127.0.0.1 DB_PORT=3306 # or 5432 DB_DATABASE=your_database DB_USERNAME=your_username DB_PASSWORD=your_password
S3 Configuration
If you plan to upload your backups to AWS S3, you must configure your S3 credentials in your .env file:
AWS_ACCESS_KEY_ID=your_access_key AWS_SECRET_ACCESS_KEY=your_secret_key AWS_DEFAULT_REGION=your_region AWS_BUCKET=your_bucket_name
Backup Retention
By default, the package will keep the latest 3 backups and automatically delete older ones to save storage space. You can change this limit by setting the MAX_BACKUPS environment variable in your .env file:
MAX_BACKUPS=5
Usage
Local Backup
To back up your database to the local disk, run:
php artisan backup:db
The command will prompt you for a backup path. If you accept the default, the backup file will be stored in the storage/app/backups directory with a timestamped filename.
S3 Backup
To back up your database and upload it directly to S3, use the --disk option:
php artisan backup:db --disk=s3
This will create a temporary local dump, upload it to the backups/ directory in your S3 bucket, and then remove the local temporary file.
Scheduled Backups
You can schedule automatic backups using Laravel's task scheduler. Add the following to your console routes or App\Console\Kernel class:
// Local backup daily at midnight Schedule::command('backup:db')->daily(); // S3 backup twice daily Schedule::command('backup:db --disk=s3')->twiceDaily(1, 13);
Author
Cyberwizard
Contact: eminibest@gmail.com
License
This package is open-sourced software licensed under the MIT license.