Skip to content

Commit

Permalink
Updates DB backups command to pull latest db and tests constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Mar 21, 2020
1 parent 2d3873b commit d604fcf
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 134 deletions.
170 changes: 170 additions & 0 deletions src/Commands/DbBackupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace AcquiaCli\Commands;

use AcquiaCloudApi\Connector\Connector;
use AcquiaCloudApi\Response\EnvironmentResponse;
use AcquiaCloudApi\Endpoints\Databases;
use AcquiaCloudApi\Endpoints\DatabaseBackups;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Helper\TableCell;

/**
* Class DomainCommand
* @package AcquiaCli\Commands
*/
class DbBackupCommand extends AcquiaCommand
{

public $databaseBackupsAdapter;

public function __construct()
{
parent::__construct();

$this->databaseBackupsAdapter = new DatabaseBackups($this->cloudapi);
}

/**
* Backs up all DBs in an environment.
*
* @param string $uuid
* @param EnvironmentResponse $environment
*
* @command database:backup
* @aliases db:backup
*/
public function dbBackup($uuid, $environment)
{
$this->backupAllEnvironmentDbs($uuid, $environment);
}

/**
* Shows a list of database backups for all databases in an environment.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
*
* @command database:backup:list
* @aliases db:backup:list
*/
public function dbBackupList($uuid, $environment, $dbName = null)
{
if (null !== $dbName) {
$this->cloudapi->addQuery('filter', "name=${dbName}");
}
$dbAdapter = new Databases($this->cloudapi);
$databases = $dbAdapter->getAll($uuid);
$this->cloudapi->clearQuery();

$table = new Table($this->output());
$table->setHeaders(['ID', 'Type', 'Timestamp']);

foreach ($databases as $database) {
$backups = $this->databaseBackupsAdapter->getAll($environment->uuid, $database->name);
$table
->addRows(
[
[new TableCell($database->name, ['colspan' => 3])],
new TableSeparator()
]
);

foreach ($backups as $backup) {
$table
->addRows([
[
$backup->id,
ucfirst($backup->type),
$backup->completedAt,
],
]);
}
}
$table->render();
}

/**
* Restores a database from a saved backup.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:restore
* @aliases db:backup:restore
*/
public function dbBackupRestore($uuid, $environment, $dbName, $backupId)
{
if ($this->confirm(
sprintf('Are you sure you want to restore backup id %s to %s?', $backupId, $environment->label)
)) {
$response = $this->databaseBackupsAdapter->restore($environment->uuid, $dbName, $backupId);
$this->waitForNotification($response);
}
}

/**
* Provides a database backup link.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:link
* @aliases db:backup:link
*/
public function dbBackupLink($uuid, $environment, $dbName, $backupId)
{
$environmentUuid = $environment->uuid;
$this->say(Connector::BASE_URI .
"/environments/${environmentUuid}/databases/${dbName}/backups/${backupId}/actions/download");
}

/**
* Downloads a database backup.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
*
* @command database:backup:download
* @aliases db:backup:download
* @option $backup Select which backup to download by backup ID. If omitted, the latest will be downloaded.
* @option $path Select a path to download the backup to.
*/
public function dbBackupDownload($uuid, $environment, $dbName, $opts = ['backup' => null, 'path' => null])
{
if (!$opts['backup']) {
$this->cloudapi->addQuery('sort', '-created');
$this->cloudapi->addQuery('limit', 1);
$backup = $this->databaseBackupsAdapter->getAll($environment->uuid, $dbName);
$this->cloudapi->clearQuery();
if (empty($backup)) {
throw new Exception('Unable to find a database backup to download.');
}
$backupId = $backup[0]->id;
} else {
$backupId = $opts['backup'];
}

$backup = $this->databaseBackupsAdapter->download($environment->uuid, $dbName, $backupId);
$backupName = sprintf('%s-%s-%s', $environment->name, $dbName, $backupId);

if (null === $opts['path']) {
$location = tempnam(sys_get_temp_dir(), $backupName) . '.sql.gz';
} else {
$location = sprintf("%s/%s.sql.gz", $opts['path'], $backupName);
}
$this->say(sprintf('Downloading database backup to %s', $location));
if (file_put_contents($location, $backup, LOCK_EX)) {
$this->say(sprintf('Database backup downloaded to %s', $location));
} else {
$this->say('Unable to download database backup.');
}
}
}
143 changes: 11 additions & 132 deletions src/Commands/DbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Output\BufferedOutput;


/**
* Class DomainCommand
Expand All @@ -17,18 +19,13 @@
class DbCommand extends AcquiaCommand
{

/**
* Backs up all DBs in an environment.
*
* @param string $uuid
* @param EnvironmentResponse $environment
*
* @command database:backup
* @aliases db:backup
*/
public function dbBackup($uuid, $environment)
public $databaseAdapter;

