mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Primeros pasos sistema backup
This commit is contained in:
@ -20,12 +20,6 @@ $routes->get('viewmode/(:alpha)', 'Viewmode::index/$1');
|
||||
$routes->get('test', 'Test::index');
|
||||
|
||||
|
||||
$routes->group('activity', ['namespace' => 'App\Controllers\Sistema'], function ($routes) {
|
||||
$routes->get('', 'Actividad::index', ['as' => 'activityList']);
|
||||
$routes->post('datatable', 'Actividad::datatable', ['as' => 'activityDT']);
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------------
|
||||
* Route Definitions
|
||||
|
||||
30
ci4/app/Config/Routes/SistemaRoutes.php
Normal file
30
ci4/app/Config/Routes/SistemaRoutes.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use CodeIgniter\Router\RouteCollection;
|
||||
|
||||
/** @var RouteCollection $routes */
|
||||
|
||||
/* Rutas para tarifas */
|
||||
$routes->group('sistema', ['namespace' => 'App\Controllers\Sistema'], function ($routes) {
|
||||
/* Actividad */
|
||||
$routes->group('actividad', ['namespace' => 'App\Controllers\Sistema'], function ($routes) {
|
||||
/**======================
|
||||
* CRUD
|
||||
*========================**/
|
||||
$routes->get('', 'Actividad::index', ['as' => 'activityList']);
|
||||
$routes->post('datatable', 'Actividad::datatable', ['as' => 'activityDT']);
|
||||
|
||||
});
|
||||
|
||||
/* Actividad */
|
||||
$routes->group('backups', ['namespace' => 'App\Controllers\Sistema'], function ($routes) {
|
||||
/**======================
|
||||
* Tool
|
||||
*========================**/
|
||||
$routes->get('', 'Backups::index', ['as' => 'backupsList']);
|
||||
$routes->get('/create', 'Backups::create', ['as' => 'backupsCreate']);
|
||||
$routes->get('restore/(:segment)', 'Backups::restore/$1', ['as' => 'backupsRestore']);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
83
ci4/app/Controllers/Sistema/Backups.php
Normal file
83
ci4/app/Controllers/Sistema/Backups.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Sistema;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use phpseclib3\Net\SFTP;
|
||||
|
||||
class Backups extends BaseController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
helper('filesystem');
|
||||
// Muestra la vista con la lista de backups
|
||||
$files = directory_map(WRITEPATH . 'backups/', 1);
|
||||
return view('themes/vuexy/form/backups/backupList', ['files' => $files]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
helper('filesystem');
|
||||
|
||||
$filename = 'backup_' . date('Ymd_His') . '.sql';
|
||||
$path = WRITEPATH . 'backups/' . $filename;
|
||||
|
||||
$dbConfig = config('Database')->default;
|
||||
|
||||
$host = $dbConfig['hostname'];
|
||||
$username = $dbConfig['username'];
|
||||
$password = $dbConfig['password'];
|
||||
$database = $dbConfig['database'];
|
||||
|
||||
$command = "mysqldump -h {$host} -u{$username} -p'{$password}' {$database} > {$path}";
|
||||
|
||||
system($command, $retval);
|
||||
|
||||
if ($retval !== 0) {
|
||||
throw new \RuntimeException("Error al crear el backup.");
|
||||
}
|
||||
|
||||
// Enviar a SFTP
|
||||
$this->sendToSFTP($path, $filename);
|
||||
|
||||
return redirect()->to(route_to('backupsList'))->with('message', 'Backup creado y enviado.');
|
||||
}
|
||||
|
||||
public function restore($file)
|
||||
{
|
||||
$path = WRITEPATH . 'backups/' . $file;
|
||||
if (!file_exists($path)) {
|
||||
throw new \CodeIgniter\Exceptions\PageNotFoundException("Backup no encontrado.");
|
||||
}
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
$sql = file_get_contents($path);
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=0;');
|
||||
$db->query($sql);
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=1;');
|
||||
|
||||
return redirect()->to('/backups')->with('message', 'Backup restaurado.');
|
||||
}
|
||||
|
||||
private function sendToSFTP($localPath, $remoteFilename)
|
||||
{
|
||||
|
||||
$sftpHost = 'sftp.hidrive.ionos.com';
|
||||
$sftpUser = 'erp2019';
|
||||
$sftpPass = 'Z2CjX7kd2h';
|
||||
$remotePath = '/users/erp2019/backups_erp/' . $remoteFilename;
|
||||
|
||||
$sftp = new SFTP($sftpHost);
|
||||
|
||||
if (!$sftp->login($sftpUser, $sftpPass)) {
|
||||
throw new \RuntimeException('Error de autenticación SFTP');
|
||||
}
|
||||
|
||||
$fileContents = file_get_contents($localPath);
|
||||
|
||||
if (!$sftp->put($remotePath, $fileContents)) {
|
||||
throw new \RuntimeException("No se pudo subir el backup al servidor SFTP.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -315,59 +315,7 @@ return [
|
||||
"group_rules_title_r" => "¡El campo del nombre del grupo es obligatorio!",
|
||||
"group_rules_dashboard_r" => "¡El campo del panel es obligatorio!",
|
||||
|
||||
// GROUP - Rules Name
|
||||
/* JJO
|
||||
"group_rules_label_group" => "Permiso de grupo",
|
||||
"group_rules_label_user" => "Usuario",
|
||||
"group_rules_label_settings" => "Configuración",
|
||||
"group_rules_label_index" => "Lista",
|
||||
"group_rules_label_add" => "Agregar",
|
||||
"group_rules_label_edit" => "Editar",
|
||||
"group_rules_label_delete" => "Eliminar",
|
||||
"group_rules_label_store" => "Guardar",
|
||||
"group_rules_label_oauth" => "Autenticaciones",
|
||||
"group_rules_label_template" => "Plantillas",
|
||||
"group_rules_label_all" => "Ver todo",
|
||||
"group_rules_label_my" => "Mis notificaciones",
|
||||
"group_rules_label_view" => "Ver notificación",
|
||||
"group_rules_label_oauth_store" => "Guardar oAuth",
|
||||
"group_rules_label_template_store" => "Guardar plantillas",
|
||||
*/
|
||||
|
||||
// AUTH - index
|
||||
"oauth_title" => "Autenticación oAuth",
|
||||
"oauth_subtitle" => "Configuración de autenticación de redes sociales",
|
||||
"oauth_label_id" => "ID de la Cuenta",
|
||||
"oauth_label_id_ph" => "Escriba su id de la cuenta",
|
||||
"oauth_label_key" => "Key de la Cuenta",
|
||||
"oauth_label_key_ph" => "Escriba su key de la cuenta",
|
||||
"oauth_label_secret" => "Llave Secreta",
|
||||
"oauth_label_secret_ph" => "Escriba su llave secreta",
|
||||
"oauth_label_view" => "Mostrar texto",
|
||||
"oauth_label_active" => "Activar red social",
|
||||
"oauth_alert_add" => "¡Guardado exitosamente!",
|
||||
"oauth_alert_error" => "¡Error al guardar!",
|
||||
|
||||
// TEMPLATE - index
|
||||
"template_title" => "Plantillas",
|
||||
"template_subtitle" => "Configuración de Plantilla",
|
||||
"template_subtitle_email" => "Plantillas de Correo Electrónico",
|
||||
"template_label_title" => "Título",
|
||||
"template_label_title_ph" => "Escriba su título",
|
||||
"template_label_message" => "Mensaje",
|
||||
"template_label_forgot_pass" => "Olvido la contraseña",
|
||||
"template_label_welcome" => "Bienvenida",
|
||||
"template_label_tfa" => "Autenticación de dos factores (2FA)",
|
||||
"template_label_tag" => "Ver palabras clave",
|
||||
"template_alert_add" => "¡Guardado exitosamente!",
|
||||
"template_alert_error" => "¡Error al guardar!",
|
||||
"template_modal_title" => "Palabras Clave",
|
||||
"template_modal_subtitle" => "A continuación, se muestran algunas palabras clave que se pueden incorporar al texto:",
|
||||
"template_modal_btn_1" => "Cerrar",
|
||||
"template_modal_copy" => "Copiado!",
|
||||
"template_modal_copy_msg" => "¡Copiado con éxito!",
|
||||
"template_label_confirmation_email" => "Confirmación por correo electrónico",
|
||||
"template_label_notification" => "Notificación de cuentas nuevas",
|
||||
|
||||
|
||||
// SETTINGS - index
|
||||
"settings_title" => "Configuración",
|
||||
@ -633,7 +581,6 @@ return [
|
||||
"permisos_tarifaenvio" => "Envío",
|
||||
"permisos_tarifaimpresion" => "Impresión",
|
||||
|
||||
"permisos_configuracion" => "Configuración",
|
||||
"permisos_tareasservicio" => "Tareas servicio",
|
||||
"permisos_formaspago" => "Formas de pago",
|
||||
"permisos_papelgenerico" => "Papel genérico",
|
||||
@ -826,11 +773,10 @@ return [
|
||||
"menu_logout" => "Salir",
|
||||
"menu_profile" => "Mi Perfil",
|
||||
"menu_activity" => "Actividad",
|
||||
"menu_backups" => "Backups",
|
||||
"menu_notification" => "Notificaciones",
|
||||
"menu_list" => "Lista",
|
||||
"menu_add" => "Agregar",
|
||||
"menu_oauth" => "Autenticaciones",
|
||||
"menu_template" => "Plantillas",
|
||||
|
||||
"menu_soporte" => "Soporte",
|
||||
"menu_soporte_new_ticket" => "Crear ticket",
|
||||
|
||||
@ -62,6 +62,7 @@ return [
|
||||
'seriesFacturasSection' => 'Series facturas',
|
||||
'ajustesSection' => 'Ajustes',
|
||||
'actividadSection' => 'Accesos',
|
||||
'backupSection' => 'Backups',
|
||||
'facturasSection' => 'Facturas',
|
||||
'albaranesPermission' => 'Albaranes',
|
||||
'vencimientosPermission' => 'Vencimientos',
|
||||
|
||||
28
ci4/app/Views/themes/vuexy/form/backups/backupList.php
Normal file
28
ci4/app/Views/themes/vuexy/form/backups/backupList.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?= $this->include("themes/_commonPartialsBs/select2bs5") ?>
|
||||
<?= $this->include("themes/_commonPartialsBs/datatables") ?>
|
||||
<?= $this->extend('themes/vuexy/main/defaultlayout') ?>
|
||||
|
||||
<?= $this->section('content'); ?>
|
||||
<!--Content Body-->
|
||||
<div class="row mt-4">
|
||||
<div class="col-md-12">
|
||||
|
||||
<h2>Backups disponibles</h2>
|
||||
<a href="<?= route_to('backupsCreate') ?>">Crear backup</a>
|
||||
<ul>
|
||||
<?php foreach ($files as $file): ?>
|
||||
<li>
|
||||
<?= $file ?>
|
||||
<a href="<?= base_url('backups/restore/' . $file) ?>">[Restaurar]</a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?= session('message') ?>
|
||||
</div>
|
||||
</div>
|
||||
<?= $this->endSection() ?>
|
||||
|
||||
<?= $this->section('additionalInlineJs') ?>
|
||||
|
||||
|
||||
<?= $this->endSection() ?>
|
||||
@ -3,7 +3,7 @@
|
||||
* SEPARADOR Y MENUS DE SISTEMA
|
||||
*/
|
||||
|
||||
if (auth()->user()->can('actividad.menu')) {
|
||||
if (auth()->user()->can('actividad.menu') || auth()->user()->can('backup.menu')) {
|
||||
?>
|
||||
<li class="menu-header small text-uppercase">
|
||||
<span class="menu-header-text">Sistema</span>
|
||||
@ -23,4 +23,20 @@ if (auth()->user()->can('actividad.menu')) {
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
/**
|
||||
* MENU BACKUP
|
||||
*/
|
||||
if (auth()->user()->can('backup.menu')) {
|
||||
?>
|
||||
<!-- Backups -->
|
||||
<li class="menu-item">
|
||||
<a href="<?= route_to("backupsList") ?>" class="menu-link">
|
||||
<i class="menu-icon tf-icons ti ti-database-import"></i>
|
||||
<div data-i18n="<?= lang("App.menu_backups") ?>"><?= lang("App.menu_backups") ?></div>
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
11
ci4/writable/backups/index.html
Normal file
11
ci4/writable/backups/index.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>403 Forbidden</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Directory access is forbidden.</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user