def create_data_buckets(self): regions = [self.region] regions.extend(RuntimeConfig.get_dr_regions(None, self)) for region in regions: bucket = self._get_data_bucket(region) AwsHelper.boto3_client('s3', region_name=region).put_bucket_policy( Bucket=bucket.name, Policy=AwsHelper.get_shelvery_bucket_policy( self.account_id, RuntimeConfig.get_share_with_accounts(self), bucket.name))
def create_backups(self) -> List[BackupResource]: """Create backups from all collected entities marked for backup by using specific tag""" # collect resources to be backed up resource_type = self.get_resource_type() self.logger.info( f"Collecting entities of type {resource_type} tagged with " f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") resources = self.get_entities_to_backup( f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") # allows user to select single entity to be backed up if RuntimeConfig.get_shelvery_select_entity(self) is not None: entity_id = RuntimeConfig.get_shelvery_select_entity(self) self.logger.info(f"Creating backups only for entity {entity_id}") resources = list( filter(lambda x: x.resource_id == entity_id, resources)) self.logger.info( f"{len(resources)} resources of type {resource_type} collected for backup" ) # create and collect backups backup_resources = [] for r in resources: backup_resource = BackupResource( tag_prefix=RuntimeConfig.get_tag_prefix(), entity_resource=r) self.logger.info( f"Processing {resource_type} with id {r.resource_id}") self.logger.info(f"Creating backup {backup_resource.name}") try: self.backup_resource(backup_resource) self.tag_backup_resource(backup_resource) self.logger.info( f"Created backup of type {resource_type} for entity {backup_resource.entity_id} " f"with id {backup_resource.backup_id}") backup_resources.append(backup_resource) except Exception as e: self.logger.exception( f"Failed to create backup {backup_resource.name}:{e}") # create backups and disaster recovery region for br in backup_resources: self.copy_backup( br, RuntimeConfig.get_dr_regions(br.entity_resource.tags, self)) for aws_account_id in RuntimeConfig.get_share_with_accounts(self): for br in backup_resources: self.share_backup(br, aws_account_id) return backup_resources
def create_data_buckets(self): regions = [self.region] regions.extend(RuntimeConfig.get_dr_regions(None, self)) for region in regions: bucket = self._get_data_bucket(region) if self._bucket_policy_changed(region, bucket.name): policy = AwsHelper.get_shelvery_bucket_policy( self.account_id, RuntimeConfig.get_share_with_accounts(self), bucket.name) self.logger.info( f"Bucket policy has changed, updating policy to {policy}") AwsHelper.boto3_client('s3', region_name=region).put_bucket_policy( Bucket=bucket.name, Policy=policy) else: self.logger.info(f"Bucket policy hasn't changed")
def create_backups(self): """Create backups from all collected entities marked for backup by using specific tag""" # collect resources to be backed up resource_type = self.get_resource_type() self.logger.info( f"Collecting entities of type {resource_type} tagged with " f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") resources = self.get_entities_to_backup( f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") self.logger.info( f"{len(resources)} resources of type {resource_type} collected for backup" ) # create and collect backups backup_resources = [] for r in resources: backup_resource = BackupResource( tag_prefix=RuntimeConfig.get_tag_prefix(), entity_resource=r) self.logger.info( f"Processing {resource_type} with id {r.resource_id}") self.logger.info(f"Creating backup {backup_resource.name}") self.backup_resource(backup_resource) self.tag_backup_resource(backup_resource) self.logger.info( f"Created backup of type {resource_type} for entity {backup_resource.entity_id} " f"with id {backup_resource.backup_id}") backup_resources.append(backup_resource) # create backups and disaster recovery region for br in backup_resources: self.copy_backup( br, RuntimeConfig.get_dr_regions(br.entity_resource.tags, self)) for aws_account_id in RuntimeConfig.get_share_with_accounts(self): for br in backup_resources: self.share_backup(br, aws_account_id)
def do_copy_backup(self, map_args={}, **kwargs): """ Copy backup to another region, actual implementation """ kwargs.update(map_args) backup_id = kwargs['BackupId'] origin_region = kwargs['OriginRegion'] backup_resource = self.get_backup_resource(origin_region, backup_id) # if backup is not available, exit and rely on recursive lambda call copy backup # in non lambda mode this should never happen if RuntimeConfig.is_offload_queueing(self): if not self.is_backup_available(origin_region,backup_id): self.copy_backup(self.get_backup_resource(backup_resource, RuntimeConfig.get_dr_regions(backup_resource.entity_resource.tags, self))) else: if not self.wait_backup_available(backup_region=origin_region, backup_id=backup_id, lambda_method='do_copy_backup', lambda_args=kwargs): return self.logger.info(f"Do copy backup {kwargs['BackupId']} ({kwargs['OriginRegion']}) to region {kwargs['Region']}") # copy backup try: src_region = kwargs['OriginRegion'] dst_region = kwargs['Region'] regional_backup_id = self.copy_backup_to_region(kwargs['BackupId'], dst_region) # create tags on backup copy original_backup_id = kwargs['BackupId'] original_backup = self.get_backup_resource(src_region, original_backup_id) resource_copy = BackupResource(None, None, True) resource_copy.backup_id = regional_backup_id resource_copy.region = kwargs['Region'] resource_copy.tags = original_backup.tags.copy() # add metadata to dr copy and original dr_copies_tag_key = f"{RuntimeConfig.get_tag_prefix()}:dr_copies" resource_copy.tags[f"{RuntimeConfig.get_tag_prefix()}:region"] = dst_region resource_copy.tags[f"{RuntimeConfig.get_tag_prefix()}:dr_copy"] = 'true' resource_copy.tags[ f"{RuntimeConfig.get_tag_prefix()}:dr_source_backup"] = f"{src_region}:{original_backup_id}" if dr_copies_tag_key not in original_backup.tags: original_backup.tags[dr_copies_tag_key] = '' original_backup.tags[dr_copies_tag_key] = original_backup.tags[ dr_copies_tag_key] + f"{dst_region}:{regional_backup_id} " self.tag_backup_resource(resource_copy) self.tag_backup_resource(original_backup) self.snspublisher.notify({ 'Operation': 'CopyBackupToRegion', 'Status': 'OK', 'DestinationRegion': kwargs['Region'], 'BackupType': self.get_engine_type(), 'BackupId': kwargs['BackupId'], }) self.store_backup_data(resource_copy) except Exception as e: self.snspublisher_error.notify({ 'Operation': 'CopyBackupToRegion', 'Status': 'ERROR', 'ExceptionInfo': e.__dict__, 'DestinationRegion': kwargs['Region'], 'BackupType': self.get_engine_type(), 'BackupId': kwargs['BackupId'], }) self.logger.exception(f"Error copying backup {kwargs['BackupId']} to {dst_region}") # shared backup copy with same accounts for shared_account_id in RuntimeConfig.get_share_with_accounts(self): backup_resource = BackupResource(None, None, True) backup_resource.backup_id = regional_backup_id backup_resource.region = kwargs['Region'] try: self.share_backup(backup_resource, shared_account_id) self.snspublisher.notify({ 'Operation': 'ShareRegionalBackupCopy', 'Status': 'OK', 'DestinationAccount': shared_account_id, 'DestinationRegion': kwargs['Region'], 'BackupType': self.get_engine_type(), 'BackupId': kwargs['BackupId'], }) except Exception as e: self.snspublisher_error.notify({ 'Operation': 'ShareRegionalBackupCopy', 'Status': 'ERROR', 'DestinationAccount': shared_account_id, 'DestinationRegion': kwargs['Region'], 'ExceptionInfo': e.__dict__, 'BackupType': self.get_engine_type(), 'BackupId': kwargs['BackupId'], }) self.logger.exception(f"Error sharing copied backup {kwargs['BackupId']} to {dst_region}")
def create_backups(self) -> List[BackupResource]: """Create backups from all collected entities marked for backup by using specific tag""" # collect resources to be backed up resource_type = self.get_resource_type() self.logger.info(f"Collecting entities of type {resource_type} tagged with " f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") resources = self.get_entities_to_backup(f"{RuntimeConfig.get_tag_prefix()}:{self.BACKUP_RESOURCE_TAG}") # allows user to select single entity to be backed up if RuntimeConfig.get_shelvery_select_entity(self) is not None: entity_id = RuntimeConfig.get_shelvery_select_entity(self) self.logger.info(f"Creating backups only for entity {entity_id}") resources = list( filter( lambda x: x.resource_id == entity_id, resources) ) self.logger.info(f"{len(resources)} resources of type {resource_type} collected for backup") # create and collect backups backup_resources = [] current_retention_type = RuntimeConfig.get_current_retention_type(self) for r in resources: backup_resource = BackupResource( tag_prefix=RuntimeConfig.get_tag_prefix(), entity_resource=r, copy_resource_tags=RuntimeConfig.copy_resource_tags(self), exluded_resource_tag_keys=RuntimeConfig.get_exluded_resource_tag_keys(self) ) # if retention is explicitly given by runtime environment if current_retention_type is not None: backup_resource.set_retention_type(current_retention_type) dr_regions = RuntimeConfig.get_dr_regions(backup_resource.entity_resource.tags, self) backup_resource.tags[f"{RuntimeConfig.get_tag_prefix()}:dr_regions"] = ','.join(dr_regions) self.logger.info(f"Processing {resource_type} with id {r.resource_id}") self.logger.info(f"Creating backup {backup_resource.name}") try: self.backup_resource(backup_resource) self.tag_backup_resource(backup_resource) self.logger.info(f"Created backup of type {resource_type} for entity {backup_resource.entity_id} " f"with id {backup_resource.backup_id}") backup_resources.append(backup_resource) self.store_backup_data(backup_resource) self.snspublisher.notify({ 'Operation': 'CreateBackup', 'Status': 'OK', 'BackupType': self.get_engine_type(), 'BackupName': backup_resource.name, 'EntityId': backup_resource.entity_id }) except Exception as e: self.snspublisher_error.notify({ 'Operation': 'CreateBackup', 'Status': 'ERROR', 'ExceptionInfo': e.__dict__, 'BackupType': self.get_engine_type(), 'BackupName': backup_resource.name, 'EntityId': backup_resource.entity_id }) self.logger.exception(f"Failed to create backup {backup_resource.name}:{e}") # create backups and disaster recovery region for br in backup_resources: self.copy_backup(br, RuntimeConfig.get_dr_regions(br.entity_resource.tags, self)) for aws_account_id in RuntimeConfig.get_share_with_accounts(self): for br in backup_resources: self.share_backup(br, aws_account_id) return backup_resources