定制 aksoyhlc/databasebackup 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

aksoyhlc/databasebackup

Composer 安装命令:

composer require aksoyhlc/databasebackup

包简介

Advanced backup system for MySQL and MariaDB databases. This package allows you to easily backup, list, download, delete, and upload databases to FTP without using `mysqldump`.

README 文档

README

Backup system for MySQL and MariaDB databases. Create, list, download, delete, and upload backups to FTP without mysqldump.

Why Use This Package

Safe and Reliable

  • No shell_exec or exec required (mysqldump needs these)
  • PHP-based, no system command execution
  • Works under hosting provider restrictions

Use Cases

  • Shared hosting without mysqldump access
  • VPS and cloud servers
  • Automated backup systems
  • Web-based backup interfaces

Key Features

  • No mysqldump dependency
  • Full database backup without shell commands
  • Selective backup (exclude tables, structure-only, data-only)
  • Optional compression and FTP upload
  • Debug logging and progress tracking

Security

  • No system command execution risk
  • Compatible with hosting security rules
  • SSL/TLS for FTP connections

Features

  • Full database backup (tables, views, triggers, stored procedures)
  • Exclude specific tables or backup only structure or data
  • Gzip compression
  • Auto-cleanup by count or age
  • FTP/FTPS upload (automatic or manual)
  • Progress callback
  • Logging

Installation

composer require aksoyhlc/databasebackup

Basic Usage

<?php

use Aksoyhlc\Databasebackup\DatabaseBackupService;

// Basic configuration
$dbConfig = [
    'host' => 'localhost',      // Database server
    'dbname' => 'database',     // Database name
    'user' => 'root',           // Username
    'pass' => 'password',       // Password
    'charset' => 'utf8mb4',     // Character set (optional)
    'port' => 3306              // Port (optional)
];

// Backup directory
$backupPath = __DIR__ . '/backups';

// Initialize backup service
$backupService = new DatabaseBackupService($dbConfig, $backupPath);

// Create backup
$result = $backupService->createBackup();

if ($result['success']) {
    echo "Backup successful: " . $result['fileName'];
} else {
    echo "Error: " . $result['message'];
}

Advanced Usage

<?php

use Aksoyhlc\Databasebackup\DatabaseBackupService;

$dbConfig = [
    'host' => 'localhost',      // Database server
    'dbname' => 'database',     // Database name
    'user' => 'root',           // Username
    'pass' => 'password',       // Password
    'charset' => 'utf8mb4',     // Character set
    'port' => 3306              // Port number
];

$backupPath = __DIR__ . '/backups';

// Advanced configuration options
$options = [
    // ---- Cache Settings ----
    'cacheTime' => 3600,        // Cache duration (seconds)
    
    // ---- Backup File Cleanup Settings ----
    'maxBackupCount' => 10,     // Maximum number of backups (excess will be deleted automatically)
    'maxBackupAgeDays' => 30,   // Maximum backup age (days, older ones will be deleted automatically)
    
    // ---- Content Filtering Settings ----
    'excludedTables' => [       // Tables to exclude from backup
        'log_table', 
        'temp_data'
    ],
    'tableModes' => [           // Table backup modes
        // 'full': Table structure and data (default)
        // 'structure_only': Only table structure
        // 'data_only': Only table data
        'large_table' => 'structure_only',
        'settings' => 'full'
    ],
    
    // ---- Backup Format Settings ----
    'compressOutput' => true,   // Compress the backup file? (gzip)
    'removeDefiners' => true,   // Remove SQL DEFINER statements?
    
    // ---- Progress Tracking ----
    'progressCallback' => function($status, $current, $total) {
        // Callback function for progress status
        echo "{$status}: {$current}/{$total}\n";
    },
    
    // ---- FTP Settings ----
    'ftpConfig' => [
        'enabled' => true,      // Is FTP backup active?
        'host' => 'ftp.example.com',  // FTP server address
        'username' => 'ftpuser',      // FTP username
        'password' => 'ftppass',      // FTP password
        'port' => 21,                 // FTP port number
        'path' => '/backups',         // Remote directory path
        'ssl' => false,               // Use SSL?
        'passive' => true             // Use passive mode?
    ]
];

$backupService = new DatabaseBackupService($dbConfig, $backupPath, $options);

// Create backup
$result = $backupService->createBackup();

