PluginProbe
ManageWP Worker / 3.8.4
ManageWP Worker v3.8.4
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / backup.class.php

backup.class.php in ManageWP Worker 3.8.4, at backup.class.php

644 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*************************************************************
3 *
4 * backup.class.php
5 *
6 * Manage Backups
7 *
8 *
9 * Copyright (c) 2011 Prelovac Media
10 * www.prelovac.com
11 **************************************************************/
12
13 class MMB_Backup extends MMB_Core
14 {
15 function __construct()
16 {
17 $upload_dir = wp_upload_dir();
18 $sec_string = md5('mmb-worker');
19 $file = "/$sec_string/backups";
20 $file_path = $upload_dir['basedir'] . $file;
21 file_put_contents($file_path . '/index.php', '');
22 if (!file_exists($file_path)) {
23 if(!mkdir($file_path, 0755, true))
24 return array('error' => 'Failed to create backup folder.');
25 }
26 parent::__construct();
27 }
28
29 function backup($args)
30 {
31 $this->_escape($args);
32
33 //type like manual, weekly, daily
34 $type = $args['type'];
35 //what like full, only db, only wp-content
36 $what = $args['what'];
37 if (trim($type) == '')
38 $type = 'manual'; //default
39
40 $upload_dir = wp_upload_dir();
41 $sec_string = md5('mmb-worker');
42 $file = "/$sec_string/backups";
43 $file_path = $upload_dir['basedir'] . $file;
44
45 if (!file_exists($file_path)) {
46 if(!mkdir($file_path, 0755, true))
47 return array('error' => 'Failed to create backup folder.');
48 }
49
50 if (trim($what) == 'full') {
51 //take wp-content backup
52 $content_backup = $this->backup_wpcontent($type);
53 if (!$content_backup) {
54 @unlink($content_backup['path']);
55 return array(
56 'error' => 'Failed to backup wp-content.'
57 );
58 }
59 }
60
61 if (trim($what) == 'full' || trim($what) == 'db') {
62 //take database backup
63 $db_backup = $this->backup_db($type);
64 if (!$db_backup) {
65 if (trim($what) == 'full')
66 @unlink($content_backup['path']);
67
68 @unlink($db_backup['path']);
69 return array(
70 'error' => 'Failed to backup database.'
71 );
72 }
73 }
74
75 include_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
76
77 // Get previous backup in tmp
78 $worker_options = get_option('mmb-worker');
79 $tmp_file = $upload_dir['basedir'] . '/' . basename($worker_options['backups'][$type]['path']);
80
81 if (rename($worker_options['backups'][$type]['path'], $tmp_file)) {
82 @unlink($worker_options['backups'][$type]['path']);
83 }
84
85 //Prepare .zip file name
86 $site_name = $this->remove_http(get_bloginfo('url'));
87 $site_name = str_replace(array(
88 "_",
89 "/"
90 ), array(
91 "",
92 "-"
93 ), $site_name);
94 $backup_file = $file_path . '/' . $site_name . '_' . $type . '_' . $what . '_' . date('Y-m-d') . '.zip';
95
96 if (!$this->check_zip()) {
97 $archive = new PclZip($backup_file);
98 }
99
100 if (trim($what) == 'full') {
101 $htaccess_path = ABSPATH . ".htaccess";
102 $wp_config_path = ABSPATH . "wp-config.php";
103 if ($this->check_zip() && $this->check_sys()) {
104 $command = "zip $backup_file -j $content_backup[path] -j $db_backup[path] -j $htaccess_path -j $wp_config_path";
105 ob_start();
106 $func = $this->check_sys();
107 switch($func)
108 {
109 case 'passthru': passthru($command, $err); break;
110 case 'exec': exec($command); break;
111 case 'system': system($command); break;
112 default: break;
113 }
114 ob_get_clean();
115 } else {
116 $result = $archive->add($content_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
117 $result = $archive->add($db_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
118 $result = $archive->add($htaccess_path, PCLZIP_OPT_REMOVE_ALL_PATH);
119 $result = $archive->add($wp_config_path, PCLZIP_OPT_REMOVE_ALL_PATH);
120 $err = !$result;
121 }
122
123 } elseif (trim($what) == 'db') {
124 if ($this->check_zip() && $this->check_sys()) {
125 $command = "zip $backup_file -j $db_backup[path]";
126 ob_start();
127 $func = $this->check_sys();
128 switch($func)
129 {
130 case 'passthru': passthru($command, $err); break;
131 case 'exec': exec($command); break;
132 case 'system': system($command); break;
133 default: break;
134 }
135 ob_get_clean();
136 } else {
137 $result = $archive->add($db_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
138 $err = !$result;
139 }
140 }
141
142 if ($err) {
143 if (rename($tmp_file, $worker_options['backups'][$type]['path'])) {
144 @unlink($tmp_file);
145 }
146 return array(
147 'error' => 'Backup failed. Cannot create backup zip file.'
148 );
149 }
150
151 @unlink($tmp_file);
152 @unlink($content_backup['path']);
153 @unlink($db_backup['path']);
154 $backup_url = $upload_dir['baseurl'] . $file . '/' . $site_name . '_' . $type . '_' . $what . '_' . date('Y-m-d') . '.zip';
155 $worker_options = get_option('mmb-worker');
156 //remove old file
157 if ($worker_options['backups'][$type]['path'] != $backup_file) {
158 @unlink($worker_options['backups'][$type]['path']);
159 }
160
161 $worker_options['backups'][$type]['path'] = $backup_file;
162 $worker_options['backups'][$type]['url'] = $backup_url;
163 update_option('mmb-worker', $worker_options);
164
165 //Everything went fine, return backup url to master
166 return $worker_options['backups'][$type]['url'];
167 }
168
169 function backup_wpcontent($type)
170 {
171 $upload_dir = wp_upload_dir();
172 $sec_string = md5('mmb-worker');
173 $file = '/' . $sec_string . '/backups/wp-content_' . date('Y-m-d') . '.zip';
174 $file_path = $upload_dir['basedir'] . $file;
175 if ($this->check_zip() && $this->check_sys()) {
176 chdir(WP_CONTENT_DIR);
177 $command = "zip -r $file_path 'plugins/' 'themes/' 'uploads/' -x 'uploads/" . $sec_string . "/*'";
178 ob_start();
179 $func = $this->check_sys();
180 switch($func)
181 {
182 case 'passthru': passthru($command, $err); break;
183 case 'exec': exec($command); break;
184 case 'system': system($command); break;
185 default: break;
186 }
187 ob_get_clean();
188 if (!$err || $err == 18) {
189 $file_url = $upload_dir['baseurl'] . $file;
190 return array(
191 'path' => $file_path,
192 'url' => $file_url
193 );
194 }
195 @unlink($file_path);
196 return false;
197
198 } else {
199 require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
200 $archive = new PclZip($file_path);
201 $result = $archive->add(WP_CONTENT_DIR . '/plugins', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
202 $result = $archive->add(WP_CONTENT_DIR . '/themes', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
203 $result = $archive->add(WP_CONTENT_DIR . '/uploads', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
204
205 $result = $archive->delete(PCLZIP_OPT_BY_NAME, 'uploads/' . $sec_string . '/');
206 if ($result) {
207 $file_url = $upload_dir['baseurl'] . $file;
208 return array(
209 'path' => $file_path,
210 'url' => $file_url
211 );
212 }
213 @unlink($file_path);
214 return false;
215 }
216 }
217
218
219 function backup_db($type)
220 {
221 $mysqldump_exists = $this->check_mysqldump();
222
223
224 if (is_array($mysqldump_exists) && $this->check_sys()) {
225
226 $result = $this->backup_db_dump($type, $mysqldump_exists);
227
228 } else {
229 $result = $this->backup_db_php($type);
230
231 }
232 return $result;
233 }
234
235 function backup_db_dump($type, $paths)
236 {
237 global $wpdb;
238 $upload_dir = wp_upload_dir();
239 $sec_string = md5('mmb-worker');
240 $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
241
242 $file = $upload_dir['path'] . '/' . DB_NAME . '.sql';
243 $file_url = $upload_dir['baseurl'] . '/' . DB_NAME . '.sql';
244
245 $command = $brace . $paths['mysqldump'] . $brace . ' --host="' . DB_HOST . '" --user="' . DB_USER . '" --password="' . DB_PASSWORD . '" --add-drop-table --skip-lock-tables "' . DB_NAME . '" > ' . $brace . $file . $brace;
246 ob_start();
247 $func = $this->check_sys();
248 switch($func)
249 {
250 case 'passthru': passthru($command, $error); break;
251 case 'exec': exec($command); break;
252 case 'system': system($command); break;
253 default: break;
254 }
255 ob_get_clean();
256
257 if ($error) {
258 $result = $this->backup_db_php($type);
259 return $result;
260 }
261
262 if (filesize($file) == 0 || !is_file($file) || $error) {
263 @unlink($file);
264 return false;
265 } else {
266 return array(
267 'path' => $file,
268 'url' => $file_url
269 );
270 }
271 }
272
273 function backup_db_php($type)
274 {
275 global $wpdb;
276 $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
277 $upload_dir = wp_upload_dir();
278 $sec_string = md5('mmb-worker');
279 $zip_file = '/' . $sec_string . '/backups/db_' . date('Y-m-d') . '.zip';
280 $zip_file_path = $upload_dir['basedir'] . $zip_file;
281
282 $file = $upload_dir['path'] . '/' . DB_NAME . '.sql';
283 $file_url = $upload_dir['baseurl'] . '/' . DB_NAME . '.sql';
284
285 foreach ($tables as $table) {
286 //drop exixting table
287 $dump_data = "DROP TABLE IF EXISTS $table[0];";
288 //create table
289 $create_table = $wpdb->get_row("SHOW CREATE TABLE $table[0]", ARRAY_N);
290 $dump_data .= "\n\n" . $create_table[1] . ";\n\n";
291
292 $count = $wpdb->get_var("SELECT count(*) FROM $table[0]");
293 if ($count > 100)
294 $count = ceil($count / 100) - 1;
295 else
296 $count = 1;
297 for ($i = 0; $i < $count; $i++) {
298 $low_limit = $i * 100;
299 $qry = "SELECT * FROM $table[0] LIMIT $low_limit, 100";
300 $rows = $wpdb->get_results($qry, ARRAY_A);
301 if (is_array($rows)) {
302 foreach ($rows as $row) {
303 //insert single row
304 $dump_data .= "INSERT INTO $table[0] VALUES(";
305 $num_values = count($row);
306 $j = 1;
307 foreach ($row as $value) {
308 $value = addslashes($value);
309 $value = ereg_replace("\n", "\\n", $value);
310 $num_values == $j ? $dump_data .= "'" . $value . "'" : $dump_data .= "'" . $value . "', ";
311 $j++;
312 unset($value);
313 }
314 $dump_data .= ");\n";
315 }
316 }
317 }
318 $dump_data .= "\n\n\n";
319
320 unset($rows);
321 file_put_contents($file, $dump_data, FILE_APPEND);
322 unset($dump_data);
323 }
324 if (filesize($file) == 0 || !is_file($file)) {
325 @unlink($file);
326 return false;
327 }
328
329 return array(
330 'path' => $file,
331 'url' => $file_url
332 );
333
334 }
335
336 function restore($args)
337 {
338 $this->_escape($args);
339 $type = $args['type'];
340 if (trim($type) == '') {
341 return false;
342 }
343 // Set paths
344 $upload_dir = wp_upload_dir();
345 $sec_string = md5('mmb-worker');
346 $backup_dir = "/$sec_string/backups";
347 $file = "/$sec_string/restore";
348 $file_path = $upload_dir['basedir'] . $file; //restore path - temporary
349 $backup_path = $upload_dir['basedir'] . $backup_dir; //backup path
350
351 // If manual backup - get backup file from master, if not - get backup file from worker
352 if ($type != 'weekly' && $type != 'daily') {
353 // Download backup file from master
354 include_once(ABSPATH . 'wp-admin/includes/file.php');
355 $tmp_file = download_url($type);
356 $backup_file = $backup_path . "/" . basename($type);
357 if (rename($tmp_file, $backup_file)) {
358 @unlink($tmp_file);
359 } else {
360 $backup_file = $tmp_file;
361 }
362
363 } else {
364 // Getting file from worker
365 $backup_file = $worker_options['backups'][$type]['path'];
366 }
367
368
369 if ($backup_file) {
370 if ($this->check_unzip() && $this->check_sys()) {
371
372 if(!mkdir($file_path))
373 return array('error' => 'Failed to create restore folder.');
374
375 chdir($file_path);
376 $command = "unzip -o $backup_file";
377 ob_start();
378 $func = $this->check_sys();
379 switch($func)
380 {
381 case 'passthru': passthru($command, $err); break;
382 case 'exec': exec($command); break;
383 case 'system': system($command); break;
384 default: break;
385 }
386 ob_get_clean();
387
388 } else {
389 require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
390 $archive = new PclZip($backup_file);
391 $extracted = $archive->extract(PCLZIP_OPT_PATH, $file_path, PCLZIP_OPT_REMOVE_ALL_PATH);
392 $err = !$extracted;
393 }
394
395 if ($err) {
396 return array(
397 'error' => 'Error extracting backup file.'
398 );
399 }
400
401 list(, $name, $what) = explode('_', basename($backup_file, '.zip'));
402
403 if (trim($what) == 'full' || trim($what) == 'db') {
404 if (!$this->restore_db($type, $file_path)) {
405 return array(
406 'error' => 'Error restoring database.'
407 );
408 }
409 }
410
411 if (trim($what) == 'full') {
412 if (!$this->restore_wpcontent($type, $file_path)) {
413 return array(
414 'error' => 'Error restoring wp-content.'
415 );
416 }
417 }
418
419 $this->delete_temp_dir($file_path);
420 }
421 else
422 {
423 return array(
424 'error' => 'Error restoring. Cannot find backup file.'
425 );
426 }
427
428 return true;
429 }
430
431 function restore_wpcontent($type, $file_path)
432 {
433 require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
434 $content_file = glob($file_path . "/*.zip");
435 $wp_config_file = glob($file_path . "/wp-config.php");
436 $htaccess_file = glob($file_path . "/.htaccess");
437 if ($this->check_unzip() && $this->check_sys()) {
438
439 chdir(WP_CONTENT_DIR);
440 $con_file = $content_file[0];
441 $command = "unzip -o $con_file";
442 ob_start();
443 $func = $this->check_sys();
444 switch($func)
445 {
446 case 'passthru': passthru($command, $err); break;
447 case 'exec': exec($command); break;
448 case 'system': system($command); break;
449 default: break;
450 }
451 ob_get_clean();
452 } else {
453 $archive = new PclZip($content_file[0]);
454 $restore_content = $archive->extract(PCLZIP_OPT_PATH, WP_CONTENT_DIR, PCLZIP_OPT_REPLACE_NEWER);
455 $err = !$restore_content;
456 }
457
458 @rename($wp_config_file[0], ABSPATH . "wp-config.php");
459 @rename($htaccess_file[0], ABSPATH . ".htaccess");
460 @unlink($wp_config_file[0]);
461 @unlink($htaccess_file[0]);
462
463 if ($err)
464 return false;
465 else
466 return true;
467 }
468
469 function restore_db($type, $file_path)
470 {
471 global $wpdb;
472
473 $mysqldump = $this->check_mysqldump();
474
475 if (is_array($mysqldump) && $this->check_sys()) {
476 $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
477
478 foreach (glob($file_path . '/*.sql') as $filename) {
479 $command = $brace . $mysqldump['mysql'] . $brace . ' --host="' . DB_HOST . '" --user="' . DB_USER . '" --password="' . DB_PASSWORD . '" ' . DB_NAME . ' < ' . $brace . $filename . $brace;
480 ob_start();
481 $func = $this->check_sys();
482 switch($func)
483 {
484 case 'passthru': passthru($command, $error); break;
485 case 'exec': exec($command); break;
486 case 'system': system($command); break;
487 default: break;
488 }
489 ob_get_clean();
490 break;
491 }
492
493 if ($error) //try php
494 {
495 foreach (glob($file_path . '/*.sql') as $filename) {
496 $current_query = '';
497 // Read in entire file
498 $lines = file($filename);
499 // Loop through each line
500 foreach ($lines as $line) {
501 // Skip it if it's a comment
502 if (substr($line, 0, 2) == '--' || $line == '')
503 continue;
504
505 // Add this line to the current query
506 $current_query .= $line;
507 // If it has a semicolon at the end, it's the end of the query
508 if (substr(trim($line), -1, 1) == ';') {
509 // Perform the query
510 $result = $wpdb->query($current_query);
511 if ($result === false)
512 return FALSE;
513 // Reset temp variable to empty
514 $current_query = '';
515 }
516 }
517 }
518 return true;
519 } else {
520 return true;
521 }
522
523 } else {
524 foreach (glob($file_path . '/*.sql') as $filename) {
525 // Temporary variable, used to store current query
526 $current_query = '';
527 // Read in entire file
528 $lines = file($filename);
529 // Loop through each line
530 foreach ($lines as $line) {
531 // Skip it if it's a comment
532 if (substr($line, 0, 2) == '--' || $line == '')
533 continue;
534
535 // Add this line to the current query
536 $current_query .= $line;
537 // If it has a semicolon at the end, it's the end of the query
538 if (substr(trim($line), -1, 1) == ';') {
539 // Perform the query
540 $result = $wpdb->query($current_query);
541 if ($result === false)
542 return FALSE;
543 // Reset temp variable to empty
544 $current_query = '';
545 }
546 }
547 }
548 return true;
549 }
550 }
551
552 function optimize_tables()
553 {
554 global $wpdb;
555 $tables = $wpdb->get_col("SHOW TABLES");
556
557 foreach ($tables as $table_name) {
558 $table_string .= $table_name . ",";
559 }
560 $table_string = rtrim($table_string);
561 $optimize = $wpdb->query("OPTIMIZE TABLE $table_string");
562 return $optimize ? true : false;
563 }
564
565 ### Function: Auto Detect MYSQL and MYSQL Dump Paths
566 function check_mysqldump()
567 {
568 global $wpdb;
569 $paths = array(
570 'mysq' => '',
571 'mysqldump' => ''
572 );
573 if (substr(PHP_OS, 0, 3) == 'WIN') {
574 $mysql_install = $wpdb->get_row("SHOW VARIABLES LIKE 'basedir'");
575 if ($mysql_install) {
576 $install_path = str_replace('\\', '/', $mysql_install->Value);
577 $paths['mysql'] = $install_path . 'bin/mysql.exe';
578 $paths['mysqldump'] = $install_path . 'bin/mysqldump.exe';
579 } else {
580 $paths['mysql'] = 'mysql.exe';
581 $paths['mysqldump'] = 'mysqldump.exe';
582 }
583 } else {
584 if (function_exists('exec')) {
585 $paths['mysql'] = @exec('which mysql');
586 $paths['mysqldump'] = @exec('which mysqldump');
587 } else {
588 $paths['mysql'] = 'mysql';
589 $paths['mysqldump'] = 'mysqldump';
590 }
591 }
592
593 if (!@file_exists(stripslashes($paths['mysqldump']))) {
594 return false;
595 }
596 if (!@file_exists(stripslashes($paths['mysql']))) {
597 return false;
598 }
599
600 return $paths;
601 }
602
603 //Check if passthru, system or exec functions exist
604 function check_sys()
605 {
606 $stats_function_disabled = 0;
607 if (!function_exists('passthru')) {
608 $stats_function_disabled++;
609 } else {
610 return 'passthru';
611 }
612
613 if (!function_exists('system')) {
614 $stats_function_disabled++;
615 } else {
616 return 'system';
617 }
618
619 if (!function_exists('exec')) {
620 $stats_function_disabled++;
621 } else {
622 return 'exec';
623 }
624
625 if ($stats_function_disabled == 3) {
626 return false;
627 }
628
629 }
630
631 function check_zip()
632 {
633 $zip = @exec('which zip');
634 return $zip ? true : false;
635 }
636
637 function check_unzip()
638 {
639 $zip = @exec('which unzip');
640 return $zip ? true : false;
641 }
642
643 }
644 ?>