|
| 1 | +""" |
| 2 | +Management command to restore PostgreSQL database. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + docker-compose -f local.yml run --rm django python manage.py database_restore path/to/backup.sql |
| 6 | + docker-compose -f production.yml run --rm django python manage.py database_restore path/to/backup.sql |
| 7 | +""" |
| 8 | + |
| 9 | +import enum |
| 10 | +import os |
| 11 | +import socket |
| 12 | +import subprocess |
| 13 | + |
| 14 | +from django.conf import settings |
| 15 | +from django.core.management.base import BaseCommand, CommandError |
| 16 | + |
| 17 | + |
| 18 | +class Server(enum.Enum): |
| 19 | + PRODUCTION = "PRODUCTION" |
| 20 | + STAGING = "STAGING" |
| 21 | + UNKNOWN = "UNKNOWN" |
| 22 | + |
| 23 | + |
| 24 | +def detect_server() -> Server: |
| 25 | + hostname = socket.gethostname().upper() |
| 26 | + if "PRODUCTION" in hostname: |
| 27 | + return Server.PRODUCTION |
| 28 | + elif "STAGING" in hostname: |
| 29 | + return Server.STAGING |
| 30 | + return Server.UNKNOWN |
| 31 | + |
| 32 | + |
| 33 | +class Command(BaseCommand): |
| 34 | + help = "Restores PostgreSQL database from backup file" |
| 35 | + |
| 36 | + def add_arguments(self, parser): |
| 37 | + parser.add_argument("backup_path", type=str, help="Path to the backup file") |
| 38 | + |
| 39 | + def handle(self, *args, **options): |
| 40 | + server = detect_server() |
| 41 | + backup_path = options["backup_path"] |
| 42 | + |
| 43 | + if not os.path.exists(backup_path): |
| 44 | + raise CommandError(f"Backup file not found: {backup_path}") |
| 45 | + |
| 46 | + db_settings = settings.DATABASES["default"] |
| 47 | + host = db_settings["HOST"] |
| 48 | + name = db_settings["NAME"] |
| 49 | + user = db_settings["USER"] |
| 50 | + password = db_settings["PASSWORD"] |
| 51 | + |
| 52 | + # Drop and recreate database |
| 53 | + drop_cmd = ["psql", "-h", host, "-U", user, "-d", "postgres", "-c", f"DROP DATABASE IF EXISTS {name}"] |
| 54 | + create_cmd = ["psql", "-h", host, "-U", user, "-d", "postgres", "-c", f"CREATE DATABASE {name}"] |
| 55 | + |
| 56 | + # Restore command |
| 57 | + restore_cmd = ["psql", "-h", host, "-U", user, "-d", name, "-f", backup_path] |
| 58 | + |
| 59 | + try: |
| 60 | + env = os.environ.copy() |
| 61 | + env["PGPASSWORD"] = password |
| 62 | + |
| 63 | + self.stdout.write(f"Dropping database {name}...") |
| 64 | + subprocess.run(drop_cmd, env=env, check=True) |
| 65 | + |
| 66 | + self.stdout.write(f"Creating database {name}...") |
| 67 | + subprocess.run(create_cmd, env=env, check=True) |
| 68 | + |
| 69 | + self.stdout.write("Restoring from backup...") |
| 70 | + subprocess.run(restore_cmd, env=env, check=True) |
| 71 | + |
| 72 | + self.stdout.write(self.style.SUCCESS(f"Successfully restored {server.value} database from {backup_path}")) |
| 73 | + |
| 74 | + except subprocess.CalledProcessError as e: |
| 75 | + self.stdout.write(self.style.ERROR(f"Restore failed on {server.value}: {str(e)}")) |
0 commit comments