承接 rast/dbbackup 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rast/dbbackup

最新稳定版本:v1.7

Composer 安装命令:

composer require rast/dbbackup

包简介

Simple MySQL backup package for Laravel

README 文档

README

Issues Forks Stars Total Downloads License

A lightweight Laravel package for creating MySQL database backups via CLI or scheduler.

Features:

  • Custom backup directories
  • Configurable mysqldump path
  • Optional ZIP compression
  • Optional email notification with backup attachment
  • Restore backups from .sql or .zip files

🔧 Requirements

  • PHP 8.2+
  • Laravel 12.x
  • MySQL database
  • mysqldump installed and executable on the server

🚀 Installation

1. Add the package to your project

composer require rast/dbbackup

2. Register the Service Provider

If your package is not auto-discovered, add it in config/app.php:

'providers' => [
    // Other providers...
    RAST\DbBackup\DbBackupServiceProvider::class,
],

⚙️ Configuration

1. Publish the config

php artisan vendor:publish --provider="RAST\DbBackup\DbBackupServiceProvider" --tag=config

This creates: config/dbbackup.php

2. Config Options

return [

    'backup_path' => storage_path('app/db_backups'),

    'mysql_path' => env('MYSQL_PATH', 'C:/xampp/mysql/bin'),

    'filename' => 'backup_{db}_{date}.sql',

    'compression' => [
        'enabled' => true,
        'type' => 'zip',
    ],

    'cleanup' => [
        'enabled' => true,
        'keep_last' => 10,
    ],

    'email' => [
        'enabled' => false,
        'to' => 'admin@example.com',
        'subject' => 'Database Backup',
        'message' => 'Your scheduled database backup is attached.',
    ],

    'logging' => true,
];

Key Options:

Option Description
backup_path Directory to store backups
mysqldump_path Path to mysqldump (full path recommended)
filename Backup file naming pattern ({db} and {date} supported)
compression Enable ZIP compression
cleanup Auto-delete old backups, keeps latest n files
email Send backup via email
logging Log backup actions to laravel.log

3. Environment Variables

Add to .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_user
DB_PASSWORD=your_password

#windows
MYSQL_PATH=C:/xampp/mysql/bin
#linux
MYSQL_PATH=/usr/bin

On cPanel/shared hosting: usually /usr/bin On Windows/XAMPP: C:/xampp/mysql/bin

⚡ Usage

1. Manual Backup

php artisan rast:db-backup

Example output:

Backup complete: storage/app/db_backups/backup_mydb_2025_11_15_173027.sql.zip

2. Optional Command Flags

Flag Description
--no-zip Skip compression
--no-email Skip email
--no-clean Skip deleting old backups
--restore Restore the old backups

Backup Example:

php artisan rast:db-backup --no-zip --no-email

Restore Example:

php artisan rast:db-backup --restore=backup.sql
  • Supports .sql and .zip backups
  • Automatically extracts .zip before restoring
php artisan rast:db-backup --restore=backup_mydb_2025_11_15_173027.sql.zip

3. Scheduling Backups

In app/Console/Kernel.php:

protected function schedule(Schedule $schedule): void
{
    $schedule->command('rast:db-backup')->dailyAt('03:00');
}

Add cron entry:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

🔐 Security Recommendations

  • Limit backup_path permissions: chmod 770 storage/app/db_backups
  • Avoid storing backups in version control
  • Optionally encrypt backup files (ZIP with password or GPG)
  • For production, consider offloading backups to S3, FTP, or cloud storage

🛠 Troubleshooting

Issue Solution
mysqldump: command not found Set MYSQL_DUMP_PATH to full path: /usr/bin/mysqldump
Permission denied writing file Ensure backup_path exists and is writable
Backup file empty Check DB credentials and privileges
Email not sent Verify mail configuration in .env

🔄 Extending the Package

  • Multiple databases: Loop through multiple connections in config/database.php
  • Custom file names: Include environment, app name, or custom prefix
  • Cloud storage: Upload backups to S3, FTP, or remote servers
  • Additional compression formats: .gz, .tar.gz using PHP functions or CLI tools

Made with ❤️ by RAST

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-15