将 DescribeDBSnapshots 与 Amazon SDK 或 CLI 配合使用 - Amazon Relational Database Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

DescribeDBSnapshots 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 DescribeDBSnapshots

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

/// <summary> /// Return a list of DB snapshots for a particular DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <returns>List of DB snapshots.</returns> public async Task<List<DBSnapshot>> DescribeDBSnapshots(string dbInstanceIdentifier) { var results = new List<DBSnapshot>(); var snapshotsPaginator = _amazonRDS.Paginators.DescribeDBSnapshots( new DescribeDBSnapshotsRequest() { DBInstanceIdentifier = dbInstanceIdentifier }); // Get the entire list using the paginator. await foreach (var snapshots in snapshotsPaginator.DBSnapshots) { results.Add(snapshots); } return results; }
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 DescribeDBSnapshots

C++
适用于 C++ 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::RDS::RDSClient client(clientConfig); Aws::RDS::Model::DescribeDBSnapshotsRequest request; request.SetDBSnapshotIdentifier(snapshotID); Aws::RDS::Model::DescribeDBSnapshotsOutcome outcome = client.DescribeDBSnapshots(request); if (outcome.IsSuccess()) { snapshot = outcome.GetResult().GetDBSnapshots()[0]; } else { std::cerr << "Error with RDS::DescribeDBSnapshots. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); return false; }
  • 有关 API 详细信息,请参阅《Amazon SDK for C++ API 参考》中的 DescribeDBSnapshots

CLI
Amazon CLI

示例 1:描述数据库实例的数据库快照

以下 describe-db-snapshots 示例将检索有关数据库实例的数据库快照的详细信息。

aws rds describe-db-snapshots \ --db-snapshot-identifier mydbsnapshot

输出:

{ "DBSnapshots": [ { "DBSnapshotIdentifier": "mydbsnapshot", "DBInstanceIdentifier": "mysqldb", "SnapshotCreateTime": "2018-02-08T22:28:08.598Z", "Engine": "mysql", "AllocatedStorage": 20, "Status": "available", "Port": 3306, "AvailabilityZone": "us-east-1f", "VpcId": "vpc-6594f31c", "InstanceCreateTime": "2018-02-08T22:24:55.973Z", "MasterUsername": "mysqladmin", "EngineVersion": "5.6.37", "LicenseModel": "general-public-license", "SnapshotType": "manual", "OptionGroupName": "default:mysql-5-6", "PercentProgress": 100, "StorageType": "gp2", "Encrypted": false, "DBSnapshotArn": "arn:aws:rds:us-east-1:123456789012:snapshot:mydbsnapshot", "IAMDatabaseAuthenticationEnabled": false, "ProcessorFeatures": [], "DbiResourceId": "db-AKIAIOSFODNN7EXAMPLE" } ] }

有关更多信息,请参阅《Amazon RDS 用户指南》中的创建数据库快照

示例 2:查找手动拍摄的快照数量

以下 describe-db-snapshots 示例使用 --query 选项中的 length 运算符来返回在特定 Amazon 区域拍摄的手动快照的数量。

aws rds describe-db-snapshots \ --snapshot-type manual \ --query "length(*[].{DBSnapshots:SnapshotType})" \ --region eu-central-1

输出:

35

有关更多信息,请参阅《Amazon RDS 用户指南》中的创建数据库快照

  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 DescribeDBSnapshots

Go
适用于 Go V2 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

type DbInstances struct { RdsClient *rds.Client } // GetSnapshot gets a DB instance snapshot. func (instances *DbInstances) GetSnapshot(snapshotName string) (*types.DBSnapshot, error) { output, err := instances.RdsClient.DescribeDBSnapshots(context.TODO(), &rds.DescribeDBSnapshotsInput{ DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't get snapshot %v: %v\n", snapshotName, err) return nil, err } else { return &output.DBSnapshots[0], nil } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 DescribeDBSnapshots

Python
SDK for Python(Boto3)
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • 有关 API 详细信息,请参阅《适用于 Python 的 Amazon SDK(Boto3)API 参考》中的 DescribeDBSnapshots

Ruby
适用于 Ruby 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

require "aws-sdk-rds" # v2: require 'aws-sdk' # List all Amazon Relational Database Service (Amazon RDS) DB instance # snapshots. # # @param rds_resource [Aws::RDS::Resource] An SDK for Ruby Amazon RDS resource. # @return instance_snapshots [Array, nil] All instance snapshots, or nil if error. def list_instance_snapshots(rds_resource) instance_snapshots = [] rds_resource.db_snapshots.each do |s| instance_snapshots.append({ "id": s.snapshot_id, "status": s.status }) end instance_snapshots rescue Aws::Errors::ServiceError => e puts "Couldn't list instance snapshots:\n #{e.message}" end
  • 有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 DescribeDBSnapshots

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。