public static void CheckForScheduledSnapshots()
{
Console.WriteLine("Checking for scheduled snapshots in " + Program.options.Region + "...");
AmazonEC2Client ec2 = Ec2Helper.CreateClient();
DescribeVolumesRequest rq = new DescribeVolumesRequest();
rq.Filters.Add(new Filter() { Name = "tag-key", Values = new List<string>() { "snapshotSchedule" } });
DescribeVolumesResponse rs = ec2.DescribeVolumes(rq);
foreach (Volume v in rs.Volumes)
{
string[] sch2 = Ec2Helper.GetTagValue(v.Tags, "snapshotSchedule").Split(' ');
string volumename = Ec2Helper.GetTagValue(v.Tags, "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.Tags, "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.Attachments.First().InstanceId), expires);
// update volume tags
CreateTagsRequest rqq = new CreateTagsRequest();
rqq.Resources.Add(v.VolumeId);
nextSnap = nextSnap.AddSeconds(-nextSnap.Second).AddMilliseconds(-nextSnap.Millisecond);
rqq.Tags.Add(new Tag { Key = "lastSnapshot", Value = now.ToString() });
rqq.Tags.Add(new Tag { Key = "nextSnapshot", Value = nextNextSnap.ToString() });
var createTagResponse = ec2.CreateTags(rqq);
}
else
{
Console.WriteLine(" Next scheduled " + nextSnap.ToString());
}
}
}