amoracr/yii2-backup

用于创建 Yii2 网站备份的组件。

资助包维护!
Paypal

安装数: 3,847

依赖项: 0

建议者: 0

安全性: 0

星标: 4

关注者: 2

分叉: 8

开放性问题: 4

类型:yii2-extension

1.4.0 2022-01-02 00:35 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:46 UTC


README

为 Yii2 应用程序提供备份和恢复功能。

本扩展基于

我将这些源代码组合起来,创建了一个更强大、更易于使用的扩展。

支持的数据库

  • MySQL
  • MariaDB(通过 MySQL 驱动程序)
  • SQLite
  • PostgreSQL

支持的压缩方法

  • Bzip2
  • Gzip
  • Zip

默认情况下,备份文件是一个包含 SQL 导出和文件夹的 tar 文件。

当前限制

  • 需要 Linux 系统。
  • 目前仅支持本地主机的 MySQL。
  • 目前仅支持本地主机的 MariaDB。
  • 目前仅支持本地主机的 PostgreSQL。

入门指南

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist amoracr/yii2-backup "*"

或者将以下内容添加到您的 composer.json 文件的 require 部分:

"amoracr/yii2-backup": "*"

配置

扩展安装后,将其添加到您的配置文件中

基本的 config/console.php

高级 console/config/main.php

最小配置

'components' => [
    ...
    'backup' => [
            'class' => 'amoracr\backup\Backup',
            // Path for storing the backups
            'backupDir' => '@app/backups',
            // Directories that will be added to backup
            'directories' => [
                // format: <inner backup filename> => <path/to/dir>
                'images' => '@app/web/images',
                'uploads' => '@app/web/uploads',
            ],
        ],
]

将创建以下备份

目录
/web/images/*
/web/uploads/*
数据库
Yii::$app->db

结果

/backups/2020-06-29T182436-0600_backup.tar/
>images/
>uploads/
>sql/db.sql

高级配置

'components' => [
    ...
    'backup' => [
            'class' => 'amoracr\backup\Backup',
            // Name for the backup
            'fileName' => 'myapp-backup',
            // Maximum age in seconds for a valid backup.
            // Older files are considered deprecated and can be deleted.
            // Minimum age is 86400 secs (1 day).
            // Maximum age is 31536000 secs (1 year).
            'expireTime'=> 86400 * 3,
            // Path for storing the backups
            'backupDir' => '@app/backups',
            // Database components to backup
            'databases' => ['db', 'db1'],
            // Compression method to apply to backup file.
            // Available options:
            // 'none' or 'tar' for tar files, backup file is not compressed.
            // 'bzip2' for tar.bz2 files, backup file is compressed with Bzip2 compression.
            // 'gzip' for tar.gz files, backup file is compressed with Gzip compression.
            // 'zip' for zip files, backup file is compressed with Zip compression.
            'compression' => 'zip',
            // Directories that will be added to backup
            'directories' => [
                // format: <inner backup filename> => <path/to/dir>
                'images' => '@app/web/images',
                'uploads' => '@app/web/uploads',
                // format: <inner backup filename> => array('path'=><path/to/dir>,'regex'=><regular/expression/>)
                // Key 'path' for setting the directory to include
                // Key 'regex' for setting the regular expression for selecting the files to include
                'pdf' => [
                   'path' => '@app/web/documents',
                   'regex' => '/\.pdf$/',
                ],
            ],
            // Files to avoid in backup accross all directories
            'skipFiles' => [
                '.gitignore',
            ]
        ],
]

结果

/backups/2020-06-29T182436-0600_myapp-backup.zip/
>images/
>uploads/
>pdf/
>sql/db.sql
>sql/db1.sql

用法

您可以在控制台命令中使用此组件。

/console/controllers/BackupController.php:/

<?php
namespace console\controllers;

class BackupController extends \yii\console\Controller
{
    public function actionBackup()
    {
        $backup = \Yii::$app->backup;
        $databases = ['db', 'db1', 'db2'];
        foreach ($databases as $k => $db) {
            $index = (string)$k;
            $backup->fileName = 'myapp-part';
            $backup->fileName .= str_pad($index, 3, '0', STR_PAD_LEFT);
            $backup->directories = [];
            $backup->databases = [$db];
            $file = $backup->create();
            $this->stdout('Backup file created: ' . $file . PHP_EOL, \yii\helpers\Console::FG_GREEN);
        }
    }
}