Merge pull request #402 from romainneutron/gz-dumps

[3.8] Add gzip/bzip2 options for DBs backup commandline tool.
This commit is contained in:
Romain Neutron
2013-06-17 07:43:05 -07:00
2 changed files with 39 additions and 23 deletions

View File

@@ -35,6 +35,7 @@
- Add plugin architecture for third party modules and customization. - Add plugin architecture for third party modules and customization.
- Add records sent-by-mail report. - Add records sent-by-mail report.
- User time limit restrictions can now be set per databox. - User time limit restrictions can now be set per databox.
- Add gzip/bzip2 options for DBs backup commandline tool.
* 3.7.12 (2013-05-13) * 3.7.12 (2013-05-13)

View File

@@ -18,7 +18,7 @@
use Alchemy\Phrasea\Command\Command; use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\Process;
class module_console_systemBackupDB extends Command class module_console_systemBackupDB extends Command
{ {
@@ -32,9 +32,11 @@ class module_console_systemBackupDB extends Command
, dirname(dirname(dirname(dirname(__DIR__)))) , dirname(dirname(dirname(dirname(__DIR__))))
); );
$this->setDescription('Backup Phraseanet Databases'); $this
->setDescription('Backup Phraseanet Databases')
$this->addArgument('directory', null, 'The directory where to backup', $dir); ->addArgument('directory', null, 'The directory where to backup', $dir)
->addOption('gzip', 'g', null, 'Gzip the output (requires gzip utility)')
->addOption('bzip', 'b', null, 'Bzip the output (requires bzip2 utility)');
return $this; return $this;
} }
@@ -43,13 +45,14 @@ class module_console_systemBackupDB extends Command
{ {
$output->write('Phraseanet is going to be backup...', true); $output->write('Phraseanet is going to be backup...', true);
$ok = $this->dump_base($this->getService('phraseanet.appbox'), $input, $output) && $ok; $res = 0;
$res += $this->dump_base($this->getService('phraseanet.appbox'), $input, $output) && $ok;
foreach ($this->getService('phraseanet.appbox')->get_databoxes() as $databox) { foreach ($this->getService('phraseanet.appbox')->get_databoxes() as $databox) {
$ok = $this->dump_base($databox, $input, $output) && $ok; $res += $this->dump_base($databox, $input, $output) && $ok;
} }
return (int) ! $ok; return $res;
} }
protected function dump_base(base $base, InputInterface $input, OutputInterface $output) protected function dump_base(base $base, InputInterface $input, OutputInterface $output)
@@ -63,33 +66,45 @@ class module_console_systemBackupDB extends Command
, $date_obj->format('Y_m_d_H_i_s') , $date_obj->format('Y_m_d_H_i_s')
); );
$output->write(sprintf('Generating %s ... ', $filename)); $command = sprintf(
'mysqldump %s %s %s %s %s %s --default-character-set=utf8',
'--host='.escapeshellarg($base->get_host()),
'--port='.escapeshellarg($base->get_port()),
'--user='.escapeshellarg($base->get_user()),
'--password='.escapeshellarg($base->get_passwd()),
'--databases',
escapeshellarg($base->get_dbname())
);
$builder = ProcessBuilder::create(array( if ($input->getOption('gzip')) {
'mysqldump', $filename .= '.gz';
'--host='.$base->get_host(), $command .= ' | gzip -9';
'--port='.$base->get_port(), } elseif ($input->getOption('bzip')) {
'--user='.$base->get_user(), $filename .= '.bz2';
'--password='.$base->get_passwd(), $command .= ' | bzip2 -9';
'--databases', $base->get_dbname(), }
'--default-character-set=utf8'
));
$proces = $builder->getProcess(); $output->write(sprintf('Generating <info>%s</info> ... ', $filename));
$proces->run();
if ($proces->isSuccessful()) { $command .= ' > ' . escapeshellarg($filename);
file_put_contents($filename, $proces->getOutput());
$process = new Process($command);
$process->run();
if (!$process->isSuccessful()) {
$output->writeln('<error>Failed</error>');
return 1;
} }
if (file_exists($filename) && filesize($filename) > 0) { if (file_exists($filename) && filesize($filename) > 0) {
$output->writeln('OK'); $output->writeln('OK');
return true; return 0;
} else { } else {
$output->writeln('<error>Failed</error>'); $output->writeln('<error>Failed</error>');
return false; return 1;
} }
} }
} }