Refactor: Implement Hysteria Bakcup in Python
This commit is contained in:
27
core/scripts/hysteria2/backup.py
Normal file
27
core/scripts/hysteria2/backup.py
Normal file
@ -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))
|
||||
@ -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
|
||||
Reference in New Issue
Block a user