11from optparse import make_option
22from subprocess import CalledProcessError
3+ import logging
34import os
45
5- from django .core .management .base import BaseCommand , CommandError
6+ from django .core .management .base import CommandError
67from django .conf import settings
78from django .db import close_connection
89
10+ from backupdb_utils .commands import BaseBackupDbCommand
911from backupdb_utils .exceptions import RestoreError
1012from backupdb_utils .files import get_latest_timestamped_file
13+ from backupdb_utils .log import section , SectionError , SectionWarning
1114from backupdb_utils .settings import BACKUP_DIR , BACKUP_CONFIG
12- from backupdb_utils .streams import err , set_verbosity , section , SectionError
1315
16+ logger = logging .getLogger (__name__ )
1417
15- class Command (BaseCommand ):
18+
19+ class Command (BaseBackupDbCommand ):
1620 help = 'Restores each database in settings.DATABASES from latest db backup.'
17- can_import_settings = True
1821
19- option_list = BaseCommand .option_list + (
22+ option_list = BaseBackupDbCommand .option_list + (
2023 make_option (
2124 '--backup-name' ,
2225 help = (
@@ -53,6 +56,8 @@ class Command(BaseCommand):
5356 )
5457
5558 def handle (self , * args , ** options ):
59+ super (Command , self ).handle (* args , ** options )
60+
5661 # Django is querying django_content_types in a hanging transaction
5762 # Because of this psql can't drop django_content_types and just hangs
5863 close_connection ()
@@ -65,16 +70,14 @@ def handle(self, *args, **options):
6570 drop_tables = options ['drop_tables' ]
6671 show_output = options ['show_output' ]
6772
68- set_verbosity (int (options ['verbosity' ]))
69-
7073 # Loop through databases
7174 for db_name , db_config in settings .DATABASES .items ():
7275 with section ("Restoring '{0}'..." .format (db_name )):
7376 # Get backup config for this engine type
7477 engine = db_config ['ENGINE' ]
7578 backup_config = BACKUP_CONFIG .get (engine )
7679 if not backup_config :
77- raise SectionError ( "! Restore for '{0}' engine not implemented" .format (engine ))
80+ raise SectionWarning ( " Restore for '{0}' engine not implemented" .format (engine ))
7881
7982 # Get backup file name
8083 backup_extension = backup_config ['backup_extension' ]
@@ -89,7 +92,7 @@ def handle(self, *args, **options):
8992 try :
9093 backup_file = get_latest_timestamped_file (backup_extension )
9194 except RestoreError as e :
92- raise SectionError ('! {0}' . format ( e ) )
95+ raise SectionError (e )
9396
9497 # Find restore command and get kwargs
9598 restore_func = backup_config ['restore_func' ]
@@ -103,8 +106,8 @@ def handle(self, *args, **options):
103106 # Run restore command
104107 try :
105108 restore_func (** restore_kwargs )
106- err ( "* Restored '{db_name}' from '{backup_file}'" .format (
109+ logger . info ( " Restored '{db_name}' from '{backup_file}'" .format (
107110 db_name = db_name ,
108111 backup_file = backup_file ))
109112 except (RestoreError , CalledProcessError ) as e :
110- raise SectionError ('! {0}' . format ( e ) )
113+ raise SectionError (e )
0 commit comments