fix: selfprivacy API service couldn't backup

This commit is contained in:
Inex Code 2024-12-24 16:19:54 +03:00
parent 1f343815eb
commit 0163efae58
No known key found for this signature in database
2 changed files with 18 additions and 14 deletions

View file

@ -14,6 +14,7 @@ from functools import lru_cache
from shutil import copyfile, copytree, rmtree from shutil import copyfile, copytree, rmtree
from selfprivacy_api.jobs import Job, JobStatus, Jobs
from selfprivacy_api.services.prometheus import Prometheus from selfprivacy_api.services.prometheus import Prometheus
from selfprivacy_api.services.mailserver import MailServer from selfprivacy_api.services.mailserver import MailServer
@ -240,7 +241,12 @@ class ServiceManager(Service):
copyfile(cls.stash_for(p), p) copyfile(cls.stash_for(p), p)
@classmethod @classmethod
def pre_backup(cls): def pre_backup(cls, job: Job):
Jobs.update(
job,
status_text="Stashing settings",
status=JobStatus.RUNNING,
)
tempdir = cls.dump_dir() tempdir = cls.dump_dir()
rmtree(join(tempdir), ignore_errors=True) rmtree(join(tempdir), ignore_errors=True)
makedirs(tempdir) makedirs(tempdir)
@ -249,7 +255,7 @@ class ServiceManager(Service):
cls.stash_a_path(p) cls.stash_a_path(p)
@classmethod @classmethod
def post_backup(cls): def post_backup(cls, job: Job):
rmtree(cls.dump_dir(), ignore_errors=True) rmtree(cls.dump_dir(), ignore_errors=True)
@classmethod @classmethod

View file

@ -456,12 +456,11 @@ class TemplatedService(Service):
mkdir(db_dumps_folder) mkdir(db_dumps_folder)
# Dump the databases # Dump the databases
for db_name in self.get_postgresql_databases(): for db_name in self.get_postgresql_databases():
if job is not None: Jobs.update(
Jobs.update( job,
job, status_text=f"Creating a dump of database {db_name}",
status_text=f"Creating a dump of database {db_name}", status=JobStatus.RUNNING,
status=JobStatus.RUNNING, )
)
db_dumper = PostgresDumper(db_name) db_dumper = PostgresDumper(db_name)
backup_file = join(db_dumps_folder, f"{db_name}.dump") backup_file = join(db_dumps_folder, f"{db_name}.dump")
db_dumper.backup_database(backup_file) db_dumper.backup_database(backup_file)
@ -500,12 +499,11 @@ class TemplatedService(Service):
db_dumps_folder = self._get_db_dumps_folder() db_dumps_folder = self._get_db_dumps_folder()
for db_name in self.get_postgresql_databases(): for db_name in self.get_postgresql_databases():
if exists(join(db_dumps_folder, f"{db_name}.dump")): if exists(join(db_dumps_folder, f"{db_name}.dump")):
if job is not None: Jobs.update(
Jobs.update( job,
job, status_text=f"Restoring database {db_name}",
status_text=f"Restoring database {db_name}", status=JobStatus.RUNNING,
status=JobStatus.RUNNING, )
)
db_dumper = PostgresDumper(db_name) db_dumper = PostgresDumper(db_name)
backup_file = join(db_dumps_folder, f"{db_name}.dump") backup_file = join(db_dumps_folder, f"{db_name}.dump")
db_dumper.restore_database(backup_file) db_dumper.restore_database(backup_file)