Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
basz committed Jan 25, 2016
0 parents commit 96cf37d
Show file tree
Hide file tree
Showing 11 changed files with 1,191 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor/
209 changes: 209 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# BsbFlysystemMysqlBackup

A small library capable of creating and persisting a Mysql dump into a Flysystem filesystem.

Dependancies;

- [Mysqldump](https://github.com/ifsnop/mysqldump-php) to create a mysql dump
- [Flysystem](https://github.com/league/flysystem) to persist the dump


Compatible with [container-interop/container-interop](https://github.com/container-interop/container-interop) but completely optional.

No wiring is provided as code but I do provide some example configuration. Factories are provided for needed services but you must write a factory for a Flysystem service (see provided example).

## Installation

```
composer require "bushbaby/flysystem-mysql-backup"
```

## Usage

### Programaticly

```
// 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();
```

### Factories (ContainerInterface)

If you choose to use the Factories to instanciate the service a `config` service is expected to be registered within the Container. That service should contain an `bsb_flysystem_mysql_backup` top level entry with the following keys.

- connection
- storage
- mysql_dump

The `connection` key must be an array with doctrine connection parameters

```
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'
],
],
],
];
```
or a doctrine connection name.

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

When you use a doctrine connection name it is assumed an implementation of `Doctrine\Common\Persistence\ManagerRegistry` is registered within the Container. That service is used to retrieve the named connection.

I use this [one](https://github.com/bushbaby/BsbDoctrineManagerRegistryServiceManager).

### Storage options

The `storage` key must be an array containing

```
return [
'bsb_flysystem_mysql_backup' => [
'storage' => [
/*
* Container service name of the Flysystem filesystem used to persisted dumps
*/
// '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 options

The `mysql_dumper` must be an array see [Mysqldump's dump settings](https://github.com/ifsnop/mysqldump-php#dump-settings) for details.

```
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,
],
],
];
```

### A Factory for a Flysystem filesystem

```
<?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;
}
}
```
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "bushbaby/flysystem-mysql-backup",
"description": "Utility to dump a MySql Database to an 'Flysystem' filesystem",
"type": "library",
"homepage": "https://github.com/bushbaby/BsbFlysystemMysqlBackup",
"license": "MIT",
"authors": [
{
"name": "Bas Kamer",
"email": "[email protected]"
}
],
"require": {
"container-interop/container-interop": "^1.0",
"ifsnop/mysqldump-php": "^2.1",
"league/flysystem": "^1.0",
"zendframework/zend-stdlib": "^2.7",
"zendframework/zend-stratigility": "^1.0"
},
"suggest": {
"bushbaby/doctrine-managerregistry-servicemanager": ""
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.3"
},
"autoload": {
"psr-4": {
"BsbFlysystemMysqlBackup\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BsbFlysystemMysqlBackupTest\\": "test/src/"
}
}
}
36 changes: 36 additions & 0 deletions src/Container/MysqlBackupServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace BsbFlysystemMysqlBackup\Container;

use BsbFlysystemMysqlBackup\Option\MysqlDumperOptions;
use BsbFlysystemMysqlBackup\Option\StorageOptions;
use BsbFlysystemMysqlBackup\Service\MysqlBackupService;
use BsbFlysystemMysqlBackup\Service\MysqlDumperService;
use Ifsnop\Mysqldump\Mysqldump as Dumper;
use Interop\Container\ContainerInterface;
use League\Flysystem\Filesystem;
use Zend\Stdlib\ArrayUtils;

class MysqlBackupServiceFactory
{
/**
* @param ContainerInterface $container
* @return MysqlBackupService
*/
public function __invoke(ContainerInterface $container)
{
/** @var Dumper $dumper */
$dumper = $container->get(MysqlDumperService::class);

/** @var StorageOptions $storageOptions */
$storageOptions = $container->get(StorageOptions::class);

/** @var MysqlDumperOptions $dumperOptions */
$dumperOptions = $container->get(MysqlDumperOptions::class);

/** @var Filesystem $filesystem */
$filesystem = $container->get($storageOptions->getFilesystem());

return new MysqlBackupService($dumper, $filesystem, $storageOptions, $dumperOptions);
}
}
26 changes: 26 additions & 0 deletions src/Container/MysqlDumperOptionsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BsbFlysystemMysqlBackup\Container;

use Interop\Container\ContainerInterface;
use BsbFlysystemMysqlBackup\Option\MysqlDumperOptions;
use Zend\Stdlib\ArrayUtils;

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

if (isset($config['bsb_flysystem_mysql_backup']['mysql_dumper'])) {
$options = $config['bsb_flysystem_mysql_backup']['mysql_dumper'];
}

return new MysqlDumperOptions($options);
}
}
Loading

0 comments on commit 96cf37d

Please sign in to comment.