Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
revert but keep binary read
Browse files Browse the repository at this point in the history
  • Loading branch information
Heavybullets8 committed May 28, 2024
1 parent f8e00f2 commit 1ee9329
Showing 1 changed file with 5 additions and 102 deletions.
107 changes: 5 additions & 102 deletions functions/backup_restore/database/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,17 @@ def restore(self, timeout=300, interval=5) -> Dict[str, str]:
if not self.primary_pod:
message = "Primary pod not found."
self.logger.error(message)
result["message"] = message

if was_stopped:
self.logger.debug(f"Stopping app {self.app_name} after restore failure.")
self.stop_app(self.app_name)

result["message"] = message
return result

try:
# Terminate active connections and drop the database
drop_database_result = self._drop_and_recreate_database()
if not drop_database_result["success"]:
result["message"] = drop_database_result["message"]
self.logger.error(result["message"])
return result
self.command, self.open_mode = self._get_restore_command()

# Restore the database from the backup file
try:
result = self._execute_restore_command()

if not result["success"]:
Expand Down Expand Up @@ -145,102 +139,13 @@ def _get_restore_command(self) -> Tuple[str, str]:
"--if-exists",
"--no-owner",
"--no-privileges",
"--disable-triggers"
"--single-transaction"
]
open_mode = 'rb'

self.logger.debug(f"Restore command for app {self.app_name}: {command}")
return command, open_mode

def _drop_and_recreate_database(self) -> Dict[str, str]:
"""
Terminate active connections, drop the database, and recreate it.
Returns:
dict: Result containing status and message.
"""
result = {
"success": False,
"message": ""
}

terminate_connections_command = [
"k3s", "kubectl", "exec",
"--namespace", self.namespace,
"--stdin",
"--container", "postgres",
self.primary_pod,
"--",
"psql",
"--dbname", "postgres",
"--command",
f"""
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '{self.database_name}'
AND pid <> pg_backend_pid();
"""
]

drop_database_command = [
"k3s", "kubectl", "exec",
"--namespace", self.namespace,
"--stdin",
"--container", "postgres",
self.primary_pod,
"--",
"psql",
"--dbname", "postgres",
"--command",
f"DROP DATABASE IF EXISTS {self.database_name};"
]

create_database_command = [
"k3s", "kubectl", "exec",
"--namespace", self.namespace,
"--stdin",
"--container", "postgres",
self.primary_pod,
"--",
"psql",
"--dbname", "postgres",
"--command",
f"CREATE DATABASE {self.database_name} OWNER {self.database_user};"
]

try:
# Terminate active connections
terminate_process = subprocess.run(terminate_connections_command, capture_output=True, text=True)
if terminate_process.returncode != 0:
result["message"] = f"Failed to terminate active connections: {terminate_process.stderr}"
self.logger.error(result["message"])
return result

# Drop the database
drop_process = subprocess.run(drop_database_command, capture_output=True, text=True)
if drop_process.returncode != 0:
result["message"] = f"Failed to drop the database: {drop_process.stderr}"
self.logger.error(result["message"])
return result

# Recreate the database
create_process = subprocess.run(create_database_command, capture_output=True, text=True)
if create_process.returncode != 0:
result["message"] = f"Failed to recreate the database: {create_process.stderr}"
self.logger.error(result["message"])
return result

result["success"] = True
result["message"] = "Database dropped and recreated successfully."
self.logger.debug(result["message"])

except Exception as e:
message = f"Failed to drop and recreate database: {e}"
self.logger.error(message, exc_info=True)
result["message"] = message

return result

def _execute_restore_command(self, retries=3, wait=5) -> Dict[str, str]:
"""
Execute the restore command on the primary pod with retry logic in case of deadlock.
Expand All @@ -258,8 +163,6 @@ def _execute_restore_command(self, retries=3, wait=5) -> Dict[str, str]:
}
self.logger.debug(f"Executing restore command on pod: {self.primary_pod} with dump file: {self.backup_file}")

self.command, self.open_mode = self._get_restore_command()

for attempt in range(retries):
try:
if self.backup_file.suffix == '.gz':
Expand Down Expand Up @@ -308,4 +211,4 @@ def _execute_restore_command(self, retries=3, wait=5) -> Dict[str, str]:

result["message"] = f"{result['message']} Restore failed after retrying."
self.logger.error(result["message"])
return result
return result

0 comments on commit 1ee9329

Please sign in to comment.