tradesy/innobackupex

innobackupex增量备份/还原包装器,支持在各种存储库(AWS S3、Google)上存储备份

安装: 10

依赖项: 0

建议者: 0

安全: 0

星星: 0

关注者: 4

分支: 1

开放问题: 0

类型:php

v1.1 2016-11-22 05:53 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:03:43 UTC


README

Innobackupex是由Percona创建的一个Perl工具,用于创建和还原MySQL的文件系统备份

这是一个Innobackupex的PHP包装器,允许我们使用面向对象设计来备份/还原MySQL服务器,并自动将备份存档到云存储解决方案,如AWS S3和Google Cloud Storage。

该包被创建为一个PSR-0命名空间的库,可以通过PHP Composer安装。

使用此库需要非常少的配置。

需求

  • PHP 5+
  • PHP Composer
  • (可选) AWS CLI
  • (可选) GCS CLI
  • 具有足够可用磁盘空间以创建备份的MySQL服务器
  • (可选)对MySQL服务器的SSH访问
  • (SSH所需) libssh2-php
  • php5-curl (guzzle所需)
  • python-mysqldb # 安装Ansible MySQL模块所需

方法

此库可以使用多种方式使用。最简单的方法是将PHP和此库下载到MySQL服务器本身。

或者,我们可以使用PHP_SSH2库在远程服务器上运行此库,只要该服务器具有mysql和ssh访问权限。

安装

在您的库的composer.json包中包含此存储库

运行composer install

如果您想使用Vagrant测试,请确保安装Ansible依赖项

ansible-galaxy install -v -r ansible/requirements.yml -p ansible/roles

配置

MySQL配置

<?php

require_once __DIR__."/../../vendor/autoload.php";

$mysql_host = "127.0.0.1";   /* this should be localhost (IP since not using unix socket) because we are connecting via ssh below */
$mysql_user = "root";
$mysql_password = "password";
$mysql_port = 3306;


// Create MySQL configuration object
$mysql_config = new \Tradesy\Innobackupex\MySQL\Configuration (
    $mysql_host,
    $mysql_user,
    $mysql_password,
    $mysql_port
);

通过加密配置启用可选的存档加密

$algorithm = "AES256";         /* Currently Supported:        "AES128", "AES192", "AES256" */
$key = "MY_STRING_ENCRYPTION_KEY"; 


// Create Encryption Configuration Object
$encryption_configuration = new \Tradesy\Innobackupex\Encryption\Configuration(
    $algorithm,
    $key 
);

***请注意,不建议在VCS存储库中明文存储凭据或密钥。上面的示例只是一个示例。您可以选择加密备份脚本,或者加密一个包含密钥的文件,该文件可以被备份脚本加载,或者可能使用环境变量或其他解决方案来处理敏感信息。***

SSH连接配置

$ssh_host = "127.0.0.1"; // or DNS name 
$ssh_port = 22; 
$ssh_user = "vagrant";
$ssh_public_key = "/path/to/public_key";
$ssh_private_key = "/path/to/private_key";

$ssh_config = new \Tradesy\Innobackupex\SSH\Configuration (
    $ssh_host,
    $ssh_port,
    $ssh_user,
    $ssh_public_key,
    $ssh_private_key,
    '',             // ssh key passphrase
    array('hostkey' => 'ssh-rsa')
);

然后我们使用此配置对象创建一个连接对象

$connection = new \Tradesy\Innobackupex\SSH\Connection($ssh_config);
$connection->setSudoAll(true); // set this to true if you are using a non-root user to SSH 

或者,您可以直接在您的服务器上运行此库,而不是通过SSH进入MySQL容器,使用LocalShell/连接对象

LocalShell连接配置

$connection = new \Tradesy\Innobackupex\LocalShell\Connection()
$connection->setSudoAll(true); // set this to true if you are using a non-root user

保存模块

保存模块确定我们将备份存储在哪里。您可以在数组中指定一个或多个保存模块

