def handle(self, *args, **options): super(Command, self).handle(*args, **options) # Django is querying django_content_types in a hanging transaction # Because of this psql can't drop django_content_types and just hangs close_connection() # Ensure backup dir present if not os.path.exists(BACKUP_DIR): raise CommandError( "Backup dir '{0}' does not exist!".format(BACKUP_DIR)) backup_name = options['backup_name'] drop_tables = options['drop_tables'] show_output = options['show_output'] # Loop through databases for db_name, db_config in settings.DATABASES.items(): with section("Restoring '{0}'...".format(db_name)): # Get backup config for this engine type engine = db_config['ENGINE'] backup_config = BACKUP_CONFIG.get(engine) if not backup_config: raise SectionWarning( "Restore for '{0}' engine not implemented".format( engine)) # Get backup file name backup_extension = backup_config['backup_extension'] if backup_name: backup_file = '{dir}/{db_name}-{backup_name}.{ext}.gz'.format( dir=BACKUP_DIR, db_name=db_name, backup_name=backup_name, ext=backup_extension, ) else: try: backup_file = get_latest_timestamped_file( backup_extension) except RestoreError as e: raise SectionError(e) # Find restore command and get kwargs restore_func = backup_config['restore_func'] restore_kwargs = { 'backup_file': backup_file, 'db_config': db_config, 'drop_tables': drop_tables, 'show_output': show_output, } # Run restore command try: restore_func(**restore_kwargs) logger.info( "Restored '{db_name}' from '{backup_file}'".format( db_name=db_name, backup_file=backup_file)) except (RestoreError, CalledProcessError) as e: raise SectionError(e)
def handle(self, *args, **options): super(Command, self).handle(*args, **options) # Django is querying django_content_types in a hanging transaction # Because of this psql can't drop django_content_types and just hangs close_connection() # Ensure backup dir present if not os.path.exists(BACKUP_DIR): raise CommandError("Backup dir '{0}' does not exist!".format(BACKUP_DIR)) backup_name = options['backup_name'] drop_tables = options['drop_tables'] show_output = options['show_output'] # Loop through databases for db_name, db_config in settings.DATABASES.items(): with section("Restoring '{0}'...".format(db_name)): # Get backup config for this engine type engine = db_config['ENGINE'] backup_config = BACKUP_CONFIG.get(engine) if not backup_config: raise SectionWarning("Restore for '{0}' engine not implemented".format(engine)) # Get backup file name backup_extension = backup_config['backup_extension'] if backup_name: backup_file = '{dir}/{db_name}-{backup_name}.{ext}.gz'.format( dir=BACKUP_DIR, db_name=db_name, backup_name=backup_name, ext=backup_extension, ) else: try: backup_file = get_latest_timestamped_file(backup_extension) except RestoreError as e: raise SectionError(e) # Find restore command and get kwargs restore_func = backup_config['restore_func'] restore_kwargs = { 'backup_file': backup_file, 'db_config': db_config, 'drop_tables': drop_tables, 'show_output': show_output, } # Run restore command try: restore_func(**restore_kwargs) logger.info("Restored '{db_name}' from '{backup_file}'".format( db_name=db_name, backup_file=backup_file)) except (RestoreError, CalledProcessError) as e: raise SectionError(e)
def handle(self, *args, **options): super(Command, self).handle(*args, **options) from django.conf import settings current_time = time.strftime('%F-%s') backup_name = options['backup_name'] or current_time show_output = options['show_output'] # Ensure backup dir present if not os.path.exists(BACKUP_DIR): os.makedirs(BACKUP_DIR) # Loop through databases for db_name, db_config in settings.DATABASES.items(): with section("Backing up '{0}'...".format(db_name)): # Get backup config for this engine type engine = db_config['ENGINE'] backup_config = BACKUP_CONFIG.get(engine) if not backup_config: raise SectionWarning("Backup for '{0}' engine not implemented".format(engine)) # Get backup file name backup_base_name = '{db_name}-{backup_name}.{backup_extension}.gz'.format( db_name=db_name, backup_name=backup_name, backup_extension=backup_config['backup_extension'], ) backup_file = os.path.join(BACKUP_DIR, backup_base_name) # Find backup command and get kwargs backup_func = backup_config['backup_func'] backup_kwargs = { 'backup_file': backup_file, 'db_config': db_config, 'show_output': show_output, } if backup_func is do_postgresql_backup: backup_kwargs['pg_dump_options'] = options['pg_dump_options'] # Run backup command try: backup_func(**backup_kwargs) logger.info("Backup of '{db_name}' saved in '{backup_file}'".format( db_name=db_name, backup_file=backup_file)) except (BackupError, CalledProcessError) as e: raise SectionError(e)