Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackupCommand.php
Go to the documentation of this file.
1 <?php
7 
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
18 
25 {
29  const INPUT_KEY_CODE = 'code';
30  const INPUT_KEY_MEDIA = 'media';
31  const INPUT_KEY_DB = 'db';
32 
38  private $objectManager;
39 
45  private $backupRollbackFactory;
46 
52  private $deploymentConfig;
53 
57  private $maintenanceModeEnabler;
58 
69  public function __construct(
70  ObjectManagerProvider $objectManagerProvider,
71  MaintenanceMode $maintenanceMode,
72  DeploymentConfig $deploymentConfig,
73  MaintenanceModeEnabler $maintenanceModeEnabler = null
74  ) {
75  $this->objectManager = $objectManagerProvider->get();
76  $this->backupRollbackFactory = $this->objectManager->get(\Magento\Framework\Setup\BackupRollbackFactory::class);
77  $this->deploymentConfig = $deploymentConfig;
78  $this->maintenanceModeEnabler =
79  $maintenanceModeEnabler ?: $this->objectManager->get(MaintenanceModeEnabler::class);
80  parent::__construct();
81  }
82 
86  protected function configure()
87  {
88  $options = [
89  new InputOption(
90  self::INPUT_KEY_CODE,
91  null,
92  InputOption::VALUE_NONE,
93  'Take code and configuration files backup (excluding temporary files)'
94  ),
95  new InputOption(
96  self::INPUT_KEY_MEDIA,
97  null,
98  InputOption::VALUE_NONE,
99  'Take media backup'
100  ),
101  new InputOption(
102  self::INPUT_KEY_DB,
103  null,
104  InputOption::VALUE_NONE,
105  'Take complete database backup'
106  ),
107  ];
108  $this->setName('setup:backup')
109  ->setDescription('Takes backup of Magento Application code base, media and database')
110  ->setDefinition($options);
111  parent::configure();
112  }
113 
117  protected function execute(InputInterface $input, OutputInterface $output)
118  {
119  if (!$this->deploymentConfig->isAvailable()
120  && ($input->getOption(self::INPUT_KEY_MEDIA) || $input->getOption(self::INPUT_KEY_DB))) {
121  $output->writeln("<info>No information is available: the Magento application is not installed.</info>");
122  // We need exit code higher than 0 here as an indication
123  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
124  }
125  $returnValue = $this->maintenanceModeEnabler->executeInMaintenanceMode(
126  function () use ($input, $output) {
127  try {
128  $inputOptionProvided = false;
129  $time = time();
130  $backupHandler = $this->backupRollbackFactory->create($output);
131  if ($input->getOption(self::INPUT_KEY_CODE)) {
132  $backupHandler->codeBackup($time);
133  $inputOptionProvided = true;
134  }
135  if ($input->getOption(self::INPUT_KEY_MEDIA)) {
136  $backupHandler->codeBackup($time, Factory::TYPE_MEDIA);
137  $inputOptionProvided = true;
138  }
139  if ($input->getOption(self::INPUT_KEY_DB)) {
140  $this->setAreaCode();
141  $backupHandler->dbBackup($time);
142  $inputOptionProvided = true;
143  }
144  if (!$inputOptionProvided) {
145  throw new \InvalidArgumentException(
146  'Not enough information provided to take backup.'
147  );
148  }
149  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
150  } catch (\Exception $e) {
151  $output->writeln('<error>' . $e->getMessage() . '</error>');
152  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
153  }
154  },
155  $output,
156  false
157  );
158 
159  return $returnValue;
160  }
161 
167  private function setAreaCode()
168  {
169  $areaCode = 'adminhtml';
171  $appState = $this->objectManager->get(\Magento\Framework\App\State::class);
172  $appState->setAreaCode($areaCode);
174  $configLoader = $this->objectManager->get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
175  $this->objectManager->configure($configLoader->load($areaCode));
176  }
177 }
return false
Definition: gallery.phtml:36
__construct(ObjectManagerProvider $objectManagerProvider, MaintenanceMode $maintenanceMode, DeploymentConfig $deploymentConfig, MaintenanceModeEnabler $maintenanceModeEnabler=null)
execute(InputInterface $input, OutputInterface $output)
$deploymentConfig