// List backups
$backups = $backupService->listBackups();
foreach ($backups as $backup) {
    echo "File: {$backup['file_name']}, Size: {$backup['size']}, Date: {$backup['date']}\n";
}

// Download a backup
$downloadInfo = $backupService->prepareDownload('backup_database_2023-01-01_12-00-00.sql.gz');
if ($downloadInfo['success']) {
    // Information for serving the file to the user
    $filePath = $downloadInfo['filePath'];
    $fileName = $downloadInfo['fileName'];
    $mimeType = $downloadInfo['mimeType'];
}

// Delete a backup
$deleteResult = $backupService->deleteBackup('backup_database_2023-01-01_12-00-00.sql.gz');

// Upload a backup to FTP
$uploadResult = $backupService->uploadBackupToFtp('backup_database_2023-01-01_12-00-00.sql.gz');

// Clean old backups
$backupService->cleanOldBackups();

Performance

DatabaseBackup uses streaming architecture and unbuffered queries to handle databases of any size with minimal resource usage. No mysqldump dependency required.

Benchmarks (1,050,000 records, 7 tables)

Mode File Size Duration
Uncompressed 1.33 GB 17.0 s
Gzip compressed 314 MB 57.5 s

Benchmark (Employees DB: 3,920,015 records, 6 tables + 2 views)

Mode File Size Duration
Gzip compressed 33.7 MB 46.6 s

vs mysqldump (1,050,000 records)

Tool File Size Duration
DatabaseBackup (stream) 1.33 GB 17.0 s
mysqldump 1.30 GB 11.8 s
DatabaseBackup (gzip) 314 MB 57.5 s
mysqldump + gzip (pipe) 314 MB 31.1 s

Key Performance Features

  • Streaming writes: SQL goes directly to disk, not accumulated in memory.
  • Unbuffered queries: One row at a time, no result set buffering.
  • Configurable batch size: Tune batchSize for INSERT performance.

Special Characters

UTF-8 characters, emoji, JSON, BLOB, backslash, and multi-line text are preserved during backup and restore.

Tested with: 😀🔥🎉💯✅, 👨‍👩‍👧‍👦, JSON, BLOB, multi-line text, O'Brien, C:\Users\path, null values.

Log Messages

// Record messages with different log levels
$backupService->logMessage("Custom information message", "INFO");
$backupService->error("An error occurred");
$backupService->info("Information message");
$backupService->debug("Detailed debugging information");

Connection Tests

// Test database connection
if ($backupService->testDatabaseConnection()) {
    echo "Database connection successful";
}

// Test FTP connection
if ($backupService->testFtpConnection()) {
    echo "FTP connection successful";
}

// Get database version
echo "Database version: " . $backupService->getDatabaseVersion();

Configuration Parameters

Database Configuration ($dbConfig)

Parameter Description Default
host Database server address 'localhost'
dbname Database name -
user Database username -
pass Database password -
charset Database character set 'utf8mb4'
port Database server port number 3306

General Configuration Options ($options)

Cache Settings

Parameter Description Default
cacheTime Cache duration (seconds) 3600

Backup File Cleanup Settings

Parameter Description Default
maxBackupCount Maximum number of backups to keep 10
maxBackupAgeDays Maximum backup age to keep (days) 365

Content Filtering Settings

Parameter Description Default
excludedTables Array of table names to exclude from backup []
tableModes Table backup modes ('full', 'structure_only', 'data_only') []

Backup Format Settings

Parameter Description Default
compressOutput Compress the backup file? (gzip) false
removeDefiners Remove SQL DEFINER statements? true
batchSize Number of rows per INSERT statement 100

Progress Tracking

Parameter Description Default
progressCallback Progress status notification function function($status, $current, $total) null

FTP Configuration (ftpConfig)

Parameter Description Default
enabled Is FTP backup active? false
host FTP server address ''
username FTP username ''
password FTP password ''
port FTP port number 21
path Remote directory path '/'
ssl Use SSL? false
passive Use passive mode? true

Requirements

  • PHP 7.4 or higher
  • PDO PHP Extension
  • MySQL or MariaDB database
  • For FTP operations: FTP PHP Extension
  • For compression: Zlib PHP Extension

License

This project is open-sourced software licensed under the GPL license

aksoyhlc/databasebackup 适用场景与选型建议

aksoyhlc/databasebackup 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 aksoyhlc/databasebackup 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-05-29