PluginProbe
ManageWP Worker / 3.9.2
ManageWP Worker v3.9.2
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.2, at backup.class.php

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