Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/v1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyshaw committed Dec 18, 2015
2 parents 963067b + 3c106e3 commit 31e46b2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/Meanbee/Magedbm/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Application extends \Symfony\Component\Console\Application
{

const APP_NAME = 'Magedbm';
const APP_VERSION = '1.3.2';
const APP_VERSION = '1.4.0';

protected $autoloader;

Expand Down
36 changes: 29 additions & 7 deletions src/Meanbee/Magedbm/Command/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'File to import, otherwise latest downloaded'
)
->addOption(
'download-only',
'-o',
InputOption::VALUE_NONE,
'Only download the backup to the current directory'
)
->addOption(
'--force',
'-f',
Expand All @@ -44,7 +50,7 @@ protected function configure()
'--drop-tables',
'-d',
InputOption::VALUE_NONE,
'Drop tables before import'
'Drop tables before import. Deprecated since 1.4.0 as all exports now drop tables automatically.'
)
->addOption(
'--region',
Expand Down Expand Up @@ -73,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
// Import overwrites databases so ask for confirmation.
$dialog = $this->getHelper('dialog');
if (!$input->getOption('force')) {
if (!$input->getOption('force') && !$input->getOption('download-only')) {
if (!$dialog->askConfirmation(
$output,
'<question>Are you sure you wish to overwrite local database [y/n]?</question>',
Expand All @@ -94,7 +100,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->downloadBackup($file, $s3, $config, $input);

$this->backupImport($file, $input);
if ($input->getOption('download-only')) {
$this->backupMove($file);
} else {
$this->backupImport($file, $input);
}
}

/**
Expand Down Expand Up @@ -188,10 +198,6 @@ protected function backupImport($file, $input)
'--compression' => 'gzip',
);

if ($input->getOption('drop-tables')) {
$params['--drop-tables'] = true;
}

try {
if ($returnCode = $importCommand->run(new ArrayInput($params), $this->getOutput())) {
$this->getOutput()->writeln('<error>magerun db:import failed to import database.</error>');
Expand All @@ -203,6 +209,22 @@ protected function backupImport($file, $input)
$this->cleanUp();
}

/**
* Move backup from tmp directory to current working directory
*
* @param $file
*/
protected function backupMove($file)
{
$filename = $this->getFilePath($file);
$newFilename = getcwd() . '/' . $file;
if (rename($filename, $newFilename)) {
$this->getOutput()->writeln("<info>Downloaded to $newFilename.</info>");
} else {
$this->getOutput()->writeln("<error>Failed to move backup to current working directory. Check $filename</error>");
}
}


/**
* Get filepath in tmp directory
Expand Down
19 changes: 16 additions & 3 deletions src/Meanbee/Magedbm/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function configure()
->setDescription('List available backups')
->addArgument(
'name',
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
'Project identifier'
)
->addOption(
Expand Down Expand Up @@ -53,15 +53,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
$s3 = $this->getS3Client($input->getOption('region'));
$config = $this->getConfig($input);

$name = $input->getArgument('name') ?: '';

try {
$results = $s3->getIterator(
'ListObjects',
array('Bucket' => $config['bucket'], 'Prefix' => $input->getArgument('name'))
array('Bucket' => $config['bucket'], 'Prefix' => $name)
);

$names = array();
foreach ($results as $item) {
$itemKeyChunks = explode('/', $item['Key']);
$this->getOutput()->writeln(sprintf('%s %dMB', array_pop($itemKeyChunks), $item['Size'] / 1024 / 1024));

if ($name) {
// If name presented, show downloads for that name
$this->getOutput()->writeln(sprintf('%s %dMB', array_pop($itemKeyChunks), $item['Size'] / 1024 / 1024));
} else {
// Otherwise show uniqued list of available names
if (!in_array($itemKeyChunks[0], $names)) {
$names[] = $itemKeyChunks[0];
$this->getOutput()->writeln($itemKeyChunks[0]);
}
}
}

if (!$results->count()) {
Expand Down
42 changes: 5 additions & 37 deletions src/Meanbee/Magedbm/Command/PutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,49 +198,15 @@ protected function cleanUp()
}

/**
* Create database backup in tmp directory.
* Use magerun db:dump if available. Otherwise use php alternative if exec not available.
* Database export without using exec.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @throws \Exception
*/
private function createBackup(InputInterface $input, OutputInterface $output)
{
$magerun = $this->getMagerun();
$filePath = $this->getFilePath($input);

try {
/** @var \N98\Magento\Command\Database\DumpCommand $dumpCommand */
$dumpCommand = $magerun->find("db:dump");

$stripOptions = $input->getOption('strip') ? : '@development';
$dumpInput = new ArrayInput(array(
'filename' => $filePath,
'--strip' => $stripOptions,
'--compression' => 'gzip',
));

if ($dumpCommand->run($dumpInput, $output)) {
throw new \Exception("magerun db:dump failed to create backup..");
}
} catch (\InvalidArgumentException $e) {
$this->createBackupWithoutExec($input, $output);

$output->writeln('<info>Finished</info>');
}
}

/**
* PHP alternative to dump database without exec
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
private function createBackupWithoutExec(InputInterface $input, OutputInterface $output)
{
// Use Magerun for getting DB details
$magerun = $this->getMagerun();
$filePath = $this->getFilePath($input);

Expand Down Expand Up @@ -291,13 +257,15 @@ private function createBackupWithoutExec(InputInterface $input, OutputInterface
array(
'include-tables' => $stripTables,
'no-data' => true,
'add-drop-table' => true,
)
);

$dumpStructure->start($filePath . '.structure');

$dump = new Mysqldump(sprintf('%s;dbname=%s', $dbHelper->dsn(), $dbName), $username, $password, array(
'exclude-tables' => $stripTables
'exclude-tables' => $stripTables,
'add-drop-table' => true,
));

$dump->start($filePath . '.data');
Expand Down

0 comments on commit 31e46b2

Please sign in to comment.