Skip to content

A Laravel package for database and storage backup with auto-cleanup.

Notifications You must be signed in to change notification settings

avcodewizard/laravel-backup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

7f610eb Β· Apr 14, 2025

History

1 Commit
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025
Apr 14, 2025

Repository files navigation

πŸ“¦ Laravel Backup Package

A simple Laravel package to automatically backup your database and storage directory, with a Blade-based UI to view, download, and delete backups.


πŸš€ Features

  • πŸ”„ Daily backup of database and storage (storage/app/public)
  • 🧼 Auto-delete backups older than configurable days (default 5 days)
  • 🧾 List, download, and delete backups via Blade UI
  • πŸ‘€ Access control using roles and middleware
  • πŸ›  Configurable via config/laravelBackup.php

πŸ“₯ Installation

Install the package via composer:

composer require avcodewizard/laravel-backup

βš™οΈ Configuration

Edit the config file at: config/laravelBackup.php

return [
    'backup_path' => storage_path('backups'),
    'keep_days' => 5, // Automatically delete backups older than 5 days
    'backup_storage_directory' => false, // true or false 
    'check_access' => false, // Enable/disable role-based access to UI
    'allowed_roles' => [], // Role Names Example: ['Admin', 'Super-Admin','Developer', 'Manager']
];
  • If you want's to backup storage directory
'backup_storage_directory' => true, // true or false 

πŸ›‘οΈ Access Control

To enable UI access control based on user roles:

  1. Set 'check_access' => true
  2. Add roles in 'allowed_roles' => ['Admin']
  3. Ensure your User model has a hasRole() method (e.g., using spatie/laravel-permission)

Middleware used:
Avcodewizard\LaravelBackup\Http\Middleware\CheckLaravelBackupAccess


πŸ–₯️ Web Interface

Access the UI at:

/laravel-backup

Example route setup (already included in the package):

Route::prefix('laravel-backup')
    ->middleware(['web', \Avcodewizard\LaravelBackup\Http\Middleware\CheckLaravelBackupAccess::class])
    ->group(function () {
        Route::get('/', [BackupController::class, 'index'])->name('laravel-backup.index');
        Route::get('/create', [BackupController::class, 'create'])->name('laravel-backup.create');
        Route::get('/download', [BackupController::class, 'download'])->name('laravel-backup.download');
        Route::delete('/delete', [BackupController::class, 'delete'])->name('laravel-backup.delete');
    });

πŸ›  Usage

Create Backup via Web

  1. Go to /laravel-backup
  2. Click Create Backup
  • If use want to create backup from ui, make sure to run the queue worker:
php artisan queue:work

Create Backup via Terminal

php artisan backup:run

🧹 Automatic Cleanup

Backups older than keep_days will be deleted automatically.

Add to Scheduler

In app/Console/Kernel.php, add:

$schedule->command('backup:run')->daily();

πŸ“‚ Backup Storage

Backups are saved in:

storage/backups/

Each backup includes:

  • YYYY-MM-DD-HH-MM-SS_database.sql
  • YYYY-MM-DD-HH-MM-SS_storage.zip

πŸ§‘β€πŸ’» Developer Notes

Publish Config & Views

php artisan vendor:publish --tag=laravel-backup

This will publish:

  • config/laravelBackup.php
  • Blade views to resources/views/vendor/laravel-backup/

Middleware Logic

The package uses a configurable middleware to restrict access:

if (!config('laravelBackup.check_access')) return $next($request);

$user = Auth::user();
if (!$user) {
    abort(403, 'Unauthorized - no user authenticated.');
}

if (!method_exists($user, 'hasRole')) {
    abort(403, 'User Role Not Implemented!');
}

if (!$user->hasAnyRole(config('laravelBackup.allowed_roles'))) {
    abort(403, 'Unauthorized - insufficient permission.');
}

return $next($request);

You can customize access logic using roles or your own permission methods.


πŸ“„ License

This package is open-sourced software licensed under the MIT license.

Β© 2025 Avcodewizard