64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Backup Status API
|
|
Hosts push after borgmatic backup, n8n polls this endpoint.
|
|
GET / → JSON summary of all hosts
|
|
POST /push?host=<name>&status=ok|error&msg=<text> → update host status
|
|
"""
|
|
from flask import Flask, request, jsonify
|
|
import json, os, time
|
|
|
|
app = Flask(__name__)
|
|
DATA_FILE = "/data/backup_status.json"
|
|
|
|
def load():
|
|
if os.path.exists(DATA_FILE):
|
|
with open(DATA_FILE) as f:
|
|
return json.load(f)
|
|
return {}
|
|
|
|
def save(data):
|
|
os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
|
|
with open(DATA_FILE, "w") as f:
|
|
json.dump(data, f, indent=2)
|
|
|
|
@app.route("/")
|
|
def status():
|
|
data = load()
|
|
now = time.time()
|
|
result = {}
|
|
for host, info in data.items():
|
|
age_h = (now - info.get("ts", 0)) / 3600
|
|
if age_h > 26:
|
|
s = "KEIN BACKUP"
|
|
elif info.get("status") == "error":
|
|
s = "FEHLER"
|
|
else:
|
|
s = "OK"
|
|
result[host] = {
|
|
"status": s,
|
|
"last_backup": info.get("time", "unbekannt"),
|
|
"msg": info.get("msg", ""),
|
|
"age_h": round(age_h, 1)
|
|
}
|
|
return jsonify(result)
|
|
|
|
@app.route("/push", methods=["POST", "GET"])
|
|
def push():
|
|
host = request.args.get("host") or request.form.get("host")
|
|
status = request.args.get("status", "ok")
|
|
msg = request.args.get("msg", "")
|
|
if not host:
|
|
return jsonify({"error": "host parameter required"}), 400
|
|
data = load()
|
|
data[host] = {
|
|
"status": status,
|
|
"msg": msg,
|
|
"ts": time.time(),
|
|
"time": time.strftime("%Y-%m-%d %H:%M")
|
|
}
|
|
save(data)
|
|
return jsonify({"ok": True, "host": host, "status": status})
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=9999)
|