public function initAction() { $backup = new \Pimcore\Backup(PIMCORE_BACKUP_DIRECTORY . "/backup_" . date("m-d-Y_H-i") . ".zip"); $initInfo = $backup->init(); $this->session->backup = $backup; $this->_helper->json($initInfo); }
<?php include_once "../../pimcore/cli/startup.php"; // get tables which are already in install.sql $installSql = file_get_contents(PIMCORE_PATH . "/modules/install/mysql/install.sql"); preg_match_all("/CREATE TABLE `(.*)`/", $installSql, $matches); $existingTables = $matches[1]; $backup = new \Pimcore\Backup($backupFile); $initInfo = $backup->init(); $stepMethodMapping = ["mysql" => "mysqlData"]; if (empty($initInfo["errors"])) { $backup->mysqlTables($existingTables); foreach ($initInfo["steps"] as $step) { if (!is_array($step[1])) { $step[1] = array(); } if (array_key_exists($step[0], $stepMethodMapping)) { // skip these tables => content / data if (in_array($step[1]["name"], ["tracking_events", "cache", "cache_tags", "http_error_log", "versions", "edit_lock", "locks", "email_log", "tmp_store"])) { continue; } verboseMessage("execute: " . $step[0] . " | with the following parameters: " . implode(",", $step[1])); $return = call_user_func_array([$backup, $stepMethodMapping[$step[0]]], $step[1]); if ($return["filesize"]) { verboseMessage("current filesize of the backup is: " . $return["filesize"]); } } } } // do some modifications $dumpFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/backup-dump.sql";
// maintenance if ($config["maintenance"] == true) { session_start(); verboseMessage("------------------------------------------------"); verboseMessage("set maintenance mode on"); Pimcore\Tool\Admin::activateMaintenanceMode(); } verboseMessage("------------------------------------------------"); verboseMessage("------------------------------------------------"); verboseMessage("starting backup into file: " . $backupFile); $options = array(); if ($mysqlTables = $opts->getOption("mysql-tables")) { $options["mysql-tables"] = $mysqlTables; } $options['only-mysql-related-tasks'] = $opts->getOption('only-mysql-related-tasks'); $backup = new \Pimcore\Backup($backupFile); $initInfo = $backup->init($options); $stepMethodMapping = array("mysql-tables" => "mysqlTables", "mysql" => "mysqlData", "mysql-complete" => "mysqlComplete", "files" => "fileStep", "complete" => "complete"); if (empty($initInfo["errors"])) { foreach ($initInfo["steps"] as $step) { if (!is_array($step[1])) { $step[1] = array(); } verboseMessage("execute: " . $step[0] . " | with the following parameters: " . implode(",", $step[1])); $return = call_user_func_array(array($backup, $stepMethodMapping[$step[0]]), $step[1]); if ($return["filesize"]) { verboseMessage("current filesize of the backup is: " . $return["filesize"]); } } } // maintenance
protected function execute(InputInterface $input, OutputInterface $output) { // defaults $config = ["filename" => "backup_" . date("m-d-Y_H-i"), "directory" => PIMCORE_BACKUP_DIRECTORY, "overwrite" => false, "cleanup" => 7]; $tmpConfig = $config; foreach ($config as $key => $value) { if ($input->getOption($key)) { $tmpConfig[$key] = $input->getOption($key); } } $config = $tmpConfig; \Zend_Registry::set("config", $config); $backupFile = $config["directory"] . "/" . $config["filename"] . ".zip"; // check for existing file if (is_file($backupFile) && !$config["overwrite"]) { $this->writeError("backup-file already exists, please use --overwrite=true or -o true to overwrite it"); exit; } elseif (is_file($backupFile)) { @unlink($backupFile); } // cleanup if ($config["cleanup"] != "false") { $files = scandir($config["directory"]); $lifetime = (int) $config["cleanup"] * 86400; foreach ($files as $file) { if (is_file($config["directory"] . "/" . $file) && preg_match("/\\.zip\$/", $file)) { if (filemtime($config["directory"] . "/" . $file) < time() - $lifetime) { $this->verboseMessage("delete: " . $config["directory"] . "/" . $file . "\n"); unlink($config["directory"] . "/" . $file); } } } } $this->verboseMessage("------------------------------------------------"); $this->verboseMessage("------------------------------------------------"); $this->verboseMessage("starting backup into file: " . $backupFile); $options = []; if ($mysqlTables = $input->getOption("mysql-tables")) { $options["mysql-tables"] = $mysqlTables; } $options['only-mysql-related-tasks'] = $input->getOption('only-mysql-related-tasks'); $backup = new \Pimcore\Backup($backupFile); $initInfo = $backup->init($options); $stepMethodMapping = ["mysql-tables" => "mysqlTables", "mysql" => "mysqlData", "mysql-complete" => "mysqlComplete", "files" => "fileStep", "complete" => "complete"]; if (empty($initInfo["errors"])) { $progress = new ProgressBar($output, count($initInfo["steps"])); if (!$output->isVerbose()) { $progress->start(); } foreach ($initInfo["steps"] as $step) { if (!is_array($step[1])) { $step[1] = []; } $message = $step[0] . ": " . implode(",", $step[1]); $return = call_user_func_array([$backup, $stepMethodMapping[$step[0]]], $step[1]); if ($return["filesize"]) { $message .= " - " . $return["filesize"]; } $progress->setMessage($message); $progress->advance(); } $progress->finish(); } $this->verboseMessage("------------------------------------------------"); $this->verboseMessage("------------------------------------------------"); /* * do not remove the string "backup finished" * deployment will check for this string to ensure that the backup has been successfully created * and no fatal error occurred during backup-creation */ $this->verboseMessage("backup finished, you can find your backup here: " . $backupFile); $this->output->writeln("\n"); $this->output->writeln("Done!"); }