##Basic installation
You can install this package via composer using:
composer require "spatie/laravel-backup:^4.0.0"
You'll need to register the service provider:
'providers' => [
Spatie\Backup\BackupServiceProvider::class,
];
To publish the config file to config/laravel-backup.php
run:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
This is the default contents of the configuration:
return [
'backup' => [
'name' => env('APP_URL'),
'source' => [
'files' => [
'include' => [
base_path(),
],
'exclude' => [
base_path('vendor'),
base_path('node_modules'),
],
'followLinks' => false,
],
'databases' => [
'mysql',
],
],
'destination' => [
'filename_prefix' => '',
'disks' => [
'local',
],
],
],
'notifications' => [
'notifications' => [
\Spatie\Backup\Notifications\Notifications\BackupHasFailed::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFound::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\CleanupHasFailed::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\BackupWasSuccessful::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\HealthyBackupWasFound::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\CleanupWasSuccessful::class => ['mail'],
],
'notifiable' => \Spatie\Backup\Notifications\Notifiable::class,
'mail' => [
'to' => 'your@email.com',
],
'slack' => [
'webhook_url' => '',
],
],
'monitorBackups' => [
[
'name' => env('APP_URL'),
'disks' => ['local'],
'newestBackupsShouldNotBeOlderThanDays' => 1,
'storageUsedMayNotBeHigherThanMegabytes' => 5000,
],
],
'cleanup' => [
'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,
'defaultStrategy' => [
'keepAllBackupsForDays' => 7,
'keepDailyBackupsForDays' => 16,
'keepWeeklyBackupsForWeeks' => 8,
'keepMonthlyBackupsForMonths' => 4,
'keepYearlyBackupsForYears' => 2,
'deleteOldestBackupsWhenUsingMoreMegabytesThan' => 5000,
],
],
];
##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('02:00');
}
Of course, the times used in the code above are just examples. Adjust them to suit your own preferences.
##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
and pg_dump
are used to dump the database. If they 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',
]
],
TIP: If you're running your app through Laravel Herd on macOS and are trying to run backup commands inside your app via Artisan::call()
, you may need to explicitly specify the dump.dump_binary_path
in your database.php
config file.