截至目前,我们已实现了AWS S3和GCS保存模块。目标是使用相同的接口实现更多模块,以便我们可以通过简单地添加到保存模块数组中的配置来存档到更多服务提供商

AWS S3保存模块

使用AWS S3保存模块有两种方法。我们可以使用AWS的PHP SDK来为我们处理上传(\Tradesy\Innobackupex\S3\Local\Upload),或者如果这不受欢迎,我们可以回退到使用shell AWS CLI(\Tradesy\Innobackupex\S3\Remote\Upload),它有自己的独立凭据和配置

为了使用本地模块,我们必须使用LocalShell连接,因为此库和composer依赖项(AWS-PHP-SDK)是必需的。

使用SSH连接时,我们受限于没有在本地库中复制此库

// Specify the storage module for the backup to use (local or remote SSH)

$s3_save_module = new \Tradesy\Innobackupex\S3\Remote\Upload(
    $connection,
    $bucket, 
    $region,
    $concurrency
);

Google Cloud Storage保存模块

目前,GCS PHP SDK无法处理大文件上传而不耗尽内存资源。因此,建议使用\Tradesy\Innobackupex\GCS\Remote\Upload模块。

$google_save_module = new \Tradesy\Innobackupex\GCS\Remote\Upload(
    $connection,
    $bucket,
    $region,
    $concurrency
);

现在你已经有了mysql配置、连接和保存模块对象,可以创建备份对象了

完整的备份对象

 $Backup = new \Tradesy\Innobackupex\Backup\Full(
     $mysql_config,
     $connection,
     [$s3_save_module, $google_save_module],     // Array of save modules, minimum one
     $encryption_configuration,                  // Encryption configuration or null
     $compress = true,                           // Specify whether to compress backup
     $compress_threads = 100,                    // Specify # threads for compression
     $parallel_threads = 100,                            // Specify # threads
     $encryption_threads = 100,                  // Specify # threads for encryption
     $memory = "4G",                             // Specify RAM Usage
     $save_directory = "/tmp/backups",           // Specify the directory used to save backup
     $save_directory_prefix = "full_backup_"     // Specify prefix for the full backup name
 );
 

要运行备份脚本,只需调用

$Backup->Backup();

这将在$save_directory中存储一个序列化的PHP对象,类型为\Tradesy\Innobackupex\Backup\Info,包含有关相关增量备份和完整备份的信息,以便后续的恢复过程或额外的增量备份使用

增量备份

创建Incremental对象

$Backup = new \Tradesy\Innobackupex\Backup\Incremental(
    $mysql_config,
    $connection,
    [$s3_save_module, $google_save_module],              // Array of save modules, minimum one
    $encryption_configuration,                          // Encryption configuration or null
    $compress = true,                                   // Specify whether to compress backup
    $compress_threads = 100,                            // Specify # threads for compression
    $parallel_threads = 100,                            // Specify # threads
    $encryption_threads = 100,                          // Specify # threads for encryption
    $memory = "4G",                                     // Specify RAM Usage
    $save_directory = "/tmp/backups",                   // Specify the directory used to save backup
    $save_directory_prefix = "incremental_backup_"      // Specify prefix for to call the full backup
);

加载上一个备份信息的序列化文件

 /*
  *   First get files present on backup server
  */
 $info = $Backup->fetchBackupInfo();
 

创建备份

 /*
  *   Create the backup
  */
 $Backup->Backup();

从备份中恢复

请确保仅在必要时,在非生产服务器上运行此操作,因为它将擦除所有现有的MySQL数据。

恢复过程大致如下

  • 下载备份信息的序列化对象/文件。这通常存储在你的bucket中同一日期的备份目录内。这包含有关如何找到存档和命名约定的信息
  • 对于每个所需的单个完整备份和相关增量备份的集合
  • 将存档下载到本地系统
  • 解压并可选地解密存档

所有这些都可以通过我们的恢复配置、连接和恢复模块自动完成

***为了开始恢复,您必须手动从目标服务器上的/var/lib/mysql中删除,以便开始恢复。

