161 lines
5.6 KiB
Ruby
Executable File
161 lines
5.6 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_FULL_YYYYMMDD_HHMMSS.bak
|
|
# Usa: rclone copy (mantiene nombre original del backup)
|
|
# ==========================================================
|
|
|
|
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
|
|
backup_name = nil
|
|
file_id = nil
|
|
|
|
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']
|
|
# Extraer el nombre EXACTO del archivo desde drive_ruta (ej: "rmonla-GDrive:drive_bkps-dasu/sysdasuten_FULL_20260415_121943.bak")
|
|
backup_name = File.basename(drive_ruta)
|
|
|
|
puts "[*] Dron: Buscando Google Drive ID real (rclone)..."
|
|
raw_json = `rclone lsjson '#{drive_ruta}' 2>/dev/null`.strip
|
|
begin
|
|
parsed = JSON.parse(raw_json).first
|
|
file_id = parsed['ID']
|
|
rescue
|
|
puts "[!] Error al parsear ID de Google Drive. rclone regresó vacio."
|
|
exit 1
|
|
end
|
|
|
|
download_url = "https://drive.google.com/uc?export=download&id=#{file_id}"
|
|
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
|
|
|
|
# Validar que backup_name tenga el formato correcto (FULL o DIF con fecha)
|
|
unless backup_name =~ /sysdasuten_(FULL|DIF)_\d{8}_\d{6}\.bak/
|
|
puts "[!] Advertencia: El nombre del backup '#{backup_name}' no sigue el formato esperado (sysdasuten_FULL_YYYYMMDD_HHMMSS.bak)"
|
|
end
|
|
|
|
# PowerShell script para download
|
|
ps_script = <<~PSCMD.strip
|
|
$ErrorActionPreference = "Stop"
|
|
$BACKUP_DIR = "F:\\BACKUP"
|
|
$BACKUP_NAME = "#{backup_name}"
|
|
$LOCAL_BACKUP = "$BACKUP_DIR\\$BACKUP_NAME"
|
|
$JSON_PATH = "C:\\temp\\dron_download_output.json"
|
|
$REMOTE_RCLONE_PATH = "#{drive_ruta}"
|
|
|
|
$null = New-Item -ItemType Directory -Force -Path "C:\\temp"
|
|
$null = New-Item -ItemType Directory -Force -Path $BACKUP_DIR
|
|
|
|
Write-Host "[1/3] Iniciando descarga con Rclone Nativo..."
|
|
Write-Host " Origen: $REMOTE_RCLONE_PATH"
|
|
Write-Host " Destino: $LOCAL_BACKUP"
|
|
|
|
Write-Host "[2/3] Descargando..."
|
|
$start = Get-Date
|
|
|
|
# Limpiamos el ultimo archivo varado (por si el anterior dejo su esqueleto de 0 bytes)
|
|
if (Test-Path $LOCAL_BACKUP) { Remove-Item $LOCAL_BACKUP -Force }
|
|
|
|
# Llamamos al rclone binario local apuntando al config
|
|
& C:\\rclone\\rclone.exe copy "$REMOTE_RCLONE_PATH" "$BACKUP_DIR\\" --config "C:\\rclone\\rclone.conf" --stats-one-line --stats 1s
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "[!] Error: Falló la descarga de rclone (Código exit: $LASTEXITCODE)"
|
|
exit 1
|
|
}
|
|
|
|
$dur = (New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
|
|
Write-Host "[+] Descarga en $([math]::Round($dur, 2))s"
|
|
|
|
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
|
|
backup_name = $BACKUP_NAME
|
|
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"
|