avcodewizard/laravel-backup 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

avcodewizard/laravel-backup

Composer 安装命令:

composer require avcodewizard/laravel-backup

包简介

A Laravel package for database and storage backup with auto-cleanup.

README 文档

README

Total Downloads Latest Stable Version License

A simple Laravel package to automatically backup your database and storage directory to multiple destinations (Local, Amazon S3, Google Drive), with a Blade-based UI to view, download, and delete backups.

🚀 Features

  • 🔄 Daily backup of database and storage (storage/app/public)
  • ☁️ Multi-destination support: Local, Amazon S3, Google Drive
  • 🧼 Auto-delete backups older than configurable days (default 5 days)
  • 🎯 Configurable cleanup scope: clean all destinations or local only
  • 🧾 List, download, and delete backups from all destinations via Blade UI
  • 👤 Access control using roles and middleware
  • 🛠 Configurable via config/laravelBackup.php

📥 Installation

Install the package via composer:

composer require avcodewizard/laravel-backup

⚙️ Configuration

Edit the config file at: config/laravelBackup.php

return [
    'destinations' => [
        'local' => [
            'enabled' => true,
            'path' => storage_path('backups'),
        ],
        's3' => [
            'enabled' => false,
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'bucket' => env('AWS_BUCKET'),
            'endpoint' => env('AWS_ENDPOINT'), // Optional: for MinIO, DO Spaces
            'path' => 'backups',
        ],
        'google_drive' => [
            'enabled' => false,
            // OAuth 2.0 credentials - get refresh token using: php artisan backup:google-auth
            'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
            'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
            'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
            'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID'), // Optional: specific folder ID
            'path' => 'backups',
        ],
    ],
    'keep_days' => 5, // Automatically delete backups older than 5 days
    'cleanup_scope' => 'all', // 'all' = clean cloud + local, 'local' = clean local only
    'backup_storage_directory' => false, // true or false - storage backup only if s3 or google_drive enabled
    'check_access' => false, // Enable/disable role-based access to UI
    'allowed_roles' => [], // Role Names Example: ['Admin', 'Super-Admin','Developer', 'Manager']
];

Backup Destinations

Enable one or more destinations:

  • Local: Saves backups to local disk (storage/backups/ by default)
  • S3: Uploads backups to Amazon S3 (or compatible services like DigitalOcean Spaces, MinIO)
  • Google Drive: Uploads backups to Google Drive

When multiple destinations are enabled, backups are saved to all enabled destinations simultaneously.

Cleanup Scope

  • 'all': Delete old backups from all destinations (local + cloud)
  • 'local': Delete old backups from local storage only (cloud backups kept indefinitely)

Storage Directory Backup

  • When backup_storage_directory is true AND S3/Google Drive is enabled: Storage is backed up to cloud only (skips local to save disk space)
  • When only local storage is enabled: Storage backup is skipped even if backup_storage_directory is true

🛡️ Access Control

To enable UI access control based on user roles:

  1. Set 'check_access' => true
  2. Add roles in 'allowed_roles' => ['Admin']
  3. Ensure your User model has a hasRole() method (e.g., using spatie/laravel-permission)

Middleware used:
Avcodewizard\LaravelBackup\Http\Middleware\CheckLaravelBackupAccess

🖥️ Web Interface

Access the UI at:

/laravel-backup

Example route setup (already included in the package):

Route::prefix('laravel-backup')
    ->middleware(['web', \Avcodewizard\LaravelBackup\Http\Middleware\CheckLaravelBackupAccess::class])
    ->group(function () {
        Route::get('/', [BackupController::class, 'index'])->name('laravel-backup.index');
        Route::get('/create', [BackupController::class, 'create'])->name('laravel-backup.create');
        Route::get('/download', [BackupController::class, 'download'])->name('laravel-backup.download');
        Route::delete('/delete', [BackupController::class, 'delete'])->name('laravel-backup.delete');
    });

🛠 Usage

Create Backup via Web

  1. Go to /laravel-backup
  2. Click Create Backup
  • If use want to create backup from ui, make sure to run the queue worker:
php artisan queue:work

Create Backup via Terminal

php artisan backup:run

🧹 Automatic Cleanup

Backups older than keep_days will be deleted automatically.

Add to Scheduler

Laravel 11+ (routes/console.php):

use Illuminate\Support\Facades\Schedule;

