]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Pages/API/RestoreRun.php
baculum: Add restore job selection in restore job wizard
[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
30                 $fileset = null;
31                 if (property_exists($params, 'fileset') && $this->getModule('misc')->isValidName($params->fileset)) {
32                         $fileset = $params->fileset;
33                 }
34                 $client = null;
35                 if (property_exists($params, 'clientid')) {
36                         $clientid = intval($params->clientid);
37                         $client_row = $this->getModule('client')->getClientById($clientid);
38                         $client = is_object($client_row) ? $client_row->name : null;
39                 } elseif (property_exists($params, 'client') && $this->getModule('misc')->isValidName($params->client)) {
40                         $client = $params->client;
41                 }
42                 $priority = property_exists($params, 'priority') ? intval($params->priority) : 10; // default priority is set to 10
43                 $where = property_exists($params, 'where') ? $params->where : null;
44                 $replace = property_exists($params, 'replace') ? $params->replace : null;
45                 $restorejob = null;
46                 if (property_exists($params, 'restorejob') && $this->getModule('misc')->isValidName($params->restorejob)) {
47                         $restorejob = $params->restorejob;
48                 }
49                 $misc = $this->getModule('misc');
50
51                 if(!is_null($fileset)) {
52                         if(!is_null($client)) {
53                                 if(preg_match($misc::RPATH_PATTERN, $rfile) === 1) {
54                                         if(!is_null($where)) {
55                                                 if(!is_null($replace)) {
56                                                         $command = array('restore',
57                                                                 'file="?' . $rfile . '"',
58                                                                 'client="' . $client . '"',
59                                                                 'where="' . $where . '"',
60                                                                 'replace="' . $replace . '"',
61                                                                 'fileset="' . $fileset . '"',
62                                                                 'priority="' . $priority . '"'
63                                                         );
64                                                         if (!is_null($restorejob)) {
65                                                                 $command[] = 'restorejob="' . $restorejob . '"';
66                                                         }
67                                                         $command[] = 'yes';
68                                                         $restore = $this->getModule('bconsole')->bconsoleCommand($this->director, $command, $this->user);
69                                                         $this->removeTmpRestoreTable($rfile);
70                                                         $this->output = $restore->output;
71                                                         $this->error = (integer)$restore->exitcode;
72                                                 } else {
73                                                         $this->output = JobError::MSG_ERROR_INVALID_REPLACE_OPTION;
74                                                         $this->error = JobError::ERROR_INVALID_REPLACE_OPTION;
75                                                 }
76                                         } else {
77                                                 $this->output = JobError::MSG_ERROR_INVALID_WHERE_OPTION;
78                                                 $this->error = JobError::ERROR_INVALID_WHERE_OPTION;
79                                         }
80                                 } else {
81                                         $this->output = JobError::MSG_ERROR_INVALID_RPATH;
82                                         $this->error = JobError::ERROR_INVALID_RPATH;
83                                 }
84                         } else {
85                                 $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
86                                 $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
87                         }
88                 } else {
89                         $this->output = JobError::MSG_ERROR_FILESET_DOES_NOT_EXISTS;
90                         $this->error = JobError::ERROR_FILESET_DOES_NOT_EXISTS;
91                 }
92         }
93
94         private function removeTmpRestoreTable($tableName) {
95                 $misc = $this->getModule('misc');
96                 if (preg_match($misc::RPATH_PATTERN, $tableName) === 1) {
97                         // @TODO: Move it to API module. It shouldn't look like here.
98                         $db_params = $this->getModule('api_config')->getConfig('db');
99                         $connection = APIDbModule::getAPIDbConnection($db_params);
100                         $connection->setActive(true);
101                         $sql = "DROP TABLE $tableName";
102                         $pdo = $connection->getPdoInstance();
103                         try {
104                                 $pdo->exec($sql);
105                         } catch(PDOException $e) {
106                                 $emsg = 'Problem during delete temporary Bvfs table. ' . $e->getMessage();
107                                 $this->getModule('logging')->log(
108                                         __FUNCTION__,
109                                         $emsg,
110                                         Logging::CATEGORY_APPLICATION,
111                                         __FILE__,
112                                         __LINE__
113                                 );
114                         }
115                         $pdo = null;
116                 }
117         }
118 }
119
120 ?>