feat(telegram): implement automatic scheduled backups

This commit is contained in:
ReturnFI
2025-09-18 11:40:31 +00:00
parent 1b11c54e4b
commit b583894d9a
2 changed files with 116 additions and 19 deletions

View File

@ -3,6 +3,7 @@
import sys
import subprocess
from pathlib import Path
import os
core_scripts_dir = Path(__file__).resolve().parents[1]
if str(core_scripts_dir) not in sys.path:
@ -10,12 +11,10 @@ if str(core_scripts_dir) not in sys.path:
from paths import TELEGRAM_ENV
def update_env_file(api_token, admin_user_ids):
def update_env_file(api_token, admin_user_ids, backup_interval):
TELEGRAM_ENV.write_text(f"""API_TOKEN={api_token}
ADMIN_USER_IDS=[{admin_user_ids}]
BACKUP_INTERVAL_HOUR={backup_interval}
""")
def create_service_file():
@ -32,12 +31,12 @@ Restart=always
WantedBy=multi-user.target
""")
def start_service(api_token, admin_user_ids):
def start_service(api_token, admin_user_ids, backup_interval=12):
if subprocess.run(["systemctl", "is-active", "--quiet", "hysteria-telegram-bot.service"]).returncode == 0:
print("The hysteria-telegram-bot.service is already running.")
return
update_env_file(api_token, admin_user_ids)
update_env_file(api_token, admin_user_ids, backup_interval)
create_service_file()
subprocess.run(["systemctl", "daemon-reload"])
@ -55,20 +54,60 @@ def stop_service():
TELEGRAM_ENV.unlink(missing_ok=True)
print("\nHysteria bot service stopped and disabled. .env file removed.")
def set_backup_interval(backup_interval):
if not os.path.exists(TELEGRAM_ENV):
print("Error: The .env file does not exist. Please start the bot first.")
sys.exit(1)
with open(TELEGRAM_ENV, 'r') as f:
lines = f.readlines()
with open(TELEGRAM_ENV, 'w') as f:
found = False
for line in lines:
if line.strip().startswith("BACKUP_INTERVAL_HOUR"):
f.write(f"BACKUP_INTERVAL_HOUR={backup_interval}\n")
found = True
else:
f.write(line)
if not found:
f.write(f"BACKUP_INTERVAL_HOUR={backup_interval}\n")
print(f"Backup interval has been set to {backup_interval} hour(s). Restarting the bot to apply changes...")
subprocess.run(["systemctl", "restart", "hysteria-telegram-bot.service"])
def print_usage():
print("Usage:")
print(" python3 runbot.py start <API_TOKEN> <ADMIN_USER_IDS> [BACKUP_INTERVAL_HOUR]")
print(" python3 runbot.py stop")
print(" python3 runbot.py set_backup_interval <BACKUP_INTERVAL_HOUR>")
print("\nDefault BACKUP_INTERVAL_HOUR is 12.")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 runbot.py {start|stop} <API_TOKEN> <ADMIN_USER_IDS>")
sys.exit(1)
print_usage()
action = sys.argv[1]
if action == "start":
if len(sys.argv) != 4:
print("Usage: python3 runbot.py start <API_TOKEN> <ADMIN_USER_IDS>")
sys.exit(1)
start_service(sys.argv[2], sys.argv[3])
if not (4 <= len(sys.argv) <= 5):
print_usage()
api_token = sys.argv[2]
admin_user_ids = sys.argv[3]
if len(sys.argv) == 5:
backup_interval = sys.argv[4]
start_service(api_token, admin_user_ids, backup_interval)
else:
start_service(api_token, admin_user_ids)
elif action == "stop":
stop_service()
elif action == "set_backup_interval":
if len(sys.argv) != 3:
print_usage()
set_backup_interval(sys.argv[2])
else:
print("Usage: python3 runbot.py {start|stop} <API_TOKEN> <ADMIN_USER_IDS>")
sys.exit(1)
print_usage()