diff --git a/restore_postgres_databases.py b/restore_postgres_databases.py index dcff2fb..d3e22c4 100644 --- a/restore_postgres_databases.py +++ b/restore_postgres_databases.py @@ -11,13 +11,13 @@ import sys import os import glob -def run_command(cmd, input_data=None): +def run_command(cmd, stdin=None): """ Run a subprocess command and abort immediately on any failure. :param cmd: list of command parts - :param input_data: bytes to send to process stdin + :param stdin: file-like object to use as stdin """ - subprocess.run(cmd, input=input_data, check=True) + subprocess.run(cmd, stdin=stdin, check=True) def main(): @@ -79,14 +79,13 @@ def main(): ) ]) - # Restore the dump into the database (will abort on first error) - print(f"Restoring dump into {dbname}…") - with open(sqlfile, "rb") as f: - sql_data = f.read() - run_command([ - "docker", "exec", "-i", container, - "psql", "-U", "postgres", "-d", dbname - ], input_data=sql_data) + # Restore the dump into the database by streaming file (will abort on first error) + print(f"Restoring dump into {dbname} (this may take a while)…") + with open(sqlfile, 'rb') as infile: + run_command([ + "docker", "exec", "-i", container, + "psql", "-U", "postgres", "-d", dbname + ], stdin=infile) print(f"✔ {dbname} restored.")