|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Xcloner_Scheduler{ |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
private $db; |
|
6
|
|
|
private $scheduler_table = "xcloner_scheduler"; |
|
7
|
|
|
|
|
8
|
|
|
private $xcloner_remote_storage; |
|
9
|
|
|
private $archive_system; |
|
10
|
|
|
private $xcloner_database; |
|
11
|
|
|
private $xcloner_settings; |
|
12
|
|
|
private $logger; |
|
13
|
|
|
private $xcloner_file_system; |
|
14
|
|
|
|
|
15
|
|
|
/*public function __call($method, $args) { |
|
|
|
|
|
|
16
|
|
|
echo "$method is not defined"; |
|
17
|
|
|
}*/ |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Xcloner $xcloner_container) |
|
20
|
|
|
{ |
|
21
|
|
|
global $wpdb; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$this->db = $wpdb; |
|
24
|
|
|
$wpdb->show_errors = false; |
|
25
|
|
|
|
|
26
|
|
|
$this->xcloner_container = $xcloner_container; |
|
|
|
|
|
|
27
|
|
|
$this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
28
|
|
|
|
|
29
|
|
|
$this->scheduler_table = $this->db->prefix.$this->scheduler_table; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function get_xcloner_container() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->xcloner_container; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function get_scheduler_list($return_only_enabled = 0 ) |
|
38
|
|
|
{ |
|
39
|
|
|
$list = $this->db->get_results("SELECT * FROM ".$this->scheduler_table); |
|
40
|
|
|
|
|
41
|
|
|
if($return_only_enabled) |
|
42
|
|
|
{ |
|
43
|
|
|
$new_list= array(); |
|
44
|
|
|
|
|
45
|
|
|
foreach($list as $res) |
|
46
|
|
|
if($res->status) |
|
47
|
|
|
{ |
|
48
|
|
|
$res->next_run_time = wp_next_scheduled('xcloner_scheduler_'.$res->id, array($res->id))+(get_option( 'gmt_offset' ) * HOUR_IN_SECONDS); |
|
49
|
|
|
$new_list[] = $res; |
|
50
|
|
|
} |
|
51
|
|
|
$list = $new_list; |
|
52
|
|
|
} |
|
53
|
|
|
return $list; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function get_next_run_schedule($xcloner_file_system = "") |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$list = $this->get_scheduler_list($return_only_enabled = 1); |
|
59
|
|
|
|
|
60
|
|
|
return $list; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function get_schedule_by_id_object($id) |
|
64
|
|
|
{ |
|
65
|
|
|
$data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id); |
|
66
|
|
|
|
|
67
|
|
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function get_schedule_by_id($id) |
|
71
|
|
|
{ |
|
72
|
|
|
$data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id, ARRAY_A); |
|
73
|
|
|
|
|
74
|
|
|
if(!$data) |
|
75
|
|
|
return false; |
|
76
|
|
|
|
|
77
|
|
|
$params = json_decode($data['params']); |
|
78
|
|
|
|
|
79
|
|
|
//print_r($params); |
|
80
|
|
|
$data['params'] = ""; |
|
81
|
|
|
$data['backup_params'] = $params->backup_params; |
|
82
|
|
|
$data['table_params'] = json_encode($params->database); |
|
83
|
|
|
$data['excluded_files'] = json_encode($params->excluded_files); |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return $data; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function delete_schedule_by_id($id) |
|
90
|
|
|
{ |
|
91
|
|
|
$hook = 'xcloner_scheduler_'.$id; |
|
92
|
|
|
wp_clear_scheduled_hook( $hook, array($id) ); |
|
93
|
|
|
|
|
94
|
|
|
$data = $this->db->delete( $this->scheduler_table , array( 'id' => $id ) ); |
|
95
|
|
|
|
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function deactivate_wp_cron_hooks() |
|
100
|
|
|
{ |
|
101
|
|
|
$list = $this->get_scheduler_list(); |
|
102
|
|
|
|
|
103
|
|
|
foreach($list as $schedule) |
|
104
|
|
|
{ |
|
105
|
|
|
$hook = 'xcloner_scheduler_'.$schedule->id; |
|
106
|
|
|
|
|
107
|
|
|
$timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
108
|
|
|
wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function update_wp_cron_hooks() |
|
113
|
|
|
{ |
|
114
|
|
|
$list = $this->get_scheduler_list(); |
|
115
|
|
|
|
|
116
|
|
|
foreach($list as $schedule) |
|
117
|
|
|
{ |
|
118
|
|
|
$hook = 'xcloner_scheduler_'.$schedule->id; |
|
119
|
|
|
|
|
120
|
|
|
//adding the xcloner_scheduler hook with xcloner_scheduler_callback callback |
|
121
|
|
|
add_action( $hook, array($this, 'xcloner_scheduler_callback'), 10, 1 ); |
|
122
|
|
|
|
|
123
|
|
|
if ( ! wp_next_scheduled( $hook, array($schedule->id) ) and $schedule->status) { |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
if($schedule->recurrence == "single") |
|
|
|
|
|
|
126
|
|
|
wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
127
|
|
|
else |
|
128
|
|
|
wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
129
|
|
|
|
|
130
|
|
|
}elseif(!$schedule->status) |
|
131
|
|
|
{ |
|
132
|
|
|
$timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
133
|
|
|
wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function update_cron_hook($id) |
|
140
|
|
|
{ |
|
141
|
|
|
$schedule = $this->get_schedule_by_id_object($id); |
|
142
|
|
|
$hook = 'xcloner_scheduler_'.$schedule->id; |
|
143
|
|
|
|
|
144
|
|
|
$timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
145
|
|
|
wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
146
|
|
|
|
|
147
|
|
View Code Duplication |
if ($schedule->status) { |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
if($schedule->recurrence == "single") |
|
150
|
|
|
wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
151
|
|
|
else{ |
|
152
|
|
|
wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function disable_single_cron($schedule_id) |
|
159
|
|
|
{ |
|
160
|
|
|
$hook = 'xcloner_scheduler_'.$schedule_id; |
|
161
|
|
|
$timestamp = wp_next_scheduled( $hook , array($schedule_id) ); |
|
162
|
|
|
wp_unschedule_event( $timestamp, $hook, array($schedule_id) ); |
|
163
|
|
|
|
|
164
|
|
|
$schedule['status'] = 0; |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$update = $this->db->update( |
|
167
|
|
|
$this->scheduler_table, |
|
168
|
|
|
$schedule, |
|
169
|
|
|
array( 'id' => $schedule_id ), |
|
170
|
|
|
array( |
|
171
|
|
|
'%s', |
|
172
|
|
|
'%s' |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
return $update; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
View Code Duplication |
public function update_hash($schedule_id, $hash) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$schedule['hash'] = $hash; |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
$update = $this->db->update( |
|
183
|
|
|
$this->scheduler_table, |
|
184
|
|
|
$schedule, |
|
185
|
|
|
array( 'id' => $schedule_id ), |
|
186
|
|
|
array( |
|
187
|
|
|
'%s', |
|
188
|
|
|
'%s' |
|
189
|
|
|
) |
|
190
|
|
|
); |
|
191
|
|
|
return $update; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
View Code Duplication |
public function update_last_backup($schedule_id, $last_backup) |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
$schedule['last_backup'] = $last_backup; |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
$update = $this->db->update( |
|
199
|
|
|
$this->scheduler_table, |
|
200
|
|
|
$schedule, |
|
201
|
|
|
array( 'id' => $schedule_id ), |
|
202
|
|
|
array( |
|
203
|
|
|
'%s', |
|
204
|
|
|
'%s' |
|
205
|
|
|
) |
|
206
|
|
|
); |
|
207
|
|
|
return $update; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
private function __xcloner_scheduler_callback($id, $schedule) |
|
211
|
|
|
{ |
|
212
|
|
|
set_time_limit(0); |
|
213
|
|
|
|
|
214
|
|
|
$this->xcloner_file_system = $this->get_xcloner_container()->get_xcloner_filesystem(); |
|
215
|
|
|
$this->xcloner_database = $this->get_xcloner_container()->get_xcloner_database(); |
|
216
|
|
|
$this->archive_system = $this->get_xcloner_container()->get_archive_system(); |
|
217
|
|
|
$this->logger = $this->get_xcloner_container()->get_xcloner_logger()->withName("xcloner_scheduler"); |
|
218
|
|
|
$this->xcloner_remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
|
219
|
|
|
|
|
220
|
|
|
if($schedule['recurrence'] == "single") |
|
221
|
|
|
{ |
|
222
|
|
|
$this->disable_single_cron($schedule['id']); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if(!$schedule) |
|
226
|
|
|
{ |
|
227
|
|
|
$this->logger->info(sprintf("Could not load schedule with id'%s'", $id), array("CRON")); |
|
228
|
|
|
return; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$this->update_hash($schedule['id'], $this->xcloner_settings->get_hash()); |
|
232
|
|
|
|
|
233
|
|
|
$this->logger->info(sprintf("Starting cron schedule '%s'", $schedule['name']), array("CRON")); |
|
234
|
|
|
|
|
235
|
|
|
$this->xcloner_file_system->set_excluded_files(json_decode($schedule['excluded_files'])); |
|
236
|
|
|
|
|
237
|
|
|
$init = 1; |
|
238
|
|
|
$continue = 1; |
|
239
|
|
|
|
|
240
|
|
|
while($continue) |
|
241
|
|
|
{ |
|
242
|
|
|
$continue = $this->xcloner_file_system->start_file_recursion($init); |
|
243
|
|
|
|
|
244
|
|
|
$init = 0; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$this->logger->info(sprintf("File scan finished"), array("CRON")); |
|
248
|
|
|
|
|
249
|
|
|
$this->logger->info(sprintf("Starting the database backup"), array("CRON")); |
|
250
|
|
|
|
|
251
|
|
|
$init = 1; |
|
252
|
|
|
$return['finished'] = 0; |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
while(!$return['finished']) |
|
255
|
|
|
{ |
|
256
|
|
|
$return = $this->xcloner_database->start_database_recursion((array)json_decode($schedule['table_params']), $return, $init); |
|
257
|
|
|
$init = 0; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$this->logger->info(sprintf("Database backup done"), array("CRON")); |
|
261
|
|
|
|
|
262
|
|
|
$this->logger->info(sprintf("Starting file archive process"), array("CRON")); |
|
263
|
|
|
|
|
264
|
|
|
$init = 0; |
|
265
|
|
|
$return['finished'] = 0; |
|
266
|
|
|
$return['extra'] = array(); |
|
267
|
|
|
|
|
268
|
|
|
while(!$return['finished']) |
|
269
|
|
|
{ |
|
270
|
|
|
$return = $this->archive_system->start_incremental_backup((array)$schedule['backup_params'], $return['extra'], $init); |
|
271
|
|
|
$init = 0; |
|
272
|
|
|
} |
|
273
|
|
|
$this->logger->info(sprintf("File archive process FINISHED."), array("CRON")); |
|
274
|
|
|
|
|
275
|
|
|
//getting the last backup archive file |
|
276
|
|
|
$return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension(); |
|
277
|
|
View Code Duplication |
if($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) |
|
|
|
|
|
|
278
|
|
|
$return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
|
279
|
|
|
|
|
280
|
|
|
$this->update_last_backup($schedule['id'], $return['extra']['backup_parent']); |
|
281
|
|
|
|
|
282
|
|
|
if($schedule['remote_storage'] and array_key_exists($schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages())) |
|
|
|
|
|
|
283
|
|
|
{ |
|
284
|
|
|
$backup_file = $return['extra']['backup_parent']; |
|
285
|
|
|
|
|
286
|
|
|
$this->logger->info(sprintf("Transferring backup to remote storage %s", strtoupper($schedule['remote_storage'])), array("CRON")); |
|
287
|
|
|
|
|
288
|
|
|
if(method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) |
|
289
|
|
|
call_user_func_array(array($this->xcloner_remote_storage, "upload_backup_to_storage"), array($backup_file, $schedule['remote_storage'])); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
if(isset($schedule['backup_params']->email_notification) and $to=$schedule['backup_params']->email_notification) |
|
|
|
|
|
|
294
|
|
|
{ |
|
295
|
|
|
try{ |
|
296
|
|
|
$from = "XCloner Schedule - ".$schedule['name']; |
|
297
|
|
|
$additional['lines_total'] = $return['extra']['lines_total']; |
|
|
|
|
|
|
298
|
|
|
$this->archive_system->send_notification($to, $from, "", $return['extra']['backup_parent'], $schedule, "", $additional); |
|
299
|
|
|
}catch(Exception $e) |
|
300
|
|
|
{ |
|
301
|
|
|
$this->logger->error($e->getMessage()); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$this->xcloner_file_system->remove_tmp_filesystem(); |
|
306
|
|
|
|
|
307
|
|
|
$this->xcloner_file_system->backup_storage_cleanup(); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
public function xcloner_scheduler_callback($id) |
|
311
|
|
|
{ |
|
312
|
|
|
$schedule = $this->get_schedule_by_id($id); |
|
313
|
|
|
|
|
314
|
|
|
try{ |
|
315
|
|
|
|
|
316
|
|
|
$this->__xcloner_scheduler_callback($id, $schedule); |
|
317
|
|
|
|
|
318
|
|
|
}catch(Exception $e){ |
|
319
|
|
|
|
|
320
|
|
|
//send email to site admin if email notification is not set in the scheduler |
|
321
|
|
|
if(!isset($schedule['backup_params']->email_notification)) |
|
322
|
|
|
$schedule['backup_params']->email_notification = get_option('admin_email'); |
|
323
|
|
|
|
|
324
|
|
|
if(isset($schedule['backup_params']->email_notification) and $to=$schedule['backup_params']->email_notification) |
|
|
|
|
|
|
325
|
|
|
{ |
|
326
|
|
|
$from = "XCloner Schedule - ".$schedule['name']; |
|
327
|
|
|
$this->archive_system->send_notification($to, $from, "Scheduled backup error","", "", $e->getMessage()); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
} |
|
336
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.