/// <summary> /// Check for any snapshots set to expire -- that have a tag key of "expires" with a value that is in the past. /// </summary> public static void CheckForExpiredSnapshots() { Console.WriteLine("Checking for expired snapshots..."); AmazonEC2 ec2 = Ec2Helper.CreateClient(); DescribeSnapshotsRequest rq = new DescribeSnapshotsRequest(); rq.WithOwner("self"); rq.WithFilter(new Filter() { Name = "tag-key", Value = new List <string>() { "expires" } }); DescribeSnapshotsResponse rs = ec2.DescribeSnapshots(rq); foreach (Snapshot s in rs.DescribeSnapshotsResult.Snapshot) { string expireText = Ec2Helper.GetTagValue(s.Tag, "expires"); DateTime expires; if (DateTime.TryParse(expireText, out expires)) { if (expires < DateTime.UtcNow) { Console.WriteLine("Deleting " + s.SnapshotId + " for " + s.VolumeId + "..."); Ec2Helper.DeleteSnapsot(s.SnapshotId); } } } }
/// <summary> /// List all volumes found in region /// </summary> public static void ListVolumes() { AmazonEC2 ec2 = Ec2Helper.CreateClient(); DescribeVolumesRequest rq = new DescribeVolumesRequest(); DescribeVolumesResponse rs = ec2.DescribeVolumes(rq); foreach (Volume v in rs.DescribeVolumesResult.Volume) { Console.WriteLine(v.VolumeId); DescribeTagsRequest trq = new DescribeTagsRequest(); trq.WithFilter(new Filter() { Name = "resource-id", Value = new List <string>() { v.VolumeId } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (ResourceTag t in trs.DescribeTagsResult.ResourceTag) { Console.WriteLine(" " + t.Key + "=" + t.Value); } } }
/// <summary> /// List all volumes found in region /// </summary> public static void ListVolumes() { var ec2 = Ec2Helper.CreateClient(); DescribeVolumesRequest rq = new DescribeVolumesRequest(); DescribeVolumesResponse rs = ec2.DescribeVolumes(rq); foreach (Volume v in rs.Volumes) { Console.WriteLine(v.VolumeId); DescribeTagsRequest trq = new DescribeTagsRequest(); trq.Filters.Add(new Filter() { Name = "resource-id", Values = new List <string>() { v.VolumeId } }); DescribeTagsResponse trs = ec2.DescribeTags(trq); foreach (TagDescription t in trs.Tags) { Console.WriteLine(" " + t.Key + "=" + t.Value); } } }
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> /// Check for any volumes that have a snapshot scheduled based on the schedule in the snapshotSchedule tag key. /// </summary> public static void CheckForScheduledSnapshots() { Console.WriteLine("Checking for scheduled snapshots..."); AmazonEC2 ec2 = Ec2Helper.CreateClient(); DescribeVolumesRequest rq = new DescribeVolumesRequest(); rq.WithFilter(new Filter() { Name = "tag-key", Value = new List <string>() { "snapshotSchedule" } }); DescribeVolumesResponse rs = ec2.DescribeVolumes(rq); foreach (Volume v in rs.DescribeVolumesResult.Volume) { string[] sch2 = Ec2Helper.GetTagValue(v.Tag, "snapshotSchedule").Split(' '); string volumename = Ec2Helper.GetTagValue(v.Tag, "Name"); DateTime lastSnap; // date of last snapshot DateTime nextSnap; // the next backup that should have occured based on last backup DateTime nextNextSnap; // the next backup that should occur assuming a backup runs now or ran at the last proper time DateTime now = DateTime.UtcNow; if (!DateTime.TryParse(Ec2Helper.GetTagValue(v.Tag, "lastSnapshot"), out lastSnap)) { lastSnap = Convert.ToDateTime("1/1/2010"); } Console.WriteLine("Checking " + v.VolumeId + " / " + volumename + "..."); //sch2 = ("hourly 4 :30 x30days").Split(' '); //lastSnap = Convert.ToDateTime("2/29/2012 6:00:15pm"); //now = Convert.ToDateTime("2/29/2012 10:00:14pm"); switch (sch2[0]) { case "hourly": // hourly, hourly 1 :15, hourly :30, hourly 4 (pass it hours between backups & when on the hour to do it, any order; defaults to every hour on the hour) int ah = GetAfterTheHour(sch2, 0); int hi = GetInt(sch2, 1); nextSnap = lastSnap.AddMinutes(-lastSnap.Minute).AddSeconds(-lastSnap.Second).AddMilliseconds(-lastSnap.Millisecond); nextSnap = nextSnap.AddHours(hi).AddMinutes(ah); // this is not right nextNextSnap = now.AddMinutes(-now.Minute).AddSeconds(-now.Second).AddMilliseconds(-now.Millisecond); nextNextSnap = nextNextSnap.AddMinutes(ah).AddHours(hi); break; case "daily": // daily, 3pm, daily 15:15, daily 3:30pm (times are UTC; defaults to midnight UTC) DateTime hour = GetTime(sch2, Convert.ToDateTime("0:00")); nextSnap = lastSnap.Date.AddDays(1).AddTicks(hour.TimeOfDay.Ticks); nextNextSnap = now.Date.AddDays(1).AddTicks(hour.TimeOfDay.Ticks); break; case "weekly": // weekly, weekly sunday, weekly thursday 3pm (times are UTC; defaults to sunday midnight UTC) DateTime whour = GetTime(sch2, Convert.ToDateTime("0:00")); DayOfWeek dow = GetDow(sch2, DayOfWeek.Sunday); if (lastSnap.DayOfWeek >= dow) { nextSnap = lastSnap.Date.AddDays(-(int)lastSnap.DayOfWeek).AddDays(7 + (int)dow).AddTicks(whour.TimeOfDay.Ticks); } else { nextSnap = lastSnap.Date.AddDays(-(int)lastSnap.DayOfWeek).AddDays((int)dow).AddTicks(whour.TimeOfDay.Ticks); } nextNextSnap = now.Date.AddDays(-(int)now.DayOfWeek).AddDays(7 + (int)dow).AddTicks(whour.TimeOfDay.Ticks); if (nextSnap == nextNextSnap) { nextNextSnap = nextNextSnap.AddDays(7); } break; default: lastSnap = now.AddYears(1); nextSnap = lastSnap; nextNextSnap = lastSnap; break; } //Console.WriteLine("last=" + lastSnap.ToString()); //Console.WriteLine("now=" + now); //Console.WriteLine("next=" + nextSnap.ToString()); //Console.WriteLine("nextNext=" + nextNextSnap.ToString()); //Console.ReadKey(); //return; if (nextSnap <= now) { // create snapshot of volume string expires = ""; int expireHours = GetExpireHours(sch2, 0); if (expireHours > 0) { expires = now.AddHours(expireHours).ToString(); } Backup(volumename, "automatic", v.VolumeId, volumename, Ec2Helper.GetInstanceName(v.Attachment[0].InstanceId), expires); // update volume tags CreateTagsRequest rqq = new CreateTagsRequest(); rqq.WithResourceId(v.VolumeId); nextSnap = nextSnap.AddSeconds(-nextSnap.Second).AddMilliseconds(-nextSnap.Millisecond); rqq.WithTag(new Tag[] { new Tag { Key = "lastSnapshot", Value = now.ToString() }, new Tag { Key = "nextSnapshot", Value = nextNextSnap.ToString() } }); var createTagResponse = ec2.CreateTags(rqq); } else { Console.WriteLine(" Next scheduled " + nextSnap.ToString()); } } }