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

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