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); }
public static void Backup(string name, string description, string volumeid, string volumename, string instancename, string expires) { Console.WriteLine("Creating snapshot of " + volumeid + " / " + volumename + " / " + instancename); AmazonEC2 ec2 = Ec2Helper.CreateClient(); CreateSnapshotRequest rq = new CreateSnapshotRequest(); rq.VolumeId = volumeid; rq.Description = description; CreateSnapshotResponse rs = ec2.CreateSnapshot(rq); string snapshotid = rs.CreateSnapshotResult.Snapshot.SnapshotId; // build tags for snapshot List <Tag> tags = new List <Tag>(); tags.Add(new Tag { Key = "Name", Value = name }); tags.Add(new Tag { Key = "source", Value = "scheduler" }); tags.Add(new Tag { Key = "instance", Value = instancename }); tags.Add(new Tag { Key = "volume", Value = volumename }); tags.Add(new Tag { Key = "expires", Value = expires.ToString() }); // get tags from volume to be applied to snapshot DescribeTagsRequest trq = new DescribeTagsRequest(); trq.WithFilter(new Filter() { Name = "resource-id", Value = new List <string>() { volumeid } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (ResourceTag t in trs.DescribeTagsResult.ResourceTag) { if (t.Key != "nextSnapshot" && t.Key != "lastSnapshot" && t.Key != "Name") { tags.Add(new Tag { Key = t.Key, Value = t.Value }); } } // apply tags to snapshopt CreateTagsRequest rqq = new CreateTagsRequest(); rqq.WithResourceId(snapshotid); rqq.WithTag(tags.ToArray()); var createTagResponse = ec2.CreateTags(rqq); }
/// <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); } }