Beispiel #1
0
        //
        // Misc

        private void FillBackup(SnapshotRule rule)
        {
            rule.BackupEnabled          = cbBackupEnable.Checked;
            rule.BackupTotalMaxSize     = (int)nbBackupTotalMaxSize.Value;
            rule.BackupTotalMaxSizeUnit = (MediumStorageUnit)cbBackupTotalMaxSizeUnit.SelectedValue;
            rule.BackupNotify           = cbBackupNotificationsEnabled.Checked;
        }
Beispiel #2
0
        //
        // Misc

        private void FillSchedule(SnapshotRule sched)
        {
            // Daily Freq
            sched.DailyFreq                   = GetDailyFreq();
            sched.DailyFreqOnce               = dtpDailyFreqOnce.Value;
            sched.DailyFreqEvery              = (int)nbDailyFreqEvery.Value * (cbDailyFreqEvery.SelectedIndex == 0 ? 1 : 60);
            sched.DailyFreqEveryExcluding     = cbDailyFreqEveryExcluding.Checked;
            sched.DailyFreqEveryExcludingFrom = dtpDailyFreqEveryExcludingFrom.Value;
            sched.DailyFreqEveryExcludingTo   = dtpDailyFreqEveryExcludingTo.Value;

            // Frequency
            sched.Freq                       = GetFreq();
            sched.FreqDailyEvery             = (int)nbFreqDaily.Value;
            sched.FreqWeekly                 = GetFreqWeekly();
            sched.FreqWeeklyEvery            = (int)nbFreqWeekly.Value;
            sched.FreqWeeklyDays             = GetWeeklyDays();
            sched.FreqMonthlyMonths          = GetMonthlyMonths();
            sched.FreqMonthlyDays            = GetMonthlyDays();
            sched.FreqCron                   = tbFreqCron.Text;
            sched.FreqCronDailyExcluding     = cbFreqCronExcluding.Checked;
            sched.FreqCronDailyExcludingFrom = dtpFreqCronExcludingFrom.Value;
            sched.FreqCronDailyExcludingTo   = dtpFreqCronExcludingTo.Value;

            // Period
            sched.PeriodStart      = dtpPeriodStart.Value;
            sched.PeriodEndEnabled = rbPeriodEndDate.Checked;
            sched.PeriodEnd        = dtpPeriodEnd.Value;

            sched.GeneratedCron       = ScheduleUtils.GenerateCron(sched);
            sched.ScheduleDescription = ScheduleUtils.GetHumanizedSchedule(sched);
        }
Beispiel #3
0
        public static DateTimeOffset?GetNextFireTime(this SnapshotRule rule, DateTimeOffset from)
        {
            ICalendar      calendar = rule.GetCalendar();
            CronExpression cron     = new CronExpression(rule.GeneratedCron);

            DateTimeOffset?it = from;

            do
            {
                it = cron.GetNextValidTimeAfter(it.Value);

                if (it.HasValue)
                {
                    if (calendar != null && !calendar.IsTimeIncluded(it.Value))
                    {
                        continue;
                    }

                    return(it.Value);
                }
                else
                {
                    return(null);
                }
            } while (true);
        }
        private void RefreshUI()
        {
            if (!Init)
            {
                return;
            }

            ValidateSnapshot();
            ValidateGeneral();

            SnapshotRule rule = Valid ? GenerateSchedule() : null;

            btnCreate.Enabled = Valid;

            lblHumanSched.Text = Valid ? rule.ScheduleDescription : "Invalid schedule.";
            tbGenCron.Text     = Valid ? rule.GeneratedCron : "";

            lblMaxCountVal.Text = Valid ? ComputeMaxSnapshotCount(rule) : "";

            if (Valid)
            {
                SetStyleValid(lblHumanSched);
            }

            else
            {
                SetStyleInvalid(lblHumanSched);
            }
        }
        private string ComputeMaxSnapshotCount(SnapshotRule rule)
        {
            var nextFireTime = rule.GetNextFireTime(DateTimeOffset.Now.AddSeconds(-1));

            if (!nextFireTime.HasValue)
            {
                return("0");
            }

            var lastFireTime = nextFireTime.Value.AddSeconds(rule.GetLifeTimeSeconds());

            CancellationTokenSource cancelSrc   = new CancellationTokenSource();
            CancellationToken       cancelToken = cancelSrc.Token;

            Task <int> computeTask =
                Task.Run(() => rule.GetFireCountBetween(nextFireTime.Value, lastFireTime, cancelToken));

            try
            {
                cancelSrc.CancelAfter(1000);
                computeTask.Wait(cancelToken);

                return(computeTask.Result.ToString());
            }
            catch (OperationCanceledException)
            {
                return("(timeout: too many)");
            }
        }
