Repository URL to install this package:
|
Version:
2.2.4 ▾
|
<?php
namespace MeltConsole\App\Commands;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* Class to deploy code.
*/
class BackupRemoteFilesCommand extends MeltCommand {
/**
* Question helper.
*
* @var \Symfony\Component\Console\Helper\QuestionHelper
*/
private $question;
/**
* {@inheritdoc}
*/
protected function configure() {
// Initial config for command.
$this
->setName('melt:backup-remote-files')
->setAliases(['melt:brf'])
->setDescription('Creates backup of files directory at docroot/sites/default/files')
->setHelp('Creates backup to backup files.')
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Environment e.g dev, qa, stage');
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output) {
// TODO: Change the autogenerated stub.
parent::interact($input, $output);
$this->question = $this->getHelper('question');
if (!$input->getOption('env')) {
// Ask the user some stuff.
$envs = $this->getEnvironmentsFromServer();
$question = new ChoiceQuestion('Choose environment to backup files: ', $envs);
if (empty($envs)) {
throw new \Exception('There are currently no environments.');
}
$env = $this->question->ask($input, $output, $question);
// Set options based on users answer.
$input->setOption('env', $env);
}
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$env = $input->getOption('env');
if (!$this->validateEnvironmentExistsOnServer($env)) {
throw new \Exception('Environment doesn\'t exist.');
}
$output->write("Creating backup of <info>docroot/sites/default/files</info> directory on the server...");
$backupPath = $this->backupRemoteFiles($env);
$output->write("Backup created at <info>{$backupPath}</info> on the server.");
}
/**
* Backs up remote files at docroot/sites/default/files.
*
* @param string $env
* Environment.
*/
protected function backupRemoteFiles(string $env) {
// Year month day hour minute.
$timestamp = date('Ymdhi');
$envDir = "{$this->projectName}-{$env}";
$backupName = "{$envDir}-backup-{$timestamp}.tar.gz";
$backupDir = '/opt/backups';
$filesDir = 'docroot/sites/default/files';
$this->runServerCommands([
"cd /{$this->serverRoot}/{$envDir}",
"tar -zcvf {$backupDir}/{$backupName} {$filesDir}",
]);
return "{$backupDir}/{$backupName}";
}
}