The Recipe Share application includes a comprehensive database backup system that provides automated, secure, and reliable backup functionality. The system supports multiple database drivers (MySQL, PostgreSQL, SQLite) and includes advanced features like compression, verification, and automatic cleanup.
- Multi-database support: MySQL, PostgreSQL, SQLite
- Flexible backup types: Full backup, structure only, data only
- Compression: Gzip compression with configurable levels
- Verification: Automatic backup integrity checking
- Retention policy: Automatic cleanup of old backups
- Security logging: Complete audit trail through SecurityLogService
- Command-line interface: Easy-to-use Artisan commands
The backup system is configured through config/backup.php. Key settings include:
'storage_path' => env('BACKUP_STORAGE_PATH', storage_path('app/backups')),
'filename_pattern' => env('BACKUP_FILENAME_PATTERN', '{database}_{date}_{time}.sql'),'compress' => env('BACKUP_COMPRESS', true),
'compression_level' => env('BACKUP_COMPRESSION_LEVEL', 6), // 1-9'retention_days' => env('BACKUP_RETENTION_DAYS', 30),
'max_backup_files' => env('BACKUP_MAX_FILES', 50),Add these variables to your .env file:
# Backup Configuration
BACKUP_STORAGE_PATH=/secure/backup/path
BACKUP_RETENTION_DAYS=30
BACKUP_MAX_FILES=50
BACKUP_COMPRESS=true
BACKUP_COMPRESSION_LEVEL=6
BACKUP_TYPE=full
BACKUP_VERIFY=true
BACKUP_NOTIFICATIONS_ENABLED=true
BACKUP_NOTIFY_FAILURE=true# Create a full backup with default settings
php artisan backup:database
# Create a compressed backup
php artisan backup:database --compress
# Create backup without verification
php artisan backup:database --no-verify# Full backup (structure + data) - default
php artisan backup:database --type=full
# Structure only (schema without data)
php artisan backup:database --type=structure_only
# Data only (no table structures)
php artisan backup:database --type=data_onlyphp artisan backup:database --list# Clean up based on retention policy
php artisan backup:database --cleanup# Use specific database connection
php artisan backup:database --connection=mysql# Create compressed full backup with verification
php artisan backup:database --type=full --compress
# Create structure-only backup for specific connection
php artisan backup:database --connection=pgsql --type=structure_only --compressFor automated daily backups, add to your crontab:
# Edit crontab
crontab -e
# Add daily backup at 2:00 AM
0 2 * * * cd /path/to/your/backend && php artisan backup:database --compress > /dev/null 2>&1
# Weekly cleanup at 3:00 AM every Sunday
0 3 * * 0 cd /path/to/your/backend && php artisan backup:database --cleanup > /dev/null 2>&1Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
// Daily backup at 2:00 AM
$schedule->command('backup:database --compress')
->dailyAt('02:00')
->environments(['production']);
// Weekly cleanup on Sundays at 3:00 AM
$schedule->command('backup:database --cleanup')
->weeklyOn(0, '03:00')
->environments(['production']);
}Default pattern: {database}_{date}_{time}.sql[.gz]
Example files:
recipeshare_2025-09-28_08-55-57.sql.gz(compressed)recipeshare_2025-09-28_08-55-57.sql(uncompressed)
storage/app/backups/
├── recipeshare_2025-09-28_08-55-57.sql.gz
├── recipeshare_2025-09-27_02-00-01.sql.gz
└── recipeshare_2025-09-26_02-00-01.sql.gz
All backup operations are logged through the SecurityLogService:
- Backup start/completion
- File sizes and durations
- Verification results
- Cleanup operations
- Error conditions
Sensitive or temporary tables are automatically excluded:
cachesessionsfailed_jobstelescope_entriestelescope_entries_tagstelescope_monitoring
Backup files are created with restricted permissions (755) and stored in a secure directory outside the web root.
Successful backup example:
🔄 Starting database backup...
✅ Backup completed successfully!
+------------+----------------------------------------+
| Property | Value |
+------------+----------------------------------------+
| File | recipeshare_2025-09-28_08-55-57.sql.gz |
| Size | 8.76 KB |
| Type | mysql |
| Compressed | Yes |
| Duration | 0.27s |
+------------+----------------------------------------+
📁 Backup saved to: /path/to/storage/app/backups/recipeshare_2025-09-28_08-55-57.sql.gz
Check Laravel logs for backup events:
tail -f storage/logs/laravel.log | grep backup# Fix backup directory permissions
chmod 755 storage/app/backups
chown -R www-data:www-data storage/app/backupsIf you see "Using a password on the command line interface can be insecure" warning, this is normal and doesn't affect functionality.
For very large databases, consider:
- Increasing PHP memory limit
- Using structure-only backups for development
- Implementing incremental backup strategies
- Uses
mysqldumpcommand - Requires MySQL client tools
- Supports all standard MySQL options
- Single transaction mode for consistency
- Uses
pg_dumpcommand - Requires PostgreSQL client tools
- Custom format for optimal compression
- Verbose output for detailed logging
- Direct file copying
- No external tools required
- Instant backup for small databases
- File locking considerations
# Uncompress if needed
gunzip backup_file.sql.gz
# Restore database
mysql -u username -p database_name < backup_file.sql# Restore from custom format
pg_restore -h host -U username -d database_name backup_file.sql# Simply copy the backup file back
cp backup_file.sql database.sqlite- Regular Testing: Periodically test backup restoration to ensure integrity
- Off-site Storage: Consider copying backups to remote storage for disaster recovery
- Monitoring: Set up alerts for backup failures
- Documentation: Keep recovery procedures documented and accessible
- Security: Encrypt backups containing sensitive data
- Retention: Balance storage costs with recovery requirements
Consider integrating with cloud storage services:
- AWS S3
- Google Cloud Storage
- Azure Blob Storage
Integrate with monitoring tools:
- Laravel Horizon for queue monitoring
- Application performance monitoring (APM)
- Log aggregation services
| Command | Description |
|---|---|
php artisan backup:database |
Create basic backup |
php artisan backup:database --compress |
Create compressed backup |
php artisan backup:database --type=structure_only |
Structure-only backup |
php artisan backup:database --type=data_only |
Data-only backup |
php artisan backup:database --list |
List all backups |
php artisan backup:database --cleanup |
Clean old backups |
php artisan backup:database --connection=name |
Use specific connection |
php artisan backup:database --no-verify |
Skip verification |
For additional help, use:
php artisan help backup:database