public function __construct()
{
$this->backupAllEnvironmentDbs($uuid, $environment);
parent::__construct();

$this->databaseAdapter = new Databases($this->cloudapi);
}

/**
Expand All @@ -41,11 +38,11 @@ public function dbBackup($uuid, $environment)
*/
public function dbList($uuid)
{
$dbAdapter = new Databases($this->cloudapi);
$databases = $dbAdapter->getAll($uuid);
$databases = $this->databaseAdapter->getAll($uuid);
$table = new Table($this->output());
$table->setHeaders(['Databases']);
foreach ($databases as $database) {
echo $database->name;
$table
->addRows([
[
Expand All @@ -56,123 +53,6 @@ public function dbList($uuid)
$table->render();
}

/**
* Shows a list of database backups for all databases in an environment.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
*
* @command database:backup:list
* @aliases db:backup:list
*/
public function dbBackupList($uuid, $environment, $dbName = null)
{
if (null !== $dbName) {
$this->cloudapi->addQuery('filter', "name=${dbName}");
}
$dbAdapter = new Databases($this->cloudapi);
$databases = $dbAdapter->getAll($uuid);
$this->cloudapi->clearQuery();

$table = new Table($this->output());
$table->setHeaders(['ID', 'Type', 'Timestamp']);

foreach ($databases as $database) {
$dbBackupsAdapter = new DatabaseBackups($this->cloudapi);
$backups = $dbBackupsAdapter->getAll($environment->uuid, $database->name);
$table
->addRows(
[
[new TableCell($database->name, ['colspan' => 3])],
new TableSeparator()
]
);

foreach ($backups as $backup) {
$table
->addRows([
[
$backup->id,
ucfirst($backup->type),
$backup->completedAt,
],
]);
}
}
$table->render();
}

/**
* Restores a database from a saved backup.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:restore
* @aliases db:backup:restore
*/
public function dbBackupRestore($uuid, $environment, $dbName, $backupId)
{
if ($this->confirm(
sprintf('Are you sure you want to restore backup id %s to %s?', $backupId, $environment->label)
)) {
$dbAdapter = new DatabaseBackups($this->cloudapi);
$response = $dbAdapter->restore($environment->uuid, $dbName, $backupId);
$this->waitForNotification($response);
}
}

/**
* Provides a database backup link.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:link
* @aliases db:backup:link
*/
public function dbBackupLink($uuid, $environment, $dbName, $backupId)
{
$environmentUuid = $environment->uuid;
$this->say(Connector::BASE_URI .
"/environments/${environmentUuid}/databases/${dbName}/backups/${backupId}/actions/download");
}

/**
* Downloads a database backup.
*
* @param string $uuid
* @param EnvironmentResponse $environment
* @param string $dbName
* @param int $backupId
*
* @command database:backup:download
* @aliases db:backup:download
*/
public function dbBackupDownload($uuid, $environment, $dbName, $backupId, $path = null)
{

$dbAdapter = new DatabaseBackups($this->cloudapi);
$backupName = sprintf('%s-%s-%s', $environment->name, $dbName, $backupId);
$backup = $dbAdapter->download($environment->uuid, $dbName, $backupId);

if (null === $path) {
$location = tempnam(sys_get_temp_dir(), $backupName) . '.sql.gz';
} else {
$location = $path . $backupName . ".sql.gz";
}
if (file_put_contents($location, $backup, LOCK_EX)) {
$this->say(sprintf('Database backup downloaded to %s', $location));
} else {
$this->say('Unable to download database backup.');
}
}

/**
* Creates a database.
*
Expand All @@ -184,8 +64,7 @@ public function dbBackupDownload($uuid, $environment, $dbName, $backupId, $path
*/
public function dbCreate($uuid, $dbName)
{
$dbAdapter = new Databases($this->cloudapi);
$response = $dbAdapter->create($uuid, $dbName);
$response = $this->databaseAdapter->create($uuid, $dbName);
$this->waitForNotification($response);
}

Expand Down
12 changes: 10 additions & 2 deletions src/Commands/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
class SshCommand extends AcquiaCommand
{

public $environmentsAdapter;

public function __construct()
{
parent::__construct();

$this->environmentsAdapter = new Environments($this->cloudapi);
}

/**
* Shows SSH connection strings for specified environments.
*
Expand All @@ -27,8 +36,7 @@ public function sshInfo($uuid, $env = null)
$this->cloudapi->addQuery('filter', "name=${env}");
}

$environmentAdapter = new Environments($this->cloudapi);
$environments = $environmentAdapter->getAll($uuid);
$environments = $this->environmentsAdapter->getAll($uuid);

$this->cloudapi->clearQuery();

Expand Down

0 comments on commit d604fcf

Please sign in to comment.