]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Pages/API/RestoreRun.php
c3b107d6ad05d8a8670114877e13f167f784d1b1
[bacula/bacula] / gui / baculum / protected / API / Pages / API / RestoreRun.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('Application.API.Class.APIDbModule');
24  
25 class RestoreRun extends BaculumAPIServer {
26
27         public function create($params) {
28                 $rfile = property_exists($params, 'rpath') ? $params->rpath : null;
29                 $client = null;
30                 if (property_exists($params, 'clientid')) {
31                         $clientid = intval($params->clientid);
32                         $client_row = $this->getModule('client')->getClientById($clientid);
33                         $client = is_object($client_row) ? $client_row->name : null;
34                 } elseif (property_exists($params, 'client') && $this->getModule('misc')->isValidName($params->client)) {
35                         $client = $params->client;
36                 }
37                 $priority = property_exists($params, 'priority') ? intval($params->priority) : 10; // default priority is set to 10
38                 $where = property_exists($params, 'where') ? $params->where : null;
39                 $replace = property_exists($params, 'replace') ? $params->replace : null;
40                 $restorejob = null;
41                 if (property_exists($params, 'restorejob') && $this->getModule('misc')->isValidName($params->restorejob)) {
42                         $restorejob = $params->restorejob;
43                 }
44                 $misc = $this->getModule('misc');
45
46                 if(!is_null($client)) {
47                         if(preg_match($misc::RPATH_PATTERN, $rfile) === 1) {
48                                 if(!is_null($where)) {
49                                         if(!is_null($replace)) {
50                                                 $command = array('restore',
51                                                         'file="?' . $rfile . '"',
52                                                         'client="' . $client . '"',
53                                                         'where="' . $where . '"',
54                                                         'replace="' . $replace . '"',
55                                                         'priority="' . $priority . '"'
56                                                 );
57                                                 if (!is_null($restorejob)) {
58                                                         $command[] = 'restorejob="' . $restorejob . '"';
59                                                 }
60                                                 $command[] = 'yes';
61                                                 $restore = $this->getModule('bconsole')->bconsoleCommand($this->director, $command, $this->user);
62                                                 $this->removeTmpRestoreTable($rfile);
63                                                 $this->output = $restore->output;
64                                                 $this->error = (integer)$restore->exitcode;
65                                         } else {
66                                                 $this->output = JobError::MSG_ERROR_INVALID_REPLACE_OPTION;
67                                                 $this->error = JobError::ERROR_INVALID_REPLACE_OPTION;
68                                         }
69                                 } else {
70                                         $this->output = JobError::MSG_ERROR_INVALID_WHERE_OPTION;
71                                         $this->error = JobError::ERROR_INVALID_WHERE_OPTION;
72                                 }
73                         } else {
74                                 $this->output = JobError::MSG_ERROR_INVALID_RPATH;
75                                 $this->error = JobError::ERROR_INVALID_RPATH;
76                         }
77                 } else {
78                         $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
79                         $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
80                 }
81         }
82
83         private function removeTmpRestoreTable($tableName) {
84                 $misc = $this->getModule('misc');
85                 if (preg_match($misc::RPATH_PATTERN, $tableName) === 1) {
86                         // @TODO: Move it to API module. It shouldn't look like here.
87                         $db_params = $this->getModule('api_config')->getConfig('db');
88                         $connection = APIDbModule::getAPIDbConnection($db_params);
89                         $connection->setActive(true);
90                         $sql = "DROP TABLE $tableName";
91                         $pdo = $connection->getPdoInstance();
92                         try {
93                                 $pdo->exec($sql);
94                         } catch(PDOException $e) {
95                                 $emsg = 'Problem during delete temporary Bvfs table. ' . $e->getMessage();
96                                 $this->getModule('logging')->log(
97                                         __FUNCTION__,
98                                         $emsg,
99                                         Logging::CATEGORY_APPLICATION,
100                                         __FILE__,
101                                         __LINE__
102                                 );
103                         }
104                         $pdo = null;
105                 }
106         }
107 }
108
109 ?>