Source code for ferrosoft.ops.commands.restoredatabase

#!/usr/bin/env python3
import argparse
import os
import sys
from subprocess import CalledProcessError

from ferrosoft.ops import restore
from ferrosoft.ops.restore import get_journal

arg_parser = argparse.ArgumentParser(
    prog=sys.argv[0],
    description="Restore database from backup",
)
arg_parser.add_argument(
    "-j",
    "--journal",
    help="Journal file created by createbackup",
    default=os.getenv("FP_JOURNAL_FILE"),
)
arg_parser.add_argument(
    "-d",
    "--directory",
    help="Directory where dump files are stored for the duration of restore",
    default=os.getenv("FP_BACKUP_DIR", "/tmp"),
)
arg_parser.add_argument(
    "-m",
    "--masterdb",
    help="Name of master database to restore to, overriding name found in journal",
    default=os.getenv("PGDATABASE"),
)
arg_parser.add_argument(
    "-L",
    "--local",
    help="Find journal file and dump files on the local file system."
    " Restore databases to PostgreSQL localhost (ignoring tenant database connection).",
    action="store_true",
    default=False,
)
arg_parser.add_argument(
    "-H",
    "--pglocalhost",
    help="Hostname (or UNIX socket) to update tenants with if local mode is enabled.",
    default="localhost",
)
arg_parser.add_argument(
    "-b",
    "--bucket",
    help="S3 bucket name for backup file storage",
    default=os.getenv("FP_BUCKET_NAME"),
)
arg_parser.add_argument(
    "--psql",
    help="Path to psql executable",
    default="/usr/bin/psql",
)
arg_parser.add_argument(
    "--pgrestore",
    help="Path to pg_restore executable",
    default="/usr/bin/pg_restore",
)
arg_parser.add_argument(
    "--aws",
    help="Path to aws executable",
    default="/usr/bin/aws",
)
arg_parser.add_argument(
    "--decompress",
    help="Path to decompression executable",
    default=os.getenv("FP_DECOMPRESS", "unzstd"),
)


[docs] def main(): args = arg_parser.parse_args() try: journal = get_journal(args.journal, args.aws, args.bucket, args.local) restore.fetch_backups( journal, args.directory, args.aws, args.bucket, args.local ) restore.decompress_backups(journal, args.directory, args.decompress) restore.restore_databases(journal, args.psql, args.local, args.masterdb) if args.local: restore.set_tenant_localhost(args.psql, args.pglocalhost) except CalledProcessError as e: print(e) sys.exit(1)
if __name__ == "__main__": main()