-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from someengineering/lloesche/pgdump
Add Postgresql backup
- Loading branch information
Showing
9 changed files
with
173 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
from argparse import ArgumentParser, Namespace | ||
from ..utils import BackupFile | ||
from ..logger import log | ||
|
||
|
||
def add_args(arg_parser: ArgumentParser) -> None: | ||
arg_parser.add_argument( | ||
"--pg-host", | ||
help="PostgreSQL host", | ||
dest="pg_host", | ||
type=str, | ||
default=os.getenv("PG_HOST"), | ||
) | ||
|
||
arg_parser.add_argument( | ||
"--pg-port", | ||
help="PostgreSQL port", | ||
dest="pg_port", | ||
type=int, | ||
default=os.getenv("PG_PORT", 5432), | ||
) | ||
|
||
arg_parser.add_argument( | ||
"--pg-user", | ||
help="PostgreSQL user", | ||
dest="gp_user", | ||
type=str, | ||
default=os.getenv("PG_USER", "root"), | ||
) | ||
|
||
arg_parser.add_argument( | ||
"--pg-password", | ||
help="PostgreSQL password", | ||
dest="pg_password", | ||
type=str, | ||
default=os.getenv("PG_PASSWORD"), | ||
) | ||
|
||
arg_parser.add_argument( | ||
"--pg-database", | ||
help="PostgreSQL database", | ||
dest="pg_database", | ||
type=str, | ||
default=os.getenv("PG_DATABASE"), | ||
) | ||
|
||
arg_parser.add_argument( | ||
"--pg-dump-args", | ||
help="Extra arguments to pass to pg_dump", | ||
dest="pg_dump_args", | ||
action="append", | ||
default=[], | ||
) | ||
|
||
|
||
def backup(args: Namespace, backup_file_path: Path, timeout: int = 900, compress: bool = True) -> bool: | ||
log.info("Starting PostgreSQL backup...") | ||
|
||
if not args.pg_host: | ||
return False | ||
|
||
env = os.environ.copy() | ||
command = [ | ||
"pg_dump", | ||
"-w", | ||
"--if-exists", | ||
"--inserts", | ||
"-h", | ||
str(args.pg_host), | ||
"-p", | ||
str(args.pg_port), | ||
"-u", | ||
str(args.pg_user), | ||
*args.pg_dump_args, | ||
] | ||
if args.pg_database: | ||
command.append("-d") | ||
command.append(args.pg_database) | ||
else: | ||
command[0] = "pg_dumpall" | ||
|
||
if args.pg_password: | ||
env["PGPASSWORD"] = args.pg_password | ||
|
||
log.debug(f"Running command: {' '.join(command)}") | ||
|
||
try: | ||
with BackupFile(backup_file_path, compress) as backup_fd: | ||
process = subprocess.Popen(command, stdout=backup_fd, stderr=subprocess.PIPE, env=env) | ||
_, stderr = process.communicate(timeout=timeout) | ||
|
||
if process.returncode == 0: | ||
log.info(f"PostgreSQL backup completed successfully. Saved to {backup_file_path}") | ||
if stderr: | ||
log.debug(stderr.decode().strip()) | ||
return True | ||
else: | ||
log.error(f"PostgreSQL backup failed with return code: {process.returncode}") | ||
if stderr: | ||
log.error(stderr.decode().strip()) | ||
except subprocess.TimeoutExpired: | ||
log.error(f"PostgreSQL backup failed with timeout after {timeout} seconds") | ||
process.kill() | ||
process.communicate() | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
boto3==1.28.63 | ||
boto3==1.34.144 | ||
# via fixbackup (pyproject.toml) | ||
botocore==1.31.63 | ||
botocore==1.34.144 | ||
# via | ||
# boto3 | ||
# s3transfer | ||
jmespath==1.0.1 | ||
# via | ||
# boto3 | ||
# botocore | ||
python-dateutil==2.8.2 | ||
python-dateutil==2.9.0.post0 | ||
# via botocore | ||
s3transfer==0.7.0 | ||
s3transfer==0.10.2 | ||
# via boto3 | ||
six==1.16.0 | ||
# via python-dateutil | ||
urllib3==2.0.7 | ||
urllib3==2.2.2 | ||
# via botocore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters