mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2025-07-06 00:33:08 +02:00
Added roles, and stop on failure
This commit is contained in:
parent
5762754ed7
commit
7d3f0a3ae3
@ -3,7 +3,7 @@
|
|||||||
Restore multiple PostgreSQL databases from .backup.sql files via a Docker container.
|
Restore multiple PostgreSQL databases from .backup.sql files via a Docker container.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
./restore_databases.py /path/to/backup_dir [--container central-postgres]
|
./restore_databases.py /path/to/backup_dir container_name
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -13,15 +13,11 @@ import glob
|
|||||||
|
|
||||||
def run_command(cmd, input_data=None):
|
def run_command(cmd, input_data=None):
|
||||||
"""
|
"""
|
||||||
Run a subprocess command and exit on failure.
|
Run a subprocess command and abort immediately on any failure.
|
||||||
:param cmd: list of command parts
|
:param cmd: list of command parts
|
||||||
:param input_data: bytes to send to process stdin
|
:param input_data: bytes to send to process stdin
|
||||||
"""
|
"""
|
||||||
try:
|
subprocess.run(cmd, input=input_data, check=True)
|
||||||
subprocess.run(cmd, input=input_data, check=True)
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"Error running command: {' '.join(cmd)}", file=sys.stderr)
|
|
||||||
sys.exit(e.returncode)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -65,7 +61,21 @@ def main():
|
|||||||
f"CREATE DATABASE \"{dbname}\";"
|
f"CREATE DATABASE \"{dbname}\";"
|
||||||
])
|
])
|
||||||
|
|
||||||
# Restore the dump into the newly created database
|
# Ensure the ownership role exists
|
||||||
|
print(f"Ensuring role '{dbname}' exists...")
|
||||||
|
run_command([
|
||||||
|
"docker", "exec", "-i", container,
|
||||||
|
"psql", "-U", "postgres", "-c",
|
||||||
|
(
|
||||||
|
"DO $$BEGIN "
|
||||||
|
f"IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '{dbname}') THEN "
|
||||||
|
f"CREATE ROLE \"{dbname}\"; "
|
||||||
|
"END IF; "
|
||||||
|
"END$$;"
|
||||||
|
)
|
||||||
|
])
|
||||||
|
|
||||||
|
# Restore the dump into the database (will abort on first error)
|
||||||
print(f"Restoring dump into {dbname}…")
|
print(f"Restoring dump into {dbname}…")
|
||||||
with open(sqlfile, "rb") as f:
|
with open(sqlfile, "rb") as f:
|
||||||
sql_data = f.read()
|
sql_data = f.read()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user