System Backup
Module for System Backup Management.
Overview
The System Backup module provides comprehensive backup management capabilities for your Open Core Business Suite installation. Built on the robust Spatie Laravel Backup package, it enables administrators to create, schedule, and manage backups of both the database and application files.
Features
- Database Backups: Full database dumps with compression
- File Backups: Backup application files and uploads
- Scheduled Backups: Automated backups on configurable schedules
- Backup Download: Download backup files directly from the admin panel
- Backup Restoration: Restore from previous backups
- Multiple Storage: Store backups locally, on cloud storage, or both
- Backup Notifications: Email alerts for backup success/failure
- Retention Policies: Automatic cleanup of old backups
- Backup Health Monitoring: Monitor backup status and disk usage
Requirements
- Open Core Business Suite (Base System)
- No additional module dependencies
- Spatie Laravel Backup (included)
- Sufficient storage space for backups
Installation
- Enable the module in your module configuration:
// config/modules.php or via admin panel
'SystemBackup' => true,
- Run the module migrations:
php artisan module:migrate SystemBackup
- Publish the backup configuration:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
Configuration
Module Settings
Access backup settings through Settings > System Backup:
| Setting | Description | Default |
|---|---|---|
| Enable Backups | Enable/disable backup functionality | Enabled |
| Backup Database | Include database in backups | Yes |
| Backup Files | Include application files | Yes |
| Storage Disk | Where to store backups | local |
| Retention Days | Days to keep daily backups | 7 |
| Retention Weeks | Weeks to keep weekly backups | 4 |
| Retention Months | Months to keep monthly backups | 6 |
| Notification Email | Email for backup notifications | - |
Backup Configuration File
For advanced configuration, edit config/backup.php:
return [
'backup' => [
'name' => env('APP_NAME', 'laravel-backup'),
'source' => [
'files' => [
'include' => [
base_path(),
],
'exclude' => [
base_path('vendor'),
base_path('node_modules'),
storage_path('app/backups'),
],
],
'databases' => [
'mysql',
],
],
'destination' => [
'filename_prefix' => '',
'disks' => [
'local',
// 's3', // Uncomment for cloud backup
],
],
],
'cleanup' => [
'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class,
'default_strategy' => [
'keep_all_backups_for_days' => 7,
'keep_daily_backups_for_days' => 16,
'keep_weekly_backups_for_weeks' => 8,
'keep_monthly_backups_for_months' => 4,
'keep_yearly_backups_for_years' => 2,
'delete_oldest_backups_when_using_more_megabytes_than' => 5000,
],
],
];
Cloud Storage Setup
To backup to cloud storage (e.g., AWS S3):
- Configure your cloud disk in
config/filesystems.php - Add the disk name to
backup.destination.disks - Set up appropriate credentials in
.env
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-backup-bucket
Usage
Manual Backup
Via Admin Panel
- Navigate to Settings > System Backup
- Click Create Backup Now
- Select backup type (Full, Database Only, or Files Only)
- Wait for backup to complete
- Download or verify the backup
Via Command Line
# Full backup
php artisan backup:run
# Database only
php artisan backup:run --only-db
# Files only
php artisan backup:run --only-files
# To specific disk
php artisan backup:run --only-to-disk=s3
Scheduled Backups
Configure automatic backups in your scheduler (app/Console/Kernel.php):
protected function schedule(Schedule $schedule)
{
// Daily backup at 1:00 AM
$schedule->command('backup:run')->daily()->at('01:00');
// Weekly cleanup of old backups
$schedule->command('backup:clean')->weekly()->at('02:00');
// Daily backup monitoring
$schedule->command('backup:monitor')->daily()->at('03:00');
}
Or configure via the admin panel schedule settings.
Viewing Backups
Via Admin Panel
- Go to Settings > System Backup > Backup List
- View all available backups with:
- Backup date and time
- Backup size
- Storage location
- Download and delete options
Via Command Line
php artisan backup:list
Downloading Backups
- Navigate to the backup list
- Click the download icon for the desired backup
- The backup ZIP file will download to your browser
Restoring from Backup
Database Restoration
# Extract backup
unzip backup-2024-01-15.zip
# Restore database
mysql -u username -p database_name < db-dumps/mysql-database.sql
Full Restoration
- Extract the backup archive
- Restore database from the SQL dump
- Copy files back to their original locations
- Clear application caches:
php artisan cache:clear
php artisan config:clear
php artisan view:clear
Backup Cleanup
Remove old backups according to retention policy:
php artisan backup:clean
Monitoring Backup Health
Check backup status and storage:
php artisan backup:monitor
API Endpoints
List Backups
GET /api/V1/system/backups
Create Backup
POST /api/V1/system/backups
Content-Type: application/json
{
"type": "full" // Options: full, database, files
}
Download Backup
GET /api/V1/system/backups/{filename}/download
Delete Backup
DELETE /api/V1/system/backups/{filename}
Backup Status
GET /api/V1/system/backups/status
Notifications
Configure email notifications for backup events:
// config/backup.php
'notifications' => [
'notifications' => [
\Spatie\Backup\Notifications\Notifications\BackupHasFailed::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFound::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\CleanupHasFailed::class => ['mail'],
\Spatie\Backup\Notifications\Notifications\BackupWasSuccessful::class => ['mail'],
],
'mail' => [
'to' => '[email protected]',
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
],
],
Best Practices
- Regular Testing: Periodically test backup restoration
- Offsite Storage: Keep copies on cloud storage or external drives
- Monitor Disk Space: Ensure adequate storage for backups
- Secure Backups: Encrypt backups containing sensitive data
- Document Procedures: Maintain restoration documentation
- Verify Completeness: Regularly check backup contents
Troubleshooting
Backup Fails with Memory Error
Increase PHP memory limit:
// In backup command or php.ini
ini_set('memory_limit', '512M');
Backup Too Large
- Exclude unnecessary files (vendor, node_modules)
- Use database-only backups more frequently
- Increase storage allocation
Backup Timeout
For large backups, increase timeout:
php artisan backup:run --timeout=3600
Cannot Download Large Backups
- Use command line to copy backups
- Configure direct download links
- Use cloud storage with signed URLs
Storage Disk Full
- Review retention policies
- Run cleanup manually:
php artisan backup:clean - Move old backups to archive storage