Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAGECLOUD-343: Method for exporting a DB Backup of production site #94

Merged
merged 18 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d41528b
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
ce0ff01
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
5bf1d58
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
763a0a2
Merge branch 'develop' of github.com:magento/ece-tools into MAGECLOUD…
NadiyaS Nov 14, 2017
238510c
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
a41b3b7
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
bb65c2e
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
4628bfb
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 14, 2017
d2a46aa
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 16, 2017
943de9f
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 16, 2017
80c2e9d
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 16, 2017
52fb015
Merge branch 'develop' of github.com:magento/ece-tools into MAGECLOUD…
NadiyaS Nov 20, 2017
4342643
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 20, 2017
2579537
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 20, 2017
3bc4435
Merge branch 'develop' of github.com:magento/ece-tools into MAGECLOUD…
NadiyaS Nov 20, 2017
ee9c113
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 20, 2017
02a19e7
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 21, 2017
41fa316
MAGECLOUD-343: Method for exporting a DB Backup of production site
NadiyaS Nov 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/App/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
namespace Magento\MagentoCloud\App;

use Magento\MagentoCloud\Command\Build;
use Magento\MagentoCloud\Command\DbDump;
use Magento\MagentoCloud\Command\Deploy;
use Magento\MagentoCloud\Command\ConfigDump;
use Magento\MagentoCloud\Command\PostDeploy;
use Magento\MagentoCloud\Config\ValidatorInterface;
use Magento\MagentoCloud\Config\Validator as ConfigValidator;
use Magento\MagentoCloud\DB\Data\ConnectionInterface;
use Magento\MagentoCloud\DB\Data\ReadConnection;
use Magento\MagentoCloud\Filesystem\DirectoryCopier;
use Magento\MagentoCloud\Process\ProcessInterface;
use Magento\MagentoCloud\Process\ProcessComposite;
use Magento\MagentoCloud\Process\Build as BuildProcess;
use Magento\MagentoCloud\Process\DbDump as DbDumpProcess;
use Magento\MagentoCloud\Process\Deploy as DeployProcess;
use Magento\MagentoCloud\Process\ConfigDump as ConfigDumpProcess;
use Magento\MagentoCloud\Process\PostDeploy as PostDeployProcess;
Expand Down Expand Up @@ -253,6 +257,18 @@ function () use ($root, $config) {
],
]);
});
$this->container->when(DbDump::class)
->needs(ProcessInterface::class)
->give(function () {
return $this->container->makeWith(ProcessComposite::class, [
'processes' => [
$this->container->make(DbDumpProcess\DbDump::class),
],
]);
});
$this->container->when(DbDumpProcess\DbDump::class)
->needs(ConnectionInterface::class)
->give(ReadConnection::class);
$this->container->when(PostDeploy::class)
->needs(ProcessInterface::class)
->give(function () {
Expand Down
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\MagentoCloud\Command\Build;
use Magento\MagentoCloud\Command\Deploy;
use Magento\MagentoCloud\Command\ConfigDump;
use Magento\MagentoCloud\Command\DbDump;
use Magento\MagentoCloud\Command\PostDeploy;
use Psr\Container\ContainerInterface;

Expand Down Expand Up @@ -54,6 +55,7 @@ protected function getDefaultCommands()
$this->container->get(Build::class),
$this->container->get(Deploy::class),
$this->container->get(ConfigDump::class),
$this->container->get(DbDump::class),
$this->container->get(PostDeploy::class),
]
);
Expand Down
69 changes: 69 additions & 0 deletions src/Command/DbDump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\Command;

use Magento\MagentoCloud\Process\ProcessInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class DbDump for safely creating backup of database
*/
class DbDump extends Command
{
const NAME = 'db-dump';

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ProcessInterface
*/
private $process;

/**
* @param ProcessInterface $process
* @param LoggerInterface $logger
*/
public function __construct(ProcessInterface $process, LoggerInterface $logger)
{
$this->process = $process;
$this->logger = $logger;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName(self::NAME)
->setDescription('Creates backup of database');

parent::configure();
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->logger->info('Starting backup.');
$this->process->execute();
$this->logger->info('Backup completed.');
} catch (\Exception $exception) {
$this->logger->critical($exception->getMessage());

throw $exception;
}
}
}
47 changes: 47 additions & 0 deletions src/DB/Data/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\DB\Data;

/**
* Database connection data interface
*/
interface ConnectionInterface
{
/**
* Returns DB host name or IP address
*
* @return string
*/
public function getHost();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add PHP strict types (:int, :string, etc to all methods you introduced)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot add strict types to all methods as some of them can return several types. Also, we need to review class Environment which returns values of environment variables, because now almost all methods return string type even if the variable does not exist. And these changes may affect current Interface as it uses Environment class


/**
* Returns TCP/IP port number to use for the connection
*
* @return int
*/
public function getPort();

/**
* Returns DB name
*
* @return string
*/
public function getDbName();

/**
* Returns user name for connecting to the server
*
* @return string
*/
public function getUser();

/**
* Returns password to use when connecting to the server
*
* @return string|null
*/
public function getPassword();
}
96 changes: 96 additions & 0 deletions src/DB/Data/ReadConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\MagentoCloud\DB\Data;

use Magento\MagentoCloud\Config\Environment;

/**
* Data for read only connection to database.
* Should be used for backup or other read operations.
*/
class ReadConnection implements ConnectionInterface
{
/**
* Resource of environment data
* @var Environment
*/
private $environment;

/**
* @param Environment $environment
*/
public function __construct(Environment $environment)
{
$this->environment = $environment;
}

/**
* Checks whether project is integration or not.
*
* @return bool Return true if project is integration, otherwise return false (for staging or production)
*/
private function isIntegration()
{
//return empty($_ENV['REGISTRY']);
//while $_ENV['REGISTRY'] is not approved by platform we check the DB host name
//TODO: use method from Environment class which will be implemented in MAGECLOUD-1122
return $this->environment->getDbHost() === 'database.internal';
}

/**
* Returns the host name for backup.
* Integration project has only one node and host should be used the same as retrieved from environment variables.
* Production or staging projects have 3 nodes but for read operations we need to connect to localhost
* with 3304 port and this connection will proxy to appropriate server.
*
* @return string
*/
public function getHost()
{
return $this->isIntegration() ? $this->environment->getDbHost() : '127.0.0.1';
}

/**
* Returns ports for DB connection for backup.
* There are several available ports:
* - 3306 - talks to master DB
* - 3307 - talks to local node
* - 3304 - is used for read only operations
* For production or staging server we cannot make such operations as backup from active master,
* so we should always use 3304 for them for localhost, this connection will proxy to appropriate server.
* For integration we have only one node and 3306 is always used.
*
* @return int
*/
public function getPort()
{
return $this->isIntegration() ? 3306 : 3304;
}

/**
* @inheritdoc
*/
public function getDbName()
{
return $this->environment->getDbName();
}

/**
* @inheritdoc
*/
public function getUser()
{
return $this->environment->getDbUser();
}

/**
* @inheritdoc
*/
public function getPassword()
{
return $this->environment->getDbPassword();
}
}
Loading