|
| 1 | +<?php |
| 2 | +// Set current working directory |
| 3 | +chdir(__DIR__); |
| 4 | + |
| 5 | +// Get database credentials from wp-config.php |
| 6 | +$wp_config_path = 'wp-config.php'; |
| 7 | + |
| 8 | +if (!file_exists($wp_config_path)) { |
| 9 | + exit("Error: $wp_config_path not found"); |
| 10 | +} |
| 11 | + |
| 12 | +$wp_config_content = file_get_contents($wp_config_path); |
| 13 | + |
| 14 | +preg_match("/define\(\s*['\"]DB_NAME['\"]\s*,\s*['\"](.*?)['\"]\s*\)/", $wp_config_content, $matches); |
| 15 | +$db_name = $matches[1]; |
| 16 | + |
| 17 | +preg_match("/define\(\s*['\"]DB_USER['\"]\s*,\s*['\"](.*?)['\"]\s*\)/", $wp_config_content, $matches); |
| 18 | +$db_user = $matches[1]; |
| 19 | + |
| 20 | +preg_match("/define\(\s*['\"]DB_PASSWORD['\"]\s*,\s*['\"](.*?)['\"]\s*\)/", $wp_config_content, $matches); |
| 21 | +$db_password = $matches[1]; |
| 22 | + |
| 23 | +// Generate random string for file names |
| 24 | +$random_string = uniqid(); |
| 25 | + |
| 26 | +// Generate file names |
| 27 | +$db_file = "backup-wp-db-$random_string.sql"; |
| 28 | +$zip_file = "backup-wp-files-$random_string.zip"; |
| 29 | + |
| 30 | +// Start logging |
| 31 | +$log = "Script started at " . date('Y-m-d H:i:s') . "\n"; |
| 32 | + |
| 33 | +// Export database |
| 34 | +$cmd = "mysqldump --no-tablespaces -u $db_user -p$db_password $db_name > $db_file"; |
| 35 | +$log .= "Running WP database export command:\n" . preg_replace('/-p(.*?)\s+/', '-pxxxxxxxx ', $cmd) . "\n"; |
| 36 | +$output = shell_exec($cmd); |
| 37 | +$log .= $output . "\n"; |
| 38 | + |
| 39 | +// Create zip backup of files |
| 40 | +$cmd = "zip -r $zip_file *"; |
| 41 | +$log .= "Running zip backup command:\n$cmd\n"; |
| 42 | +$output = shell_exec($cmd); |
| 43 | +$log .= $output . "\n"; |
| 44 | + |
| 45 | +// Remove database backup file |
| 46 | +$cmd = "rm $db_file"; |
| 47 | +$log .= "Removing database backup file:\n$cmd\n"; |
| 48 | +$output = shell_exec($cmd); |
| 49 | +$log .= $output . "\n"; |
| 50 | + |
| 51 | +// End logging |
| 52 | +$log .= "Script finished at " . date('Y-m-d H:i:s') . "\n"; |
| 53 | +$log .= "Extract the zip file to see files and db sql file. \n"; |
| 54 | + |
| 55 | +// Output log to browser |
| 56 | +echo "<pre>$log</pre>"; |
| 57 | +?> |
0 commit comments