From d7a85ca04fa30a5f31af5a563b399ef5f9afe36e Mon Sep 17 00:00:00 2001 From: imnavajas Date: Fri, 2 May 2025 14:10:49 +0200 Subject: [PATCH] Primeros pasos sistema backup --- ci4/app/Config/Routes.php | 6 -- ci4/app/Config/Routes/SistemaRoutes.php | 30 +++++++ ci4/app/Controllers/Sistema/Backups.php | 83 +++++++++++++++++++ ci4/app/Language/es/App.php | 58 +------------ ci4/app/Language/es/RolesPermisos.php | 1 + .../themes/vuexy/form/backups/backupList.php | 28 +++++++ .../themes/vuexy/main/menus/sistema_menu.php | 18 +++- ci4/writable/backups/index.html | 11 +++ 8 files changed, 172 insertions(+), 63 deletions(-) create mode 100644 ci4/app/Config/Routes/SistemaRoutes.php create mode 100644 ci4/app/Controllers/Sistema/Backups.php create mode 100644 ci4/app/Views/themes/vuexy/form/backups/backupList.php create mode 100644 ci4/writable/backups/index.html diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 352a871e..a0d6df47 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -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 diff --git a/ci4/app/Config/Routes/SistemaRoutes.php b/ci4/app/Config/Routes/SistemaRoutes.php new file mode 100644 index 00000000..7a2b1d2a --- /dev/null +++ b/ci4/app/Config/Routes/SistemaRoutes.php @@ -0,0 +1,30 @@ +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']); + + }); + +}); \ No newline at end of file diff --git a/ci4/app/Controllers/Sistema/Backups.php b/ci4/app/Controllers/Sistema/Backups.php new file mode 100644 index 00000000..0d6fd9b8 --- /dev/null +++ b/ci4/app/Controllers/Sistema/Backups.php @@ -0,0 +1,83 @@ + $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."); + } + + } +} diff --git a/ci4/app/Language/es/App.php b/ci4/app/Language/es/App.php index 417cb626..b64ca4ba 100755 --- a/ci4/app/Language/es/App.php +++ b/ci4/app/Language/es/App.php @@ -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", diff --git a/ci4/app/Language/es/RolesPermisos.php b/ci4/app/Language/es/RolesPermisos.php index e9d23840..9326991d 100755 --- a/ci4/app/Language/es/RolesPermisos.php +++ b/ci4/app/Language/es/RolesPermisos.php @@ -62,6 +62,7 @@ return [ 'seriesFacturasSection' => 'Series facturas', 'ajustesSection' => 'Ajustes', 'actividadSection' => 'Accesos', + 'backupSection' => 'Backups', 'facturasSection' => 'Facturas', 'albaranesPermission' => 'Albaranes', 'vencimientosPermission' => 'Vencimientos', diff --git a/ci4/app/Views/themes/vuexy/form/backups/backupList.php b/ci4/app/Views/themes/vuexy/form/backups/backupList.php new file mode 100644 index 00000000..cc306dce --- /dev/null +++ b/ci4/app/Views/themes/vuexy/form/backups/backupList.php @@ -0,0 +1,28 @@ +include("themes/_commonPartialsBs/select2bs5") ?> +include("themes/_commonPartialsBs/datatables") ?> +extend('themes/vuexy/main/defaultlayout') ?> + +section('content'); ?> + +
+
+ +

Backups disponibles

+ Crear backup + + +
+
+endSection() ?> + +section('additionalInlineJs') ?> + + +endSection() ?> \ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/main/menus/sistema_menu.php b/ci4/app/Views/themes/vuexy/main/menus/sistema_menu.php index 7d7a649c..870964cc 100755 --- a/ci4/app/Views/themes/vuexy/main/menus/sistema_menu.php +++ b/ci4/app/Views/themes/vuexy/main/menus/sistema_menu.php @@ -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')) { ?> + + user()->can('backup.menu')) { + ?> + + + + diff --git a/ci4/writable/backups/index.html b/ci4/writable/backups/index.html new file mode 100644 index 00000000..b702fbc3 --- /dev/null +++ b/ci4/writable/backups/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + +