PluginProbe
ManageWP Worker / 3.8.7
ManageWP Worker v3.8.7
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.7, at backup.class.php

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