Skip to content

Commit

Permalink
Add backup support for all versions (#273)
Browse files Browse the repository at this point in the history
* Add support for different versions for backup/restore

- Now dump and restore is supported for PG 9.5, 9.6, 10, 11, 12
- Added pg_dump executables and associated dlls for all versions of PG
- Based on the version of the server, it finds the folder where to look for pg_dump/pg_restore exe and uses that to execute backup and restore
  • Loading branch information
swjain23 authored Aug 31, 2020
1 parent 483ff6b commit bed6e62
Show file tree
Hide file tree
Showing 109 changed files with 231 additions and 84 deletions.
64 changes: 49 additions & 15 deletions ossdbtoolsservice/disaster_recovery/disaster_recovery_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
import os
import subprocess
import sys
from typing import Any, List, Dict
from typing import Any, Dict, List, Tuple

import inflection

from ossdbtoolsservice.connection import ConnectionInfo
from ossdbtoolsservice.disaster_recovery.contracts import BACKUP_REQUEST, BackupParams, BackupType, RESTORE_REQUEST, RestoreParams
from ossdbtoolsservice.connection.contracts import ConnectionType
from ossdbtoolsservice.disaster_recovery.contracts import (BACKUP_REQUEST,
RESTORE_REQUEST,
BackupParams,
BackupType,
RestoreParams)
from ossdbtoolsservice.hosting import RequestContext, ServiceProvider
from ossdbtoolsservice.utils import constants
from ossdbtoolsservice.tasks import Task, TaskResult, TaskStatus
from ossdbtoolsservice.utils import constants


class DisasterRecoveryService:
Expand Down Expand Up @@ -104,7 +109,8 @@ def _perform_backup_restore(connection_info: ConnectionInfo, process_args: List[
def _perform_backup(connection_info: ConnectionInfo, params: BackupParams, task: Task) -> TaskResult:
"""Call out to pg_dump to do a backup"""
try:
pg_dump_location = _get_pg_exe_path('pg_dump')
connection = connection_info.get_connection(ConnectionType.DEFAULT)
pg_dump_location = _get_pg_exe_path('pg_dump', connection.server_version)
except ValueError as e:
return TaskResult(TaskStatus.FAILED, str(e))
pg_dump_args = [pg_dump_location,
Expand All @@ -121,7 +127,8 @@ def _perform_backup(connection_info: ConnectionInfo, params: BackupParams, task:
def _perform_restore(connection_info: ConnectionInfo, params: RestoreParams, task: Task) -> TaskResult:
"""Call out to pg_restore to restore from a backup"""
try:
pg_restore_location = _get_pg_exe_path('pg_restore')
connection = connection_info.get_connection(ConnectionType.DEFAULT)
pg_restore_location = _get_pg_exe_path('pg_restore', connection.server_version)
except ValueError as e:
return TaskResult(TaskStatus.FAILED, str(e))
pg_restore_args = [pg_restore_location]
Expand All @@ -143,27 +150,54 @@ def _get_backup_restore_connection_params(connection_options: dict) -> List[str]
return params


def _get_pg_exe_path(exe_name: str) -> str:
def _get_pg_exe_path(exe_name: str, server_version: Tuple[int, int, int]) -> str:
"""
Find the path to the given PostgreSQL utility executable for the current operating system
Find the path to the given PostgreSQL utility executable for the current operating system in a server specific version folder
:param exe_name: The name of the program to find (without .exe). e.g. 'pg_dump'
:param server_version: Tuple of the connected server version components (major, minor, ignored)
:returns: The path to the requested executable
:raises ValueError: if there is no file corresponding to the given exe_name
"""

base_location = os.path.join(os.path.dirname(sys.argv[0]), 'pg_exes')
platform = sys.platform
if platform == 'win32':
path = os.path.join(base_location, 'win', exe_name + '.exe')
os_root = os.path.join(base_location, 'win')
path_suffix = exe_name + '.exe'
elif platform == 'darwin':
path = os.path.join(base_location, 'mac', 'bin', exe_name)
os_root = os.path.join(base_location, 'mac')
path_suffix = os.path.join('bin', exe_name)
else:
path = os.path.join(base_location, 'linux', 'bin', exe_name)

# Verify that the file exists
if not os.path.exists(path):
raise ValueError(f'Could not find executable file {path}') # TODO: Localize
return path
os_root = os.path.join(base_location, 'linux')
path_suffix = os.path.join('bin', exe_name)

# Get the list of folders in the os specific root folder
all_folders: List[str] = [os.path.normpath(x[0]) for x in os.walk(os_root)]
for folder in all_folders:
folderName = os.path.basename(folder)
version = folderName.split('.')
# Get the major version value
try:
major = int(version[0])
except ValueError:
major = 0
minor = 0
# Set minor version if version length is more than 1 (ex 9.5, 9.6)
if (len(version) > 1):
try:
minor = int(version[1])
except ValueError:
minor = 0

if major == int(server_version[0]) and minor == server_version[1]:
exe_path = os.path.join(folder, path_suffix)
if not os.path.exists(exe_path):
raise ValueError(f'Could not find executable file {exe_path}')
return exe_path

version_string = '.'.join(str(ver) for ver in server_version)
raise ValueError(f'Exe folder {os_root} does not contain {exe_name} for version {version_string}')


# Map from backup types to the corresponding pg_dump format option value
Expand Down
Binary file added ossdbtoolsservice/pg_exes/linux/10/bin/pg_dump
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/linux/11/bin/pg_dump
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/linux/12/bin/pg_dump
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/linux/9.5/bin/pg_dump
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/linux/9.6/bin/pg_dump
Binary file not shown.
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/linux/bin/pg_dump
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/linux/bin/pg_restore
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/linux/lib/libpq.so.5
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/linux/lib/libz.so.1
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/10/bin/pg_dump
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/10/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/11/bin/pg_dump
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/11/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/12/bin/pg_dump
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/12/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/9.5/bin/pg_dump
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/9.5/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/9.6/bin/pg_dump
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/mac/9.6/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/mac/bin/pg_dump
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/mac/bin/pg_restore
Binary file not shown.
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/mac/lib/libpq.5.dylib
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/libiconv-2.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/libintl-8.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/libpq.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/pg_dump.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/pg_restore.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/psql.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/10/zlib1.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/libiconv-2.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/libintl-9.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/libpq.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/pg_dump.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/pg_restore.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/psql.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/11/zlib1.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/libiconv-2.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/libintl-8.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/libpq.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/pg_dump.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/pg_restore.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/psql.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/12/zlib1.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/libiconv-2.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/libintl-8.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/libpq.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/pg_dump.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/pg_restore.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/psql.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.5/zlib1.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/libiconv-2.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/libintl-8.dll
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/libpq.dll
Binary file not shown.
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/pg_dump.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/pg_restore.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/psql.exe
Binary file not shown.
Binary file added ossdbtoolsservice/pg_exes/win/9.6/zlib1.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/libeay32.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/libiconv-2.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/libintl-8.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/libpq.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/pg_dump.exe
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/pg_restore.exe
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/ssleay32.dll
Binary file not shown.
Binary file removed ossdbtoolsservice/pg_exes/win/zlib1.dll
Binary file not shown.
Loading

0 comments on commit bed6e62

Please sign in to comment.