Beispiel #6
0
        public static int GetFireCountBetween(this SnapshotRule rule, DateTimeOffset from, DateTimeOffset to, CancellationToken cancel)
        {
            int            count    = 0;
            ICalendar      calendar = rule.GetCalendar();
            CronExpression cron     = new CronExpression(rule.GeneratedCron);

            DateTimeOffset?it = from;

            do
            {
                it = cron.GetNextValidTimeAfter(it.Value);

                if (it.HasValue)
                {
                    if (calendar != null && !calendar.IsTimeIncluded(it.Value))
                    {
                        continue;
                    }

                    else if (it.Value > to)
                    {
                        break;
                    }

                    count++;
                }
                else
                {
                    break;
                }
            } while (cancel == null || cancel.IsCancellationRequested == false);

            return(count);
        }
Beispiel #7
0
        public bool DeleteRule(SnapshotRule rule, bool deleteSnapshots)
        {
            rule.Enabled = false;

            if (deleteSnapshots)
            {
                using (var vss = new VssClient(new VssHost()))
                {
                    vss.Initialize(VssSnapshotContext.All, VssBackupType.Incremental);
                    PruningMgr.Instance.DeleteAllForRule(vss, rule);
                }
            }

            bool ret = RulesMap.TryRemove(rule.Id, out SnapshotRule value);

            if (!ret)
            {
                return(false);
            }

            VssScheduler.CreateAllTriggers(Rules);

            SaveRules();

            return(ret);
        }
Beispiel #8
0
        private void DgSnapshotRules_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {
                SnapshotRule rule = senderGrid.Rows[e.RowIndex].DataBoundItem as SnapshotRule;

                switch (senderGrid.Columns[e.ColumnIndex].Name)
                {
                case "Edit":
                    rule = EditSnapshotRuleForm.DisplayInstance(rule);

                    if (rule != null)
                    {
                        AddOrUpdateSnapshotRule(rule);
                    }
                    break;

                case "Delete":
                    var res = MessageBox.Show("Do you also want to delete existing Snapshots ?", "Confirm", MessageBoxButtons.YesNoCancel);

                    if (res != DialogResult.Cancel)
                    {
                        DeleteSnapshotRule(rule, res == DialogResult.Yes);
                    }

                    break;
                }
            }
        }
Beispiel #9
0
        private void btnAddSchedule_Click(object sender, EventArgs e)
        {
            SnapshotRule rule = EditSnapshotRuleForm.DisplayInstance();

            if (rule != null)
            {
                AddOrUpdateSnapshotRule(rule);
            }
        }
Beispiel #10
0
        private void InitEditSchedule(SnapshotRule sched)
        {
            // Daily frequency
            SelectDailyFreq(sched.DailyFreq);
            dtpDailyFreqOnce.Value = sched.DailyFreqOnce;

            if (sched.DailyFreqEvery % 60 == 0)
            {
                nbDailyFreqEvery.Value         = sched.DailyFreqEvery / 60;
                cbDailyFreqEvery.SelectedIndex = 1;
            }
            else
            {
                nbDailyFreqEvery.Value         = sched.DailyFreqEvery;
                cbDailyFreqEvery.SelectedIndex = 0;
            }

            cbDailyFreqEveryExcluding.Checked    = sched.DailyFreqEveryExcluding;
            dtpDailyFreqEveryExcludingFrom.Value = sched.DailyFreqEveryExcludingFrom;
            dtpDailyFreqEveryExcludingTo.Value   = sched.DailyFreqEveryExcludingTo;


            // Frequency
            SelectFreq(sched.Freq);

            nbFreqDaily.Value = sched.FreqDailyEvery;

            SelectFreqWeekly(sched.FreqWeekly);
            nbFreqWeekly.Value = sched.FreqWeeklyEvery;

            for (int i = 0; i < DayCheckboxes.Length; i++)
            {
                DayCheckboxes[i].Checked = sched.FreqWeeklyDays.Contains(i);
            }

            for (int i = 0; i < cblFreqMonthlyMonths.Items.Count; i++)
            {
                cblFreqMonthlyMonths.SetItemChecked(i, sched.FreqMonthlyMonths.Contains(i));
            }

            for (int i = 0; i < cblFreqMonthlyDays.Items.Count; i++)
            {
                cblFreqMonthlyDays.SetItemChecked(i, sched.FreqMonthlyDays.Contains(i));
            }

            tbFreqCron.Text                = sched.FreqCron;
            cbFreqCronExcluding.Checked    = sched.FreqCronDailyExcluding;
            dtpFreqCronExcludingFrom.Value = sched.FreqCronDailyExcludingFrom;
            dtpFreqCronExcludingTo.Value   = sched.FreqCronDailyExcludingTo;


            // Period
            dtpPeriodStart.Value = sched.PeriodStart;

            SelectPeriodEnd(sched.PeriodEndEnabled == false);
            dtpPeriodEnd.Value = sched.PeriodEnd;
        }
        //
        // Misc

        private void FillAdvanced(SnapshotRule rule)
        {
            rule.VssContext             = (VssSnapshotContextInternal)cbSnapContext.SelectedValue;
            rule.VssBackupType          = (VssBackupTypeInternal)cbSnapType.SelectedValue;
            rule.VssIncludeWriters      = new List <string>(tbSnapInclWriters.Text.Split(','));
            rule.VssExcludeWriters      = new List <string>(tbSnapExclWriters.Text.Split(','));
            rule.MaxRetryCount          = (int)nbSnapFailRetryCount.Value;
            rule.RetryRestartVSSService = cbSnapFailRestartVSS.Checked;
            //rule.PruningStrategy = (PruningStrategy)cbPruningStrategy.SelectedItem;
        }
 public void InitEditAdvanced(SnapshotRule rule)
 {
     cbSnapContext.SelectedIndex  = cbSnapContext.Items.IndexOf(rule.VssContext);
     cbSnapType.SelectedIndex     = cbSnapType.Items.IndexOf(rule.VssBackupType);
     tbSnapInclWriters.Text       = String.Join(",", rule.VssIncludeWriters);
     tbSnapExclWriters.Text       = String.Join(",", rule.VssExcludeWriters);
     nbSnapFailRetryCount.Value   = rule.MaxRetryCount;
     cbSnapFailRestartVSS.Checked = rule.RetryRestartVSSService;
     //cbPruningStrategy.SelectedIndex = cbPruningStrategy.Items.IndexOf(rule.PruningStrategy);
 }
