tw22
This commit is contained in:
23
Backup/20 ZIP komprese vytvoreneho fbk.py
Normal file
23
Backup/20 ZIP komprese vytvoreneho fbk.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
# =========================
|
||||
# CONFIG
|
||||
# =========================
|
||||
|
||||
SRC = Path(r"D:\medicusbackup\MEDICUS_2026-01-24_17-07-44.fbk")
|
||||
DST = SRC.with_suffix(".zip")
|
||||
|
||||
# =========================
|
||||
# ZIP MAX COMPRESSION
|
||||
# =========================
|
||||
|
||||
with zipfile.ZipFile(
|
||||
DST,
|
||||
mode="w",
|
||||
compression=zipfile.ZIP_DEFLATED,
|
||||
compresslevel=9
|
||||
) as z:
|
||||
z.write(SRC, arcname=SRC.name)
|
||||
|
||||
print(f"ZIP created: {DST}")
|
||||
39
Backup/21 ZIP komprese s prubehem.py
Normal file
39
Backup/21 ZIP komprese s prubehem.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
SRC = Path(r"D:\medicusbackup\MEDICUS_2026-01-24_17-07-44.fbk")
|
||||
DST = SRC.with_suffix(".zip")
|
||||
|
||||
total = SRC.stat().st_size
|
||||
processed = 0
|
||||
chunk = 8 * 1024 * 1024 # 8 MB
|
||||
|
||||
with zipfile.ZipFile(
|
||||
DST,
|
||||
"w",
|
||||
compression=zipfile.ZIP_DEFLATED,
|
||||
compresslevel=9,
|
||||
) as zf:
|
||||
|
||||
zi = zipfile.ZipInfo(SRC.name)
|
||||
zi.compress_type = zipfile.ZIP_DEFLATED
|
||||
|
||||
# ⬇⬇⬇ DŮLEŽITÉ ⬇⬇⬇
|
||||
with zf.open(zi, "w", force_zip64=True) as z:
|
||||
with open(SRC, "rb") as f:
|
||||
while True:
|
||||
buf = f.read(chunk)
|
||||
if not buf:
|
||||
break
|
||||
|
||||
z.write(buf)
|
||||
processed += len(buf)
|
||||
|
||||
pct = processed * 100 / total
|
||||
print(
|
||||
f"\rZIP: {pct:6.2f}% ({processed//1024//1024} MB)",
|
||||
end="",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
print("\nHotovo.")
|
||||
141
Backup/22 3 komprese test.py
Normal file
141
Backup/22 3 komprese test.py
Normal file
@@ -0,0 +1,141 @@
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
import zstandard as zstd
|
||||
|
||||
# ============================================================
|
||||
# CONFIG
|
||||
# ============================================================
|
||||
|
||||
SRC = Path(r"D:\medicusbackup\MEDICUS_2026-01-24_17-07-44.fbk")
|
||||
BASE = SRC.with_suffix("")
|
||||
CHUNK = 8 * 1024 * 1024 # 8 MB
|
||||
|
||||
SEVENZIP_EXE = r"c:\Program Files (x86)\7-Zip\7z.exe"
|
||||
|
||||
ORIG_SIZE = SRC.stat().st_size
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ZIP (ZIP64 + progress)
|
||||
# ============================================================
|
||||
|
||||
def zip_test():
|
||||
dst = BASE.with_suffix(".zip")
|
||||
total = ORIG_SIZE
|
||||
processed = 0
|
||||
|
||||
start = time.time()
|
||||
|
||||
with zipfile.ZipFile(
|
||||
dst,
|
||||
"w",
|
||||
compression=zipfile.ZIP_DEFLATED,
|
||||
compresslevel=9,
|
||||
) as zf:
|
||||
|
||||
zi = zipfile.ZipInfo(SRC.name)
|
||||
zi.compress_type = zipfile.ZIP_DEFLATED
|
||||
|
||||
# ⬇⬇⬇ KLÍČOVÁ OPRAVA ⬇⬇⬇
|
||||
with zf.open(zi, "w", force_zip64=True) as z:
|
||||
with open(SRC, "rb") as f:
|
||||
while buf := f.read(CHUNK):
|
||||
z.write(buf)
|
||||
processed += len(buf)
|
||||
print(
|
||||
f"\rZIP {processed*100/total:6.2f}%",
|
||||
end="",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
print()
|
||||
return time.time() - start, dst.stat().st_size
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 7Z (CLI + progress)
|
||||
# ============================================================
|
||||
|
||||
def sevenzip_test():
|
||||
dst = BASE.with_suffix(".7z")
|
||||
|
||||
start = time.time()
|
||||
|
||||
proc = subprocess.Popen(
|
||||
[SEVENZIP_EXE, "a", "-t7z", "-mx=9", str(dst), str(SRC)],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
)
|
||||
|
||||
for line in proc.stdout:
|
||||
if "%" in line:
|
||||
print(f"\r7Z {line.strip():>6}", end="", flush=True)
|
||||
|
||||
proc.wait()
|
||||
print()
|
||||
|
||||
return time.time() - start, dst.stat().st_size
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ZSTD (stream + progress)
|
||||
# ============================================================
|
||||
|
||||
def zstd_test():
|
||||
dst = BASE.with_suffix(".zst")
|
||||
total = ORIG_SIZE
|
||||
processed = 0
|
||||
|
||||
cctx = zstd.ZstdCompressor(level=19)
|
||||
start = time.time()
|
||||
|
||||
with open(SRC, "rb") as src, open(dst, "wb") as out:
|
||||
with cctx.stream_writer(out) as compressor:
|
||||
while buf := src.read(CHUNK):
|
||||
compressor.write(buf)
|
||||
processed += len(buf)
|
||||
print(
|
||||
f"\rZSTD {processed*100/total:6.2f}%",
|
||||
end="",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
print()
|
||||
return time.time() - start, dst.stat().st_size
|
||||
|
||||
|
||||
# ============================================================
|
||||
# MAIN
|
||||
# ============================================================
|
||||
|
||||
results = {}
|
||||
|
||||
print("\n--- ZIP ---")
|
||||
t, s = zip_test()
|
||||
results["ZIP"] = (t, s)
|
||||
|
||||
# print("\n--- 7Z ---")
|
||||
# t, s = sevenzip_test()
|
||||
# results["7Z"] = (t, s)
|
||||
#
|
||||
# print("\n--- ZSTD ---")
|
||||
# t, s = zstd_test()
|
||||
# results["ZSTD"] = (t, s)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# SUMMARY
|
||||
# ============================================================
|
||||
|
||||
print("\n=== SUMMARY ===")
|
||||
for name, (t, size) in results.items():
|
||||
ratio = 100 * (1 - size / ORIG_SIZE)
|
||||
print(
|
||||
f"{name:5s}: "
|
||||
f"{t:7.1f} s "
|
||||
f"{size/1024/1024:8.1f} MB "
|
||||
f"(-{ratio:5.1f} %)"
|
||||
)
|
||||
161
Backup/24 zip_fbk_and_notify.py
Normal file
161
Backup/24 zip_fbk_and_notify.py
Normal file
@@ -0,0 +1,161 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
import zipfile
|
||||
import time
|
||||
import traceback
|
||||
|
||||
from EmailMessagingGraph import send_mail
|
||||
|
||||
# ============================================================
|
||||
# CONFIG
|
||||
# ============================================================
|
||||
|
||||
GBAK = r"C:\Program Files\Firebird\Firebird_2_5_CGM\bin\gbak.exe"
|
||||
DB = r"localhost/3050:Z:\Medicus 3\data\MEDICUS.FDB"
|
||||
BACKUP_DIR = Path(r"D:\medicusbackup")
|
||||
|
||||
FB_USER = "SYSDBA"
|
||||
FB_PASS = "masterkey"
|
||||
|
||||
MAIL_TO = "vladimir.buzalka@buzalka.cz"
|
||||
|
||||
CHUNK = 8 * 1024 * 1024 # 8 MB
|
||||
|
||||
|
||||
# ============================================================
|
||||
# MAIN
|
||||
# ============================================================
|
||||
|
||||
def main():
|
||||
BACKUP_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
now = datetime.now()
|
||||
ts = now.strftime("%Y-%m-%d_%H-%M-%S")
|
||||
|
||||
fbk = BACKUP_DIR / f"MEDICUS_{ts}.fbk"
|
||||
zipf = fbk.with_suffix(".zip")
|
||||
log = BACKUP_DIR / f"MEDICUS_{ts}.log"
|
||||
|
||||
report = []
|
||||
report.append(f"Čas spuštění: {now}")
|
||||
report.append(f"Databáze: {DB}")
|
||||
report.append("")
|
||||
|
||||
try:
|
||||
# ====================================================
|
||||
# 1) GBAK
|
||||
# ====================================================
|
||||
t0 = time.time()
|
||||
|
||||
cmd = [
|
||||
GBAK,
|
||||
"-b",
|
||||
"-user", FB_USER,
|
||||
"-pas", FB_PASS,
|
||||
DB,
|
||||
str(fbk),
|
||||
"-v",
|
||||
]
|
||||
|
||||
with open(log, "w", encoding="utf-8") as f:
|
||||
subprocess.run(
|
||||
cmd,
|
||||
stdout=f,
|
||||
stderr=subprocess.STDOUT,
|
||||
check=True,
|
||||
)
|
||||
|
||||
t_gbak = time.time() - t0
|
||||
fbk_size = fbk.stat().st_size
|
||||
|
||||
report.append("GBAK záloha: OK")
|
||||
report.append(f"FBK soubor: {fbk}")
|
||||
report.append(f"Velikost FBK: {fbk_size/1024/1024:.1f} MB")
|
||||
report.append(f"Čas GBAK: {t_gbak:.1f} s")
|
||||
report.append("")
|
||||
|
||||
# ====================================================
|
||||
# 2) ZIP
|
||||
# ====================================================
|
||||
t1 = time.time()
|
||||
processed = 0
|
||||
|
||||
with zipfile.ZipFile(
|
||||
zipf,
|
||||
"w",
|
||||
compression=zipfile.ZIP_DEFLATED,
|
||||
compresslevel=9,
|
||||
) as zf:
|
||||
|
||||
zi = zipfile.ZipInfo(fbk.name)
|
||||
zi.compress_type = zipfile.ZIP_DEFLATED
|
||||
|
||||
with zf.open(zi, "w", force_zip64=True) as z:
|
||||
with open(fbk, "rb") as src:
|
||||
while buf := src.read(CHUNK):
|
||||
z.write(buf)
|
||||
processed += len(buf)
|
||||
pct = processed * 100 / fbk_size
|
||||
print(
|
||||
f"\rZIP: {pct:6.2f}% "
|
||||
f"({processed//1024//1024} MB)",
|
||||
end="",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
print("\nZIP hotov.")
|
||||
|
||||
t_zip = time.time() - t1
|
||||
zip_size = zipf.stat().st_size
|
||||
|
||||
report.append("ZIP komprese: OK")
|
||||
report.append(f"ZIP soubor: {zipf}")
|
||||
report.append(f"Velikost ZIP: {zip_size/1024/1024:.1f} MB")
|
||||
report.append(
|
||||
f"Kompresní poměr: "
|
||||
f"{100 * (1 - zip_size / fbk_size):.1f} %"
|
||||
)
|
||||
report.append(f"Čas ZIP: {t_zip:.1f} s")
|
||||
report.append("")
|
||||
|
||||
# ====================================================
|
||||
# 3) DELETE FBK
|
||||
# ====================================================
|
||||
fbk.unlink()
|
||||
|
||||
report.append("FBK soubor byl po úspěšné kompresi smazán.")
|
||||
report.append("")
|
||||
report.append(f"LOG: {log}")
|
||||
|
||||
# ====================================================
|
||||
# MAIL OK
|
||||
# ====================================================
|
||||
send_mail(
|
||||
MAIL_TO,
|
||||
"✅ MEDICUS – záloha + ZIP OK",
|
||||
"\n".join(report),
|
||||
)
|
||||
|
||||
except Exception:
|
||||
err = traceback.format_exc()
|
||||
|
||||
send_mail(
|
||||
MAIL_TO,
|
||||
"❌ MEDICUS – chyba při záloze / ZIP",
|
||||
f"""Při záloze MEDICUS došlo k chybě.
|
||||
|
||||
Čas: {now}
|
||||
|
||||
Chyba:
|
||||
{err}
|
||||
|
||||
FBK (pokud existuje): {fbk}
|
||||
LOG: {log}
|
||||
""",
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user