wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.MySQLBackupController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.MySQLBackupController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/application/controller/admintool/class.BackupController.php");
20 
21 /**
22  * @class MySQLBackupController
23  * @ingroup Controller
24  * @brief This Controller enhances the file backup defined by BackupController
25  * by a backup of a given MySQL database.
26  *
27  * <b>Input actions:</b>
28  * - see BackupController
29  *
30  * <b>Output actions:</b>
31  * - see BackupController
32  *
33  * @param[in] paramsSection The configuration section which holds the database connection parameters.
34  *
35  * @note This controller uses the programs mysqldump and mysql. So these must
36  * be included in the system's search path. The database user also needs the
37  * FILE and LOCK TABLES permissions.
38  *
39  * @author ingo herwig <ingo@wemove.com>
40  */
42 {
43  var $PARAMS_SECTION_VARNAME = 'MySQLBackupController.paramsSection';
44 
45  /**
46  * @see Controller::validate()
47  */
48  function validate()
49  {
50  $session = &SessionData::getInstance();
51 
52  if(strlen($this->_request->getValue('paramsSection')) == 0 && !$session->exist($this->PARAMS_SECTION_VARNAME))
53  {
54  $this->setErrorMsg("No 'paramsSection' given in data.");
55  return false;
56  }
57  return parent::validate();
58  }
59  /**
60  * @see Controller::initialize()
61  */
62  function initialize(&$request, &$response)
63  {
64  parent::initialize($request, $response);
65 
66  // store parameters in session
67  if ($request->getAction() != 'continue')
68  {
69  $session = &SessionData::getInstance();
70  $session->set($this->PARAMS_SECTION_VARNAME, $this->_request->getValue('paramsSection'));
71  }
72  }
73  /**
74  * @see BackupController::getAdditionalWorkPackage()
75  */
76  function getAdditionalWorkPackage($number, $action)
77  {
78  if ($number == 1)
79  {
80  if ($this->_request->getAction() == 'makebackup')
81  return array('name' => 'make mysql backup', 'size' => 1, 'oids' => array(1), 'callback' => 'backupMySQL');
82  else if ($this->_request->getAction() == 'restorebackup')
83  return array('name' => 'restore mysql backup', 'size' => 1, 'oids' => array(1), 'callback' => 'restoreMySQL');
84  }
85  else
86  return null;
87  }
88 
89  /**
90  * Create a backup of the database
91  */
92  function backupMySQL()
93  {
94  $params = $this->getConnectionParameters();
95  $command = 'C:\Programme\xampp\mysql\bin\mysqldump --opt '.$params['dbName'].' --host='.$params['dbHostName'].' --user='.$params['dbUserName'].' --password='.$params['dbPassword'].' > "'.dirname($_SERVER['SCRIPT_FILENAME']).'/'.$this->getBackupDir().'database.sql"';
96  $result = shell_exec($command);
97  Log::debug("create mysql backup command: ".$command, __CLASS__);
98  }
99 
100  /**
101  * Restore a backup to the database
102  */
103  function restoreMySQL()
104  {
105  $params = $this->getConnectionParameters();
106  $command = 'C:\Programme\xampp\mysql\bin\mysql '.$params['dbName'].' --host='.$params['dbHostName'].' --user='.$params['dbUserName'].' --password='.$params['dbPassword'].' < "'.dirname($_SERVER['SCRIPT_FILENAME']).'/'.$this->getBackupDir().'database.sql"';
107  $result = shell_exec($command);
108  Log::debug("restore mysql backup command: ".$command, __CLASS__);
109  }
110 
111  /**
112  * Get connection parameters
113  * @return Assoziative array hoding the parameter configuration section
114  */
116  {
117  $params = array();
118 
119  // store parameters in session
120  $session = &SessionData::getInstance();
121  $paramSection = $session->get($this->PARAMS_SECTION_VARNAME);
122 
123  $parser = &InifileParser::getInstance();
124  if (($params = $parser->getSection($paramSection)) === false)
125  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
126  return $params;
127  }
128 }
129 ?>
debug($message, $category)
Definition: class.Log.php:39
This Controller creates a backup (action 'makebackup') from a directory and restores (action 'restore...
getAdditionalWorkPackage($number, $action)
throwEx($message, $file='', $line='')
initialize(&$request, &$response)
This Controller enhances the file backup defined by BackupController by a backup of a given MySQL dat...