ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Restore app backups into the requested path
Juan Denisfe8920e8131c
diff --git a/backend/app/services/backup_service.py b/backend/app/services/backup_service.pyindex 0db91b83..07c71cb2 100644--- a/backend/app/services/backup_service.py+++ b/backend/app/services/backup_service.py@@ -406,15 +406,35 @@ class BackupService:if not restore_path:restore_path = f"/var/www/{backup_info['app_name']}"- # Backup existing if present- if os.path.exists(restore_path):- timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')- backup_existing = f"{restore_path}.backup_{timestamp}"- shutil.move(restore_path, backup_existing)-- # Extract backup (filter='data' blocks path traversal in archives)- with tarfile.open(files_backup, 'r:gz') as tar:- tar.extractall(os.path.dirname(restore_path), filter='data')+ # The archive holds one top-level dir named after the app's+ # ORIGINAL directory, so extract beside the target and move that+ # dir into place — extracting straight into the parent restored+ # into the old name whenever restore_path was a different one.+ parent = os.path.dirname(os.path.abspath(restore_path))+ os.makedirs(parent, exist_ok=True)+ staging = tempfile.mkdtemp(prefix='.restore-', dir=parent)+ backup_existing = None+ try:+ # filter='data' blocks path traversal in archives+ with tarfile.open(files_backup, 'r:gz') as tar:+ tar.extractall(staging, filter='data')+ entries = os.listdir(staging)+ if len(entries) != 1 or not os.path.isdir(os.path.join(staging, entries[0])):+ return {'success': False,+ 'error': 'Invalid backup: expected one top-level directory'}++ if os.path.exists(restore_path):+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')+ backup_existing = f"{restore_path}.backup_{timestamp}"+ shutil.move(restore_path, backup_existing)+ try:+ shutil.move(os.path.join(staging, entries[0]), restore_path)+ except Exception:+ if backup_existing:+ shutil.move(backup_existing, restore_path)+ raise+ finally:+ shutil.rmtree(staging, ignore_errors=True)return {'success': True,diff --git a/backend/tests/test_backup_restore_application.py b/backend/tests/test_backup_restore_application.pynew file mode 100644index 00000000..e3d6c578--- /dev/null+++ b/backend/tests/test_backup_restore_application.py@@ -0,0 +1,71 @@+"""restore_application must put the files at restore_path — not at the app's+original directory name next to it — and must never lose the live copy."""+import json+import os+import tarfile++from app.services.backup_service import BackupService+++def _make_backup(tmp_path, app_dir_name='myapp', files=None):+ src = tmp_path / 'src' / app_dir_name+ src.mkdir(parents=True)+ for name, body in (files or {'index.html': 'v1'}).items():+ (src / name).write_text(body)+ bdir = tmp_path / 'bk'+ bdir.mkdir()+ archive = bdir / 'files.tar.gz'+ with tarfile.open(archive, 'w:gz') as tar:+ tar.add(src, arcname=app_dir_name)+ (bdir / 'backup.json').write_text(json.dumps(+ {'files_backup': str(archive), 'app_name': app_dir_name}))+ return bdir+++def test_restores_into_a_custom_path(tmp_path):+ bdir = _make_backup(tmp_path)+ target = tmp_path / 'www' / 'restored'+ target.mkdir(parents=True)+ (target / 'live.txt').write_text('live')++ result = BackupService.restore_application(str(bdir), str(target))++ assert result['success'], result+ assert (target / 'index.html').read_text() == 'v1'+ assert not (target / 'live.txt').exists()+ assert not (tmp_path / 'www' / 'myapp').exists()+ previous = [p for p in os.listdir(tmp_path / 'www') if p.startswith('restored.backup_')]+ assert len(previous) == 1+ assert (tmp_path / 'www' / previous[0] / 'live.txt').read_text() == 'live'+ assert not [p for p in os.listdir(tmp_path / 'www') if p.startswith('.restore-')]+++def test_restores_into_a_path_that_does_not_exist_yet(tmp_path):+ bdir = _make_backup(tmp_path)+ target = tmp_path / 'www' / 'fresh'++ result = BackupService.restore_application(str(bdir), str(target))++ assert result['success'], result+ assert (target / 'index.html').read_text() == 'v1'+++def test_a_bad_archive_leaves_the_live_copy_in_place(tmp_path):+ bdir = tmp_path / 'bk'+ bdir.mkdir()+ archive = bdir / 'files.tar.gz'+ loose = tmp_path / 'loose.txt'+ loose.write_text('x')+ with tarfile.open(archive, 'w:gz') as tar:+ tar.add(loose, arcname='loose.txt') # no top-level directory+ (bdir / 'backup.json').write_text(json.dumps(+ {'files_backup': str(archive), 'app_name': 'myapp'}))+ target = tmp_path / 'www' / 'site'+ target.mkdir(parents=True)+ (target / 'live.txt').write_text('live')++ result = BackupService.restore_application(str(bdir), str(target))++ assert not result['success']+ assert (target / 'live.txt').read_text() == 'live'+ assert os.listdir(tmp_path / 'www') == ['site']