84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Sichert den Client-Log dauerhaft.
|
|
|
|
Der Client rotiert seinen Log und wirft Altes weg. Dieses Werkzeug hängt neue
|
|
Bytes an eine Kopie im Ordner korpus/ und merkt sich den Stand.
|
|
|
|
python3 korpus.py # einmal sichern
|
|
python3 korpus.py --watch # dauerhaft mitlaufen
|
|
"""
|
|
import os as _os, sys as _sys
|
|
_w = _os.path.dirname(_os.path.abspath(__file__))
|
|
while _w != "/" and not _os.path.isdir(_os.path.join(_w, "hand")):
|
|
_w = _os.path.dirname(_w)
|
|
WURZEL = _w
|
|
for _u in ("lesen", "entscheiden", "hand", "lobby", "korpus", "betrieb"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import datetime
|
|
import os
|
|
|
|
import umgebung
|
|
import sys
|
|
import sys
|
|
import time
|
|
|
|
LOG = umgebung.log()
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
DIR = os.path.join(WURZEL, "archiv")
|
|
STAND = os.path.join(DIR, "stand.txt")
|
|
|
|
|
|
def load_stand():
|
|
try:
|
|
parts = open(STAND).read().split()
|
|
return int(parts[0]), int(parts[1]), parts[2]
|
|
except Exception:
|
|
return 0, 0, ""
|
|
|
|
|
|
def save_stand(inode, offset, target):
|
|
os.makedirs(DIR, exist_ok=True)
|
|
open(STAND, "w").write(f"{inode} {offset} {target}")
|
|
|
|
|
|
def sichere():
|
|
"""Hängt neue Bytes an. Gibt (geschrieben, Zieldatei) zurück."""
|
|
if not os.path.exists(LOG):
|
|
return 0, ""
|
|
st = os.stat(LOG)
|
|
inode, offset, target = load_stand()
|
|
if st.st_ino != inode or not target:
|
|
stamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
|
|
target = os.path.join(DIR, f"main-{stamp}.log")
|
|
offset = 0
|
|
os.makedirs(DIR, exist_ok=True)
|
|
with open(LOG, "rb") as src:
|
|
src.seek(offset)
|
|
neu = src.read()
|
|
if neu:
|
|
with open(target, "ab") as dst:
|
|
dst.write(neu)
|
|
save_stand(st.st_ino, st.st_size, target)
|
|
return len(neu), target
|
|
|
|
|
|
def main():
|
|
if "--watch" in sys.argv:
|
|
print(f"sichere dauerhaft nach {DIR} (Abbruch mit Strg-C)")
|
|
while True:
|
|
try:
|
|
n, ziel = sichere()
|
|
if n:
|
|
print(f" {time.strftime('%H:%M:%S')} {n} Bytes -> {os.path.basename(ziel)}")
|
|
except Exception as exc:
|
|
print(" Fehler:", exc)
|
|
time.sleep(20)
|
|
n, ziel = sichere()
|
|
print(f"{n} neue Bytes gesichert -> {ziel}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|