mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Merge branch 'feat/backups' of https://git.imnavajas.es/jjimenez/safekat into feat/backups
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) {
|
$routes->group('backups', ['namespace' => 'App\Controllers\Sistema'], function ($routes) {
|
||||||
/**======================
|
/**======================
|
||||||
* Tool
|
* Tool
|
||||||
@ -24,7 +24,9 @@ $routes->group('sistema', ['namespace' => 'App\Controllers\Sistema'], function (
|
|||||||
$routes->get('', 'Backups::index', ['as' => 'backupsList']);
|
$routes->get('', 'Backups::index', ['as' => 'backupsList']);
|
||||||
$routes->get('create', 'Backups::create', ['as' => 'backupsCreate']);
|
$routes->get('create', 'Backups::create', ['as' => 'backupsCreate']);
|
||||||
$routes->get('delete-local/(:num)', 'Backups::deleteLocal/$1', ['as' => 'backupsDeleteLocal']);
|
$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.');
|
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;
|
$path = WRITEPATH . 'backups/' . $file;
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
@ -166,6 +166,53 @@ class Backups extends BaseController
|
|||||||
return redirect()->to(route_to('backupsList'))->with('error', 'Archivo no encontrado.');
|
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)
|
private function sendToSFTP($localPath, $remoteFilename)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -35,12 +35,12 @@
|
|||||||
</span></td>
|
</span></td>
|
||||||
<td class="text-nowrap">
|
<td class="text-nowrap">
|
||||||
<?php if ($b['local']): ?>
|
<?php if ($b['local']): ?>
|
||||||
<a href="<?= base_url('backups/restore/' . $b['filename']) ?>"
|
<a href="<?= route_to('backupsRestoreLocal', $b['filename']) ?>"
|
||||||
class="btn btn-sm btn-warning">Restaurar</a>
|
class="btn btn-sm btn-warning">Restaurar Local</a>
|
||||||
<a href="<?= base_url('backups/delete-local/' . $b['id']) ?>"
|
<a href="<?= route_to('backupsDeleteLocal', $b['id']) ?>"
|
||||||
class="btn btn-sm btn-danger">Eliminar local</a>
|
class="btn btn-sm btn-danger">Eliminar local</a>
|
||||||
<?php elseif ($b['remoto']): ?>
|
<?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>
|
class="btn btn-sm btn-warning">Restaurar Remoto</a>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<span class="text-muted">No disponible</span>
|
<span class="text-muted">No disponible</span>
|
||||||
@ -59,14 +59,12 @@
|
|||||||
<?= $this->endSection() ?>
|
<?= $this->endSection() ?>
|
||||||
|
|
||||||
<?= $this->section('additionalInlineJs') ?>
|
<?= $this->section('additionalInlineJs') ?>
|
||||||
<script>
|
$(document).ready(function () {
|
||||||
$(document).ready(function () {
|
$('#tablaBackups').DataTable({
|
||||||
$('#tablaBackups').DataTable({
|
order: [[1, 'desc']],
|
||||||
order: [[1, 'desc']],
|
language: {
|
||||||
language: {
|
url: '/assets/vendor/datatables/i18n/es-ES.json' // ajusta si usas idioma español
|
||||||
url: '/assets/vendor/datatables/i18n/es-ES.json' // ajusta si usas idioma español
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
</script>
|
});
|
||||||
<?= $this->endSection() ?>
|
<?= $this->endSection() ?>
|
||||||
Reference in New Issue
Block a user