Features: 1) Add dynamic project-based remote_dir path generation for backup storage; 2) Support for EVIBES_PROJECT_NAME environment variable in backup paths;

Fixes: 1) Correct root backup path for `FileSystemStorage`;

Extra: 1) Remove unnecessary blank line; 2) Improve code readability with formatted strings and variable standardization; 3) Minor cleanup of backup storage configurations;
This commit is contained in:
Egor Pavlovich Gorbunov 2025-08-05 20:50:21 +03:00
parent 877f71f614
commit 2cbea05ea7

View file

@ -1,5 +1,4 @@
from os import getenv from os import getenv
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
DBBACKUP_CONNECTORS = { DBBACKUP_CONNECTORS = {
@ -12,12 +11,18 @@ DBBACKUP_CONNECTORS = {
if getenv("DBBACKUP_HOST") and getenv("DBBACKUP_USER") and getenv("DBBACKUP_PASS"): if getenv("DBBACKUP_HOST") and getenv("DBBACKUP_USER") and getenv("DBBACKUP_PASS"):
dbbackup_server_type = getenv("DBBACKUP_TYPE", "sftp") 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: match dbbackup_server_type:
case "sftp": case "sftp":
DBBACKUP_STORAGE = "storages.backends.sftpstorage.SFTPStorage" DBBACKUP_STORAGE = "storages.backends.sftpstorage.SFTPStorage"
DBBACKUP_STORAGE_OPTIONS = { DBBACKUP_STORAGE_OPTIONS = {
"host": getenv("DBBACKUP_HOST"), "host": getenv("DBBACKUP_HOST"),
"root_path": "/db_backups/", "root_path": f"/{remote_dir}",
"params": { "params": {
"username": getenv("DBBACKUP_USER"), "username": getenv("DBBACKUP_USER"),
"password": getenv("DBBACKUP_PASS"), "password": getenv("DBBACKUP_PASS"),
@ -28,13 +33,23 @@ if getenv("DBBACKUP_HOST") and getenv("DBBACKUP_USER") and getenv("DBBACKUP_PASS
"file_mode": 0o600, "file_mode": 0o600,
"dir_mode": 0o700, "dir_mode": 0o700,
} }
case "ftp": case "ftp":
DBBACKUP_STORAGE = "storages.backends.ftp.FTPStorage" DBBACKUP_STORAGE = "storages.backends.ftp.FTPStorage"
DBBACKUP_STORAGE_OPTIONS = { DBBACKUP_STORAGE_OPTIONS = {
"location": f"ftp://{getenv('DBBACKUP_USER')}:{getenv('DBBACKUP_PASS')}@{getenv('DBBACKUP_HOST')}:21", "location": (
f"ftp://{getenv('DBBACKUP_USER')}:"
f"{getenv('DBBACKUP_PASS')}@"
f"{getenv('DBBACKUP_HOST')}:21/"
f"{remote_dir}"
),
} }
case _: case _:
raise ImproperlyConfigured(f"Invalid DBBACKUP_TYPE: {dbbackup_server_type}") raise ImproperlyConfigured(f"Invalid DBBACKUP_TYPE: {dbbackup_server_type}")
else: else:
DBBACKUP_STORAGE = "django.core.files.storage.FileSystemStorage" DBBACKUP_STORAGE = "django.core.files.storage.FileSystemStorage"
DBBACKUP_STORAGE_OPTIONS = {"location": "/app/db_backups/"} DBBACKUP_STORAGE_OPTIONS = {
"location": "/app/backups/",
}