Beispiel #13
0
        private void InitEditBackup(SnapshotRule sched)
        {
            //cbBackupEnable.Checked = sched.BackupEnabled;

            glBackup.DataSource = sched.BackupRules.ToArray();

            nbBackupTotalMaxSize.Value             = sched.BackupTotalMaxSize;
            cbBackupTotalMaxSizeUnit.SelectedIndex = cbBackupTotalMaxSizeUnit.Items.IndexOf(sched.BackupTotalMaxSizeUnit);

            cbBackupNotificationsEnabled.Checked = sched.BackupNotify;
        }
        private void InitEdit(SnapshotRule rule)
        {
            tbName.Text       = rule.Name;
            cbEnabled.Checked = rule.Enabled;

            btnCreate.Text = "Save";

            InitEditSchedule(rule);
            InitEditGeneral(rule);
            InitEditBackup(rule);
            InitEditAdvanced(rule);
        }
Beispiel #15
0
        public static ICalendar GetCalendar(this SnapshotRule rule)
        {
            if (rule.HasCalendar() == false)
            {
                return(null);
            }

            // Modify here to handle several calendar if necessary in the future
            // Beware of calling GetExcludingDayRange if no exclusion range enabled
            rule.GetExcludingDayRange(out DateTime from, out DateTime to);
            return(new DailyCalendar(from, to));
        }
Beispiel #16
0
        public bool DeleteRule(SnapshotRule rule, bool deleteSnapshots)
        {
            try
            {
                return(RuleMgr.Instance.DeleteRule(rule, deleteSnapshots));
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to delete rule {Id}", rule?.Id);

                return(false);
            }
        }
Beispiel #17
0
        private IEnumerable <SnapshotInstance> GetPruningListForRule(SnapshotRule rule)
        {
            List <SnapshotInstance> instances = SnapshotRuleInstanceMap.SafeGet(rule.Id);

            if (instances == null)
            {
                return(new List <SnapshotInstance>());
            }

            long lifetime = rule.GetLifeTimeSeconds();

            return(instances.Where(i => i.CreatedAt.AddSeconds(lifetime) < DateTime.Now).ToList());
        }
Beispiel #18
0
        public void InitEditGeneral(SnapshotRule rule)
        {
            nbLifetime.Value         = rule.LifeTimeValue;
            cbLifetime.SelectedIndex = cbLifetime.Items.IndexOf(rule.LifeTimeUnit);

            for (int i = 0; i < cblDriveLetters.Items.Count; i++)
            {
                if (rule.Volumes.Contains(((Volume)cblDriveLetters.Items[i]).DeviceID))
                {
                    cblDriveLetters.SetItemChecked(i, true);
                }
            }
        }
        public static SnapshotRule DisplayInstance(SnapshotRule schedule = null)
        {
            _instance = _instance ?? new EditSnapshotRuleForm(schedule);

            _instance.ShowDialog();

            SnapshotRule ret = _instance.DialogResult == DialogResult.Yes
        ? _instance.SnapshotRule
        : null;

            _instance = null;

            return(ret);
        }
Beispiel #20
0
        public void DeleteAllForRule(VssClient vss, SnapshotRule rule)
        {
            lock (_lockProcessing)
            {
                IEnumerable <SnapshotInstance> allRuleInstances = SafeGetInstances(rule.Id);

                foreach (SnapshotInstance instance in allRuleInstances)
                {
                    vss.DeleteSnapshot(instance.SnapshotId);
                }

                CleanupInstances(allRuleInstances.Select(i => i.SnapshotId));
            }
        }
Beispiel #21
0
        public bool AddOrUpdateRule(SnapshotRule rule)
        {
            try
            {
                RuleMgr.Instance.AddOrUpdateRule(rule);

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to add/update rule {Id}", rule?.Id);

                return(false);
            }
        }
        //
        // Misc

        private SnapshotRule GenerateSchedule()
        {
            var ret = new SnapshotRule()
            {
                Id      = RuleId > 0 ? RuleId : DateTime.Now.Ticks,
                Name    = tbName.Text,
                Enabled = cbEnabled.Checked
            };

            FillSchedule(ret);
            FillGeneral(ret);
            FillBackup(ret);
            FillAdvanced(ret);

            return(ret);
        }
