mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Avances
This commit is contained in:
@ -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']);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -35,12 +35,12 @@
|
||||
</span></td>
|
||||
<td class="text-nowrap">
|
||||
<?php if ($b['local']): ?>
|
||||
<a href="<?= base_url('backups/restore/' . $b['filename']) ?>"
|
||||
class="btn btn-sm btn-warning">Restaurar</a>
|
||||
<a href="<?= base_url('backups/delete-local/' . $b['id']) ?>"
|
||||
<a href="<?= route_to('backupsRestoreLocal', $b['filename']) ?>"
|
||||
class="btn btn-sm btn-warning">Restaurar Local</a>
|
||||
<a href="<?= route_to('backupsDeleteLocal', $b['id']) ?>"
|
||||
class="btn btn-sm btn-danger">Eliminar local</a>
|
||||
<?php elseif ($b['remoto']): ?>
|
||||
<a href="<?= base_url('backups/restore/' . $b['filename'] . '/remote') ?>"
|
||||
<a href="<?= route_to('backupsRestoreRemote', $b['filename']) ?>"
|
||||
class="btn btn-sm btn-warning">Restaurar Remoto</a>
|
||||
<?php else: ?>
|
||||
<span class="text-muted">No disponible</span>
|
||||
@ -59,14 +59,12 @@
|
||||
<?= $this->endSection() ?>
|
||||
|
||||
<?= $this->section('additionalInlineJs') ?>
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
$('#tablaBackups').DataTable({
|
||||
order: [[1, 'desc']],
|
||||
language: {
|
||||
url: '/assets/vendor/datatables/i18n/es-ES.json' // ajusta si usas idioma español
|
||||
}
|
||||
});
|
||||
$(document).ready(function () {
|
||||
$('#tablaBackups').DataTable({
|
||||
order: [[1, 'desc']],
|
||||
language: {
|
||||
url: '/assets/vendor/datatables/i18n/es-ES.json' // ajusta si usas idioma español
|
||||
}
|
||||
});
|
||||
</script>
|
||||
});
|
||||
<?= $this->endSection() ?>
|
||||
Reference in New Issue
Block a user