import wmill import json import mysql.connector def main(backups: list, batch_id: str, total_time: str, total_size: str, raw_payload: dict, debug_mode: bool = False): if not backups: print("Keine Backups zum Speichern.") return {"inserted": 0} if debug_mode: print(f"[DEBUG] DB-Insert übersprungen (debug_mode=True)") print(f"[DEBUG] batch_id={batch_id} | {len(backups)} VMs | total_time={total_time!r} | total_size={total_size!r}") for b in backups: print(f"[DEBUG] VM {b['vmid']} ({b['vm_name']}): status={b['status']}, size={b['size']}, duration={b['duration_sec']}s") return {"inserted": 0, "batch_id": batch_id, "debug": True} db_cfg = json.loads(wmill.get_variable("f/Backup/mysql_config")) conn = mysql.connector.connect(**db_cfg) cur = conn.cursor() inserted = 0 for b in backups: try: cur.execute(""" INSERT INTO Kunden.`bronze.proxmox_backup_log` (batch_id, vmid, vm_name, status, duration_sec, size, filename, log_text, total_time, total_size, raw_payload) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """, ( batch_id, b['vmid'], b['vm_name'], b['status'], b['duration_sec'], b['size'], b['filename'], b['log_text'], total_time, total_size, json.dumps(raw_payload) )) inserted += 1 except Exception as e: print(f"Fehler beim Insert für VM {b['vmid']}: {e}") conn.commit() cur.close() conn.close() print(f"{inserted} Backup-Einträge in DB gespeichert (batch_id: {batch_id})") return {"inserted": inserted, "batch_id": batch_id}