Schedule::call(function () {
    Artisan::call('backup:run');
})->name('backup:run')->withoutOverlapping()->daily();

Laravel 10 and below (app/Console/Kernel.php):

$schedule->command('backup:run')->daily();

📂 Backup Storage

Backups are saved to all enabled destinations:

Local Storage

storage/backups/

Cloud Storage (S3, Google Drive)

Backups are stored in the configured path (default: backups/ folder)

File Naming

Each backup includes:

  • YYYY-MM-DD-HH-MM-SS_database.sql.gz - Database backup
  • YYYY-MM-DD-HH-MM-SS_storage.zip - Storage backup (cloud only)

🧑‍💻 Developer Notes

Publish Config & Views

php artisan vendor:publish --tag=laravel-backup

This will publish:

  • config/laravelBackup.php
  • Blade views to resources/views/vendor/laravel-backup/

Middleware Logic

The package uses a configurable middleware to restrict access:

if (!config('laravelBackup.check_access')) return $next($request);

$user = Auth::user();
if (!$user) {
    abort(403, 'Unauthorized - no user authenticated.');
}

if (!method_exists($user, 'hasRole')) {
    abort(403, 'User Role Not Implemented!');
}

if (!$user->hasAnyRole(config('laravelBackup.allowed_roles'))) {
    abort(403, 'Unauthorized - insufficient permission.');
}

return $next($request);

You can customize access logic using roles or your own permission methods.

☁️ Cloud Storage Setup

Amazon S3 / DigitalOcean Spaces / MinIO

  1. Install AWS SDK
composer require aws/aws-sdk-php
  1. Add AWS Credentials to .env
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-backup-bucket
# AWS_ENDPOINT=https://s3.custom-endpoint.com  # Optional: for MinIO, DO Spaces
  1. Enable S3 in config/laravelBackup.php
'destinations' => [
    's3' => [
        'enabled' => true,
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'bucket' => env('AWS_BUCKET'),
        'endpoint' => env('AWS_ENDPOINT'), // Optional: for MinIO, DO Spaces
        'path' => 'backups', // Folder in your bucket
    ],
],

Google Drive

OAuth 2.0

Uses YOUR Google Drive storage for backups. Generate a refresh token to authenticate.

  1. Install Google API Client
composer require google/apiclient
  1. Create OAuth Credentials

  2. Go to Google Cloud Console

  3. Create a new project or select existing

  4. Enable Google Drive API

  5. Go to APIs & ServicesCredentialsCreate CredentialsOAuth client ID

  6. Application type: Web application

  7. Authorized redirect URIs: http://localhost

  8. Copy your Client ID and Client Secret

  9. Add Test User (Required for Unverified Apps)

  10. In Google Cloud Console, go to APIs & ServicesOAuth consent screen (left sidebar)

  11. Make sure Publishing Status is "Testing"

  12. Go to Audience section

  13. Under Test users, click Add Users

  14. Add your Google email address

  15. Click Save

  16. Generate Refresh Token

Option A: Using credentials from config (if already set)

php artisan backup:google-auth

Option B: Passing credentials directly

php artisan backup:google-auth --client-id=YOUR_CLIENT_ID --client-secret=YOUR_CLIENT_SECRET

Follow the prompts to authorize and get your refresh token.

  1. Add to .env
GOOGLE_DRIVE_CLIENT_ID=your-client-id
GOOGLE_DRIVE_CLIENT_SECRET=your-client-secret
GOOGLE_DRIVE_REFRESH_TOKEN=your-refresh-token
GOOGLE_DRIVE_FOLDER_ID=your-folder-id  # Optional
  1. Enable in config
'destinations' => [
    'google_drive' => [
        'enabled' => true,
        'client_id' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refresh_token' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folder_id' => env('GOOGLE_DRIVE_FOLDER_ID'),
        'path' => 'backups',
    ],
],

Note: If your refresh token becomes invalid (user revoked access), regenerate it:

php artisan backup:google-auth

📄 License

This package is open-sourced software licensed under the MIT license.

© 2025 Avcodewizard

avcodewizard/laravel-backup 适用场景与选型建议

avcodewizard/laravel-backup 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「backup」 「laravel」 「database backup」 「storage backup」 「auto-backup」 「auto-cleanup」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 avcodewizard/laravel-backup 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 avcodewizard/laravel-backup 我们能提供哪些服务?
定制开发 / 二次开发

基于 avcodewizard/laravel-backup 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-14