使用AWS SDK以编程方式创建/恢复EBS快照的示例

时间:2015-05-19 23:36:23

标签: java amazon-web-services

我没有通过谷歌的例子在代码中找到很多信息,所有内容都显示了一个使用cli工具的例子。

对于备份,请纠正我,如果我错了,这就是我的(未经测试)

AWSCredentials cred = new BasicAWSCredentials(accessKey,secretKey);
AmazonEC2 ec2 = new AmazonEC2Client(cred);
CreateSnapshotRequest snapshotRequest = new CreateSnapshotRequest(volumeId,description);
ec2.createSnapshot(snapshotRequest);

据我所知,这是一个我无法控制的S3存储库。我假设我可以做某种类型的“列表快照”并选择一个来恢复。

我希望能够使用快照还原EBS,我发现了一些有关使用createVolume或类似内容从快照创建新EBS的信息。我想用特定的快照恢复当前的EBS。

1 个答案:

答案 0 :(得分:1)

有关创建快照的信息,请参见以下内容:

//从本地:

            //I guess it's resolved from your end. But to help other. Below is the code to Create a snapshot from C# AWS SDK
            //Download AWSSDK.Core/AWSSDK.EC2/AWSSDK.S3 from nuget Package Manager and use the below code for reference
            IAmazonEC2 client = new AmazonEC2Client(awsAccessKeyId, SecretAccessKey, RegionEndpoint.APSouth1); 
            //Change Region end point based on your location
            CreateSnapshotsRequest createSnapshotsRequest = new CreateSnapshotsRequest();
            createSnapshotsRequest.InstanceSpecification = new InstanceSpecification
            {
                InstanceId = "instance id",
                ExcludeBootVolume = false
            };

            //tags can be added using below:
            //createSnapshotsRequest.TagSpecifications = new List<TagSpecification>
            //{
            //    new TagSpecification
            //    {
            //        Tags= new List<Tag>{
            //            new Tag
            //            {
            //                Key = tagKey,
            //                Value = tagValue
            //            }
            //        },
            //        ResourceType="snapshot"
            //    }
            //};

            var response = client.CreateSnapshots(createSnapshotsRequest);
            Console.WriteLine(response.HttpStatusCode);
            Console.WriteLine(response.Snapshots);


            //If you are running the app on EC2 then use below:
            //I guess it's resolved from your end. But to help other. Below is the code to Create a snapshot from C# AWS SDK
            //Download AWSSDK.Core/AWSSDK.EC2/AWSSDK.S3 from nuget Package Manager and use the below code for reference
            KeyValuePair<string, IAMSecurityCredentialMetadata> iamSecurityCredentialMetadata = EC2InstanceMetadata.IAMSecurityCredentials.FirstOrDefault();
            IAmazonEC2 client = new AmazonEC2Client(iamSecurityCredentialMetadata.Value.AccessKeyId, iamSecurityCredentialMetadata.Value.SecretAccessKey, iamSecurityCredentialMetadata.Value.Token, EC2InstanceMetadata.Region);
            CreateSnapshotsRequest createSnapshotsRequest = new CreateSnapshotsRequest();
            createSnapshotsRequest.InstanceSpecification = new InstanceSpecification
            {
                InstanceId = EC2InstanceMetadata.InstanceId,
                ExcludeBootVolume = false
            };

            //tags can be added using below:
            //createSnapshotsRequest.TagSpecifications = new List<TagSpecification>
            //{
            //    new TagSpecification
            //    {
            //        Tags= new List<Tag>{
            //            new Tag
            //            {
            //                Key = tagKey,
            //                Value = tagValue
            //            }
            //        },
            //        ResourceType="snapshot"
            //    }
            //};

            var response = client.CreateSnapshots(createSnapshotsRequest);
            Console.WriteLine(response.HttpStatusCode);
            Console.WriteLine(response.Snapshots);