mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-02-05 23:50:37 +00:00
refactor: Simplify postgre dumps
This commit is contained in:
parent
0d5038cd41
commit
914b392b1d
|
@ -10,30 +10,19 @@ class PostgresDumper:
|
|||
self.socket_dir = r"%2Frun%2Fpostgresql"
|
||||
|
||||
def backup_database(self, backup_file: str):
|
||||
# Create the database dump and pipe it to gzip
|
||||
# Create the database dump in custom format
|
||||
dump_command = [
|
||||
"pg_dump",
|
||||
f"--dbname=postgresql://{self.user}@{self.socket_dir}/{self.db_name}",
|
||||
"--format=custom",
|
||||
f"--file={backup_file}",
|
||||
]
|
||||
gzip_command = ["gzip", "--rsyncable"]
|
||||
|
||||
with open(backup_file, "wb") as f_out:
|
||||
dump_process = subprocess.Popen(dump_command, stdout=subprocess.PIPE)
|
||||
gzip_process = subprocess.Popen(
|
||||
gzip_command, stdin=dump_process.stdout, stdout=f_out
|
||||
)
|
||||
dump_process.stdout.close() # Allow dump_process to receive a SIGPIPE if gzip_process exits
|
||||
gzip_process.communicate()
|
||||
subprocess.run(dump_command, check=True)
|
||||
|
||||
return backup_file
|
||||
|
||||
def restore_database(self, backup_file: str):
|
||||
# Decompress the backup file
|
||||
gunzip_command = ["gunzip", backup_file]
|
||||
subprocess.run(gunzip_command, check=True)
|
||||
|
||||
# Restore the database from the decompressed file
|
||||
dump_file = backup_file.replace(".gz", "")
|
||||
restore_command = [
|
||||
"pg_restore",
|
||||
"--dbname=postgresql://{}@{}/{}".format(
|
||||
|
@ -41,6 +30,6 @@ class PostgresDumper:
|
|||
),
|
||||
"--clean",
|
||||
"--create",
|
||||
dump_file,
|
||||
backup_file,
|
||||
]
|
||||
subprocess.run(restore_command, check=True)
|
||||
|
|
|
@ -495,14 +495,14 @@ class TemplatedService(Service):
|
|||
status=JobStatus.RUNNING,
|
||||
)
|
||||
db_dumper = PostgresDumper(db_name)
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.sql.gz")
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.dump")
|
||||
logger.warning(f"Pre backup: backup_file: {backup_file}")
|
||||
db_dumper.backup_database(backup_file)
|
||||
|
||||
def _clear_db_dumps(self):
|
||||
db_dumps_folder = self._get_db_dumps_folder()
|
||||
for db_name in self.get_postgresql_databases():
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.sql.gz")
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.dump")
|
||||
if exists(backup_file):
|
||||
remove(backup_file)
|
||||
unpacked_file = backup_file.replace(".gz", "")
|
||||
|
@ -514,7 +514,7 @@ class TemplatedService(Service):
|
|||
db_dumps_folder = self._get_db_dumps_folder()
|
||||
# Remove the backup files
|
||||
for db_name in self.get_postgresql_databases():
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.sql.gz")
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.dump")
|
||||
if exists(backup_file):
|
||||
remove(backup_file)
|
||||
|
||||
|
@ -532,7 +532,7 @@ class TemplatedService(Service):
|
|||
# Recover the databases
|
||||
db_dumps_folder = self._get_db_dumps_folder()
|
||||
for db_name in self.get_postgresql_databases():
|
||||
if exists(join(db_dumps_folder, f"{db_name}.sql.gz")):
|
||||
if exists(join(db_dumps_folder, f"{db_name}.dump")):
|
||||
if job is not None:
|
||||
Jobs.update(
|
||||
job,
|
||||
|
@ -540,7 +540,7 @@ class TemplatedService(Service):
|
|||
status=JobStatus.RUNNING,
|
||||
)
|
||||
db_dumper = PostgresDumper(db_name)
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.sql.gz")
|
||||
backup_file = join(db_dumps_folder, f"{db_name}.dump")
|
||||
db_dumper.restore_database(backup_file)
|
||||
else:
|
||||
logger.error(f"Database dump for {db_name} not found")
|
||||
|
|
Loading…
Reference in a new issue