PluginProbe
ManageWP Worker / 3.8.3
ManageWP Worker v3.8.3
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.3, at backup.class.php

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