Beispiel #23
0
        private bool AddOrUpdateSnapshotRule(SnapshotRule rule)
        {
            try
            {
                SnapshotClient.AddOrUpdateRule(rule);

                dgSnapshotRules.DataSource = SnapshotClient.GetRules();

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error while requesting Addition/Edition of SnapshotRule {Id}.", rule?.Id);

                return(false);
            }
        }
Beispiel #24
0
        private bool DeleteSnapshotRule(SnapshotRule rule, bool deleteSnapshots)
        {
            try
            {
                SnapshotClient.DeleteRule(rule, deleteSnapshots);

                dgSnapshotRules.DataSource = SnapshotClient.GetRules();

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error while requesting Deletion of SnapshotRule {Id}.", rule?.Id);

                return(false);
            }
        }
Beispiel #25
0
        public static string GetHumanizedSchedule(SnapshotRule rule)
        {
            string descriptorCron = GenerateCron(rule, true);

            string expr = ExpressionDescriptor.GetDescription(descriptorCron);

            if (rule.IsExcludingDayRange())
            {
                rule.GetExcludingDayRange(out DateTime from, out DateTime to);

                expr += String.Format(", Excluding from {0} to {1}",
                                      from.ToShortTimeString(),
                                      to.ToShortTimeString()
                                      );
            }

            return(expr);
        }
Beispiel #26
0
        public void AddOrUpdateRule(SnapshotRule newRule)
        {
            SnapshotRule oldRule = RulesMap.SafeGet(newRule.Id);

            if (oldRule != null)
            {
                newRule.CopyProperties(oldRule);
            }

            else
            {
                RulesMap[newRule.Id] = newRule;
            }

            SaveRules();

            VssScheduler.CreateAllTriggers(Rules);
        }
        //
        // Constructors

        protected EditSnapshotRuleForm(SnapshotRule rule)
        {
            InitializeComponent();
            SyncContext = SynchronizationContext.Current;

            InitBase();

            if (rule != null)
            {
                RuleId   = rule.Id;
                RuleName = rule.Name;
                InitEdit(rule);
            }
            else
            {
                RuleId = -1;
            }

            Init = true;
            RefreshUI();
        }
Beispiel #28
0
        public static ITrigger GetTrigger(this SnapshotRule rule)
        {
            TriggerBuilder builder = TriggerBuilder.Create()
                                     .WithIdentity(rule.ToString(), "SnapshotJob")
                                     .WithCronSchedule(rule.GeneratedCron)
                                     .StartAt(rule.PeriodStart)
                                     .UsingJobData("RuleId", rule.Id)
                                     .WithDescription(rule.ToString());

            if (rule.PeriodEndEnabled)
            {
                builder = builder.EndAt(rule.PeriodEnd);
            }

            if (rule.HasCalendar())
            {
                builder = builder.ModifiedByCalendar(rule.GetCalendarName());
            }

            return(builder.Build());
        }
Beispiel #29
0
        public static void GetExcludingDayRange(
            this SnapshotRule rule,
            out DateTime from,
            out DateTime to)
        {
            if (rule.Freq == Freq.Cron && rule.FreqCronDailyExcluding)
            {
                from = rule.FreqCronDailyExcludingFrom;
                to   = rule.FreqCronDailyExcludingTo;
            }

            else if (rule.DailyFreq == DailyFreq.Every && rule.DailyFreqEveryExcluding)
            {
                from = rule.DailyFreqEveryExcludingFrom;
                to   = rule.DailyFreqEveryExcludingTo;
            }

            else
            {
                throw new InvalidOperationException("GetExcludingDayRange called without excluding one");
            }
        }
Beispiel #30
0
 private void FillGeneral(SnapshotRule rule)
 {
     rule.LifeTimeValue = (int)nbLifetime.Value;
     rule.LifeTimeUnit  = (Timespan)cbLifetime.SelectedValue;
     rule.Volumes       = new List <string>(cblDriveLetters.CheckedItems.Select(v => ((Volume)v).DeviceID));
 }