redaelfillali / laravel-backup
Laravel Backup solution for shared hosting
Requires
- php: ^8.1
- google/apiclient: ^2.19
- illuminate/support: ^10.0|^11.0|^12.0
- league/flysystem: ^3.32
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
README
A simple Laravel package for backing up files and databases with support for different backup types and scheduling. This package allows you to perform backups without the use of proc_open, and can be triggered via a console command or a custom controller route.
Requirements
- Laravel: ^10.0 | ^11.0 | ^12.0
- PHP:
- For Laravel 10: ^8.1
- For Laravel 11 and 12: ^8.2
Features
- Backup Path: Easily set the destination folder for backups.
- Backup Types:
- Full Backup (both files and database)
- Files Only
- Database Only
- No
proc_openDependency: The package works without relying onproc_open, ensuring compatibility with restrictive hosting environments. - Command and Controller Support: Can be used via a console command or integrated into your application with a custom controller and route.
- Scheduled Backups: Utilize Laravel's task scheduling system to automate backups.
- Retention Cleanup: When running database backups, the package automatically deletes backup files older than a configurable number of days from the configured
backup.pathdirectory.
Installation
You can install this package via Composer:
composer require redaelfillali/laravel-backup
Configuration
After installation, publish the configuration file using the following Artisan command:
php artisan vendor:publish --provider="Redaelfillali\LaravelBackup\BackupServiceProvider"
This will copy the configuration file to the config/backup.php file, where you can adjust settings such as the backup path, timeout, and retention period.
Usage
Running Backups via Command
You can run a backup using the following Artisan command:
php artisan backup:run
Specify a backup type (full, files, or database):
php artisan backup:run full php artisan backup:run database php artisan backup:run files
Override the backup path:
php artisan backup:run full --path=/your/custom/path
Running Backups via Controller
You can trigger a backup by sending a POST request to the built-in routes or by calling BackupManager directly:
use Redaelfillali\LaravelBackup\Helpers\BackupManager; class BackupController extends Controller { public function createBackup() { BackupManager::backup('full', config('backup.path')); return response()->json(['message' => 'Backup completed successfully']); } }
Built-in Routes
The package registers the following POST routes automatically:
| Method | URI | Description |
|---|---|---|
| POST | /backup/run |
Run a backup (pass type and optional path in the request body) |
| POST | /backup/database |
Run a database-only backup |
| POST | /backup/files |
Run a files-only backup |
Example request using the /backup/run route:
Route::post('/trigger-backup', function () { return Http::post(url('/backup/run'), ['type' => 'database']); });
Scheduling Backups
You can schedule backups using Laravel's task scheduling system. Add the following to your App\Console\Kernel class (Laravel 10) or routes/console.php (Laravel 11+):
Laravel 10 — app/Console/Kernel.php:
use Illuminate\Console\Scheduling\Schedule; protected function schedule(Schedule $schedule) { $schedule->command('backup:run database') ->daily(); }
Laravel 11+ — routes/console.php:
use Illuminate\Support\Facades\Schedule; Schedule::command('backup:run database')->daily();
Custom Route Definition
If you prefer to define your own route, you can do so by calling BackupManager directly:
use Redaelfillali\LaravelBackup\Helpers\BackupManager; Route::post('/backup', function () { BackupManager::backup('full', config('backup.path')); return response()->json(['message' => 'Backup completed successfully']); })->middleware('auth');
Note: It is strongly recommended to protect backup routes with authentication middleware to prevent unauthorised access.
Configuration Reference
| Key | Default | Description |
|---|---|---|
path |
storage/backups/Y/m/d |
Destination directory for backup files |
types |
['full', 'files', 'database'] |
Available backup types |
timeout |
3600 |
Maximum execution time in seconds |
retention_period |
7 |
Days to keep old backups before automatic deletion |
backup_name |
backup-Y-m-d-H-i-s |
Filename prefix for database backup files |
enable_notifications |
true |
Enable/disable backup notifications |
Testing
The package includes a full test suite built with PHPUnit and Orchestra Testbench.
Running the Tests
Install the development dependencies and run the test suite:
composer install
composer test
Or run PHPUnit directly:
./vendor/bin/phpunit
Test Structure
tests/
├── TestCase.php # Base test case (Orchestra Testbench)
├── Unit/
│ └── BackupManagerTest.php # Unit tests for BackupManager
└── Feature/
├── BackupCommandTest.php # Feature tests for the Artisan command
└── BackupControllerTest.php # Feature tests for the HTTP routes
| Suite | File | Tests |
|---|---|---|
| Unit | BackupManagerTest |
Directory creation, zip validity, no duplicate entries, SQL dump content, PDO quoting, null value handling, dynamic DB connection, retention cleanup, full backup |
| Feature | BackupCommandTest |
All backup types, default type, configured path, exception → exit code 1 |
| Feature | BackupControllerTest |
All three routes, invalid type → HTTP 422, exception → HTTP 500 |
License
This package is open-source software licensed under the MIT license.