File size: 6,644 Bytes
5573b21 884155c 5573b21 c5de7ac 5573b21 c5de7ac 5573b21 c5de7ac 5573b21 c5de7ac 5573b21 256382c | 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 | from flask import Flask, jsonify
import requests
import schedule
import time
from datetime import datetime
import logging
import threading
import os
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
app = Flask(__name__)
# Global state
scheduler_thread = None
scheduler_running = False
last_run_status = {
"last_run": None,
"last_status": "never",
"results": {}
}
# ==================== BACKUP FUNCTIONS ====================
def backup_main_db():
try:
url = "https://dooratre-db.hf.space/admin/backup"
headers = {"X-Admin-Secret": "Dbpassword2000$"}
response = requests.post(url, headers=headers, timeout=60)
logging.info(f"[Main DB] Status: {response.status_code}")
return {"status": response.status_code, "ok": response.ok}
except Exception as e:
logging.error(f"[Main DB] Error: {e}")
return {"status": "error", "ok": False, "error": str(e)}
def backup_tele_db():
try:
url = "https://dooratre-tele-db.hf.space/api/backup"
headers = {"X-API-Key": "Telegrampassword2000$"}
response = requests.post(url, headers=headers, timeout=60)
logging.info(f"[Tele DB] Status: {response.status_code}")
return {"status": response.status_code, "ok": response.ok}
except Exception as e:
logging.error(f"[Tele DB] Error: {e}")
return {"status": "error", "ok": False, "error": str(e)}
def backup_cards_db():
try:
url = "https://dooratre-cards.hf.space/admin/backup?secret=Cardspassword2000$"
response = requests.post(url, timeout=60)
logging.info(f"[Cards DB] Status: {response.status_code}")
return {"status": response.status_code, "ok": response.ok}
except Exception as e:
logging.error(f"[Cards DB] Error: {e}")
return {"status": "error", "ok": False, "error": str(e)}
def backup_main_app(server_num):
try:
url = f"https://serverclass{server_num}-app.hf.space/backup_db"
headers = {"X-Admin-Secret": "Adminpassword2000$"}
response = requests.post(url, headers=headers, timeout=60)
logging.info(f"[Server {server_num}] Status: {response.status_code}")
return {"status": response.status_code, "ok": response.ok}
except Exception as e:
logging.error(f"[Server {server_num}] Error: {e}")
return {"status": "error", "ok": False, "error": str(e)}
def backup_all_servers():
results = {}
logging.info("Starting backup for all 55 servers...")
for i in range(1, 56):
results[f"server{i}"] = backup_main_app(i)
time.sleep(1)
logging.info("Finished backup for all 55 servers.")
return results
def run_all_backups():
global last_run_status
logging.info("=" * 60)
logging.info(f"Starting daily backup at {datetime.now()}")
logging.info("=" * 60)
results = {
"main_db": backup_main_db(),
"tele_db": backup_tele_db(),
"cards_db": backup_cards_db(),
"servers": backup_all_servers()
}
last_run_status["last_run"] = datetime.now().isoformat()
last_run_status["last_status"] = "completed"
last_run_status["results"] = results
logging.info("=" * 60)
logging.info(f"Daily backup completed at {datetime.now()}")
logging.info("=" * 60)
return results
# ==================== SCHEDULER ====================
def scheduler_loop():
global scheduler_running
logging.info("Scheduler loop started.")
while scheduler_running:
schedule.run_pending()
time.sleep(30)
logging.info("Scheduler loop stopped.")
# ==================== ROUTES ====================
@app.route("/")
def home():
return jsonify({
"service": "Backup Scheduler",
"version": "1.0",
"endpoints": ["/start", "/stop", "/health", "/run-now", "/status"],
"scheduler_running": scheduler_running,
"schedule": "Every 6 hours"
})
@app.route("/start", methods=["GET", "POST"])
def start():
global scheduler_thread, scheduler_running
if scheduler_running:
return jsonify({
"status": "already_running",
"message": "Scheduler is already running."
}), 200
schedule.clear()
schedule.every(6).hours.do(run_all_backups)
scheduler_running = True
scheduler_thread = threading.Thread(target=scheduler_loop, daemon=True)
scheduler_thread.start()
logging.info("Scheduler STARTED via API.")
return jsonify({
"status": "started",
"message": "Scheduler started. Will run every 6 hours.",
"interval": "every 6 hours"
}), 200
@app.route("/stop", methods=["GET", "POST"])
def stop():
global scheduler_running
if not scheduler_running:
return jsonify({
"status": "not_running",
"message": "Scheduler is not running."
}), 200
scheduler_running = False
schedule.clear()
logging.info("Scheduler STOPPED via API.")
return jsonify({
"status": "stopped",
"message": "Scheduler has been stopped."
}), 200
@app.route("/health", methods=["GET"])
def health():
next_run = None
jobs = schedule.get_jobs()
if jobs:
next_run = str(jobs[0].next_run)
return jsonify({
"status": "healthy",
"scheduler_running": scheduler_running,
"current_time": datetime.now().isoformat(),
"next_scheduled_run": next_run,
"last_run": last_run_status["last_run"],
"last_status": last_run_status["last_status"]
}), 200
@app.route("/run-now", methods=["GET", "POST"])
def run_now():
threading.Thread(target=run_all_backups, daemon=True).start()
return jsonify({
"status": "triggered",
"message": "Backup started in background. Check /health for status."
}), 200
@app.route("/status", methods=["GET"])
def status():
return jsonify({
"scheduler_running": scheduler_running,
"last_run": last_run_status["last_run"],
"last_status": last_run_status["last_status"],
"last_results": last_run_status["results"]
}), 200
# ==================== MAIN ====================
if __name__ == "__main__":
# Auto-start scheduler
schedule.every(6).hours.do(run_all_backups)
scheduler_running = True
scheduler_thread = threading.Thread(target=scheduler_loop, daemon=True)
scheduler_thread.start()
logging.info("Scheduler auto-started on app launch.")
port = int(os.environ.get("PORT", 7860))
app.run(host="0.0.0.0", port=port, debug=False) |