Fix pipeline attempt (6): Backup

This commit is contained in:
Kylian Schmidt
2025-09-08 15:59:43 +02:00
parent 620db78ee7
commit 375fe36902
2 changed files with 27 additions and 28 deletions
+4 -12
View File
@@ -16,22 +16,14 @@ def test_backup_creates_zip(tmp_path, monkeypatch):
monkeypatch.setattr(backup, 'WEB_FOLDER', web_folder) monkeypatch.setattr(backup, 'WEB_FOLDER', web_folder)
monkeypatch.setattr(backup, 'BACKUP_FOLDER', backup_folder) monkeypatch.setattr(backup, 'BACKUP_FOLDER', backup_folder)
# Manually execute the backup logic # Call the backup function
backup.create_backup()
# Check that backup was created
today = datetime.date.today().strftime('%Y%m%d') today = datetime.date.today().strftime('%Y%m%d')
backup_name = f'backup-{today}.zip' backup_name = f'backup-{today}.zip'
backup_path = backup_folder / backup_name backup_path = backup_folder / backup_name
# Remove if exists
if backup_path.exists():
backup_path.unlink()
# Create backup manually using the backup module's logic
with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf:
for path in web_folder.rglob("*"):
if path.is_file():
arcname = path.relative_to(web_folder.parent)
zipf.write(path, arcname)
assert backup_path.exists() assert backup_path.exists()
with zipfile.ZipFile(backup_path, 'r') as z: with zipfile.ZipFile(backup_path, 'r') as z:
names = z.namelist() names = z.namelist()
+21 -14
View File
@@ -5,19 +5,26 @@ from pathlib import Path
WEB_FOLDER = Path("plots") WEB_FOLDER = Path("plots")
BACKUP_FOLDER = Path("backups") BACKUP_FOLDER = Path("backups")
today = datetime.date.today().strftime("%Y%m%d")
backup_name = f"backup-{today}.zip"
backup_path = BACKUP_FOLDER / backup_name
BACKUP_FOLDER.mkdir(parents=True, exist_ok=True) def create_backup():
"""Create a backup of the web folder."""
today = datetime.date.today().strftime("%Y%m%d")
backup_name = f"backup-{today}.zip"
backup_path = BACKUP_FOLDER / backup_name
if backup_path.exists(): BACKUP_FOLDER.mkdir(parents=True, exist_ok=True)
print(f"Backup already exists: {backup_path}")
else: if backup_path.exists():
print(f"Creating backup: {backup_path}") print(f"Backup already exists: {backup_path}")
with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf: else:
for path in WEB_FOLDER.rglob("*"): print(f"Creating backup: {backup_path}")
if path.is_file(): with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf:
arcname = path.relative_to(WEB_FOLDER.parent) for path in WEB_FOLDER.rglob("*"):
zipf.write(path, arcname) if path.is_file():
print("✅ Backup complete.") arcname = path.relative_to(WEB_FOLDER.parent)
zipf.write(path, arcname)
print("✅ Backup complete.")
if __name__ == "__main__":
create_backup()