File size: 13,574 Bytes
0f0ef8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# -*- coding: utf-8 -*-
"""
db_export_import.py — Backup & Restore (Export/Import) do banco ATIVO (Produção/Teste)
Recursos:
• Exibe banco ativo (prod/test) e URL do engine
• Exporta todas as tabelas para:
- ZIP (CSV por tabela + manifest.json)
- Excel (.xlsx) (1 aba por tabela + manifest sheet)
• Importa (upload) de:
- ZIP (CSV por tabela)
- Excel (.xlsx)
• Modos de import: APPEND ou REPLACE (cuidado com FK)
• Snapshot físico para SQLite: cópia do arquivo (.db) — backup/restore rápido
Dependências:
- pandas, openpyxl, sqlalchemy, zipfile, io, json, datetime
"""
import os
import io
import json
import zipfile
from datetime import datetime
import streamlit as st
import pandas as pd
from sqlalchemy import inspect, text
from banco import get_engine, db_info, SessionLocal
from utils_auditoria import registrar_log
# Ambiente (prod/test) — se db_router não existir, fallback para 'prod'
try:
from db_router import current_db_choice
_HAS_ROUTER = True
except Exception:
_HAS_ROUTER = False
def current_db_choice() -> str:
return "prod"
# =========================
# Helpers: tabelas e I/O
# =========================
def list_tables(engine) -> list[str]:
"""Retorna nomes de todas as tabelas via SQLAlchemy inspection."""
inspector = inspect(engine)
return inspector.get_table_names()
def _read_table_df(engine, table_name: str) -> pd.DataFrame:
"""Lê toda a tabela como DataFrame."""
try:
# pandas + SQLAlchemy: lê tabela diretamente
return pd.read_sql_table(table_name, con=engine)
except Exception:
# fallback: SELECT com aspas (útil para SQLite com nomes case-sensitive)
return pd.read_sql(f'SELECT * FROM "{table_name}"', con=engine)
def _write_table_df(engine, table_name: str, df: pd.DataFrame, mode: str = "append"):
"""
Escreve DataFrame em tabela.
mode: "append" (adiciona) ou "replace" (sobrescreve todos os dados).
Observação: nos fluxos de import, quando 'replace' foi selecionado,
todas as tabelas são truncadas previamente, e aqui usamos 'append'.
"""
if mode not in ("append", "replace"):
mode = "append"
df.to_sql(table_name, con=engine, if_exists=mode, index=False)
# =========================
# Export: ZIP (CSV) & Excel
# =========================
def export_zip(engine, ambiente: str) -> bytes:
"""
Exporta todas as tabelas para um ZIP:
- 1 CSV por tabela (UTF-8-BOM)
- manifest.json com metadados (ambiente, timestamp, url, tabelas)
"""
tables = list_tables(engine)
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as z:
for t in tables:
df = _read_table_df(engine, t)
csv_bytes = df.to_csv(index=False, encoding="utf-8-sig").encode("utf-8-sig")
z.writestr(f"{t}.csv", csv_bytes)
manifest = {
"ambiente": ambiente,
"timestamp": datetime.now().isoformat(),
"engine_url": str(engine.url),
"tables": tables,
"format": "zip/csv",
"version": "1.0",
}
z.writestr("manifest.json", json.dumps(manifest, ensure_ascii=False, indent=2))
buf.seek(0)
return buf.getvalue()
def export_excel(engine, ambiente: str) -> bytes:
"""
Exporta todas as tabelas para um Excel (.xlsx):
- 1 aba por tabela (limitada a 31 caracteres)
- "manifest" com metadados
"""
tables = list_tables(engine)
buf = io.BytesIO()
with pd.ExcelWriter(buf, engine="openpyxl") as writer:
# manifest
manifest = pd.DataFrame([{
"ambiente": ambiente,
"timestamp": datetime.now().isoformat(),
"engine_url": str(engine.url),
"tables": ", ".join(tables),
"format": "xlsx",
"version": "1.0",
}])
manifest.to_excel(writer, sheet_name="manifest", index=False)
# tabelas → 1 aba por tabela
for t in tables:
df = _read_table_df(engine, t)
sheet = t[:31] if len(t) > 31 else t
df.to_excel(writer, sheet_name=sheet, index=False)
buf.seek(0)
return buf.getvalue()
# =========================
# Import: ZIP (CSV) & Excel
# =========================
def import_zip(engine, file_bytes: bytes, mode: str = "append") -> dict:
"""
Importa dados de um ZIP (CSV por tabela).
mode: "append" ou "replace".
Retorna um relatório {table: {"rows": int, "mode": str}}.
"""
report = {}
zbuf = io.BytesIO(file_bytes)
with zipfile.ZipFile(zbuf, "r") as z:
# Se replace, limpar tabelas (cuidado com FK)
if mode == "replace":
_truncate_all(engine)
for name in z.namelist():
if not name.lower().endswith(".csv"):
continue
table = os.path.splitext(os.path.basename(name))[0]
csv_bytes = z.read(name)
df = pd.read_csv(io.BytesIO(csv_bytes), dtype=str) # dtype=str para evitar coercões agressivas
# Conversões leves de datetime (best-effort)
for col in df.columns:
if "data" in col.lower() or "date" in col.lower():
try:
df[col] = pd.to_datetime(df[col], errors="ignore")
except Exception:
pass
# replace já truncou; aqui fazemos append
_write_table_df(engine, table, df, mode="append")
report[table] = {"rows": int(len(df)), "mode": mode}
return report
def import_excel(engine, file_bytes: bytes, mode: str = "append") -> dict:
"""
Importa dados de um Excel (.xlsx) com múltiplas abas (1 por tabela).
mode: "append" ou "replace".
"""
report = {}
xbuf = io.BytesIO(file_bytes)
xls = pd.ExcelFile(xbuf, engine="openpyxl")
sheets = [s for s in xls.sheet_names if s.lower() != "manifest"]
if mode == "replace":
_truncate_all(engine)
for sheet in sheets:
df = xls.parse(sheet_name=sheet, dtype=str)
# best-effort para datas
for col in df.columns:
if "data" in col.lower() or "date" in col.lower():
try:
df[col] = pd.to_datetime(df[col], errors="ignore")
except Exception:
pass
table = sheet
_write_table_df(engine, table, df, mode="append")
report[table] = {"rows": int(len(df)), "mode": mode}
return report
# =========================
# Truncate (REPLACE mode)
# =========================
def _truncate_all(engine):
"""
Limpa todas as tabelas do banco ativo (cuidado!).
• Para SQLite: desabilita FK temporariamente, apaga, e reabilita.
• Para outros bancos: executa DELETE tabela; considere ordem por FK se necessário.
"""
insp = inspect(engine)
tables = insp.get_table_names()
with engine.begin() as conn:
url = str(engine.url)
is_sqlite = url.startswith("sqlite")
if is_sqlite:
conn.execute(text("PRAGMA foreign_keys=OFF"))
# Apaga conteúdo (sem considerar ordem de FK — OK para SQLite com FK OFF)
for t in tables:
conn.execute(text(f'DELETE FROM "{t}"'))
if is_sqlite:
conn.execute(text("PRAGMA foreign_keys=ON"))
# =========================
# Snapshot físico (SQLite)
# =========================
def snapshot_sqlite(engine, ambiente: str) -> bytes:
"""
Cria um snapshot (cópia física) do arquivo SQLite do banco ativo.
Retorna o conteúdo do arquivo para download.
"""
url = str(engine.url)
if not url.startswith("sqlite:///"):
raise RuntimeError("Snapshot físico disponível apenas para SQLite.")
db_path = url.replace("sqlite:///", "")
if not os.path.isfile(db_path):
raise FileNotFoundError(f"Arquivo SQLite não encontrado: {db_path}")
with open(db_path, "rb") as f:
data = f.read()
# auditoria
try:
registrar_log(usuario=st.session_state.get("usuario"),
acao=f"Snapshot SQLite ({ambiente})",
tabela="backup",
registro_id=None)
except Exception:
pass
return data
# =========================
# UI (Streamlit)
# =========================
def main():
st.title("🗄️ Backup & Restore | Export/Import de Banco")
# Banco ativo e info
ambiente = current_db_choice()
info = db_info()
st.caption(f"🧭 Ambiente: {'Produção' if ambiente == 'prod' else 'Teste'}")
st.caption(f"🔗 Engine URL: {info.get('url')}")
engine = get_engine()
st.divider()
st.subheader("⬇️ Exportar dados")
colA, colB, colC = st.columns(3)
with colA:
if st.button("Exportar ZIP (CSV por tabela)", type="primary"):
try:
zip_bytes = export_zip(engine, ambiente)
fname = f"backup_{ambiente}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
st.download_button("📥 Baixar ZIP", data=zip_bytes, file_name=fname, mime="application/zip")
registrar_log(usuario=st.session_state.get("usuario"),
acao=f"Export ZIP (ambiente={ambiente})",
tabela="backup", registro_id=None)
except Exception as e:
st.error(f"Falha ao exportar ZIP: {e}")
with colB:
if st.button("Exportar Excel (.xlsx)", type="primary"):
try:
xlsx_bytes = export_excel(engine, ambiente)
fname = f"backup_{ambiente}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
st.download_button("📥 Baixar Excel", data=xlsx_bytes, file_name=fname,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
registrar_log(usuario=st.session_state.get("usuario"),
acao=f"Export XLSX (ambiente={ambiente})",
tabela="backup", registro_id=None)
except Exception as e:
st.error(f"Falha ao exportar Excel: {e}")
with colC:
# Snapshot físico apenas para SQLite
url = str(engine.url)
if url.startswith("sqlite:///"):
if st.button("Snapshot físico (SQLite)", type="secondary"):
try:
snap_bytes = snapshot_sqlite(engine, ambiente)
fname = f"snapshot_{ambiente}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.db"
st.download_button("📥 Baixar Snapshot (.db)", data=snap_bytes, file_name=fname, mime="application/octet-stream")
except Exception as e:
st.error(f"Falha ao criar snapshot: {e}")
else:
st.caption("ℹ️ Snapshot físico disponível apenas para SQLite.")
st.divider()
st.subheader("⬆️ Importar dados")
mode = st.radio("Modo de importação:", ["APPEND (adicionar)", "REPLACE (substituir tudo)"], horizontal=True)
mode_val = "append" if "APPEND" in mode else "replace"
up_col1, up_col2 = st.columns(2)
with up_col1:
zip_file = st.file_uploader("Upload ZIP (CSV por tabela)", type=["zip"])
if zip_file is not None and st.button("Importar do ZIP", type="primary"):
try:
report = import_zip(engine, zip_file.read(), mode=mode_val)
st.success(f"Import ZIP concluído ({mode_val}).")
st.json(report)
registrar_log(usuario=st.session_state.get("usuario"),
acao=f"Import ZIP ({mode_val}, ambiente={ambiente})",
tabela="restore", registro_id=None)
except Exception as e:
st.error(f"Falha ao importar ZIP: {e}")
with up_col2:
xls_file = st.file_uploader("Upload Excel (.xlsx)", type=["xlsx"])
if xls_file is not None and st.button("Importar do Excel", type="primary"):
try:
report = import_excel(engine, xls_file.read(), mode=mode_val)
st.success(f"Import Excel concluído ({mode_val}).")
st.json(report)
registrar_log(usuario=st.session_state.get("usuario"),
acao=f"Import XLSX ({mode_val}, ambiente={ambiente})",
tabela="restore", registro_id=None)
except Exception as e:
st.error(f"Falha ao importar Excel: {e}")
st.divider()
st.info("⚠️ Recomendações:\n"
"• Para restore completo com integridade referencial, prefira snapshot físico no SQLite, ou migrações controladas em bancos como Postgres/SQL Server.\n"
"• O modo REPLACE desabilita FK temporariamente no SQLite para permitir limpeza; use com cautela.\n"
"• Em produção, considere gerar backups com versionamento e retenção (ex.: timestamp no nome do arquivo).")
def render():
# compatível com seu roteador/menu
main()
if __name__ == "__main__":
st.set_page_config(page_title="Backup & Restore | ARM", layout="wide")
main()
|