小熊猫/flysystem-mysql-backup

此包已被弃用且不再维护。未建议替代包。

将MySql数据库导出到'Flysystem'文件系统的实用工具

0.4.0 2017-04-15 13:19 UTC

This package is auto-updated.

Last update: 2021-08-23 21:30:21 UTC


README

一个小型库,能够创建并持久化Mysql导出到Flysystem文件系统。

依赖关系;

container-interop/container-interop 兼容,但完全可选。

未提供代码化的连接,但我提供了一些示例配置。提供所需服务的工厂,但您必须编写一个Flysystem服务的工厂(参见提供的示例)。

安装

composer require "bushbaby/flysystem-mysql-backup"

用法

编程式

// setup dumper 
$dumpOptions    = new MysqlDumperOptions();
$dumper         = new MysqlDumperService($dsn, $user, $password, $dumpOptions);

// setup storage 
$filesystem     = new Filesystem(new SomeFlystemAdapter());

// setup backup service
$storageOptions = new StorageOptions();
$backup         = new MysqlBackupService($dumperService, $filesystem, $storageOptions, $dumpSettings);

// invoke
$backup->doBackup();

$backup->pruneStorage();

工厂(ContainerInterface)

如果您选择使用工厂来实例化服务,则容器中应注册一个config服务。该服务应包含一个bsb_flysystem_mysql_backup顶级条目,其中包含以下键。

  • 连接
  • 存储
  • mysql导出

连接键必须是一个包含doctrine连接参数的数组

return [
    'bsb_flysystem_mysql_backup' => [
        'connection' => [
            'host'          => 'localhost',
            'user'          => 'dbuser',
            'password'      => 'dbpass',
            'dbname'        => 'dbname',
            'charset'       => 'utf8',
            'driverOptions' => [
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
            ],
        ],
    ],
];    

或一个doctrine连接名称。

return [
    'bsb_flysystem_mysql_backup' => [
        'connection' => 'orm_default',
    ],
];    

当您使用doctrine连接名称时,假设容器中注册了Doctrine\Common\Persistence\ManagerRegistry的实现。该服务用于检索命名连接。

我使用这个实现

存储选项

存储键必须是一个包含以下内容的数组

return [
    'bsb_flysystem_mysql_backup' => [
        'storage' => [

            /*
             * Container service name of the Flysystem filesystem used to persisted dumps
             *
             * @since 0.2.0 this may also be the name of a filesystem as it has been registered to the BsbFlysystem Manager
             */
            // 'filesystem'      => 'Container/Name/Of/FilesystemService',

            /*
             * Path within the Flysystem filesystem where dumps are persisted
             */
            // 'path'            => '/',

            /*
             * Store the basename of the last created backup in this file
             * false|string
             */
            // 'write_latest'    => false,
			
			/*
			 * Prune backup files from storage after creating a backup
			 */
            // 'auto_prune'    	  => false,
            
            /*
             * Prune backup files from storage when there are more than x files
             *
             * default 0 (disabled)
             */
            // 'prune_max_count' => 0,

            /*
             * Prune backup files from storage after x seconds
             *
             * default 0 (disabled)
             */
            // 'prune_max_ttl'   => 0,
        ],
    ],
];    

Mysqldump选项

mysql_dumper必须是一个数组,有关详细信息,请参阅Mysqldump的导出设置

return [
    'bsb_flysystem_mysql_backup' => [
        'mysql_dumper' => [
            'include_tables'        => [],
            'exclude_tables'        => [],
            'compress'              => Mysqldump::GZIP,
            'no_data'               => false,
            'add_drop_table'        => true,
            'single_transaction'    => true,
            'lock_tables'           => true,
            'add_locks'             => true,
            'extended_insert'       => false,
            'complete_insert'       => false,
            'disable_keys'          => true,
            'where'                 => '',
            'no_create_info'        => false,
            'skip_triggers'         => false,
            'add_drop_trigger'      => true,
            'routines'              => false,
            'hex_blob'              => true,
            'databases'             => false,
            'add_drop_database'     => false,
            'skip_tz_utc'           => false,
            'no_autocommit'         => true,
            'default_character_set' => Mysqldump::UTF8,
            'skip_comments'         => false,
            'skip_dump_date'        => false,
        ],
    ],
];    

Flysystem文件系统的工厂

<?php

namespace MyNamespace\Container;

use Aws\S3\S3Client;
use Interop\Container\ContainerInterface;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Zend\Stdlib\ArrayUtils;

class FilesystemFactory
{
    /**
     * @param ContainerInterface $container
     * @return Filesystem
     */
    public function __invoke(ContainerInterface $container)
    {
        $config = $container->get('config')['mysql_backup_to_s3'];

        $client = new S3Client([
            'credentials' => $config['credentials'],
            'region'      => $config['region'],
            // frankfurt
            'version'     => $config['version'],
            // or latest, but not recommended in production
        ]);

        $adapter    = new AwsS3Adapter($client, $config['bucket']);
        $filesystem = new Filesystem($adapter);

        return $filesystem;
    }
}