Database backup and restore with gzip compression, multi-driver support, and Telegram integration.
Use this skill when implementing database backup/restore workflows, configuring backup compression, or setting up automated Telegram backup notifications.
In config/hdd-laravel-helpers.php:
'database-backup' => [
'gzip_compress' => env('HDD_DATABASE_BACKUP_GZIP_COMPRESS', true),
'mysqldump_binary' => env('HDD_DATABASE_BACKUP_MYSQLDUMP_BINARY', 'mysqldump'),
'mysql_client_binary' => env('HDD_DATABASE_BACKUP_MYSQL_CLIENT_BINARY', 'mysql'),
'gzip_binary' => env('HDD_DATABASE_BACKUP_GZIP_BINARY', 'gzip'),
],
Environment variables:
HDD_DATABASE_BACKUP_GZIP_COMPRESS=true
HDD_DATABASE_BACKUP_MYSQLDUMP_BINARY=mysqldump
HDD_DATABASE_BACKUP_MYSQL_CLIENT_BINARY=mysql
HDD_DATABASE_BACKUP_GZIP_BINARY=gzip
use HassanDomeDenea\HddLaravelHelpers\Services\DatabaseBackupManager;
$manager = new DatabaseBackupManager();
// Create backup (returns file path)
$backupPath = $manager->backup('local'); // Uses Storage disk name
// Restore from backup
$manager->restore($backupPath);
// Restore and run migrations after
$manager->restore($backupPath, migrate: true);
| Driver | Backup Tool | Restore Tool |
|---|---|---|
| MySQL / MariaDB | mysqldump | mysql client |
| SQLite | File copy | File copy |
| PostgreSQL | pg_dump | psql |
Uses mysqldump with --single-transaction --quick --skip-lock-tables flags.
Password is passed via MYSQL_PWD environment variable (not on command line).
Simple file copy of the database file.
Uses pg_dump for backup and psql for restore.
Password is passed via PGPASSWORD environment variable.
When gzip_compress is enabled (default), backups are compressed after creation:
.gz extension appended\x1F\x8B), not file extensionFiles are named: {app-name}_{connection}_{timestamp}.{ext}
Example: my-app_mysql_20260307_143022.sql.gz
Send backups to a Telegram channel automatically:
HDD_TELEGRAM_BOT_TOKEN=your-bot-token
HDD_TELEGRAM_BACKUP_CHAT_ID=your-chat-id
In config/hdd-laravel-helpers.php:
'telegram' => [
'bot_token' => env('HDD_TELEGRAM_BOT_TOKEN'),
'backup_chat_id' => env('HDD_TELEGRAM_BACKUP_CHAT_ID'),
],
php artisan backup:telegram
This runs BackupDatabaseToTelegramCommand which creates a backup and sends it to the configured Telegram chat.
// In app/Console/Kernel.php or routes/console.php
Schedule::command('backup:telegram')->daily();
use HassanDomeDenea\HddLaravelHelpers\Services\DatabaseBackupManager;
use Illuminate\Support\Facades\Storage;
// Backup workflow
$manager = new DatabaseBackupManager();
// Create backup on local disk
$backupPath = $manager->backup('local');
// Copy to S3 for offsite storage
$filename = basename($backupPath);
Storage::disk('s3')->put(
"backups/{$filename}",
file_get_contents($backupPath)
);
// Restore workflow
$manager->restore($backupPath, migrate: true);