mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Avances
This commit is contained in:
@ -6,6 +6,12 @@ use App\Models\Sistema\BackupModel;
|
|||||||
use phpseclib3\Net\SFTP;
|
use phpseclib3\Net\SFTP;
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
|
|
||||||
|
/*
|
||||||
|
erp-backup
|
||||||
|
5#xJ)6nENB!F~x^
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
class Backups extends BaseController
|
class Backups extends BaseController
|
||||||
{
|
{
|
||||||
protected $backupModel;
|
protected $backupModel;
|
||||||
@ -379,6 +385,7 @@ class Backups extends BaseController
|
|||||||
public function restoreLocal($file)
|
public function restoreLocal($file)
|
||||||
{
|
{
|
||||||
$path = WRITEPATH . 'backups/' . $file;
|
$path = WRITEPATH . 'backups/' . $file;
|
||||||
|
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
throw new \CodeIgniter\Exceptions\PageNotFoundException("Backup no encontrado.");
|
throw new \CodeIgniter\Exceptions\PageNotFoundException("Backup no encontrado.");
|
||||||
}
|
}
|
||||||
@ -399,21 +406,37 @@ class Backups extends BaseController
|
|||||||
|
|
||||||
$sqlFile = $sqlFiles[0];
|
$sqlFile = $sqlFiles[0];
|
||||||
|
|
||||||
$dbConfig = config('Database')->default;
|
// === Verificar que el archivo SQL existe y tiene contenido
|
||||||
$host = $dbConfig['hostname'];
|
if (!file_exists($sqlFile)) {
|
||||||
$username = $dbConfig['username'];
|
throw new \RuntimeException("Archivo SQL no encontrado.");
|
||||||
$password = $dbConfig['password'];
|
|
||||||
$database = $dbConfig['database'];
|
|
||||||
|
|
||||||
$cmd = "mysql -h {$host} -u{$username} -p'{$password}' {$database} < {$sqlFile}";
|
|
||||||
system($cmd, $retval);
|
|
||||||
|
|
||||||
if ($retval !== 0) {
|
|
||||||
throw new \RuntimeException("Error al restaurar la base de datos. Código: $retval");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
array_map('unlink', glob($extractPath . '*'));
|
if (filesize($sqlFile) === 0) {
|
||||||
rmdir($extractPath);
|
throw new \RuntimeException("El archivo SQL está vacío.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Configuración de base de datos
|
||||||
|
$dbConfig = config('Database')->default;
|
||||||
|
$host = escapeshellarg($dbConfig['hostname']);
|
||||||
|
$username = escapeshellarg($dbConfig['username']);
|
||||||
|
$password = escapeshellarg($dbConfig['password']);
|
||||||
|
$database = escapeshellarg($dbConfig['database']);
|
||||||
|
|
||||||
|
// === Construcción del comando con stderr redirigido
|
||||||
|
$cmd = "mysql -h $host -u $username -p$password $database -e \"source $sqlFile\" 2>&1";
|
||||||
|
|
||||||
|
// === Ejecutar y capturar la salida
|
||||||
|
exec($cmd, $output, $retval);
|
||||||
|
|
||||||
|
// === Verificar resultado
|
||||||
|
if ($retval !== 0) {
|
||||||
|
throw new \RuntimeException("Error al restaurar la base de datos:\n" . implode("\n", $output));
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Limpieza
|
||||||
|
helper('filesystem');
|
||||||
|
delete_files($extractPath, true); // elimina contenido
|
||||||
|
rmdir($extractPath); // elimina el directorio
|
||||||
|
|
||||||
return redirect()->to(route_to('backupsList'))->with('message', 'Backup restaurado correctamente (vía sistema).');
|
return redirect()->to(route_to('backupsList'))->with('message', 'Backup restaurado correctamente (vía sistema).');
|
||||||
} else {
|
} else {
|
||||||
@ -424,54 +447,76 @@ class Backups extends BaseController
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function restoreRemote($filename)
|
public function restoreRemote($filename)
|
||||||
{
|
{
|
||||||
helper('filesystem');
|
helper('filesystem');
|
||||||
|
|
||||||
// Buscar el backup en la base de datos
|
$entorno = getenv('SK_ENVIRONMENT');
|
||||||
|
|
||||||
|
if ($entorno === 'development') {
|
||||||
|
// Construir ruta remota directamente
|
||||||
|
$remotePath = '/users/erp2019/backups_erp/' . $filename;
|
||||||
|
$localPath = WRITEPATH . 'backups/' . $filename;
|
||||||
|
|
||||||
|
$sftpHost = getenv('HIDRIVE_HOST');
|
||||||
|
$sftpUser = getenv('HIDRIVE_USER');
|
||||||
|
$sftpPass = getenv('HIDRIVE_PASS');
|
||||||
|
|
||||||
|
$sftp = new SFTP($sftpHost);
|
||||||
|
if (!$sftp->login($sftpUser, $sftpPass)) {
|
||||||
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo autenticar en el servidor SFTP.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileContents = $sftp->get($remotePath);
|
||||||
|
if ($fileContents === false) {
|
||||||
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo descargar el archivo remoto.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_file($localPath, $fileContents) === false) {
|
||||||
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo guardar el archivo localmente.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restaurar directamente
|
||||||
|
return $this->restoreLocal($filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Producción: flujo normal con base de datos
|
||||||
$backup = $this->backupModel->where('filename', $filename)->first();
|
$backup = $this->backupModel->where('filename', $filename)->first();
|
||||||
|
|
||||||
if (!$backup || empty($backup['path_remote'])) {
|
if (!$backup || empty($backup['path_remote'])) {
|
||||||
return redirect()->to(route_to('backupsList'))->with('error', 'Backup remoto no encontrado en la base de datos.');
|
return redirect()->to(route_to('backupsList'))->with('error', 'Backup remoto no encontrado en la base de datos.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parámetros SFTP
|
|
||||||
$sftpHost = getenv('HIDRIVE_HOST');
|
|
||||||
$sftpUser = getenv('HIDRIVE_USER');
|
|
||||||
$sftpPass = getenv('HIDRIVE_PASS');
|
|
||||||
$remotePath = $backup['path_remote'];
|
$remotePath = $backup['path_remote'];
|
||||||
$localPath = WRITEPATH . 'backups/' . $filename;
|
$localPath = WRITEPATH . 'backups/' . $filename;
|
||||||
|
|
||||||
// Conectar al SFTP
|
$sftpHost = getenv('HIDRIVE_HOST');
|
||||||
$sftp = new SFTP($sftpHost);
|
$sftpUser = getenv('HIDRIVE_USER');
|
||||||
|
$sftpPass = getenv('HIDRIVE_PASS');
|
||||||
|
|
||||||
|
$sftp = new SFTP($sftpHost);
|
||||||
if (!$sftp->login($sftpUser, $sftpPass)) {
|
if (!$sftp->login($sftpUser, $sftpPass)) {
|
||||||
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo autenticar en el servidor SFTP.');
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo autenticar en el servidor SFTP.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Descargar el archivo
|
|
||||||
$fileContents = $sftp->get($remotePath);
|
$fileContents = $sftp->get($remotePath);
|
||||||
|
|
||||||
if ($fileContents === false) {
|
if ($fileContents === false) {
|
||||||
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo descargar el archivo remoto.');
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo descargar el archivo remoto.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guardar localmente
|
|
||||||
if (write_file($localPath, $fileContents) === false) {
|
if (write_file($localPath, $fileContents) === false) {
|
||||||
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo guardar el archivo localmente.');
|
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo guardar el archivo localmente.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualizar la base de datos para marcar el archivo como local
|
$this->backupModel->update($backup['id'], ['path_local' => $localPath]);
|
||||||
$this->backupModel->update($backup['id'], [
|
|
||||||
'path_local' => $localPath,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Restaurar usando el método local
|
|
||||||
return $this->restoreLocal($filename);
|
return $this->restoreLocal($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function sendToSFTP($localPath, $remoteFilename)
|
private function sendToSFTP($localPath, $remoteFilename)
|
||||||
{
|
{
|
||||||
$sftpHost = getenv('HIDRIVE_HOST');
|
$sftpHost = getenv('HIDRIVE_HOST');
|
||||||
|
|||||||
Reference in New Issue
Block a user