PluginProbe
ManageWP Worker / 3.8.2
ManageWP Worker v3.8.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.8.2, at backup.class.php

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