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
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', '[email protected]')
]
];
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);
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.