z230
This commit is contained in:
128
Syncthing/04 CheckagainstTreesize.py
Normal file
128
Syncthing/04 CheckagainstTreesize.py
Normal file
@@ -0,0 +1,128 @@
|
||||
import pymysql
|
||||
import sys
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
|
||||
# ==============================
|
||||
# ⚙️ NASTAVENÍ
|
||||
# ==============================
|
||||
|
||||
# Soubor je nyní přímo ve složce s tímto skriptem
|
||||
PHYSICAL_LIST_FILE = "physical_files_list.txt"
|
||||
|
||||
# DB Nastavení
|
||||
DB_CONFIG = {
|
||||
"host": "192.168.1.76",
|
||||
"port": 3307,
|
||||
"user": "root",
|
||||
"password": "Vlado9674+",
|
||||
"database": "torrents",
|
||||
}
|
||||
|
||||
|
||||
# ==============================
|
||||
|
||||
def get_db_paths():
|
||||
print("📡 Stahuji seznam cest z MySQL (to chvilku potrvá)...")
|
||||
paths = set()
|
||||
try:
|
||||
conn = pymysql.connect(**DB_CONFIG)
|
||||
cursor = conn.cursor()
|
||||
# Stáhneme VŠECHNY cesty, které jsou v #ColdData
|
||||
# LIKE optimalizace: MySQL použije index, pokud sloupec full_path má index
|
||||
sql = "SELECT full_path FROM file_md5_index WHERE full_path LIKE '/mnt/user/#ColdData/%'"
|
||||
cursor.execute(sql)
|
||||
|
||||
for row in cursor.fetchall():
|
||||
paths.add(row[0])
|
||||
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
sys.exit(f"❌ Chyba DB: {e}")
|
||||
return paths
|
||||
|
||||
|
||||
def main():
|
||||
print("🚀 AUDIT: Fyzický disk vs. Databáze")
|
||||
print(f"📂 Vstup: {PHYSICAL_LIST_FILE} (Lokální)")
|
||||
print("=====================================================")
|
||||
|
||||
if not os.path.exists(PHYSICAL_LIST_FILE):
|
||||
sys.exit(f"❌ Soubor '{PHYSICAL_LIST_FILE}' nenalezen ve složce projektu!")
|
||||
|
||||
# 1. Načíst DB do paměti
|
||||
db_paths = get_db_paths()
|
||||
print(f"✅ V DB načteno: {len(db_paths):,} záznamů.")
|
||||
|
||||
# 2. Příprava na hledání "Ghost files"
|
||||
ghost_check_set = db_paths.copy()
|
||||
|
||||
# 3. Spočítat řádky pro progress bar
|
||||
print("⏳ Sčítám řádky v souboru...", end="\r")
|
||||
try:
|
||||
total_lines = sum(1 for _ in open(PHYSICAL_LIST_FILE, 'r', encoding='utf-8', errors='ignore'))
|
||||
except:
|
||||
total_lines = 0
|
||||
|
||||
# 4. Porovnání
|
||||
print(f"🔍 Jdu porovnávat {total_lines:,} souborů...")
|
||||
|
||||
missing_in_db = 0
|
||||
found_ok = 0
|
||||
physical_count = 0
|
||||
|
||||
f_missing = open("report_CHYBI_V_DB.txt", "w", encoding="utf-8")
|
||||
|
||||
with open(PHYSICAL_LIST_FILE, "r", encoding="utf-8", errors="ignore") as f:
|
||||
with tqdm(total=total_lines, unit="file", dynamic_ncols=True) as pbar:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line: continue
|
||||
|
||||
# Očekáváme formát: /mnt/user/.../soubor|velikost
|
||||
try:
|
||||
parts = line.rsplit("|", 1)
|
||||
path = parts[0]
|
||||
except IndexError:
|
||||
continue
|
||||
|
||||
physical_count += 1
|
||||
|
||||
# --- KONTROLA ---
|
||||
if path in db_paths:
|
||||
found_ok += 1
|
||||
# Odebrat ze seznamu duchů (našli jsme ho)
|
||||
if path in ghost_check_set:
|
||||
ghost_check_set.remove(path)
|
||||
else:
|
||||
# Je na disku, není v DB
|
||||
missing_in_db += 1
|
||||
f_missing.write(f"{path}\n")
|
||||
|
||||
pbar.update(1)
|
||||
|
||||
f_missing.close()
|
||||
|
||||
# Zbytek jsou duchové
|
||||
ghost_files = list(ghost_check_set)
|
||||
|
||||
with open("report_NAVIC_V_DB.txt", "w", encoding="utf-8") as f_ghost:
|
||||
f_ghost.write("TOTO JSOU ZÁZNAMY V DB, KTERÉ NA DISKU NEEXISTUJÍ:\n")
|
||||
for p in ghost_files:
|
||||
f_ghost.write(f"{p}\n")
|
||||
|
||||
print("\n\n📊 VÝSLEDEK AUDITU")
|
||||
print("=====================================================")
|
||||
print(f"💾 Fyzických souborů: {physical_count:,}")
|
||||
print(f"✅ V pořádku (V DB): {found_ok:,}")
|
||||
print("-----------------------------------------------------")
|
||||
print(f"❌ CHYBÍ V DB (Re-index): {missing_in_db:,}")
|
||||
print(f" -> 'report_CHYBI_V_DB.txt'")
|
||||
print("-----------------------------------------------------")
|
||||
print(f"👻 NAVÍC V DB (Smazat): {len(ghost_files):,}")
|
||||
print(f" -> 'report_NAVIC_V_DB.txt'")
|
||||
print("=====================================================")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user