#!/usr/bin/env python3
"""Loop de dump automatique MariaDB.
Attend 3h00 du matin puis execute backup.py, en boucle.
"""
import time
import subprocess
import os
from datetime import datetime

while True:
    now = datetime.now()
    # Calculer secondes jusqu'a 3h00
    seconds_until_3am = ((3 - now.hour) * 3600) + ((0 - now.minute) * 60) - now.second
    if seconds_until_3am <= 0:
        seconds_until_3am += 86400
    
    print(f"[db_backup] Prochain dump dans {seconds_until_3am // 3600}h{(seconds_until_3am % 3600) // 60}m", flush=True)
    time.sleep(seconds_until_3am)
    
    # Executer le backup
    env = {**os.environ}
    subprocess.run(['python3', '/app/backup.py'], env=env)