mhmdomer / laravel-database-backup
Backup your laravel database by a simple artisan command
Installs: 4 390
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 1
Forks: 5
Open Issues: 0
pkg:composer/mhmdomer/laravel-database-backup
Requires
- php: ^7.4|^8.0
- illuminate/console: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- brianium/paratest: ^6.2
- nunomaduro/collision: ^5.3
- orchestra/testbench: ^6.15
- phpunit/phpunit: ^9.3
- vimeo/psalm: ^4.8
README
Backup your laravel database by a simple artisan command
This package will allow you to backup your laravel app database and you can also choose to send the backup file via email by simply running the command php artisan database:backup
Supported Databases
- Mysql
- Postgresql
- sqlite
Requirements
- If you are using Mysql, make sure
mysqldumpis installed on your system - If you are using Postgresql, make sure
pg_dumpis installed on your system
Installation
You can install the package via composer:
composer require mhmdomer/laravel-database-backup
You can publish the config file with:
php artisan vendor:publish --tag=database-backup
You can configure the maximum_backup_files and whether to send an email when a backup occurs as well as specifying the email to send the backup file to
This is the contents of the published config file:
return [ /* |------------------------------------------------------------------------- | Backup Folder |------------------------------------------------------------------------- | | The path of the folder to save backups on and retrieve backups from. */ 'backup_folder' => storage_path('app/backup'), /* |------------------------------------------------------------------------- | Maximum Backup Files |------------------------------------------------------------------------- | | The maximum number of files that should be present inside the backup folder, | each new backup after this limit will result in removing the oldest backup file */ 'maximum_backup_files' => 10, /* |------------------------------------------------------------------------- | Mail Settings |------------------------------------------------------------------------- | Email configuration for backups. */ "mail" => [ /* |------------------------------------------------------------------------- | Send Mail |------------------------------------------------------------------------- | Specify if an email with the backup file attached should | be sent when creating a backup. */ 'send' => env('DB_BACKUP_SEND_MAIL', false), /* |------------------------------------------------------------------------- | Backup Mail |------------------------------------------------------------------------- | Specify the email that should receive the backup file. */ 'to' => env('DB_BACKUP_EMAIL', 'example@example.com') ] ];
Usage
To create a backup of your database you can run:
php artisan database:backup
The above command is typically run as a schedule command,
for example, you can add the following line in the schedule function
inside app\Console\Kernel.php
$schedule->command('database:backup')->daily();
To disable sending a backup email you can add --no-mail option:
php artisan database:backup --no-mail
To get the latest backup file:
DatabaseBackup::getLatestBackupFile();
To get all backup files:
DatabaseBackup::getBackupFiles();
To download the latest backup file:
$backupFile = DatabaseBackup::getLatestBackupFile(); return response()->download($backupFile);
Listening to Events
Mhmdomer\DatabaseBackup\Events\DatabaseBackupComplete Event will be fired after each backup success, this event has a string public property called $path containing the path of the backup file so you can use it to download the file
Similarly,Mhmdomer\DatabaseBackup\Events\DatabaseBackupFailed Event will be fired after each backup failure, this event has an Exception public property called $exception containing the exception that caused the database backup failure. For example, you can add listeners to listen for these events by editing your EventServiceProvider like this:
protected $listen = [ Mhmdomer\DatabaseBackup\Events\DatabaseBackupComplete::class => [ SendSuccessMessage::class, ], Mhmdomer\DatabaseBackup\Events\DatabaseBackupFailed::class => [ LogException::class, ], ];
change SendSuccessMessage::class and LogException::class to match your own listeners
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
