schon/evibes/settings/dbbackup.py
Egor fureunoir Gorbunov e373f28358 Features: 1) Introduced AbsoluteFTPStorage class for path customization in FTP storage; 2) Updated dbbackup settings to use AbsoluteFTPStorage for FTP storage configurations.
Fixes: 1) Corrected `DBBACKUP_PATH` default to ensure proper backup directory structure.

Extra: 1) Minor cleanup in FTP storage configuration handling.
2025-08-05 21:08:32 +03:00

55 lines
1.9 KiB
Python

from os import getenv
from django.core.exceptions import ImproperlyConfigured
DBBACKUP_CONNECTORS = {
"default": {
"SINGLE_TRANSACTION": False,
"IF_EXISTS": True,
"RESTORE_SUFFIX": "--set ON_ERROR_STOP=off",
}
}
if getenv("DBBACKUP_HOST") and getenv("DBBACKUP_USER") and getenv("DBBACKUP_PASS"):
dbbackup_server_type = getenv("DBBACKUP_TYPE", "sftp")
project_name = getenv("EVIBES_PROJECT_NAME", "evibes_common").lower().replace(" ", "_")
raw_path = getenv("DBBACKUP_PATH", f"/backups/{project_name}/")
cleaned = raw_path.strip("/")
remote_dir = f"{cleaned}/"
match dbbackup_server_type:
case "sftp":
DBBACKUP_STORAGE = "storages.backends.sftpstorage.SFTPStorage"
DBBACKUP_STORAGE_OPTIONS = {
"host": getenv("DBBACKUP_HOST"),
"root_path": f"/{remote_dir}",
"params": {
"username": getenv("DBBACKUP_USER"),
"password": getenv("DBBACKUP_PASS"),
"allow_agent": False,
"look_for_keys": False,
},
"interactive": False,
"file_mode": 0o600,
"dir_mode": 0o700,
}
case "ftp":
DBBACKUP_STORAGE = "evibes.ftpstorage.AbsoluteFTPStorage"
DBBACKUP_STORAGE_OPTIONS = {
"location": (
f"ftp://{getenv('DBBACKUP_USER')}:"
f"{getenv('DBBACKUP_PASS')}@"
f"{getenv('DBBACKUP_HOST')}:21/"
f"{raw_path}"
),
}
case _:
raise ImproperlyConfigured(f"Invalid DBBACKUP_TYPE: {dbbackup_server_type}")
else:
DBBACKUP_STORAGE = "django.core.files.storage.FileSystemStorage"
DBBACKUP_STORAGE_OPTIONS = {
"location": "/app/backups/",
}