spatie/nova-backup-tool
Laravel Nova tool for managing application backups via spatie/laravel-backup. View all backups, run new backups, download archives, and delete old backups from the Nova dashboard.
Installation
composer require spatie/nova-backup-tool
Publish the configuration file:
php artisan vendor:publish --provider="Spatie\NovaBackupTool\NovaBackupToolServiceProvider" --tag="nova-backup-tool-config"
Register the Tool
Add the tool to your Nova toolset in app/Providers/NovaServiceProvider.php:
public function tools()
{
return [
// ...
new \Spatie\NovaBackupTool\NovaBackupTool,
];
}
First Backup
spatie/laravel-backup package.config/nova-backup-tool.php – Define backup storage (e.g., S3, local disk), retention policies, and excluded directories.spatie/laravel-backup docs (e.g., adding databases, files, or custom commands).config/nova-backup-tool.php to auto-backup daily:
'schedule' => [
'command' => 'backup:run',
'expression' => '0 3 * * *', // Runs at 3 AM daily
],
'retention' => [
'keep_backups_for_days' => 30,
],
php artisan schedule:run
public function authorizedToAccessNova(Request $request)
{
return $request->user()->can('manage backups');
}
php artisan vendor:publish --tag="nova-backup-tool-views"
storage/backups (configured in config/nova-backup-tool.php).spatie/laravel-backup:
'storage' => [
'disk' => 's3',
'bucket' => 'my-backups',
],
Leverage spatie/laravel-backup to include/exclude items:
// config/backup.php
'sources' => [
\Spatie\Backup\Tasks\DatabaseTask::class,
\Spatie\Backup\Tasks\FilesTask::class => [
'directories' => [storage_path('app/public')],
],
],
Event Listeners
Hook into backup events (e.g., BackupStarted, BackupCompleted) to log or notify:
use Spatie\Backup\Events\BackupCompleted;
BackupCompleted::listen(function (BackupCompleted $event) {
Log::info('Backup completed', ['backup_name' => $event->backup->name]);
});
Nova Actions Add custom actions to backups (e.g., "Verify Backup"):
use Laravel\Nova\Actions\Action;
class VerifyBackup extends Action
{
public function handle(NovaRequest $request, $backup)
{
// Logic to verify backup integrity
return Action::message('Backup verified successfully.');
}
}
API Access Expose backup endpoints via Nova’s API for programmatic access:
Route::get('/api/backups', function () {
return \Spatie\NovaBackupTool\Backup::all();
});
Storage Permissions
storage/backups) is writable:
chmod -R 755 storage/backups
.env.Scheduler Conflicts
schedule:run, ensure the cron job runs frequently enough (e.g., every 5 minutes) to trigger the backup command:
* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
Large Backups
max_execution_time in php.ini if needed.Nova Caching
php artisan nova:cache-reset
Backup Failures
storage/logs/laravel.log for errors during backup execution.config/backup.php:
'logging' => 'verbose',
Tool Not Showing
NovaServiceProvider and the user has permissions.Storage Issues
php artisan backup:test
Exclude Sensitive Data
Use .env to exclude directories (e.g., storage/logs):
BACKUP_EXCLUDE_DIRECTORIES=storage/logs,storage/debug-bar
Compress Backups
Enable compression in config/backup.php to reduce storage usage:
'compression' => true,
Backup Verification
Add a BackupTask to verify backups post-creation:
'tasks' => [
\Spatie\Backup\Tasks\VerifyTask::class,
],
Multi-Environment Configs
Use environment-specific configs (e.g., config/backup-local.php) and load them conditionally:
$backupConfig = config('backup-' . app()->environment);
Nova Tool Icons
Customize the tool icon by overriding the icon property in your NovaBackupTool instance:
new \Spatie\NovaBackupTool\NovaBackupTool(['icon' => 'heroicon-o-cloud-arrow-up']),
Backup Notifications Integrate with Laravel Notifications to alert teams when backups complete:
BackupCompleted::listen(function () {
Notification::route('mail', 'admin@example.com')
->notify(new BackupCompletedNotification());
});
Testing Backups
Use spatie/laravel-backup's testing utilities to mock backups in PHPUnit:
$this->artisan('backup:run')->expectsOutput('Backup completed');
How can I help you explore Laravel packages today?