diff --git a/ci4/app/Config/Routes/SistemaRoutes.php b/ci4/app/Config/Routes/SistemaRoutes.php index a0a8a90c..d0d0559d 100644 --- a/ci4/app/Config/Routes/SistemaRoutes.php +++ b/ci4/app/Config/Routes/SistemaRoutes.php @@ -16,7 +16,7 @@ $routes->group('sistema', ['namespace' => 'App\Controllers\Sistema'], function ( }); - /* Actividad */ + /* Backups */ $routes->group('backups', ['namespace' => 'App\Controllers\Sistema'], function ($routes) { /**====================== * Tool @@ -24,7 +24,9 @@ $routes->group('sistema', ['namespace' => 'App\Controllers\Sistema'], function ( $routes->get('', 'Backups::index', ['as' => 'backupsList']); $routes->get('create', 'Backups::create', ['as' => 'backupsCreate']); $routes->get('delete-local/(:num)', 'Backups::deleteLocal/$1', ['as' => 'backupsDeleteLocal']); - $routes->get('restore/(:segment)', 'Backups::restore/$1', ['as' => 'backupsRestore']); + $routes->get('restore/(:segment)/local', 'Backups::restoreLocal/$1', ['as' => 'backupsRestoreLocal']); + $routes->get('restore/(:segment)/remote', 'Backups::restoreRemote/$1', ['as' => 'backupsRestoreRemote']); + }); diff --git a/ci4/app/Controllers/Sistema/Backups.php b/ci4/app/Controllers/Sistema/Backups.php index 28e93ff3..40ef0b54 100644 --- a/ci4/app/Controllers/Sistema/Backups.php +++ b/ci4/app/Controllers/Sistema/Backups.php @@ -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) { diff --git a/ci4/app/Views/themes/vuexy/form/backups/backupList.php b/ci4/app/Views/themes/vuexy/form/backups/backupList.php index 247752f3..6984f811 100644 --- a/ci4/app/Views/themes/vuexy/form/backups/backupList.php +++ b/ci4/app/Views/themes/vuexy/form/backups/backupList.php @@ -35,12 +35,12 @@ - Restaurar - Restaurar Local + Eliminar local - Restaurar Remoto No disponible @@ -59,14 +59,12 @@ endSection() ?> section('additionalInlineJs') ?> - +}); endSection() ?> \ No newline at end of file