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

Commit

Permalink
Add cleaning of old databases as they are saved.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyshaw committed Sep 23, 2015
1 parent 8261a9a commit de9cdbb
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/Meanbee/Magedbm/Command/PutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class PutCommand extends BaseCommand
{
const HISTORY_COUNT = 5;

protected $filename;

/**
Expand All @@ -36,6 +38,18 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
'Tables to exclude from export. Default is magerun\'s @development option.'
)
->addOption(
'--no-clean',
null,
InputOption::VALUE_NONE,
'Do not remove old databases on S3.'
)
->addOption(
'--history-count',
null,
InputOption::VALUE_REQUIRED,
'Database history count to keep on S3.'
)
->addOption(
'--region',
'-r',
Expand All @@ -62,7 +76,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->createBackup($input, $output);

$s3 = $this->getS3Client($input->getOption('region'));
$config = $this->getConfig($input);

Expand All @@ -83,6 +97,38 @@ protected function execute(InputInterface $input, OutputInterface $output)
} finally {
$this->cleanUp();
}

if (!$input->getOption('no-clean')) {
$this->maintainDatabaseHistory($input, $output, $s3, $config);
}
}

/**
* @param $input
* @param $output
* @param $s3
* @param $config
*/
protected function maintainDatabaseHistory($input, $output, $s3, $config) {
try {
$results = $s3->getIterator(
'ListObjects',
array('Bucket' => $config['bucket'], 'Prefix' => $input->getArgument('name'), 'sort_results' => true)
);

$results = iterator_to_array($results, true);
$historyCount = $input->getOption('history-count') ?: self::HISTORY_COUNT;
$deleteCount = count($results) - $historyCount;

for ($i = 0; $i < $deleteCount; $i++) {
$s3->deleteMatchingObjects($config['bucket'], $results[$i]['Key']);
}

} catch (InstanceProfileCredentialsException $e) {
$this->getOutput()->writeln('<error>AWS credentials not found. Please run `configure` command.</error>');
} catch (\Exception $e) {
$this->getOutput()->writeln('<error>' . $e->getMessage() . '</error>');
}
}

/**
Expand Down

0 comments on commit de9cdbb

Please sign in to comment.