Filament Spatie Laravel Backup

repository·main·Indexed 42 days ago

https://github.com/shuvroroy/filament-spatie-laravel-backup

A Filament-based user interface for managing application backups, leveraging spatie/laravel-backup to create, monitor, download, and delete backups directly from the Filament admin panel.

Tokens
6.7K
Snippets
32
Records
37
Agent score
53%

What's inside Filament Spatie Laravel Backup

  1. Manage backup action permissions

    main

    The package uses Laravel Gates to control access to specific backup actions. By default, it checks for the following abilities:

    • create-backup: Permission to create a new backup.
    • download-backup: Permission to download an existing backup.
    • delete-backup: Permission to delete an existing backup.

    You can define these in your AppServiceProvider using standard Laravel Gate::define calls. If you use Spatie Laravel Permission or Filament Shield, register these exact permission names within those packages.

    Gate::define('create-backup', fn (User $user): bool => (bool) $user->is_admin);
    Gate::define('download-backup', fn (User $user): bool => (bool) $user->is_admin);
    Gate::define('delete-backup', fn (User $user): bool => (bool) $user->is_admin);
  2. Configure the backup queue

    main

    By default, backup jobs use the application's default queue. For production, it is highly recommended to use a non-sync connection and run a dedicated worker to avoid web server timeouts.

    Use usingQueueConnection(string $connection) and usingQueue(string $queue) to route jobs.

    ->plugin(
        FilamentSpatieLaravelBackupPlugin::make()
            ->usingQueueConnection('redis')
            ->usingQueue('backups')
    )
    
    // Run the worker:
    // php artisan queue:work redis --queue=backups
  3. Customise the backup job timeout

    main

    You can control the timeout for the backup job using timeout(int $seconds) or noTimeout().

    • timeout($seconds): Sets the Laravel per-job queue timeout and attempts to set the PHP set_time_limit().
    • noTimeout(): Sets the timeout to 0 to allow the job to run indefinitely (subject to queue worker memory limits).
    ->plugin(
        FilamentSpatieLaravelBackupPlugin::make()
            ->timeout(120)
    )
    
    // Or to disable timeout:
    ->plugin(
        FilamentSpatieLaravelBackupPlugin::make()
            ->noTimeout()
    )
  4. Customise the Backups page

    main

    If you need to change the page's behavior (e.g., changing the heading), extend the package's base page class and then register your custom class in your AdminPanelProvider using the usingPage() method.

    <?php
    
    namespace App\
    Filament\Pages;
    
    use ShuvroRoy\\FilamentSpatieLaravelBackup\\Pages\\Backups as BaseBackups;
    
    class Backups extends BaseBackups
    {
        public function getHeading(): string
        {
            return 'Application Backups';
        }
    }
    
    // In AdminPanelProvider.php
    ->plugin(
        FilamentSpatieLaravelBackupPlugin::make()
            ->usingPage(Backups::class)
    );
  5. Install Filament Spatie Laravel Backup

    main

    Install the package via Composer and publish the necessary configuration and assets.

    1. Install the package:
    composer require shuvroroy/filament-spatie-laravel-backup
    1. Publish the Spatie Laravel Backup configuration to define your backup settings (name, sources, destinations, etc.):
    php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" --tag="backup-config"
    1. Publish the package's Filament assets:
    php artisan filament:assets
    1. (Optional) Publish the translation files:
    php artisan vendor:publish --tag="filament-spatie-backup-translations"
  6. Optimise for large or remote backup destinations

    main

    When using slow remote disks (like S3), you can tune the plugin to improve performance:

    • cacheDuration(int $seconds): Cache destination metadata. Default is 30s. Use 0 to disable.
    • backupLimit(?int $limit): Limit the table to the newest $N$ backups. Use null to show all.
    • statusListRecordsTable(bool $show): Hide the health summary table to reduce load. Default is true.
    FilamentSpatieLaravelBackupPlugin::make()
        ->cacheDuration(60)
        ->backupLimit(15)
        ->statusListRecordsTable(false)
  7. Upgrade from v3.x to v4.0

    main

    To upgrade to version 4.0, update your package constraint and dependencies using Composer:

    composer require shuvroroy/filament-spatie-laravel-backup:^4.0 --with-all-dependencies

    Supported Versions (v4.0)

    • PHP: 8.2–8.5
    • Laravel: 12–13
    • Filament: 4–5
    • Spatie Laravel Backup: 9–10

    Note: PHP 8.2 applications must use Laravel 12 and Spatie Laravel Backup 9. Laravel 13 and Spatie Laravel Backup 10 require a newer PHP version.

    If you have not published Spatie's configuration, you should do so now to ensure compatibility with the health_checks key:

    php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" --tag="backup-config"
  8. Configure backup queue for production

    main

    While the sync queue works for local development, production environments should process backups asynchronously to avoid blocking Livewire requests.

    1. Set a supported queue connection in your .env:
    QUEUE_CONNECTION=database
    1. Ensure a queue worker is running:
    php artisan queue:work database
  9. Customise navigation icon, label, group, and sort order

    main

    You can modify how the Backups page appears in the Filament navigation without extending the page class. Use the following methods on the plugin instance:

    • navigationIcon(string $icon): Set the Heroicon (default: heroicon-o-archive-box-arrow-down).
    • navigationLabel(string|Closure $label): Set the display name.
    • navigationGroup(string|Closure|null $group): Set the group. Pass null to remove it from any group.
    • navigationSort(int $sort): Set the sort order.

    All these methods accept closures for dynamic values.

    <?php
    
    use ShuvroRoy\\FilamentSpatieLaravelBackup\\FilamentSpatieLaravelBackupPlugin;
    
    // ... inside panel()
    ->plugin(
        FilamentSpatieLaravelBackupPlugin::make()
            ->navigationIcon('heroicon-o-server-stack')
            ->navigationLabel('Backups')
            ->navigationGroup('Settings')
            ->navigationSort(3)
    );
  10. Configure Queue Connection for Production in v4.0

    main

    In v4.0, backup jobs are dispatched immediately. Using a sync queue connection will perform the backup within the Livewire request, which may cause web server or proxy timeouts.

    Production deployments should use an asynchronous connection (e.g., redis) and ensure a worker is running:

    FilamentSpatieLaravelBackupPlugin::make()
        ->usingQueueConnection('redis')
        ->usingQueue('backups');
  11. Configure the plugin in PanelProvider (v2.0+)

    main

    Starting from v2.0, the package requires registration within a Filament PanelProvider. If you previously used a configuration file to set backups, interval, or queue values, these must now be set via method calls on the plugin object during registration.

    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugin(
                FilamentSpatieLaravelBackupPlugin::make()
                    ->usingPage(Backups::class)
                    ->usingQueue('my-queue')
                    ->usingPollingInterval('10s')
                    ->statusListRecordsTable(false)
            );
    }
    <?php
    
    namespace App\Providers\Filament;
    
    use Filament\Panel;
    use Filament\PanelProvider;
    use App\Filament\Pages\Backups;
    use ShuvroRoy\FilamentSpatieLaravelBackup\FilamentSpatieLaravelBackupPlugin;
    
    class AdminPanelProvider extends PanelProvider
    {
        public function panel(Panel $panel): Panel
        {
            return $panel
                // ...
                ->plugin(
                    FilamentSpatieLaravelBackupPlugin::make()
                        ->usingPage(Backups::class)
                        ->usingQueue('my-queue')
                        ->usingPollingInterval('10s') // default value is 30s
                        ->statusListRecordsTable(false) // default value is true
                );
        }
    }