diff --git a/core/cli_api.py b/core/cli_api.py index b4dc9b1..428c491 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -36,7 +36,7 @@ class Command(Enum): UPDATE_GEO = os.path.join(SCRIPT_DIR, 'hysteria2', 'update_geo.py') LIST_USERS = os.path.join(SCRIPT_DIR, 'hysteria2', 'list_users.sh') SERVER_INFO = os.path.join(SCRIPT_DIR, 'hysteria2', 'server_info.sh') - BACKUP_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'backup.sh') + BACKUP_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'backup.py') RESTORE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restore.sh') INSTALL_TELEGRAMBOT = os.path.join(SCRIPT_DIR, 'telegrambot', 'runbot.py') SHELL_SINGBOX = os.path.join(SCRIPT_DIR, 'singbox', 'singbox_shell.sh') @@ -182,7 +182,7 @@ def change_hysteria2_sni(sni: str): def backup_hysteria2(): '''Backups Hysteria configuration. Raises an exception on failure.''' try: - run_cmd(['bash', Command.BACKUP_HYSTERIA2.value]) + run_cmd(['python3', Command.BACKUP_HYSTERIA2.value]) except subprocess.CalledProcessError as e: raise Exception(f"Backup failed: {e}") except Exception as ex: diff --git a/core/scripts/hysteria2/backup.py b/core/scripts/hysteria2/backup.py new file mode 100644 index 0000000..01dd60f --- /dev/null +++ b/core/scripts/hysteria2/backup.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import zipfile +from pathlib import Path +from datetime import datetime + +backup_dir = Path("/opt/hysbackup") +backup_file = backup_dir / f"hysteria_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip" + +files_to_backup = [ + Path("/etc/hysteria/ca.key"), + Path("/etc/hysteria/ca.crt"), + Path("/etc/hysteria/users.json"), + Path("/etc/hysteria/config.json"), + Path("/etc/hysteria/.configs.env"), +] + +backup_dir.mkdir(parents=True, exist_ok=True) + +try: + with zipfile.ZipFile(backup_file, 'w') as zipf: + for file_path in files_to_backup: + if file_path.exists(): + zipf.write(file_path, arcname=file_path.name) + print("Backup successfully created") +except Exception as e: + print("Backup failed!", str(e)) diff --git a/core/scripts/hysteria2/backup.sh b/core/scripts/hysteria2/backup.sh deleted file mode 100644 index fda800f..0000000 --- a/core/scripts/hysteria2/backup.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -BACKUP_DIR="/opt/hysbackup" -BACKUP_FILE="$BACKUP_DIR/hysteria_backup_$(date +%Y%m%d_%H%M%S).zip" - -if [ ! -d "$BACKUP_DIR" ]; then - mkdir -p "$BACKUP_DIR" -fi - -FILES_TO_BACKUP=( - "/etc/hysteria/ca.key" - "/etc/hysteria/ca.crt" - "/etc/hysteria/users.json" - "/etc/hysteria/config.json" - "/etc/hysteria/.configs.env" -) - -zip -j "$BACKUP_FILE" "${FILES_TO_BACKUP[@]}" >/dev/null - -if [ $? -eq 0 ]; then - echo "Backup successfully created" -else - echo "Backup failed!" -fi