Problema: 6 drones repetían el mismo patrón de ejecución remota: - Base64 encoding de scripts PowerShell - SSH vía srv-dasu con sshpass - Lectura de JSON output desde dasu-sql4 - Verificación de archivos en fenix vía xp_cmdshell Solución: adn/tools/cli/drones/lib/dasu_executor.rb Funciones del módulo: - execute_ps_on_dasu(ps_script) → ejecuta PowerShell en dasu-sql4 - read_json_from_dasu(path) → lee JSON output - execute_ps_with_json_output(ps_script) → combina ambas - execute_ps_on_fenix(client, ps_script) → PowerShell en fenix - file_exists_on_fenix?(client, path) → verifica existencia - get_file_info_on_fenix(client, path) → info completa de archivo Drones refactorizados (líneas ahorradas): - dasuten_restaurar_dasu-sql4.rb: 200 → 95 líneas (-52%) - dasuten_restaurar_diferencial_dasu-sql4.rb: 207 → 100 líneas (-52%) - dasuten_download_drive-dasu-sql4.rb: 269 → 130 líneas (-52%) - dasuten_verificar-integridad_dasu-sql4.rb: 182 → 85 líneas (-53%) - dasuten_exportar_srvv-fenix.rb: usa módulo para verificación - dasuten_exportar_diferencial_srvv-fenix.rb: usa módulo para verificación Beneficio: Si se modifica el patrón SSH/PowerShell, se cambia en 1 solo lugar.
158 lines
5.4 KiB
Ruby
Executable File
158 lines
5.4 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
# ==========================================================
|
|
# adn/tools/cli/drones/dasuten_download_drive-dasu-sql4.rb
|
|
# ==========================================================
|
|
# Dron: Download desde Google Drive a dasu-sql4
|
|
# Flujo: Google Drive → F:\BACKUP\sysdasuten_compressed_ADN.bak
|
|
# Usa: Invoke-WebRequest (sin rclone)
|
|
# ==========================================================
|
|
|
|
require 'time'
|
|
require 'json'
|
|
require 'optparse'
|
|
require_relative 'lib/dasu_executor'
|
|
|
|
opciones = {
|
|
test_file: nil,
|
|
file_id: nil
|
|
}
|
|
|
|
OptionParser.new do |opts|
|
|
opts.banner = "Uso: dron lanzar --nota 'Download' --nodo dasu-sql4 -- ruby dasuten_download_drive.rb [opciones]"
|
|
opts.on("--test-file ARCHIVO", "Archivo específico para test (ej: test_Backup.bak)") { |f| opciones[:test_file] = f }
|
|
opts.on("--file-id ID", "Google Drive file ID") { |id| opciones[:file_id] = id }
|
|
end.parse!(ARGV)
|
|
|
|
BACKUP_DIR_WINDOWS = "F:\\BACKUP"
|
|
BACKUP_NAME_DEFAULT = "sysdasuten_compressed_ADN.bak"
|
|
JSON_PATH = "C:\temp\dron_download_output.json"
|
|
|
|
# Determinar archivo a descargar
|
|
if opciones[:test_file]
|
|
puts "[*] Dron: Download desde Google Drive"
|
|
puts " Nodo: dasu-sql4"
|
|
puts " 🧪 MODO TEST: Descargando archivo específico"
|
|
file_id = opciones[:file_id] || "1P0cioDV9kZNXhIJhhdRMhWWjIRsVGcxQ"
|
|
download_url = "https://drive.google.com/uc?export=download&id=#{file_id}"
|
|
backup_name = opciones[:test_file]
|
|
puts " File ID: #{file_id}"
|
|
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
|
|
elsif opciones[:file_id]
|
|
puts "[*] Dron: Download desde Google Drive"
|
|
puts " Nodo: dasu-sql4"
|
|
puts " Download por file ID"
|
|
download_url = "https://drive.google.com/uc?export=download&id=#{opciones[:file_id]}"
|
|
backup_name = File.basename(opciones[:file_id])
|
|
puts " File ID: #{opciones[:file_id]}"
|
|
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
|
|
else
|
|
UPLOAD_OUTPUT = '/tmp/dron_upload_output.json'
|
|
if File.exist?(UPLOAD_OUTPUT)
|
|
upload_data = JSON.parse(File.read(UPLOAD_OUTPUT))
|
|
drive_ruta = upload_data['drive_ruta']
|
|
file_id = File.basename(drive_ruta)
|
|
backup_name = File.basename(drive_ruta)
|
|
download_url = "https://drive.google.com/uc?export=download&id=#{file_id}"
|
|
puts "[*] Dron: Download desde Google Drive"
|
|
puts " Nodo: dasu-sql4"
|
|
puts " Drive ruta: #{drive_ruta}"
|
|
puts " File ID: #{file_id}"
|
|
puts " Destino: #{BACKUP_DIR_WINDOWS}\\#{backup_name}"
|
|
else
|
|
puts "[!] Error: Se requiere --test-file o --file-id"
|
|
exit 1
|
|
end
|
|
end
|
|
|
|
# PowerShell script para download
|
|
ps_script = <<~PSCMD.strip
|
|
$ErrorActionPreference = "Stop"
|
|
$BACKUP_DIR = "F:\\BACKUP"
|
|
$DOWNLOAD_URL = "#{download_url}"
|
|
$BACKUP_NAME = "#{backup_name}"
|
|
$LOCAL_BACKUP = "$BACKUP_DIR\\$BACKUP_NAME"
|
|
$JSON_PATH = "C:\\temp\\dron_download_output.json"
|
|
|
|
$null = New-Item -ItemType Directory -Force -Path "C:\\temp"
|
|
$null = New-Item -ItemType Directory -Force -Path $BACKUP_DIR
|
|
|
|
Write-Host "[1/3] Iniciando descarga..."
|
|
Write-Host " URL: $DOWNLOAD_URL"
|
|
|
|
Write-Host "[2/3] Descargando..."
|
|
$start = Get-Date
|
|
try {
|
|
$response = Invoke-WebRequest -Uri $DOWNLOAD_URL -UseBasicParsing -Headers @{
|
|
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
|
}
|
|
|
|
$html = $response.Content
|
|
$fileId = ""
|
|
$uuid = ""
|
|
$confirm = "t"
|
|
if ($html -match 'name="id" value="([^"]+)"') { $fileId = $matches[1] }
|
|
if ($html -match 'name="uuid" value="([^"]+)"') { $uuid = $matches[1] }
|
|
if ($html -match 'name="confirm" value="([^"]+)"') { $confirm = $matches[1] }
|
|
|
|
Write-Host " File ID: $fileId"
|
|
Write-Host " UUID: $uuid"
|
|
|
|
$downloadConfirmUrl = "https://drive.usercontent.google.com/download?id=$fileId&export=download&confirm=$confirm&uuid=$uuid"
|
|
Write-Host " URL confirmada: $downloadConfirmUrl"
|
|
|
|
$finalResponse = Invoke-WebRequest -Uri $downloadConfirmUrl -UseBasicParsing -Headers @{
|
|
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
|
}
|
|
|
|
[System.IO.File]::WriteAllBytes($LOCAL_BACKUP, $finalResponse.Content)
|
|
|
|
$dur = (New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
|
|
Write-Host "[+] Descarga en $([math]::Round($dur, 2))s"
|
|
} catch {
|
|
Write-Host "[!] Error: $_"
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "[3/3] Verificando..."
|
|
if (Test-Path $LOCAL_BACKUP) {
|
|
$tam = (Get-Item $LOCAL_BACKUP).Length
|
|
Write-Host " Tamano: $([math]::Round($tam / 1MB, 2)) MB"
|
|
if ($tam -eq 0) {
|
|
Write-Host "[!] Archivo vacío"
|
|
exit 1
|
|
}
|
|
} else {
|
|
Write-Host "[!] No existe"
|
|
exit 1
|
|
}
|
|
|
|
$res = @{
|
|
paso = "download_complete"
|
|
backup_local = $LOCAL_BACKUP
|
|
drive_origen = "Google Drive"
|
|
nodo = "dasu-sql4"
|
|
duracion = [math]::Round($dur, 2)
|
|
timestamp = (Get-Date -Format "o")
|
|
siguiente_paso = "restaurar_backup"
|
|
}
|
|
$res | ConvertTo-Json | Out-File -FilePath $JSON_PATH -Encoding utf8
|
|
Write-Host " Output: $($res | ConvertTo-Json -Compress)"
|
|
PSCMD
|
|
|
|
puts "\n[1/2] Ejecutando download en dasu-sql4..."
|
|
DasuExecutor.execute_ps_on_dasu(ps_script, output_path: JSON_PATH)
|
|
|
|
puts "\n[2/2] Leyendo output JSON..."
|
|
resultado = DasuExecutor.read_json_from_dasu(JSON_PATH)
|
|
|
|
puts " Output: #{resultado.to_json}"
|
|
|
|
puts "\n✅ Download completado"
|
|
puts " Backup: #{resultado['backup_local']}"
|
|
puts " Duración: #{resultado['duracion']}s"
|
|
|
|
File.write('/tmp/dron_download_output.json', resultado.to_json)
|
|
puts " JSON guardado en: /tmp/dron_download_output.json"
|