mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Avances
This commit is contained in:
@ -1,46 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Sistema;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\Sistema\BackupModel;
|
||||
use phpseclib3\Net\SFTP;
|
||||
use ZipArchive;
|
||||
|
||||
class Backups extends BaseController
|
||||
{
|
||||
protected $backupModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->backupModel = new BackupModel();
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
$entries = $this->backupModel->orderBy('created_at', 'DESC')->findAll();
|
||||
|
||||
$backups = [];
|
||||
foreach ($entries as $entry) {
|
||||
$file = $entry['filename'];
|
||||
$localPath = $entry['path_local'];
|
||||
$remotePath = $entry['path_remote'];
|
||||
|
||||
$isLocal = $localPath && file_exists($localPath);
|
||||
$isRemote = !empty($remotePath);
|
||||
|
||||
if ($isLocal) {
|
||||
$fecha = date('Y-m-d H:i', filemtime($localPath));
|
||||
$tamano = filesize($localPath);
|
||||
$tamanoFmt = number_format($tamano / 1024, 2) . ' KB';
|
||||
|
||||
// Actualizar la BD si ha cambiado
|
||||
if ($entry['size'] != $tamano) {
|
||||
$this->backupModel->update($entry['id'], [
|
||||
'size' => $tamano,
|
||||
]);
|
||||
}
|
||||
|
||||
} else {
|
||||
$fecha = $entry['created_at'] ?? '-';
|
||||
$tamano = $entry['size'] ?? null;
|
||||
$tamanoFmt = $tamano ? number_format($tamano / 1024, 2) . ' KB' : '-';
|
||||
}
|
||||
|
||||
$backups[] = [
|
||||
'id' => $entry['id'],
|
||||
'filename' => $file,
|
||||
'fecha' => $fecha,
|
||||
'tamano' => $tamanoFmt,
|
||||
'local' => $isLocal,
|
||||
'remoto' => $isRemote,
|
||||
];
|
||||
}
|
||||
|
||||
return view('themes/vuexy/form/backups/backupList', ['backups' => $backups]);
|
||||
}
|
||||
|
||||
|
||||
public function create()
|
||||
{
|
||||
helper('filesystem');
|
||||
|
||||
$filename = 'backup_' . date('Ymd_His') . '.sql';
|
||||
$path = WRITEPATH . 'backups/' . $filename;
|
||||
$timestamp = date('Ymd_His');
|
||||
$sqlFilename = "backup_{$timestamp}.sql";
|
||||
$zipFilename = "backup_{$timestamp}.zip";
|
||||
$sqlPath = WRITEPATH . 'backups/' . $sqlFilename;
|
||||
$zipPath = WRITEPATH . 'backups/' . $zipFilename;
|
||||
|
||||
$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}";
|
||||
|
||||
$command = "mysqldump -h {$host} -u{$username} -p'{$password}' {$database} > {$sqlPath}";
|
||||
system($command, $retval);
|
||||
|
||||
if ($retval !== 0) {
|
||||
throw new \RuntimeException("Error al crear el backup.");
|
||||
}
|
||||
|
||||
// Enviar a SFTP
|
||||
$this->sendToSFTP($path, $filename);
|
||||
// Crear el zip
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
|
||||
$zip->addFile($sqlPath, $sqlFilename);
|
||||
$zip->close();
|
||||
unlink($sqlPath); // eliminar el .sql original
|
||||
} else {
|
||||
throw new \RuntimeException("Error al comprimir el backup.");
|
||||
}
|
||||
|
||||
return redirect()->to(route_to('backupsList'))->with('message', 'Backup creado y enviado.');
|
||||
// Insertar en BD
|
||||
$backupId = $this->backupModel->insert([
|
||||
'filename' => $zipFilename,
|
||||
'type' => 'manual',
|
||||
'path_local' => $zipPath,
|
||||
'path_remote' => null,
|
||||
'size' => filesize($zipPath),
|
||||
'status' => 'pendiente',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
], true);
|
||||
|
||||
// Enviar a SFTP
|
||||
$this->sendToSFTP($zipPath, $zipFilename);
|
||||
|
||||
// Actualizar BD
|
||||
$remotePath = "/users/erp2019/backups_erp/" . $zipFilename;
|
||||
$this->backupModel->update($backupId, [
|
||||
'path_remote' => $remotePath,
|
||||
'status' => 'subido'
|
||||
]);
|
||||
|
||||
return redirect()->to(route_to('backupsList'))->with('message', 'Backup creado, comprimido y enviado.');
|
||||
}
|
||||
|
||||
public function restore($file)
|
||||
@ -50,18 +126,49 @@ class Backups extends BaseController
|
||||
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;');
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($path) === TRUE) {
|
||||
$extractPath = WRITEPATH . 'backups/tmp_restore/';
|
||||
if (!is_dir($extractPath)) {
|
||||
mkdir($extractPath, 0775, true);
|
||||
}
|
||||
$zip->extractTo($extractPath);
|
||||
$zip->close();
|
||||
|
||||
return redirect()->to('/backups')->with('message', 'Backup restaurado.');
|
||||
$sqlFiles = glob($extractPath . '*.sql');
|
||||
if (count($sqlFiles) === 0) {
|
||||
throw new \RuntimeException("No se encontró ningún archivo .sql en el ZIP");
|
||||
}
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
$sql = file_get_contents($sqlFiles[0]);
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=0;');
|
||||
$db->query($sql);
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=1;');
|
||||
|
||||
array_map('unlink', glob($extractPath . '*'));
|
||||
rmdir($extractPath);
|
||||
|
||||
return redirect()->to('/backups')->with('message', 'Backup restaurado correctamente.');
|
||||
} else {
|
||||
throw new \RuntimeException("No se pudo abrir el archivo ZIP");
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteLocal($id)
|
||||
{
|
||||
$backup = $this->backupModel->find($id);
|
||||
if ($backup && $backup['path_local'] && file_exists($backup['path_local'])) {
|
||||
unlink($backup['path_local']);
|
||||
$this->backupModel->update($id, ['path_local' => null]);
|
||||
return redirect()->to(route_to('backupsList'))->with('message', 'Backup local eliminado.');
|
||||
}
|
||||
return redirect()->to(route_to('backupsList'))->with('error', 'Archivo no encontrado.');
|
||||
}
|
||||
|
||||
|
||||
private function sendToSFTP($localPath, $remoteFilename)
|
||||
{
|
||||
|
||||
$sftpHost = 'sftp.hidrive.ionos.com';
|
||||
$sftpUser = 'erp2019';
|
||||
$sftpPass = 'Z2CjX7kd2h';
|
||||
@ -78,6 +185,5 @@ class Backups extends BaseController
|
||||
if (!$sftp->put($remotePath, $fileContents)) {
|
||||
throw new \RuntimeException("No se pudo subir el backup al servidor SFTP.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user