PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.4
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.4
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
← All changes | includes/features.php +0 -701 7.107.4 View file →
@@ -1,710 +1,9 @@
1 1 <?php
2 2
3 3 // Anonimization code
4 4 add_filter('wpdbbkp_process_db_fields', 'bkpforwp_anonimize_database', 10, 3);
5 -add_action('wp_ajax_wpdbbkp_check_extract_status', 'wpdbbkp_check_extract_status');
6 -function wpdbbkp_check_extract_status(){
7 - if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
8 - wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
9 - return;
10 - }
11 - $get_progress = get_transient('wpdbbkp_track_progress');
12 - if($get_progress==false){
13 - wp_send_json_success(['success'=>1,'message'=>esc_html__('Starring Extraction Process', 'wpdbbkp')]);
14 - }else{
15 - if($get_progress=='Process Completed'){
16 - delete_transient('wpdbbkp_track_progress');
17 - }
18 - wp_send_json_success(['success'=>1,'message'=>$get_progress]);
19 - }
20 -}
21 5
22 -add_action('wp_ajax_wpdbbkp_upload_site_chunk', 'wpdbbkp_upload_site_chunk');
23 -function wpdbbkp_upload_site_chunk() {
24 - if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
25 - wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
26 - return;
27 - }
28 - if (!isset($_FILES['file']) || !isset($_POST['fileName']) || !isset($_POST['offset'])) {
29 - wp_send_json_error("Invalid request.");
30 - }
31 -
32 - $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp';
33 - if (!file_exists($upload_dir)) {
34 - mkdir($upload_dir, 0755, true);
35 - }
36 -
37 - $file_name = sanitize_file_name($_POST['fileName']);
38 - $file_path = $upload_dir . '/' . $file_name;
39 -
40 - // Append chunk to the file
41 - file_put_contents($file_path, file_get_contents($_FILES['file']['tmp_name']), FILE_APPEND);
42 -
43 - wp_send_json_success(esc_html__('Chunk uploaded successfully.', 'wpdbbkp'));
44 -}
45 -add_action('wp_ajax_wpdbbkp_extract_uploaded_site', 'wpdbbkp_extract_uploaded_site');
46 -function wpdbbkp_extract_uploaded_site() {
47 -
48 - try {
49 -
50 - if ( !isset( $_POST['wpdbbkp_admin_security_nonce'] ) || !wp_verify_nonce( wp_unslash($_POST['wpdbbkp_admin_security_nonce']), 'wpdbbkp_ajax_check_nonce' ) ){
51 -
52 - wp_send_json_success(esc_html__('Invalid nonce check.', 'wpdbbkp'));
53 -
54 - return;
55 -
56 - }
57 -
58 - ini_set('max_execution_time', 0); // No time limit
59 -
60 - set_time_limit(0);
61 -
62 - error_log("AJAX extraction started");
63 -
64 -
65 -
66 - if (!isset($_POST['fileName'])) {
67 -
68 - throw new Exception("No file provided.");
69 -
70 - }
71 -
72 -
73 -
74 - $upload_dir = WP_CONTENT_DIR . '/uploads/wpdbbkp/temp';
75 -
76 - $backup_file = $upload_dir . '/' . sanitize_file_name($_POST['fileName']);
77 -
78 -
79 -
80 - if (!file_exists($backup_file)) {
81 -
82 - throw new Exception("Backup file not found.");
83 -
84 - }
85 -
86 -
87 -
88 - $extract_path = $upload_dir . '/extracted/';
89 -
90 - $zip = new ZipArchive();
91 -
92 - if ($zip->open($backup_file) !== TRUE) {
93 -
94 - throw new Exception("Failed to open the ZIP archive.");
95 -
96 - }
97 -
98 -
99 -
100 - if (!file_exists($extract_path)) {
101 -
102 - mkdir($extract_path, 0755, true);
103 -
104 - }
105 -
106 -
107 -
108 - error_log("ZIP contains " . $zip->numFiles . " files");
109 -
110 -
111 -
112 -
113 -
114 - $root_folder = '';
115 -
116 - for ($i = 0; $i < $zip->numFiles; $i++) {
117 -
118 - $entry = $zip->getNameIndex($i);
119 -
120 - if (preg_match('#^(.*?)/wp-content/#', $entry, $matches)) {
121 -
122 - $root_folder = $matches[1] . '/';
123 -
124 - break;
125 -
126 - }
127 -
128 - }
129 -
130 - error_log("Detected root folder: $root_folder");
131 -
132 -
133 -
134 -
135 -
136 - for ($i = 0; $i < $zip->numFiles; $i++) {
137 -
138 - $entry = $zip->getNameIndex($i);
139 -
140 - $normalized_entry = str_replace($root_folder, '', $entry);
141 -
142 -
143 -
144 - if (preg_match('#^wp-content/plugins/#', $normalized_entry) ||
145 -
146 - preg_match('#^wp-content/themes/#', $normalized_entry) ||
147 -
148 - (pathinfo($normalized_entry, PATHINFO_EXTENSION) === 'sql' && substr_count($normalized_entry, '/') === 0)
149 -
150 - ) {
151 -
152 - if ($zip->extractTo($extract_path, $entry)) {
153 -
154 - set_transient('wpdbbkp_track_progress', $entry, 3600);
155 -
156 - error_log("Successfully extracted: $entry");
157 -
158 -
159 -
160 - } else {
161 -
162 - error_log("Extraction failed: $entry");
163 -
164 - }
165 -
166 - }
167 -
168 - }
169 -
170 -
171 -
172 - $zip->close();
173 -
174 - set_transient('wpdbbkp_track_progress', 'Extraction of files completed. Now taking live plugins & themes', 3600);
175 -
176 - $plugins_path = $extract_path . '/wp-content/plugins/';
177 -
178 - $themes_path = $extract_path . '/wp-content/themes/';
179 -
180 -
181 -
182 - // Move extracted files to wp-content
183 -
184 - $ignore_plugins = ['wp-database-backup'];
185 -
186 -
187 -
188 - if (file_exists($plugins_path)) {
189 -
190 - wpdbbkp_move_extracted_files($plugins_path, WP_CONTENT_DIR . '/plugins/', $ignore_plugins);
191 -
192 - } else {
193 -
194 - error_log("Plugins directory not found in extracted folder.");
195 -
196 - }
197 -
198 -
199 -
200 - if (file_exists($themes_path)) {
201 -
202 - wpdbbkp_move_extracted_files($themes_path, WP_CONTENT_DIR . '/themes/');
203 -
204 - } else {
205 -
206 - error_log("Themes directory not found in extracted folder OR extraction failed.");
207 -
208 - }
209 -
210 -
211 -
212 - // Process SQL file if found
213 -
214 - $sql_file = wpdbbkp_find_sql_file($extract_path);
215 -
216 - if ($sql_file) {
217 -
218 - set_transient('wpdbbkp_track_progress', 'Migrating Database...', 3600);
219 -
220 - $wp_config_prefix = wpdbbkp_get_wp_config_table_prefix();
221 - error_log("Sql File .".$sql_file);
222 - wpdbbkp_update_sql_table_prefix($sql_file);
223 -
224 - wpdbbkp_restore_database($sql_file);
225 -
226 - } else {
227 -
228 - error_log("No SQL file found in extracted folder.");
229 -
230 - }
231 -
232 -
233 -
234 - error_log("Extraction completed successfully");
235 -
236 - $wpdbbkp_folder = WP_CONTENT_DIR . '/uploads/wpdbbkp';
237 - if (file_exists($wpdbbkp_folder)) {
238 - wpdbbkp_delete_folder($wpdbbkp_folder);
239 - error_log("Deleted wpdbbkp folder after successful migration.");
240 - }
241 - set_transient('wpdbbkp_track_progress', 'Process Completed', 3600);
242 - wp_send_json_success(esc_html__('Plugins, Themes, Database & Table Prefix Updated!', 'wpdbbkp'));
243 -
244 -
245 -
246 - } catch (Exception $e) {
247 -
248 - error_log("Error: " . $e->getMessage());
249 -
250 - wp_send_json_error($e->getMessage());
251 -
252 - }
253 -
254 -}
255 -
256 -function wpdbbkp_delete_folder($folder_path) {
257 - if (!is_dir($folder_path)) {
258 - return;
259 - }
260 -
261 - $files = array_diff(scandir($folder_path), array('.', '..'));
262 -
263 - foreach ($files as $file) {
264 - $file_path = $folder_path . DIRECTORY_SEPARATOR . $file;
265 - if (is_dir($file_path)) {
266 - wpdbbkp_delete_folder($file_path);
267 - } else {
268 - set_transient('wpdbbkp_track_progress', 'Cleaning : '.$file_path, 3600);
269 - unlink($file_path);
270 - }
271 - }
272 -
273 - rmdir($folder_path);
274 -}
275 -
276 -
277 -
278 -
279 -function wpdbbkp_find_sql_file($directory) {
280 -
281 - $files = scandir($directory);
282 -
283 - foreach ($files as $file) {
284 -
285 - if (pathinfo($file, PATHINFO_EXTENSION) === 'sql') {
286 -
287 - return $directory . '/' . $file;
288 -
289 - }
290 -
291 - }
292 -
293 - return false;
294 -
295 -}
296 -
297 -
298 -
299 -function wpdbbkp_get_wp_config_table_prefix() {
300 -
301 - $config_file = ABSPATH . 'wp-config.php';
302 - error_log("[ERROR] Config Path: $config_file");
303 - if (!file_exists($config_file)) {
304 -
305 - return 'wp_';
306 -
307 - }
308 -
309 - $config_contents = file_get_contents($config_file);
310 -
311 - if (preg_match("/\$table_prefix\s*=\s*'([^']+)'/", $config_contents, $matches)) {
312 -
313 - return $matches[1];
314 -
315 - }
316 -
317 - return 'wp_';
318 -
319 -}
320 -function wpdbbkp_update_sql_table_prefix($sql_file) {
321 - if (!file_exists($sql_file)) {
322 - die("❌ ERROR: SQL file not found: $sql_file\n");
323 - }
324 -
325 - $sql_content = file_get_contents($sql_file);
326 -
327 - // Find wp-config.php
328 - $wp_config_path = find_wp_config();
329 - if (!$wp_config_path) {
330 - die("❌ ERROR: wp-config.php not found!\n");
331 - }
332 -
333 - // Extract table prefix from wp-config.php
334 - $config_content = file_get_contents($wp_config_path);
335 - if (preg_match("/\\\$table_prefix\s*=\s*['\"]([^'\"]+)['\"]\s*;/", $config_content, $matches)) {
336 - $new_prefix = $matches[1];
337 - echo "✅ SUCCESS: Extracted new prefix from wp-config.php: '$new_prefix'\n";
338 - } else {
339 - die("❌ ERROR: Could not extract table prefix from wp-config.php!\n");
340 - }
341 -
342 - // Detect old prefix from the SQL file
343 - if (preg_match("/(CREATE TABLE|INSERT INTO|ALTER TABLE|UPDATE|DELETE FROM)\s+[`']?([a-zA-Z0-9]+?_)/i", $sql_content, $matches)) {
344 - $old_prefix = $matches[2];
345 - echo "✅ SUCCESS: Found old prefix in SQL file: '$old_prefix'\n";
346 - } else {
347 - die("❌ ERROR: No table prefix found in the SQL file!\n");
348 - }
349 -
350 - // Prevent replacing if old prefix is same as new prefix
351 - if ($old_prefix === $new_prefix) {
352 - die("ℹ️ INFO: Old and new prefixes are the same ('$old_prefix'), no changes needed.\n");
353 - }
354 -
355 - // Replace all occurrences of the old prefix with the new one
356 - $sql_content = preg_replace("/\b" . preg_quote($old_prefix, '/') . "/i", $new_prefix, $sql_content);
357 -
358 - // Write the updated content back to the SQL file
359 - file_put_contents($sql_file, $sql_content);
360 -
361 - echo "✅ SUCCESS: Table prefix updated from '$old_prefix' to '$new_prefix' in $sql_file!\n";
362 -}
363 -
364 -/**
365 -* Finds the root wp-config.php file by checking parent directories.
366 -*/
367 -function find_wp_config() {
368 - $dir = __DIR__;
369 -
370 - while ($dir !== dirname($dir)) { // Keep going up until the root
371 - $config_path = $dir . '/wp-config.php';
372 - if (file_exists($config_path)) {
373 - return $config_path;
374 - }
375 - $dir = dirname($dir);
376 - }
377 -
378 - return false; // wp-config.php not found
379 -}
380 -
381 -
382 -
383 -
384 -
385 -
386 -
387 -
388 -function wpdbbkp_move_extracted_files($source, $destination, $ignore = []) {
389 - if (!file_exists($source)) return;
390 -
391 - if (!file_exists($destination)) {
392 - mkdir($destination, 0755, true);
393 - }
394 -
395 - $files = array_diff(scandir($source), array('.', '..'));
396 - foreach ($files as $file) {
397 - $srcFile = rtrim($source, '/') . '/' . $file;
398 - $destFile = rtrim($destination, '/') . '/' . $file;
399 -
400 - if (in_array($file, $ignore)) {
401 - continue;
402 - }
403 -
404 - set_transient('wpdbbkp_track_progress', 'Copying: ' . $destFile, 3600);
405 -
406 - if (is_dir($srcFile)) {
407 - if (file_exists($destFile)) {
408 - wpdbbkp_delete_directory($destFile);
409 - }
410 - rename($srcFile, $destFile);
411 - } else {
412 - if (file_exists($destFile)) {
413 - unlink($destFile);
414 - }
415 - rename($srcFile, $destFile);
416 - }
417 - }
418 -}
419 -
420 -function wpdbbkp_delete_directory($dir) {
421 - if (!is_dir($dir)) {
422 - return unlink($dir);
423 - }
424 -
425 - $files = array_diff(scandir($dir), array('.', '..'));
426 - foreach ($files as $file) {
427 - $filePath = $dir . DIRECTORY_SEPARATOR . $file;
428 - if (is_dir($filePath)) {
429 - wpdbbkp_delete_directory($filePath);
430 - } else {
431 - unlink($filePath);
432 - }
433 - }
434 -
435 - return rmdir($dir);
436 -}
437 -
438 -
439 -function wpdbbkp_restore_database($sql_file) {
440 -
441 - global $wpdb;
442 -
443 - $new_site_url = get_site_url();
444 -
445 -
446 -
447 - if (!file_exists($sql_file)) {
448 -
449 - error_log("[ERROR] SQL file not found: $sql_file");
450 -
451 - return false;
452 -
453 - }
454 -
455 -
456 -
457 - $sql_content = file_get_contents($sql_file);
458 -
459 - if (!$sql_content) {
460 -
461 - error_log("[ERROR] Failed to read SQL file: $sql_file");
462 -
463 - return false;
464 -
465 - }
466 -
467 -
468 -
469 - // Fetch old site URL from wp_options before updating
470 -
471 - $old_site_url = $wpdb->get_var("SELECT option_value FROM {$wpdb->options} WHERE option_name = 'siteurl'");
472 -
473 -
474 -
475 - if (!$old_site_url) {
476 -
477 - error_log("[ERROR] Failed to fetch old site URL.");
478 -
479 - return false;
480 -
481 - }
482 -
483 -
484 -
485 - error_log("[INFO] Old Site URL detected: $old_site_url");
486 -
487 -
488 -
489 - // Split SQL statements properly
490 -
491 - $queries = preg_split('/;\s*\n/', $sql_content, -1, PREG_SPLIT_NO_EMPTY);
492 -
493 -
494 -
495 - // Tables to exclude (e.g., Users and Usermeta for security reasons)
496 -
497 - $excluded_tables = ['users','usermeta','options'];
498 -
499 -
500 -
501 - $wpdb->query('SET foreign_key_checks = 0'); // Disable FK checks
502 -
503 - $wpdb->query('START TRANSACTION'); // Start transaction
504 -
505 -
506 -
507 - try {
508 -
509 - foreach ($queries as $query) {
510 -
511 - $query = trim($query);
512 -
513 - if (empty($query)) continue;
514 -
515 - $query = str_replace('options','option_tmp',$query);
516 -
517 - // Check for CREATE TABLE and extract table name
518 -
519 - if (preg_match('/CREATE TABLE `([^`]*)`/', $query, $matches)) {
520 -
521 - $table_name = $matches[1];
522 -
523 -
524 -
525 - // Skip excluded tables
526 -
527 - foreach ($excluded_tables as $excluded) {
528 -
529 - if (strpos($table_name, $excluded) !== false) {
530 -
531 - error_log("[SKIPPED] Table excluded: $table_name");
532 -
533 - continue 2;
534 -
535 - }
536 -
537 - }
538 -
539 -
540 -
541 - // Drop existing table before restoring
542 -
543 - $wpdb->query("DROP TABLE IF EXISTS `$table_name`");
544 -
545 - error_log("[DROPPED] Table: $table_name");
546 -
547 - }
548 -
549 -
550 -
551 - // Skip execution of queries for excluded tables
552 -
553 - foreach ($excluded_tables as $excluded) {
554 -
555 - if (strpos($query, $excluded) !== false) {
556 -
557 - error_log("[SKIPPED] Query contains excluded table reference.");
558 -
559 - continue 2;
560 -
561 - }
562 -
563 - }
564 -
565 -
566 -
567 - // Execute the query
568 -
569 - $result = $wpdb->query($query);
570 -
571 - if ($result === false) {
572 -
573 - throw new Exception("[ERROR] Failed to execute query: " . $wpdb->last_error);
574 -
575 - }
576 -
577 - }
578 -
579 -
580 -
581 -
582 -
583 -
584 -
585 - // **Find and replace old URLs in all tables**
586 -
587 - $tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
588 -
589 - foreach ($tables as $table) {
590 -
591 - $table_name = $table[0];
592 -
593 -
594 -
595 - // Skip excluded tables
596 -
597 - foreach ($excluded_tables as $excluded) {
598 -
599 - if (strpos($table_name, $excluded) !== false) {
600 -
601 - error_log("[SKIPPED] URL Replacement in: $table_name");
602 -
603 - continue 2;
604 -
605 - }
606 -
607 - }
608 -
609 -
610 -
611 - // Get all columns for the table
612 -
613 - $columns = $wpdb->get_results("SHOW COLUMNS FROM `$table_name`", ARRAY_A);
614 -
615 - foreach ($columns as $column) {
616 -
617 - $column_name = $column['Field'];
618 -
619 -
620 -
621 - // Update all occurrences of old URL in text-based columns
622 -
623 - $wpdb->query(
624 -
625 - $wpdb->prepare(
626 -
627 - "UPDATE `$table_name` SET `$column_name` = REPLACE(`$column_name`, %s, %s) WHERE `$column_name` LIKE %s",
628 -
629 - $old_site_url,
630 -
631 - $new_site_url,
632 -
633 - '%' . $wpdb->esc_like($old_site_url) . '%'
634 -
635 - )
636 -
637 - );
638 -
639 - }
640 -
641 - }
642 -
643 -
644 -
645 - $blogname = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'blogname'");
646 -
647 - if ($blogname) {
648 -
649 - $wpdb->query(
650 -
651 - $wpdb->prepare(
652 -
653 - "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'blogname'",
654 -
655 - $blogname
656 -
657 - )
658 -
659 - );
660 -
661 - error_log("[SUCCESS] Updated wp_options with blogname from option_tmp");
662 -
663 - }
664 - $active_plugins = $wpdb->get_var("SELECT option_value FROM {$wpdb->prefix}option_tmp WHERE option_name = 'active_plugins'");
665 -
666 - if ($active_plugins) {
667 -
668 - $wpdb->query(
669 -
670 - $wpdb->prepare(
671 -
672 - "UPDATE {$wpdb->prefix}options SET option_value = %s WHERE option_name = 'active_plugins'",
673 -
674 - $active_plugins
675 -
676 - )
677 -
678 - );
679 -
680 - error_log("[SUCCESS] Updated wp_options with active_plugins from option_tmp");
681 -
682 - }
683 -
684 - $wpdb->query('COMMIT'); // Commit transaction
685 -
686 - error_log("[SUCCESS] Database restoration and URL replacement completed from: $sql_file");
687 -
688 - return true;
689 -
690 -
691 -
692 - } catch (Exception $e) {
693 -
694 - $wpdb->query('ROLLBACK'); // Rollback in case of an error
695 -
696 - error_log($e->getMessage());
697 -
698 - return false;
699 -
700 - } finally {
701 -
702 - $wpdb->query('SET foreign_key_checks = 1'); // Re-enable FK checks
703 -
704 - }
705 -
706 -}
707 6 function bkpforwp_anonimize_database($value, $table, $column)
708 7 {
709 8 $enable_anonymization = get_option('bkpforwp_enable_anonymization', false);
710 9 $anonymization_type = get_option('bkpforwp_anonymization_type', false);