使用 Amazon SDK 创建 Amazon RDS 数据库实例的快照
以下代码示例显示了如何创建 Amazon RDS 数据库实例的快照。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- .NET
-
- Amazon SDK for .NET
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Create a snapshot of a DB instance. /// </summary> /// <param name="dbInstanceIdentifier">DB instance identifier.</param> /// <param name="snapshotIdentifier">Identifier for the snapshot.</param> /// <returns>DB snapshot object.</returns> public async Task<DBSnapshot> CreateDBSnapshot(string dbInstanceIdentifier, string snapshotIdentifier) { var response = await _amazonRDS.CreateDBSnapshotAsync( new CreateDBSnapshotRequest() { DBSnapshotIdentifier = snapshotIdentifier, DBInstanceIdentifier = dbInstanceIdentifier }); return response.DBSnapshot; }
-
有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 CreateDBSnapshot。
-
- 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::CreateDBSnapshotRequest request; request.SetDBInstanceIdentifier(DB_INSTANCE_IDENTIFIER); request.SetDBSnapshotIdentifier(snapshotID); Aws::RDS::Model::CreateDBSnapshotOutcome outcome = client.CreateDBSnapshot(request); if (outcome.IsSuccess()) { std::cout << "Snapshot creation has started." << std::endl; } else { std::cerr << "Error with RDS::CreateDBSnapshot. " << outcome.GetError().GetMessage() << std::endl; cleanUpResources(PARAMETER_GROUP_NAME, DB_INSTANCE_IDENTIFIER, client); return false; }
-
有关 API 详细信息,请参阅《Amazon SDK for C++ API 参考》中的 CreateDBSnapshot。
-
- Go
-
- SDK for Go V2
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 type DbInstances struct { RdsClient *rds.Client } // CreateSnapshot creates a snapshot of a DB instance. func (instances *DbInstances) CreateSnapshot(instanceName string, snapshotName string) ( *types.DBSnapshot, error) { output, err := instances.RdsClient.CreateDBSnapshot(context.TODO(), &rds.CreateDBSnapshotInput{ DBInstanceIdentifier: aws.String(instanceName), DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't create snapshot %v: %v\n", snapshotName, err) return nil, err } else { return output.DBSnapshot, nil } }
-
有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 CreateDBSnapshot
。
-
- Java
-
- SDK for Java 2.x
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 // Create an Amazon RDS snapshot. public static void createSnapshot(RdsClient rdsClient, String dbInstanceIdentifier, String dbSnapshotIdentifier) { try { CreateDbSnapshotRequest snapshotRequest = CreateDbSnapshotRequest.builder() .dbInstanceIdentifier(dbInstanceIdentifier) .dbSnapshotIdentifier(dbSnapshotIdentifier) .build(); CreateDbSnapshotResponse response = rdsClient.createDBSnapshot(snapshotRequest); System.out.println("The Snapshot id is " + response.dbSnapshot().dbiResourceId()); } catch (RdsException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } }
-
有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 CreateDBSnapshot。
-
- Python
-
- 适用于 Python (Boto3) 的 SDK
-
注意
在 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 create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id) snapshot = response['DBSnapshot'] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response['Error']['Code'], err.response['Error']['Message']) raise else: return snapshot
-
有关 API 详细信息,请参阅《适用于 Python 的 Amazon SDK(Boto3)API 参考》中的 CreateDBSnapshot。
-
- Ruby
-
- SDK for Ruby
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 require "aws-sdk-rds" # v2: require 'aws-sdk' # Create a snapshot for an Amazon Relational Database Service (Amazon RDS) # DB instance. # # @param rds_resource [Aws::RDS::Resource] The resource containing SDK logic. # @param db_instance_name [String] The name of the Amazon RDS DB instance. # @return [Aws::RDS::DBSnapshot, nil] The snapshot created, or nil if error. def create_snapshot(rds_resource, db_instance_name) id = "snapshot-#{rand(10**6)}" db_instance = rds_resource.db_instance(db_instance_name) db_instance.create_snapshot({ db_snapshot_identifier: id }) rescue Aws::Errors::ServiceError => e puts "Couldn't create DB instance snapshot #{id}:\n #{e.message}" end
-
有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 CreateDBSnapshot。
-
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。