feat: migrate user management from json to mongodb
This commit is contained in:
@ -1,27 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import zipfile
|
||||
import subprocess
|
||||
import shutil
|
||||
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"
|
||||
# --- Configuration ---
|
||||
DB_NAME = "blitz_panel"
|
||||
BACKUP_ROOT_DIR = Path("/opt/hysbackup")
|
||||
TIMESTAMP = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
BACKUP_FILENAME = BACKUP_ROOT_DIR / f"hysteria_backup_{TIMESTAMP}.zip"
|
||||
TEMP_DUMP_DIR = BACKUP_ROOT_DIR / f"mongodump_{TIMESTAMP}"
|
||||
|
||||
files_to_backup = [
|
||||
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)
|
||||
def create_backup():
|
||||
"""Dumps the MongoDB database and zips it with config files."""
|
||||
try:
|
||||
BACKUP_ROOT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
TEMP_DUMP_DIR.mkdir(parents=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))
|
||||
print(f"Dumping database '{DB_NAME}'...")
|
||||
mongodump_cmd = [
|
||||
"mongodump",
|
||||
f"--db={DB_NAME}",
|
||||
f"--out={TEMP_DUMP_DIR}"
|
||||
]
|
||||
subprocess.run(mongodump_cmd, check=True, capture_output=True)
|
||||
print("Database dump successful.")
|
||||
|
||||
print(f"Creating backup archive: {BACKUP_FILENAME}")
|
||||
with zipfile.ZipFile(BACKUP_FILENAME, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for file_path in FILES_TO_BACKUP:
|
||||
if file_path.exists() and file_path.is_file():
|
||||
zipf.write(file_path, arcname=file_path.name)
|
||||
print(f" - Added {file_path.name}")
|
||||
else:
|
||||
print(f" - Warning: Skipping missing file {file_path}")
|
||||
|
||||
dump_content_path = TEMP_DUMP_DIR / DB_NAME
|
||||
if dump_content_path.exists():
|
||||
for file_path in dump_content_path.rglob('*'):
|
||||
arcname = file_path.relative_to(TEMP_DUMP_DIR)
|
||||
zipf.write(file_path, arcname=arcname)
|
||||
print(f" - Added database dump for '{DB_NAME}'")
|
||||
|
||||
print("\nBackup successfully created.")
|
||||
|
||||
except FileNotFoundError:
|
||||
print("\nBackup failed! 'mongodump' command not found. Is MongoDB installed and in your PATH?")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print("\nBackup failed! Error during mongodump.")
|
||||
print(f" - Stderr: {e.stderr.decode().strip()}")
|
||||
except Exception as e:
|
||||
print(f"\nBackup failed! An unexpected error occurred: {e}")
|
||||
finally:
|
||||
if TEMP_DUMP_DIR.exists():
|
||||
shutil.rmtree(TEMP_DUMP_DIR)
|
||||
print("Temporary dump directory cleaned up.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_backup()
|
||||
Reference in New Issue
Block a user