{"action":"created","issue":{"url":"https://api.github.com/repos/pimcore/pimcore/issues/275","labels_url":"https://api.github.com/repos/pimcore/pimcore/issues/275/labels{/name}","comments_url":"https://api.github.com/repos/pimcore/pimcore/issues/275/comments","events_url":"https://api.github.com/repos/pimcore/pimcore/issues/275/events","html_url":"https://github.com/pimcore/pimcore/issues/275","id":112767075,"number":275,"title":"add param \"m | maintenance\" to pimcore backup","user":{"login":"rolandstoll","id":2950960,"avatar_url":"https://avatars.githubusercontent.com/u/2950960?v=3","gravatar_id":"","url":"https://api.github.com/users/rolandstoll","html_url":"https://github.com/rolandstoll","followers_url":"https://api.github.com/users/rolandstoll/followers","following_url":"https://api.github.com/users/rolandstoll/following{/other_user}","gists_url":"https://api.github.com/users/rolandstoll/gists{/gist_id}","starred_url":"https://api.github.com/users/rolandstoll/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rolandstoll/subscriptions","organizations_url":"https://api.github.com/users/rolandstoll/orgs","repos_url":"https://api.github.com/users/rolandstoll/repos","events_url":"https://api.github.com/users/rolandstoll/events{/privacy}","received_events_url":"https://api.github.com/users/rolandstoll/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pimcore/pimcore/labels/Proposal","name":"Proposal","color":"c7def8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-22T09:44:52Z","updated_at":"2015-10-27T08:46:17Z","closed_at":null,"body":"It would be quite useful to add a optional parameter like \"-m\" / \"--maintenance\" to the cli backup of pimcore. During backup other scripts (e.g. cronjobs) could be prevented to be executed (and maybe produce conflicts).\r\n\r\ncode implementation in `pimcore/cli/backup.php`:\r\n\r\n```php\r\n<?php \r\n/**\r\n * Pimcore\r\n *\r\n * LICENSE\r\n *\r\n * This source file is subject to the new BSD license that is bundled\r\n * with this package in the file LICENSE.txt.\r\n * It is also available through the world-wide-web at this URL:\r\n * http://www.pimcore.org/license\r\n *\r\n * @copyright  Copyright (c) 2009-2014 pimcore GmbH (http://www.pimcore.org)\r\n * @license    http://www.pimcore.org/license     New BSD License\r\n */\r\n\r\nchdir(__DIR__);\r\n\r\ninclude_once(\"startup.php\");\r\n\r\ntry {\r\n    $opts = new \\Zend_Console_Getopt(array(\r\n        'filename|f=s'    => 'filename for the backup (default: backup_m-d-Y_H-i) .zip is added automatically',\r\n        'directory|d=s'   => 'target directory (absolute path without trailing slash) for the backup-file (default: ' . PIMCORE_BACKUP_DIRECTORY . ')',\r\n        'overwrite|o' => 'overwrite existing backup with the same filename, default: true',\r\n        'cleanup|c=s' => 'in days, backups in the target directory which are older than the given days will be deleted, default 7, use false to disable it',\r\n        'verbose|v' => 'show detailed information during the backup',\r\n        'mysql-tables=s' => 'a comma separated list of mysql tables to backup e.g \"translations_website,translations_admin\" ',\r\n        'only-mysql-related-tasks' => 'executes only mysql related tasks.',\r\n        'maintenance|m' => 'set maintenance mode during backup',\r\n        'help|h' => 'display this help'\r\n    ));\r\n} catch (Exception $e) {\r\n    echo \"There's a problem with your commandline interface, I will now create a backup with the default configuration.\";\r\n    echo \"\\n\";\r\n    echo \"For details, see the error below:\";\r\n    echo \"\\n\";\r\n    echo $e->getMessage();\r\n}\r\n\r\n\r\ntry {\r\n    $opts->parse();\r\n} catch (\\Zend_Console_Getopt_Exception $e) {\r\n    echo \"There's a problem with your configuration, I will now create a backup with the default configuration.\";\r\n    echo \"\\n\";\r\n    echo \"For details, see the error below:\";\r\n    echo \"\\n\";\r\n    echo $e->getMessage();\r\n}\r\n\r\n\r\n// defaults\r\n$config = array(\r\n    \"filename\" => \"backup_\" . date(\"m-d-Y_H-i\"),\r\n    \"directory\" => PIMCORE_BACKUP_DIRECTORY,\r\n    \"overwrite\" => false,\r\n    \"cleanup\" => 7,\r\n    \"verbose\" => false,\r\n    \"maintenance\" => false\r\n);\r\n\r\n\r\n// display help message\r\nif ($opts->getOption(\"help\")) {\r\n    echo $opts->getUsageMessage();\r\n    exit;\r\n}\r\n\r\n$tmpConfig = $config;\r\nforeach ($config as $key => $value) {\r\n    if ($opts->getOption($key)) {\r\n        $tmpConfig[$key] = $opts->getOption($key);\r\n    }\r\n}\r\n$config = $tmpConfig;\r\n\\Zend_Registry::set(\"config\", $config);\r\n\r\n\r\n$backupFile = $config[\"directory\"] . \"/\" . $config[\"filename\"] . \".zip\";\r\n\r\n\r\n// check for existing file\r\nif (is_file($backupFile) && !$config[\"overwrite\"]) {\r\n    echo \"backup-file already exists, please use --overwrite=true or -o true to overwrite it\";\r\n    exit;\r\n} else if (is_file($backupFile)) {\r\n    @unlink($backupFile);\r\n}\r\n\r\n// cleanup\r\nif ($config[\"cleanup\"] != \"false\") {\r\n    $files = scandir($config[\"directory\"]);\r\n    $lifetime = (int) $config[\"cleanup\"] * 86400;\r\n    foreach ($files as $file) {\r\n        if (is_file($config[\"directory\"] . \"/\" . $file) && preg_match(\"/\\.zip$/\", $file)) {\r\n            if (filemtime($config[\"directory\"] . \"/\" . $file) < (time() - $lifetime)) {\r\n                verboseMessage(\"delete: \" . $config[\"directory\"] . \"/\" . $file . \"\\n\");\r\n                unlink($config[\"directory\"] . \"/\" . $file);\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n// maintenance\r\nif ($config[\"maintenance\"] == true) {\r\n    session_start();\r\n    verboseMessage(\"------------------------------------------------\");\r\n    verboseMessage(\"set maintenance mode on\");\r\n    Pimcore\\Tool\\Admin::activateMaintenanceMode();\r\n}\r\n\r\nverboseMessage(\"------------------------------------------------\");\r\nverboseMessage(\"------------------------------------------------\");\r\nverboseMessage(\"starting backup into file: \" . $backupFile);\r\n$options = array();\r\nif ($mysqlTables = $opts->getOption(\"mysql-tables\")) {\r\n    $options[\"mysql-tables\"] = $mysqlTables;\r\n}\r\n$options['only-mysql-related-tasks'] = $opts->getOption('only-mysql-related-tasks');\r\n\r\n\r\n\r\n$backup = new \\Pimcore\\Backup($backupFile);\r\n$initInfo = $backup->init($options);\r\n\r\n$stepMethodMapping = array(\r\n    \"mysql-tables\" => \"mysqlTables\",\r\n    \"mysql\" => \"mysqlData\",\r\n    \"mysql-complete\" => \"mysqlComplete\",\r\n    \"files\" => \"fileStep\",\r\n    \"complete\" => \"complete\"\r\n);\r\n\r\nif (empty($initInfo[\"errors\"])) {\r\n    foreach ($initInfo[\"steps\"] as $step) {\r\n        if (!is_array($step[1])) {\r\n            $step[1] = array();\r\n        }\r\n        verboseMessage(\"execute: \" . $step[0] . \" | with the following parameters: \" . implode(\",\", $step[1]));\r\n        $return = call_user_func_array(array($backup, $stepMethodMapping[$step[0]]), $step[1]);\r\n        if ($return[\"filesize\"]) {\r\n            verboseMessage(\"current filesize of the backup is: \" . $return[\"filesize\"]);\r\n        }\r\n    }\r\n}\r\n\r\n\r\n// maintenance\r\nif ($config[\"maintenance\"] == true) {\r\n    verboseMessage(\"------------------------------------------------\");\r\n    verboseMessage(\"set maintenance mode off\");\r\n    Pimcore\\Tool\\Admin::deactivateMaintenanceMode();\r\n}\r\n\r\n\r\nverboseMessage(\"------------------------------------------------\");\r\nverboseMessage(\"------------------------------------------------\");\r\n/*\r\n * do not remove the string \"backup finished\"\r\n * deployment will check for this string to ensure that the backup has been successfully created\r\n * and no fatal error occurred during backup-creation\r\n */\r\nverboseMessage(\"backup finished, you can find your backup here: \" . $backupFile);\r\n\r\n\r\nfunction verboseMessage($m) {\r\n    $config = \\Zend_Registry::get(\"config\");\r\n    if ($config[\"verbose\"]) {\r\n        echo $m . \"\\n\";\r\n    }\r\n}\r\n```\r\n\r\n**Explanation**\r\n- parameter \"m | maintenance\" has been added\r\n- if \"m\" is set, maintenance mode will be activated before and deactivated after backup\r\n\r\n**Warning**\r\nThis implementation works for me, but to be honest, there might be danger if this script fails or is cancelled: maintenance mode will stay active and pimcore is not accessible..."},"comment":{"url":"https://api.github.com/repos/pimcore/pimcore/issues/comments/151415737","html_url":"https://github.com/pimcore/pimcore/issues/275#issuecomment-151415737","issue_url":"https://api.github.com/repos/pimcore/pimcore/issues/275","id":151415737,"user":{"login":"brusch","id":142037,"avatar_url":"https://avatars.githubusercontent.com/u/142037?v=3","gravatar_id":"","url":"https://api.github.com/users/brusch","html_url":"https://github.com/brusch","followers_url":"https://api.github.com/users/brusch/followers","following_url":"https://api.github.com/users/brusch/following{/other_user}","gists_url":"https://api.github.com/users/brusch/gists{/gist_id}","starred_url":"https://api.github.com/users/brusch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brusch/subscriptions","organizations_url":"https://api.github.com/users/brusch/orgs","repos_url":"https://api.github.com/users/brusch/repos","events_url":"https://api.github.com/users/brusch/events{/privacy}","received_events_url":"https://api.github.com/users/brusch/received_events","type":"User","site_admin":false},"created_at":"2015-10-27T08:46:17Z","updated_at":"2015-10-27T08:46:17Z","body":"merci beaucoup! "}}