def backup_from_latest_automated(self, backup_resource: BackupResource): rds_client = AwsHelper.boto3_client('rds', arn=self.role_arn, external_id=self.role_external_id) auto_snapshots = rds_client.describe_db_cluster_snapshots( DBClusterIdentifier=backup_resource.entity_id, SnapshotType='automated', # API always returns in date descending order, and we only need last one MaxRecords=20) auto_snapshots = sorted(auto_snapshots['DBClusterSnapshots'], key=lambda k: k['SnapshotCreateTime'], reverse=True) if len(auto_snapshots) == 0: self.logger.info( f"There is no latest automated backup for cluster {backup_resource.entity_id}," f" fallback to RDS_CREATE_SNAPSHOT mode. Creating snapshot directly on cluster..." ) return self.backup_from_cluster(backup_resource) # TODO handle case when there are no latest automated backups automated_snapshot_id = auto_snapshots[0][ 'DBClusterSnapshotIdentifier'] response = rds_client.copy_db_cluster_snapshot( SourceDBClusterSnapshotIdentifier=automated_snapshot_id, TargetDBClusterSnapshotIdentifier=backup_resource.name, CopyTags=False) backup_resource.resource_properties = response['DBClusterSnapshot'] backup_resource.backup_id = backup_resource.name return backup_resource
def backup_from_latest_automated(self, backup_resource: BackupResource): rds_client = AwsHelper.boto3_client('rds', arn=self.role_arn, external_id=self.role_external_id) response = rds_client.describe_db_snapshots( DBInstanceIdentifier=backup_resource.entity_id, SnapshotType='automated', # API always returns in date descending order, and we only need last one MaxRecords=20) # filter out any snapshots that could be in progress available_snapshots = [ snap for snap in response['DBSnapshots'] if snap['Status'] == 'available' ] auto_snapshots = sorted(available_snapshots, key=lambda k: k['SnapshotCreateTime'], reverse=True) if len(auto_snapshots) == 0: self.logger.info( f"There is no latest automated backup for cluster {backup_resource.entity_id}," f" fallback to RDS_CREATE_SNAPSHOT mode. Creating snapshot directly on cluster..." ) return self.backup_from_instance(backup_resource) automated_snapshot_id = auto_snapshots[0]['DBSnapshotIdentifier'] response = rds_client.copy_db_snapshot( SourceDBSnapshotIdentifier=automated_snapshot_id, TargetDBSnapshotIdentifier=backup_resource.name, CopyTags=False) backup_resource.resource_properties = response['DBSnapshot'] backup_resource.backup_id = backup_resource.name return backup_resource