从磁盘加载备份信息对象

 $BackupInfo = unserialize($connection->getFileContents("/tmp/backups/tradesy_percona_backup_info"));

使用之前相同的$mysql_config对象。

使用本地shell连接以实现最快的恢复

$connection = new \Tradesy\Innobackupex\LocalShell\Connection();
$connection->setSudoAll(true); 

创建所需的恢复模块

$aws_restore_module = new \Tradesy\Innobackupex\S3\Local\Download(
    $connection,
    $bucket,
    $region,
    $concurrency
);

创建恢复对象

  $Restore = new \Tradesy\Innobackupex\Restore\Mysql(
      $mysql_config,
      $connection,
      [$aws_restore_module],
      $encryption_configuration,                          // Encryption configuration or null
      $parallel_threads = 100,                            // Parallel threads
      $memory = "10G"                                     // Specify RAM Usage
  );
  $Restore->setBackupInfo($BackupInfo);

准备好后,只需调用恢复方法。所有存档将通过恢复模块下载并自动准备。此外,该方法将恢复数据库。

 $Restore->runRestore();

示例Crontab

# MySQL Backups
# daily full backup at 12:15 am
15 0 * * * /usr/bin/php /var/cli/mysql_backups/CreateFullBackup.php >> /var/log/cron/mysql_backups.log 2>&1
 
# hourly incremental backups (except 24th backup)
15 1-23 * * * /usr/bin/php /var/cli/mysql_backups/CreateIncrementalBackup.php >> /var/log/cron/mysql_backups.log 2>&1

有关更多示例,请参阅./Examples目录或测试

测试

AWS/GCS配置(可选)

You must configure your aws cli by adding api key credentials to:
  /home/vagrant/.aws/boto
  
and configure gsutil by running 
    gsutil configure
    
 You may have to update tests to reference personal buckets as well.

启动Vagrant

vagrant up

登录到虚拟机

vagrant ssh

切换到共享目录

cd /var/www

安装composer包

composer install --dev

创建SSH密钥

ssh-keygen(按几次回车键)

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

运行PHPUnit

./vendor/bin/phpunit -v --debug

要查看完整的测试覆盖率,请运行

./vendor/bin/phpunit --debug --verbose  --coverage-html html

目前测试覆盖率约为80%,排除GCS本地模块后为87.5%

####在测试套件失败后重置数据库

在主机上

vagrant ssh -c 'sudo rm -rf /var/lib/mysql'


vagrant provision --provision-with reset_mysql

已知问题

GCS本地模块(PHP SDK)无法使用。这是由于使用了file_get/put_contents,它对于大文件来说不稳定。请改用GCS\Remote\Upload|Download模块。

TODO

  • 记录使用情况和实现可配置的日志记录(Monolog)。
  • 添加更多模块
  • 为未加密和无压缩(扁平)备份添加测试
  • 与非Percona MySQL进行测试

许可

版权所有(c)2016,Matt Margolin

保留所有权利。

在满足以下条件的情况下,允许重新分发和使用源代码和二进制形式,无论是否修改:

  1. 源代码的重新分发必须保留上述版权声明、本条件列表和以下免责声明。

  2. 二进制形式的重新分发必须在文档和/或其他随分发提供的材料中复制上述版权声明、本条件列表和以下免责声明。

  3. 未经具体事先书面许可,不得使用版权持有者的名称或其贡献者的名称来认可或推广源自本软件的产品。

本软件由版权所有者和贡献者提供,“现状”以及任何明示或暗示的保证(包括但不限于适销性和特定用途适用性保证)均予以放弃。在任何情况下,版权所有者或贡献者均不对任何直接、间接、偶然、特殊、示范性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)承担责任,无论损害发生的原因是什么,以及基于何种责任理论(包括合同、严格责任或侵权责任,包括疏忽或其他),即使被告知了此类损害的可能性。

作者信息

马特·玛戈林

mm0 在 github 上