avcodewizard / laravel-backup
A Laravel package for database and storage backup with auto-cleanup.
Requires
- php: >=8.0
- illuminate/support: >=10.0
README
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:
- Set
'check_access' => true
- Add roles in
'allowed_roles' => ['Admin']
- Ensure your
User
model has ahasRole()
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
- Go to
/laravel-backup
- 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