To modify your given PHP script to incorporate the backup and restore of user information and shadow files as you requested, we can integrate commands for backing up the /etc/passwd and /etc/shadow files directly into the backupUsersCommand and restoreUsersCommand. Since directly manipulating these files requires root privileges and careful handling, ensure that your script is used responsibly and securely.
Please note: the approach described below assumes you have a method to execute privileged commands (sudo) without password prompts (for example, by configuring /etc/sudoers for specific commands), which should be used with caution.
Modified PHP Script
<?php
require 'vendor/autoload.php';
use phpseclib3\Net\SSH2;
$backupCommand = "sudo tar cvzf /var/backup/server_backup.tar.gz /etc/passwd /etc/shadow /etc/group /etc/gshadow";
$transferBackupFile = "/var/backup/server_backup.tar.gz";
$restoreCommand = "sudo tar xzvf /var/backup/server_backup.tar.gz -C /";
$servers = require 'servers.php';
if (isset($_POST['submit'])) {
if (isset($servers[$_POST['server1']]) && isset($servers[$_POST['server2']])) {
$server1 = $servers[$_POST['server1']];
$server2 = $servers[$_POST['server2']];
$ssh = new SSH2($server1['host'], $server1['port']);
if ($ssh->login($server1['username'], $server1['password'])) {
// Backup users on server1
$backupOutput = $ssh->exec($backupCommand);
// Transfer the backup file from server1 to server2
$sftp = new \phpseclib3\Net\SFTP($server2['host'], $server2['port']);
if ($sftp->login($server2['username'], $server2['password'])) {
$sftp->put('/var/backup/server_backup.tar.gz', $transferBackupFile, \phpseclib3\Net\SFTP::SOURCE_LOCAL_FILE);
// Restore users and other files on server2
$ssh2 = new SSH2($server2['host'], $server2['port']);
if ($ssh2->login($server2['username'], $server2['password'])) {
$restoreOutput = $ssh2->exec($restoreCommand);
echo "<div style=\"background:green; color:white; text-align:center; font-size:25px;width:auto; margin:30px auto; border-radius:4px; border:2px solid black; padding:20px;\">Backup and Restore completed successfully between {$server1['host']} and {$server2['host']}\n</div>";
} else {
echo "<div style=\"background:red; color:white; text-align:center; font-size:25px;width:400px; margin:30px auto; border-radius:4px; border:2px solid black; padding:20px;\">Cannot login to server {$server2['host']}\n</div>";
}
} else {
echo "<div style=\"background:red; color:white; text-align:center; font-size:25px;width:400px; margin:30px auto; border-radius:4px; border:2px solid black; padding:20px;\">Cannot login to server (SFTP) {$server2['host']}\n</div>";
}
$ssh->disconnect();
} else {
echo "<div style=\"background:red; color:white; text-align:center; font-size:25px;width:400px; margin:30px auto; border-radius:4px; border:2px solid black; padding:20px;\">Cannot connect to server {$server1['host']}\n</div>";
}
} else {
echo '<div style="background:red; color:white; text-align:center; font-size:25px;width:400px; margin:30px auto; border-radius:4px; border:2px solid black; padding:20px;">Select two servers to transfer users!</div>';
}
}
?>
Key Changes and Considerations
- Backup Command: Added a
sudo tar command to backup essential system files: /etc/passwd, /etc/shadow, /etc/group, and /etc/gshadow. Handle this with caution to ensure system security.
- Transfer and Restore Process: Simplified to focus on transferring the backup file and restoring it on the second server.
- Privilege Execution: The script assumes that non-interactive
sudo commands can be run without a password prompt, which might require specific sudoers configuration. This carries security risks and should be managed carefully.
Security and Minimal Privilege Principle: This script, as designed, could pose security risks, especially in environments sensitive to privilege escalation and unauthorized access. Always adhere to the principle of least privilege, tailor sudoers configurations to the minimal required scope, audit logs, and consider employing additional security measures like command whitelisting.
Ensure you test this script in a safe, non-production environment to fine-tune and verify its behavior before any operational use.