forked from FreePBX/backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup.class.php
357 lines (338 loc) · 9.74 KB
/
Backup.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Copyright Sangoma Technologies, Inc 2015
*/
namespace FreePBX\modules;
$setting = array('authenticate' => true, 'allowremote' => false);
class Backup implements \BMO {
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new Exception('Not given a FreePBX Object');
}
$this->FreePBX = $freepbx;
$this->db = $freepbx->Database;
//include __DIR__.'/functions.inc/class.backup.php';
//$this->Backup = new \FreePBX\modules\Backup();
}
public function install(){
}
public function uninstall(){
}
public function backup(){
}
public function restore($backup){
}
public function doConfigPageInit($page) {
$_REQUEST['submit'] = isset($_REQUEST['submit'])?$_REQUEST['submit']:'';
$_REQUEST['action'] = isset($_REQUEST['action'])?$_REQUEST['action']:'';
$_REQUEST['id'] = isset($_REQUEST['id'])?$_REQUEST['id']:'';
switch ($page) {
case 'backup':
if ($_REQUEST['submit'] == _('Delete') && $_REQUEST['action'] == 'save') {
$_REQUEST['action'] = 'delete';
}
switch ($_REQUEST['action']) {
case 'wizard':
$current_servers = backup_get_server('all_detailed');
$server = array();
$backup = array();
$create_server = false;
$backup['bu_server'] = '0';
extract($_REQUEST, EXTR_SKIP);
foreach ($current_servers as $key => $value) {
if ($value['name'] == 'Local Storage' && $value['type'] == 'local' && $value['immortal'] == 'true') {
$backup['storage_servers'][] = $value['id'];
}
if ($value['name'] == 'Config server' && $value['type'] == 'mysql' && $value['immortal'] == 'true') {
$mysqlserver = $value['id'];
}
if ($value['name'] == 'CDR server' && $value['type'] == 'mysql' && $value['immortal'] == 'true') {
$cdrserver = $value['id'];
}
if ($value['type'] == $wizremtype) {
//If a server has the same host AND user we assume it already exists
$create_server = !($wizserveraddr == $value['host'] && $wizserveruser == $value['user']);
}
}
if ($create_server && $wizremote == 'yes') {
$server['name'] = $wizsevername;
$server['desc'] = $wizdesc ? $wizdesc : _('Wizard').": ".$wizname;
$server['type'] = $wizremtype;
$server['host'] = $wizserveraddr;
$server['user'] = $wizserveruser;
$server['path'] = $wizremotepath ? $wizremotepath : '';
switch ($wizremtype) {
case 'ftp':
$server['port'] = $wizserverport ? $wizserverport : '21';
$server['password'] = $wizftppass ? $wizftppass : '';
$server['transfer'] = $wiztrans;
break;
case 'ssh':
$server['port'] = $wizserverport ? $wizserverport : '22';
$server['key'] = $wizsshkeypath;
break;
}
$backup['storage_servers'][] = backup_put_server($server);
}
//Create Backup Job
$backup['name'] = $wizname;
$backup['desc'] = $wizdesc;
if ($wiznotif == 'yes' && !empty($wizemail)) {
$backup['email'] = $wizemail;
} else {
$backup['email'] = '';
}
//cron
$backup['cron_minute'] = array('*');
$backup['cron_dom'] = array('*');
$backup['cron_dow'] = array('*');
$backup['cron_hour'] = array('*');
$backup['cron_month'] = array('*');
$backup['cron_schedule'] = $wizfreq;
switch ($wizfreq) {
case 'daily':
$backup['cron_hour'] = array($wizat);
break;
case 'weekly':
$backup['cron_dow'] = array($wizat[0]);
$backup['cron_hour'] = array($wizat[1]);
break;
case 'monthly':
$backup['cron_dom'] = array($wizat[0]);
$backup['cron_hour'] = array('23');
$backup['cron_minute'] = array('59');
break;
}
$backup['delete_amount'] = '3'; //3 runs
$backup['delete_time'] = '30'; //clear backups older than 30...
$backup['delete_time_type'] = 'days'; //...days
//Backup Configs
if(isset($mysqlserver)){
$backup['type'][] = 'mysql';
$backup['path'][] = 'server-'.$mysqlserver;
$backup['exclude'][] = '';
}
$backup['type'][] = 'astdb';
$backup['path'][] = '';
$backup['exclude'][] = '';
$backup['type'][] = 'dir';
$backup['path'][] = '__AMPWEBROOT__/admin';
$backup['exclude'][] = '';
$backup['type'][] = 'dir';
$backup['path'][] = '__ASTETCDIR__';
$backup['exclude'][] = '';
$backup['type'][] = 'dir';
$backup['path'][] = '__AMPBIN__';
$backup['exclude'][] = '';
if (PHP_OS == "FreeBSD") {
$backup['type'][] = 'dir';
$backup['path'][] = '/usr/local/etc/dahdi';
$backup['exclude'][] = '';
$backup['type'][] = 'dir';
$backup['path'][] = '/usr/local/lib/dahdi';
$backup['exclude'][] = '';
} else {
$backup['type'][] = 'dir';
$backup['path'][] = '/etc/dahdi';
$backup['exclude'][] = '';
}
$backup['type'][] = 'file';
$backup['path'][] = '/etc/freepbx.conf';
$backup['exclude'][] = '';
//Backup CDR
if ($wizcdr == 'yes' && isset($cdrserver)) {
$backup['type'][] = 'mysql';
$backup['path'][] = 'server-'.$cdrserver;
$backup['exclude'][] = '';
}
//Backup Voicemail
if ($wizvm == 'yes') {
$backup['type'][] = 'dir';
$backup['path'][] = '__ASTSPOOLDIR__/voicemail';
$backup['exclude'][] = '';
}
//Backup Voicemail
if ($wizbu == 'yes') {
$backup['type'][] = 'dir';
$backup['path'][] = '__ASTSPOOLDIR__/monitor';
$backup['exclude'][] = '';
}
backup_put_backup($backup);
break;
case 'delete':
backup_del_backup($_REQUEST['id']);
//TODO: Not allowed in 5.5
unset($_REQUEST['action']);
unset($_REQUEST['id']);
break;
}
break;
}
}
/**
* Action bar in 13+
* @param [type] $request [description]
*/
public function getActionBar($request) {
$buttons = array(
'reset' => array(
'name' => 'reset',
'id' => 'reset',
'value' => _('Reset'),
),
'submit' => array(
'name' => 'submit',
'id' => 'submit',
'value' => _('Save'),
),
'run' => array(
'name' => 'run',
'id' => 'run_backup',
'value' => _('Save and Run'),
),
'delete' => array(
'name' => 'delete',
'id' => 'delete',
'value' => _('Delete'),
),
);
switch ($request['display']) {
case 'backup':
case 'backup_servers':
if (empty($request['id'])) {
unset($buttons['delete']);
unset($buttons['run']);
}
if (empty($request['action']) || (strtolower($request['action']) == 'delete')) {
$buttons = array();
}
if($request['display'] != 'backup' && isset($buttons['run'])){
unset($buttons['run']);
}
break;
case 'backup_templates':
if (isset($request['action']) && $request['action'] == "edit" || $request['action'] == "save") {
unset($buttons['run']);
unset($buttons['reset']);
if (!$request['id']) {
unset($buttons['delete']);
}
} else {
$buttons = array();
}
break;
default:
$buttons = array();
}
return $buttons;
}
/**
* Ajax Request for BMO
* @param string $req [description]
* @param [type] $setting [description]
*/
public function ajaxRequest($req, &$setting) {
switch ($req) {
case 'getJSON':
return true;
break;
default:
return false;
break;
}
}
/**
* Ajax Handler for BMO
*/
public function ajaxHandler() {
switch ($_REQUEST['command']) {
case 'getJSON':
switch ($_REQUEST['jdata']) {
case 'backupGrid':
return array_values($this->listBackups());
break;
case 'serverGrid':
return array_values($this->listServers());
break;
case 'templateGrid':
return array_values($this->listTemplates());
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
}
/**
* List all Servers
*/
public function listServers() {
$sql = 'SELECT * FROM backup_servers ORDER BY name';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$ret = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $ret;
}
/**
* List all templates
*/
public function listTemplates() {
$sql = 'SELECT * FROM backup_templates ORDER BY name';
$stmt = $this->db->prepare($sql);
$stmt->execute();
$ret = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $ret;
}
/**
* List all backups
*/
public function listBackups() {
$sql = 'SELECT * FROM backup ORDER BY name';
$ret = $this->db->query($sql, \PDO::FETCH_ASSOC);
$backups = array();
//set index to server id for easy retrieval
foreach ($ret as $s) {
//set index to id for easy retrieval
$backups[$s['id']] = $s;
//default name in one is missing
if (!$backups[$s['id']]['name']) {
$backups[$s['id']]['name'] = _('Backup').' '.$s['id'];
}
}
return $backups;
}
public function getRightNav($request) {
$var = array(
'id' => isset($request['id']) ? $request['id'] : '',
'display' => isset($request['display']) ? $request['display'] : '',
);
switch ($request['display']) {
case 'backup':
if(isset($request['action']) && $request['action'] == 'edit'){
$var['backup'] = backup_get_backup('all');
}
return load_view(dirname(__FILE__) . '/views/rnav/backup.php', $var);
break;
case 'backup_restore':
$var['servers'] = backup_get_server('all');
return load_view(dirname(__FILE__) . '/views/rnav/restore.php', $var);
break;
case 'backup_servers':
if(isset($request['action']) && $request['action'] == 'edit'){
$var['servers'] = backup_get_server('all');
}
return load_view(dirname(__FILE__) . '/views/rnav/servers.php', $var);
break;
case 'backup_templates':
if(isset($request['action']) && $request['action'] == 'edit'){
$var['templates'] = backup_get_template('all');
}
return load_view(dirname(__FILE__) . '/views/rnav/templates.php', $var);
break;
}
}
}