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
- Mysql
- Postgresql
- sqlite
- 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
You can install the package via composer:
composer require mhmdomer/laravel-database-backupYou can publish the config file with:
php artisan vendor:publish --tag=database-backupYou 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')
]
];To create a backup of your database you can run:
php artisan database:backupThe 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-mailTo 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);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
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
