Skip to content

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.

Fail scheduled backups whose offsite upload fails

Juan Denis5fb35e8cc036
diff --git a/backend/app/services/backup_service.py b/backend/app/services/backup_service.py
index 07c71cb2..6fd8c6c4 100644
--- a/backend/app/services/backup_service.py
+++ b/backend/app/services/backup_service.py
@@ -1,5 +1,6 @@
import os
import json
+import logging
import shutil
import tarfile
import tempfile
@@ -21,6 +22,8 @@ from app.utils.system import (
from app.services.telemetry_service import TelemetryService, generate_correlation_id
from app.services import backup_schedule_service
+logger = logging.getLogger(__name__)
+
# Unified job kind for asynchronous scheduled backups (see register_jobs).
BACKUP_JOB_KIND = 'backup.run'
@@ -818,8 +821,11 @@ class BackupService:
if meta_path and os.path.exists(meta_path):
with open(meta_path, 'w') as f:
json.dump(backup_info, f, indent=2)
+ else:
+ # The row stays 'local' with its upload button; say why.
+ logger.warning('Auto-upload of %s failed: %s', backup_path, result.get('error'))
except Exception:
- pass
+ logger.exception('Auto-upload of %s failed', backup_path)
# --- Smart backups (incremental + compression tiering) ---
@@ -1016,18 +1022,10 @@ class BackupService:
paths_list = [p.strip() for p in target.split(',') if p.strip()]
result = cls.backup_files(paths_list, backup_name=f"scheduled_{sched.get('name', 'backup')}")
- # Upload to remote if configured on this schedule
+ # Upload to remote if configured on this schedule. The operator
+ # asked for an offsite copy, so not getting one is a failure.
if result and result.get('success') and sched.get('upload_remote', False):
- try:
- from app.services.storage_provider_service import StorageProviderService
- backup_path = result.get('path') or result.get('backup', {}).get('path')
- if backup_path:
- if os.path.isdir(backup_path):
- StorageProviderService.upload_directory(backup_path)
- else:
- StorageProviderService.upload_file(backup_path)
- except Exception:
- pass
+ result = cls._upload_scheduled(result)
# Update schedule status
config = cls.get_config()
@@ -1058,6 +1056,22 @@ class BackupService:
cls.save_config(config)
cls._send_backup_notification(sched.get('name', 'Backup'), False, str(e), correlation_id=correlation_id)
+ @staticmethod
+ def _upload_scheduled(result: Dict) -> Dict:
+ from app.services.storage_provider_service import StorageProviderService
+ backup_path = result.get('path') or result.get('backup', {}).get('path')
+ if not backup_path:
+ upload = {'success': False, 'error': 'the backup reported no path'}
+ elif os.path.isdir(backup_path):
+ upload = StorageProviderService.upload_directory(backup_path)
+ else:
+ upload = StorageProviderService.upload_file(backup_path)
+ if upload.get('success'):
+ return result
+ return {**result, 'success': False,
+ 'error': f"Saved locally at {backup_path}, but the offsite upload "
+ f"failed: {upload.get('error', 'unknown error')}"}
+
@classmethod
def _send_backup_notification(cls, backup_name: str, success: bool, message: str,
correlation_id: str = None) -> None:
diff --git a/backend/tests/test_backup_offsite_failure.py b/backend/tests/test_backup_offsite_failure.py
new file mode 100644
index 00000000..c377cb9e
--- /dev/null
+++ b/backend/tests/test_backup_offsite_failure.py
@@ -0,0 +1,49 @@
+"""A scheduled backup that was told to go offsite and didn't is a failure.
+
+The upload helpers report failure in their return value, which the
+scheduler ignored, so the schedule read 'success' and no "backup failed"
+notification went out while the only copy sat on the box being backed up.
+"""
+import pytest
+
+from app.services.backup_service import BackupService
+from app.services.storage_provider_service import StorageProviderService
+
+SCHED = {'id': 's1', 'name': 'Nightly', 'backup_type': 'files',
+ 'target': '/srv/data', 'upload_remote': True}
+
+
+def run(app, tmp_path, monkeypatch):
+ archive = tmp_path / 'files.tar.gz'
+ archive.write_bytes(b'x')
+ config = {'schedules': [dict(SCHED)]}
+ notified = []
+ monkeypatch.setattr(BackupService, 'get_config', classmethod(lambda cls: config))
+ monkeypatch.setattr(BackupService, 'save_config', classmethod(lambda cls, c: c))
+ monkeypatch.setattr(BackupService, 'backup_files', classmethod(
+ lambda cls, paths, backup_name=None: {'success': True, 'path': str(archive)}))
+ monkeypatch.setattr(BackupService, '_send_backup_notification', classmethod(
+ lambda cls, name, ok, message, correlation_id=None: notified.append((ok, message))))
+
+ def go(upload_result):
+ monkeypatch.setattr(StorageProviderService, 'upload_file',
+ classmethod(lambda cls, path, remote_key=None: upload_result))
+ BackupService._run_scheduled_backup(dict(SCHED))
+ return config['schedules'][0]['last_status'], notified
+
+ return go
+
+
+def test_failed_offsite_upload_fails_the_backup_and_notifies(run):
+ status, notified = run({'success': False, 'error': 'AccessDenied'})
+ assert status == 'failed'
+ assert len(notified) == 1 and notified[0][0] is False
+ assert 'offsite upload failed: AccessDenied' in notified[0][1]
+ assert 'Saved locally at' in notified[0][1]
+
+
+def test_successful_offsite_upload_stays_a_success(run):
+ status, notified = run({'success': True, 'remote_key': 'k'})
+ assert status == 'success'
+ assert notified == []