##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' => config('app.name'),
'source' => [
'files' => [
'include' => [
base_path(),
],
'exclude' => [
base_path('vendor'),
base_path('node_modules'),
],
'followLinks' => false,
],
'databases' => [
'mysql',
],
],
'database_dump_compressor' => null,
'destination' => [
'filename_prefix' => '',
'disks' => [
'local',
],
],
'temporary_directory' => storage_path('app/backup-temp'),
],
'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@example.com',
],
'slack' => [
'webhook_url' => '',
'channel' => null,
'username' => null,
'icon' => null,
],
],
'monitorBackups' => [
[
'name' => config('app.name'),
'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
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',
]
],
Mongodb often requires authentication on production servers. By defeault it uses an authentication database. You can add the name of the admin database as an option to the dump
config key in the connection definition.
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
...,
'options' => [
'database' => 'admin'
],
'dump' => [
'mongodb_user_auth' => 'admin'
]
],
],