public void TakeSnapshots(IEnumerable<Instance> instances, Ec2BootstrapConfig config) { foreach (var instance in instances) { foreach (var volumeId in instance.BlockDeviceMappings.Select(x => x.Ebs.VolumeId)) { var request = new CreateSnapshotRequest { Description = "Snapshot for ConDep integration testing", VolumeId = volumeId }; var response = _client.CreateSnapshot(request); config.Instances .Single(x => x.InstanceId == instance.InstanceId) .BaseSnapshots.Add(new Ec2Snapshot { SnapshotId = response.Snapshot.SnapshotId, VolumeId = response.Snapshot.VolumeId, VolumeSize = response.Snapshot.VolumeSize }); } } var snapshotIds = config.Instances.SelectMany(x => x.BaseSnapshots.Select(y => y.SnapshotId)).ToList(); _tagHandler.CreateNameTags(config.BootstrapId, snapshotIds); Logger.WithLogSection("Waiting for snapshots to finish", () => WaitForSnapshotsToFinish(snapshotIds)); }
protected override void ProcessRecord() { AmazonEC2 client = base.GetClient(); Amazon.EC2.Model.CreateSnapshotRequest request = new Amazon.EC2.Model.CreateSnapshotRequest(); request.VolumeId = this._VolumeId; request.Description = this._Description; Amazon.EC2.Model.CreateSnapshotResponse response = client.CreateSnapshot(request); base.WriteObject(response.CreateSnapshotResult.Snapshot, true); }
public static void CreateNewSnapshots(List<string> volumeIds) { foreach (var volume in volumeIds) { var description = string.Format("{0} vol={1}", DateTime.UtcNow.ToShortDateString(), volume); var createSnapshotRequest = new CreateSnapshotRequest {Description = description, VolumeId = volume}; var response = Ec2Client.CreateSnapshot(createSnapshotRequest); Console.WriteLine("Snapshot:{0} of Volume:{1} created", response.Snapshot.SnapshotId, volume); } }
public object Execute(ExecutorContext context) { var cmdletContext = context as CmdletContext; // create request var request = new Amazon.EC2.Model.CreateSnapshotRequest(); if (cmdletContext.Description != null) { request.Description = cmdletContext.Description; } if (cmdletContext.OutpostArn != null) { request.OutpostArn = cmdletContext.OutpostArn; } if (cmdletContext.TagSpecification != null) { request.TagSpecifications = cmdletContext.TagSpecification; } if (cmdletContext.VolumeId != null) { request.VolumeId = cmdletContext.VolumeId; } CmdletOutput output; // issue call var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint); try { var response = CallAWSServiceOperation(client, request); object pipelineOutput = null; pipelineOutput = cmdletContext.Select(response, this); output = new CmdletOutput { PipelineOutput = pipelineOutput, ServiceResponse = response }; } catch (Exception e) { output = new CmdletOutput { ErrorResponse = e }; } return(output); }
public static void Backup(string name, string description, string volumeid, string volumename, string instancename, string expires) { Console.WriteLine(" Creating snapshot of " + volumeid + " / " + volumename + " / " + instancename); AmazonEC2Client ec2 = Ec2Helper.CreateClient(); CreateSnapshotRequest rq = new CreateSnapshotRequest(); rq.VolumeId = volumeid; rq.Description = description; CreateSnapshotResponse rs = ec2.CreateSnapshot(rq); string snapshotid = rs.Snapshot.SnapshotId; // build tags for snapshot CreateTagsRequest rqq = new CreateTagsRequest(); rqq.Resources.Add(snapshotid); rqq.Tags.Add(new Tag { Key = "Name", Value = name }); rqq.Tags.Add(new Tag { Key = "source", Value = "scheduler" }); rqq.Tags.Add(new Tag { Key = "instance", Value = instancename }); rqq.Tags.Add(new Tag { Key = "volume", Value = volumename }); rqq.Tags.Add(new Tag { Key = "expires", Value = expires.ToString() }); // get tags from volume to be applied to snapshot DescribeTagsRequest trq = new DescribeTagsRequest(); trq.Filters.Add(new Filter() { Name = "resource-id", Values = new List<string>() { volumeid } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (TagDescription t in trs.Tags) { if(t.Key!="nextSnapshot" && t.Key!="lastSnapshot" && t.Key!="Name") rqq.Tags.Add(new Tag { Key = t.Key, Value = t.Value}); } // apply tags to snapshopt var createTagResponse = ec2.CreateTags(rqq); }
internal CreateSnapshotResponse CreateSnapshot(CreateSnapshotRequest request) { var task = CreateSnapshotAsync(request); try { return task.Result; } catch(AggregateException e) { ExceptionDispatchInfo.Capture(e.InnerException).Throw(); return null; } }
public Snapshot SnapshotVolume(Ec2Key ec2Key, string volumeId, string snapshotDescription, string backupType) { AmazonEC2 ec2 = CreateAmazonEc2Client(ec2Key); var ec2Request = new CreateSnapshotRequest {Description = snapshotDescription, VolumeId = volumeId}; CreateSnapshotResponse result = ec2.CreateSnapshot(ec2Request); var request = new CreateTagsRequest(); request.ResourceId = new List<string> { result.CreateSnapshotResult.Snapshot.SnapshotId }; request.Tag = new List<Tag> { new Tag { Key = "Name", Value = "EC2BackupUtility" }, new Tag { Key = "EC2BackupSnapshotType", Value = backupType } }; ec2.CreateTags(request); return result.CreateSnapshotResult.Snapshot; }
private Amazon.EC2.Model.CreateSnapshotResponse CallAWSServiceOperation(IAmazonEC2 client, Amazon.EC2.Model.CreateSnapshotRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Elastic Compute Cloud (EC2)", "CreateSnapshot"); try { #if DESKTOP return(client.CreateSnapshot(request)); #elif CORECLR return(client.CreateSnapshotAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
/// <summary> /// Initiates the asynchronous execution of the CreateSnapshot operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateSnapshot operation on AmazonEC2Client.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndCreateSnapshot /// operation.</returns> public IAsyncResult BeginCreateSnapshot(CreateSnapshotRequest request, AsyncCallback callback, object state) { var marshaller = new CreateSnapshotRequestMarshaller(); var unmarshaller = CreateSnapshotResponseUnmarshaller.Instance; return BeginInvoke<CreateSnapshotRequest>(request, marshaller, unmarshaller, callback, state); }
IAsyncResult invokeCreateSnapshot(CreateSnapshotRequest createSnapshotRequest, AsyncCallback callback, object state, bool synchronized) { IRequest irequest = new CreateSnapshotRequestMarshaller().Marshall(createSnapshotRequest); var unmarshaller = CreateSnapshotResponseUnmarshaller.GetInstance(); AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller); Invoke(result); return result; }
/// <summary> /// Initiates the asynchronous execution of the CreateSnapshot operation. /// <seealso cref="Amazon.EC2.IAmazonEC2.CreateSnapshot"/> /// </summary> /// /// <param name="createSnapshotRequest">Container for the necessary parameters to execute the CreateSnapshot operation on AmazonEC2.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndCreateSnapshot /// operation.</returns> public IAsyncResult BeginCreateSnapshot(CreateSnapshotRequest createSnapshotRequest, AsyncCallback callback, object state) { return invokeCreateSnapshot(createSnapshotRequest, callback, state, false); }
/// <summary> /// <para>Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, to make copies of Amazon EBS /// volumes, and to save data before shutting down an instance.</para> <para>When a snapshot is created, any AWS Marketplace product codes that /// are associated with the source volume are propagated to the snapshot.</para> <para>You can take a snapshot of an attached volume that is in /// use. However, snapshots only capture data that has been written to your Amazon EBS volume at the time the snapshot command is issued; this /// may exclude any data that has been cached by any applications or the operating system. If you can pause any file writes to the volume long /// enough to take a snapshot, your snapshot should be complete. However, if you cannot pause all file writes to the volume, you should unmount /// the volume from within the instance, issue the snapshot command, and then remount the volume to ensure a consistent and complete snapshot. /// You may remount and use your volume while the snapshot status is <c>pending</c> .</para> <para>To create a snapshot for Amazon EBS volumes /// that serve as root devices, you should stop the instance before taking the snapshot.</para> <para>For more information, see <a /// href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html" >Creating an Amazon EBS Snapshot</a> in the <i>Amazon /// Elastic Compute Cloud User Guide</i> .</para> /// </summary> /// /// <param name="createSnapshotRequest">Container for the necessary parameters to execute the CreateSnapshot service method on /// AmazonEC2.</param> /// /// <returns>The response from the CreateSnapshot service method, as returned by AmazonEC2.</returns> /// public CreateSnapshotResponse CreateSnapshot(CreateSnapshotRequest createSnapshotRequest) { IAsyncResult asyncResult = invokeCreateSnapshot(createSnapshotRequest, null, null, true); return EndCreateSnapshot(asyncResult); }
// take a snapshot of each volume void TakeSnapShot() { foreach (string s in volumes) { string descr = ""; CreateSnapshotRequest csr = new CreateSnapshotRequest(); csr.VolumeId = s; descr += DateTime.UtcNow.ToString("dd MMM, HH:mm - " + s + " - backup"); csr.Description = descr; Console.WriteLine("Taking snapshot for volume: " + s); aec.CreateSnapshot(csr); PruneSnapShots(s); Console.WriteLine("Completed taking snapshot for volume: " + s); } }
/// <summary> /// Creates a snapshot of an EBS volume and stores it in Amazon S3. You can use snapshots /// for backups, to make copies of EBS volumes, and to save data before shutting down /// an instance. /// /// /// <para> /// When a snapshot is created, any AWS Marketplace product codes that are associated /// with the source volume are propagated to the snapshot. /// </para> /// /// <para> /// You can take a snapshot of an attached volume that is in use. However, snapshots only /// capture data that has been written to your EBS volume at the time the snapshot command /// is issued; this may exclude any data that has been cached by any applications or the /// operating system. If you can pause any file systems on the volume long enough to take /// a snapshot, your snapshot should be complete. However, if you cannot pause all file /// writes to the volume, you should unmount the volume from within the instance, issue /// the snapshot command, and then remount the volume to ensure a consistent and complete /// snapshot. You may remount and use your volume while the snapshot status is <code>pending</code>. /// </para> /// /// <para> /// To create a snapshot for EBS volumes that serve as root devices, you should stop the /// instance before taking the snapshot. /// </para> /// /// <para> /// Snapshots that are taken from encrypted volumes are automatically encrypted. Volumes /// that are created from encrypted snapshots are also automatically encrypted. Your encrypted /// volumes and any associated snapshots always remain protected. /// </para> /// /// <para> /// For more information, see <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html">Amazon /// Elastic Block Store</a> and <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html">Amazon /// EBS Encryption</a> in the <i>Amazon Elastic Compute Cloud User Guide</i>. /// </para> /// </summary> /// <param name="request">Container for the necessary parameters to execute the CreateSnapshot service method.</param> /// /// <returns>The response from the CreateSnapshot service method, as returned by EC2.</returns> public CreateSnapshotResponse CreateSnapshot(CreateSnapshotRequest request) { var marshaller = new CreateSnapshotRequestMarshaller(); var unmarshaller = CreateSnapshotResponseUnmarshaller.Instance; return Invoke<CreateSnapshotRequest,CreateSnapshotResponse>(request, marshaller, unmarshaller); }
/// <summary> /// Initiates the asynchronous execution of the CreateSnapshot operation. /// <seealso cref="Amazon.EC2.IAmazonEC2.CreateSnapshot"/> /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateSnapshot operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public async Task<CreateSnapshotResponse> CreateSnapshotAsync(CreateSnapshotRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateSnapshotRequestMarshaller(); var unmarshaller = CreateSnapshotResponseUnmarshaller.GetInstance(); var response = await Invoke<IRequest, CreateSnapshotRequest, CreateSnapshotResponse>(request, marshaller, unmarshaller, signer, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); return response; }
/// <summary> /// <para> Create a snapshot of the volume identified by volume ID. A volume does not have to be detached at the time the snapshot is taken. /// </para> <para><b>NOTE:</b> Snapshot creation requires that the system is in a consistent state. For instance, this means that if taking a /// snapshot of a database, the tables must be read-only locked to ensure that the snapshot will not contain a corrupted version of the /// database. Therefore, be careful when using this API to ensure that the system remains in the consistent state until the create snapshot /// status has returned. </para> /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateSnapshot service method on /// AmazonEC2.</param> /// /// <returns>The response from the CreateSnapshot service method, as returned by AmazonEC2.</returns> public CreateSnapshotResponse CreateSnapshot(CreateSnapshotRequest request) { var task = CreateSnapshotAsync(request); try { return task.Result; } catch(AggregateException e) { throw e.InnerException; } }
static Snapshot CreateSnapshot(string VolumeID, string name) { AmazonEC2 ec2 = GetEC2Client(); var request = new CreateSnapshotRequest() .WithVolumeId(VolumeID) .WithDescription(name); var response = ec2.CreateSnapshot(request); var snapshot = response.CreateSnapshotResult.Snapshot; ec2.CreateTags(new CreateTagsRequest() .WithResourceId(snapshot.SnapshotId) .WithTag(new Tag { Key = "Name", Value = name }) .WithTag(new Tag { Key = "BackupDate", Value = DateTime.Today.ToShortDateString() })); while (CheckSnapshotCompletion(snapshot.SnapshotId) == false) { System.Threading.Thread.Sleep(5000); Console.WriteLine("Checking Status"); } return snapshot; }
/// <summary> /// Create a snapshot of the volume identified by volume ID. A volume does not have to be detached /// at the time the snapshot is taken. /// Important Note: /// Snapshot creation requires that the system is in a consistent state. /// For instance, this means that if taking a snapshot of a database, the tables must /// be read-only locked to ensure that the snapshot will not contain a corrupted /// version of the database. Therefore, be careful when using this API to ensure that /// the system remains in the consistent state until the create snapshot status /// has returned. /// /// </summary> /// <param name="service">Instance of AmazonEC2 service</param> /// <param name="request">CreateSnapshotRequest request</param> public static void InvokeCreateSnapshot(AmazonEC2 service, CreateSnapshotRequest request) { try { CreateSnapshotResponse response = service.CreateSnapshot(request); Console.WriteLine ("Service Response"); Console.WriteLine ("============================================================================="); Console.WriteLine (); Console.WriteLine(" CreateSnapshotResponse"); if (response.IsSetCreateSnapshotResult()) { Console.WriteLine(" CreateSnapshotResult"); CreateSnapshotResult createSnapshotResult = response.CreateSnapshotResult; if (createSnapshotResult.IsSetSnapshot()) { Console.WriteLine(" Snapshot"); Snapshot snapshot = createSnapshotResult.Snapshot; if (snapshot.IsSetSnapshotId()) { Console.WriteLine(" SnapshotId"); Console.WriteLine(" {0}", snapshot.SnapshotId); } if (snapshot.IsSetVolumeId()) { Console.WriteLine(" VolumeId"); Console.WriteLine(" {0}", snapshot.VolumeId); } if (snapshot.IsSetStatus()) { Console.WriteLine(" Status"); Console.WriteLine(" {0}", snapshot.Status); } if (snapshot.IsSetStartTime()) { Console.WriteLine(" StartTime"); Console.WriteLine(" {0}", snapshot.StartTime); } if (snapshot.IsSetProgress()) { Console.WriteLine(" Progress"); Console.WriteLine(" {0}", snapshot.Progress); } } } if (response.IsSetResponseMetadata()) { Console.WriteLine(" ResponseMetadata"); ResponseMetadata responseMetadata = response.ResponseMetadata; if (responseMetadata.IsSetRequestId()) { Console.WriteLine(" RequestId"); Console.WriteLine(" {0}", responseMetadata.RequestId); } } } catch (AmazonEC2Exception ex) { Console.WriteLine("Caught Exception: " + ex.Message); Console.WriteLine("Response Status Code: " + ex.StatusCode); Console.WriteLine("Error Code: " + ex.ErrorCode); Console.WriteLine("Error Type: " + ex.ErrorType); Console.WriteLine("Request ID: " + ex.RequestId); Console.WriteLine("XML: " + ex.XML); } }
/// <summary> /// Create a snapshot of a volume. /// </summary> /// <param name="volumeId"></param> /// <param name="description"></param> /// <returns></returns> public string CreateSnapShot(string volumeId, string description) { var request = new CreateSnapshotRequest { VolumeId = volumeId, Description = description }; CreateSnapshotResponse response = Client.CreateSnapshot(request); return response.CreateSnapshotResult.Snapshot.SnapshotId; }
/// <summary> /// Initiates the asynchronous execution of the CreateSnapshot operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateSnapshot operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task<CreateSnapshotResponse> CreateSnapshotAsync(CreateSnapshotRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateSnapshotRequestMarshaller(); var unmarshaller = CreateSnapshotResponseUnmarshaller.Instance; return InvokeAsync<CreateSnapshotRequest,CreateSnapshotResponse>(request, marshaller, unmarshaller, cancellationToken); }
/// <summary> /// <para>Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, to make copies of Amazon EBS /// volumes, and to save data before shutting down an instance.</para> <para>When a snapshot is created, any AWS Marketplace product codes that /// are associated with the source volume are propagated to the snapshot.</para> <para>You can take a snapshot of an attached volume that is in /// use. However, snapshots only capture data that has been written to your Amazon EBS volume at the time the snapshot command is issued; this /// may exclude any data that has been cached by any applications or the operating system. If you can pause any file writes to the volume long /// enough to take a snapshot, your snapshot should be complete. However, if you cannot pause all file writes to the volume, you should unmount /// the volume from within the instance, issue the snapshot command, and then remount the volume to ensure a consistent and complete snapshot. /// You may remount and use your volume while the snapshot status is <c>pending</c> .</para> <para>To create a snapshot for Amazon EBS volumes /// that serve as root devices, you should stop the instance before taking the snapshot.</para> <para>For more information, see <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-creating-snapshot.html">Creating an Amazon EBS Snapshot</a> in the <i>Amazon /// Elastic Compute Cloud User Guide</i> .</para> /// </summary> /// /// <param name="createSnapshotRequest">Container for the necessary parameters to execute the CreateSnapshot service method on /// AmazonEC2.</param> /// /// <returns>The response from the CreateSnapshot service method, as returned by AmazonEC2.</returns> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> public Task<CreateSnapshotResponse> CreateSnapshotAsync(CreateSnapshotRequest createSnapshotRequest, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateSnapshotRequestMarshaller(); var unmarshaller = CreateSnapshotResponseUnmarshaller.GetInstance(); return Invoke<IRequest, CreateSnapshotRequest, CreateSnapshotResponse>(createSnapshotRequest, marshaller, unmarshaller, signer, cancellationToken); }