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

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

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

redaelfillali/laravel-backup

最新稳定版本:1.1.1

Composer 安装命令:

composer require redaelfillali/laravel-backup

包简介

Laravel Backup solution for shared hosting

README 文档

README

A simple Laravel package for backing up files and databases with support for different backup types and scheduling. This package allows you to perform backups without the use of proc_open, and can be triggered via a console command or a custom controller route.

Requirements

  • Laravel: ^10.0 | ^11.0 | ^12.0
  • PHP:
    • For Laravel 10: ^8.1
    • For Laravel 11 and 12: ^8.2

Features

  • Backup Path: Easily set the destination folder for backups.
  • Backup Types:
    • Full Backup (both files and database)
    • Files Only
    • Database Only
  • No proc_open Dependency: The package works without relying on proc_open, ensuring compatibility with restrictive hosting environments.
  • Command and Controller Support: Can be used via a console command or integrated into your application with a custom controller and route.
  • Scheduled Backups: Utilize Laravel's task scheduling system to automate backups.
  • Retention Cleanup: When running database backups, the package automatically deletes backup files older than a configurable number of days from the configured backup.path directory.

Installation

You can install this package via Composer:

composer require redaelfillali/laravel-backup

Configuration

After installation, publish the configuration file using the following Artisan command:

php artisan vendor:publish --provider="Redaelfillali\LaravelBackup\BackupServiceProvider"

This will copy the configuration file to the config/backup.php file, where you can adjust settings such as the backup path, timeout, and retention period.

Usage

Running Backups via Command

You can run a backup using the following Artisan command:

php artisan backup:run

Specify a backup type (full, files, or database):

php artisan backup:run full
php artisan backup:run database
php artisan backup:run files

Override the backup path:

php artisan backup:run full --path=/your/custom/path

Running Backups via Controller

You can trigger a backup by sending a POST request to the built-in routes or by calling BackupManager directly:

use Redaelfillali\LaravelBackup\Helpers\BackupManager;

class BackupController extends Controller
{
    public function createBackup()
    {
        BackupManager::backup('full', config('backup.path'));

        return response()->json(['message' => 'Backup completed successfully']);
    }
}

Built-in Routes

The package registers the following POST routes automatically:

Method URI Description
POST /backup/run Run a backup (pass type and optional path in the request body)
POST /backup/database Run a database-only backup
POST /backup/files Run a files-only backup

Example request using the /backup/run route:

Route::post('/trigger-backup', function () {
    return Http::post(url('/backup/run'), ['type' => 'database']);
});

Scheduling Backups

You can schedule backups using Laravel's task scheduling system. Add the following to your App\Console\Kernel class (Laravel 10) or routes/console.php (Laravel 11+):

Laravel 10 — app/Console/Kernel.php:

use Illuminate\Console\Scheduling\Schedule;

protected function schedule(Schedule $schedule)
{
    $schedule->command('backup:run database')
             ->daily();
}

Laravel 11+ — routes/console.php:

use Illuminate\Support\Facades\Schedule;

Schedule::command('backup:run database')->daily();

Custom Route Definition

If you prefer to define your own route, you can do so by calling BackupManager directly:

use Redaelfillali\LaravelBackup\Helpers\BackupManager;

Route::post('/backup', function () {
    BackupManager::backup('full', config('backup.path'));
    return response()->json(['message' => 'Backup completed successfully']);
})->middleware('auth');

Note: It is strongly recommended to protect backup routes with authentication middleware to prevent unauthorised access.

Configuration Reference

Key Default Description
path storage/backups/Y/m/d Destination directory for backup files
types ['full', 'files', 'database'] Available backup types
timeout 3600 Maximum execution time in seconds
retention_period 7 Days to keep old backups before automatic deletion
backup_name backup-Y-m-d-H-i-s Filename prefix for database backup files
enable_notifications true Enable/disable backup notifications

Testing

The package includes a full test suite built with PHPUnit and Orchestra Testbench.

Running the Tests

Install the development dependencies and run the test suite:

composer install
composer test

Or run PHPUnit directly:

./vendor/bin/phpunit

Test Structure

tests/
├── TestCase.php                       # Base test case (Orchestra Testbench)
├── Unit/
│   └── BackupManagerTest.php          # Unit tests for BackupManager
└── Feature/
    ├── BackupCommandTest.php          # Feature tests for the Artisan command
    └── BackupControllerTest.php       # Feature tests for the HTTP routes
Suite File Tests
Unit BackupManagerTest Directory creation, zip validity, no duplicate entries, SQL dump content, PDO quoting, null value handling, dynamic DB connection, retention cleanup, full backup
Feature BackupCommandTest All backup types, default type, configured path, exception → exit code 1
Feature BackupControllerTest All three routes, invalid type → HTTP 422, exception → HTTP 500

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固