Skip to content

Commit 4b77def

Browse files
Backup Restoration file
1 parent d507e43 commit 4b77def

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

restore.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
ini_set('max_execution_time', 0);
4+
date_default_timezone_set('UTC');
5+
$log_file = 'restore.log';
6+
7+
// Rename wp-config.php to old-wp-config.php
8+
rename('wp-config.php', 'old-wp-config.php');
9+
10+
// Delete unwanted files and folders
11+
exec('rm -rf index.php xmlrpc.php wp-*');
12+
13+
// Extract zip file
14+
exec('unzip backup-wp-files-*.zip');
15+
16+
// Read database details from old-wp-config.php
17+
$old_config = file_get_contents('old-wp-config.php');
18+
preg_match("/define\(\s*'DB_NAME',\s*'(.+)'\s*\);/", $old_config, $db_name_match);
19+
preg_match("/define\(\s*'DB_USER',\s*'(.+)'\s*\);/", $old_config, $db_user_match);
20+
preg_match("/define\(\s*'DB_PASSWORD',\s*'(.+)'\s*\);/", $old_config, $db_password_match);
21+
preg_match("/define\(\s*'DB_HOST',\s*'(.+)'\s*\);/", $old_config, $db_host_match);
22+
23+
// Update wp-config.php with database details
24+
$config_file = file_get_contents('old-wp-config.php');
25+
$config_file = preg_replace("/define\(\s*'DB_NAME',\s*'(.+)'\s*\);/", "define('DB_NAME', '{$db_name_match[1]}');", $config_file);
26+
$config_file = preg_replace("/define\(\s*'DB_USER',\s*'(.+)'\s*\);/", "define('DB_USER', '{$db_user_match[1]}');", $config_file);
27+
$config_file = preg_replace("/define\(\s*'DB_PASSWORD',\s*'(.+)'\s*\);/", "define('DB_PASSWORD', '{$db_password_match[1]}');", $config_file);
28+
$config_file = preg_replace("/define\(\s*'DB_HOST',\s*'(.+)'\s*\);/", "define('DB_HOST', '{$db_host_match[1]}');", $config_file);
29+
file_put_contents('wp-config.php', $config_file);
30+
31+
// Drop all tables in database
32+
$drop_cmd = "mysql -u {$db_user_match[1]} -p{$db_password_match[1]} -h {$db_host_match[1]} -e 'show tables' --silent {$db_name_match[1]} | xargs -I\"@@\" mysql -u {$db_user_match[1]} -p{$db_password_match[1]} -h {$db_host_match[1]} -e 'DROP TABLE IF EXISTS @@' {$db_name_match[1]} 2>&1";
33+
$drop_output = shell_exec($drop_cmd);
34+
file_put_contents($log_file, "Drop Command Output: \n{$drop_output}\n\n", FILE_APPEND);
35+
36+
// Import database from SQL file
37+
$sql_file = shell_exec('ls backup-wp-db-*.sql');
38+
$import_cmd = "mysql -u {$db_user_match[1]} -p{$db_password_match[1]} -h {$db_host_match[1]} {$db_name_match[1]} < {$sql_file} 2>&1";
39+
$import_output = shell_exec($import_cmd);
40+
file_put_contents($log_file, "Import Command Output: \n{$import_output}\n\n", FILE_APPEND);
41+
42+
43+
// Log execution
44+
$timestamp = date('Y-m-d H:i:s');
45+
$log_message = "Executed restore script on {$timestamp}\n";
46+
file_put_contents($log_file, $log_message, FILE_APPEND);
47+
48+
echo "Restoration completed.\n";
49+
50+
?>

0 commit comments

Comments
 (0)