selfprivacy-rest-api/selfprivacy_api/backup/postgres.py
2024-12-23 21:59:23 +03:00

36 lines
1,014 B
Python

import subprocess
class PostgresDumper:
"""--dbname=postgresql://postgres@%2Frun%2Fpostgresql/pleroma"""
def __init__(self, db_name: str):
self.db_name = db_name
self.user = "postgres"
self.socket_dir = r"%2Frun%2Fpostgresql"
def backup_database(self, backup_file: str):
# 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}",
]
subprocess.run(dump_command, check=True)
return backup_file
def restore_database(self, backup_file: str):
restore_command = [
"pg_restore",
"--dbname=postgresql://{}@{}/{}".format(
self.user, self.socket_dir, self.db_name
),
"--clean",
"--create",
backup_file,
]
subprocess.run(restore_command, check=True)