##Basic installation
You can install this package via composer using:
composer require spatie/laravel-backup
The package will automatically register its service provider.
To publish the config file to config/backup.php
run:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
This is the default contents of the configuration:
return [
'backup' => [
'name' => env('APP_NAME', 'laravel-backup'),
'source' => [
'files' => [
'include' => [
base_path(),
],
'exclude' => [
base_path('vendor'),
base_path('node_modules'),
],
'follow_links' => false,
'ignore_unreadable_directories' => false,
'relative_path' => null,
],
'databases' => [
'mysql',
],
],
'database_dump_compressor' => null,
'database_dump_file_extension' => '',
'destination' => [
'filename_prefix' => '',
'disks' => [
'local',
],
],
'temporary_directory' => storage_path('app/backup-temp'),
'password' => env('BACKUP_ARCHIVE_PASSWORD'),
'encryption' => \ZipArchive::EM_AES_256,
],
'notifications' => [
'notifications' => [
\Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFoundNotification::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\CleanupHasFailedNotification::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\BackupWasSuccessfulNotification::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\HealthyBackupWasFoundNotification::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\CleanupWasSuccessfulNotification::class => ['mail'],
],
'notifiable' => \Spatie\Backup\Notifications\Notifiable::class,
'mail' => [
'to' => 'your@example.com',
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
],
'slack' => [
'webhook_url' => '',
'channel' => null,
'username' => null,
'icon' => null,
],
],
'monitor_backups' => [
[
'name' => env('APP_NAME', 'laravel-backup'),
'disks' => ['local'],
'health_checks' => [
\Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumAgeInDays::class => 1,
\Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumStorageInMegabytes::class => 5000,
],
],
],
'cleanup' => [
'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,
'default_strategy' => [
'keep_all_backups_for_days' => 7,
'keep_daily_backups_for_days' => 16,
'keep_weekly_backups_for_weeks' => 8,
'keep_monthly_backups_for_months' => 4,
'keep_yearly_backups_for_years' => 2,
'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
],
],
];
##Configuring the backup disk
By default, the backup will be saved into the public/laravel-backup/
directory of your laravel application. This folder most probably is configured to be public.
We recommend that you create a disk named backups
(you can use any name you prefer) in filesystems.php
and specify that name in the disk
key of the backup.php
config file.
##Scheduling
After you have performed the basic installation you can start using the backup:run
, backup:clean
, backup:list
and backup:monitor
-commands. In most cases you'll want to schedule these commands so you don't have to manually run backup:run
everytime you need a new backup.
The commands can be scheduled in Laravel's console kernel, just like any other command.
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->daily()->at('01:00');
$schedule->command('backup:run')->daily()->at('01:30');
}
Of course, the times used in the code above are just examples. Adjust them to suit your own preferences. It is generally a good idea to avoid the timeslot between 02:00 and 03:00 at night in areas where daylight saving time changes occur, as this causes sometimes a double backup or (worse) no backup at all.
If a backup cannot be taken successfully, the backup:run
command returns an exit code of 1 which signals a general error, so you can use laravel's task hooks to specify code to be executed if the scheduled backup succeeds or fails:
$schedule
->command('backup:run')->daily()->at('01:00')
->onFailure(function () {
...
})
->onSuccess(function () {
...
});
##Monitoring
If your application is broken, the scheduled jobs cannot run anymore. You might also simply forget to add a cron job needed to trigger Laravel's scheduling. In either case, you may think backups are being made when in fact nothing is being backed up.
To find out about problems with your backups, the package ships with monitoring functionality. It will inform you when backups become too old or when they take up too much storage.
Learn how to set up monitoring.
##Dumping the database
mysqldump
is used to backup MySQL databases. pg_dump
is used to dump PostgreSQL databases. If these binaries are not installed in a default location, you can add a key named dump.dump_binary_path
in Laravel's own database.php
config file. Only fill in the path to the binary. Do not include the name of the binary itself.
If your database dump takes a long time, you might exceed the default timeout of 60 seconds. You can set a higher (or lower) limit by providing a dump.timeout
config key which specifies, in seconds, how long the command may run.
Here's an example for MySQL:
'connections' => [
'mysql' => [
'driver' => 'mysql'
...,
'dump' => [
'dump_binary_path' => '/path/to/the/binary',
'use_single_transaction',
'timeout' => 60 * 5,
'exclude_tables' => ['table1', 'table2'],
'add_extra_option' => '--optionname=optionvalue',
]
],
##File extensions of database dumps
By default, database dump files are named .sql
, except for the MongoDB driver which are named .archive
. If you would like to override this, you can set the file extension to be used in the config.
For example, to save a database dump as a .txt
file:
'backup' => [
...,
'database_dump_file_extension' => 'txt',
],
This relates to the names of the database dump files within the overall backup zip
file that is generated.
##Custom database dumpers
If you need to have a custom database dumper for a driver, you can use DbDumpFactory::extend()
. It expects the first argument to be the driver name and the second to be a callback that returns an instance of Spatie\DbDumper\DbDumper
.
DbDumperFactory::extend('mysql', function() {
return new YourCustomMysqlDumper();
});
use Spatie\DbDumper\DbDumper;
class YourCustomMysqlDumper extends DbDumper
{
}