Merge branch 'feat/backups' of https://git.imnavajas.es/jjimenez/safekat into feat/backups

This commit is contained in:
2025-06-10 20:37:47 +02:00
3 changed files with 63 additions and 16 deletions

View File

@ -119,7 +119,7 @@ class Backups extends BaseController
return redirect()->to(route_to('backupsList'))->with('message', 'Backup creado, comprimido y enviado.');
}
public function restore($file)
public function restoreLocal($file)
{
$path = WRITEPATH . 'backups/' . $file;
if (!file_exists($path)) {
@ -166,6 +166,53 @@ class Backups extends BaseController
return redirect()->to(route_to('backupsList'))->with('error', 'Archivo no encontrado.');
}
public function restoreRemote($filename)
{
helper('filesystem');
// Buscar el backup en la base de datos
$backup = $this->backupModel->where('filename', $filename)->first();
if (!$backup || empty($backup['path_remote'])) {
return redirect()->to(route_to('backupsList'))->with('error', 'Backup remoto no encontrado en la base de datos.');
}
// Parámetros SFTP
$sftpHost = 'sftp.hidrive.ionos.com';
$sftpUser = 'erp2019';
$sftpPass = 'Z2CjX7kd2h';
$remotePath = $backup['path_remote'];
$localPath = WRITEPATH . 'backups/' . $filename;
// Conectar al SFTP
$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.');
}
// Descargar el archivo
$fileContents = $sftp->get($remotePath);
if ($fileContents === false) {
return redirect()->to(route_to('backupsList'))->with('error', 'No se pudo descargar el archivo remoto.');
}
// Guardar localmente
if (write_file($localPath, $fileContents) === false) {
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,
]);
// Restaurar usando el método local
return $this->restoreLocal($filename);
}
private function sendToSFTP($localPath, $remoteFilename)
{