Esempio n. 1
0
        public MainWindow()
        {
            InitializeComponent();
            var viewModel = new BackupInfoViewModel();

            this.DataContext = viewModel;
        }
Esempio n. 2
0
        /// <summary>
        /// Remove a backup job from the jobs pool with given BackupItem HashCode.
        /// </summary>
        /// <param name="backupItemHashCode"></param>
        public static async Task DequeueBackupJob(string backupItemHashCode)
        {
            // First, check the backupItemHashCode to make sure it is something that is usable.
            if (string.IsNullOrWhiteSpace(backupItemHashCode) ||
                backupItemHashCode.Length != 64)
            {
                return;
            }

            Debug.WriteLine(string.Format("'{0}' removed at: {1}", backupItemHashCode.Substring(0, 5), DateTime.Now));
            // Grab the Scheduler instance from the Factory
            try
            {
                IScheduler scheduler = await _SchedulerFactory.GetScheduler();

                JobKey jobToCheck = new JobKey(backupItemHashCode, "ActiveBackups");

                if (await scheduler.CheckExists(jobToCheck))
                {
                    await scheduler.DeleteJob(jobToCheck);

                    BackupInfoViewModel.SetBackupItemStatus(backupItemHashCode, (int)StatusCodes.UNQUEUED);
                }

                BackupInfoViewModel.SaveConfig();
                //Debug.WriteLine("Job de-queued, saving config...");
            }
            catch (Exception e)
            {
                Debug.WriteLine("RemoveBackupJob: " + e.Message);
            }
        }
Esempio n. 3
0
        public static async Task QueueBackupJob(BackupItem backupItem)
        {
            try
            {
                //Debug.WriteLine(backupItem.HashCode);
                // First, check the HashCode of the BackupItem to make sure we have the info to make a job.
                if (string.IsNullOrWhiteSpace(backupItem.HashCode) ||
                    backupItem.HashCode.Length != 64)
                {
                    return;
                }

                // Grab the Scheduler instance from the Factory
                IScheduler scheduler = await _SchedulerFactory.GetScheduler();

                // Check if the job exists.
                JobKey jobID = new JobKey(backupItem.HashCode, "ActiveBackups");

                if (await scheduler.CheckExists(jobID))
                {
                    await scheduler.DeleteJob(jobID);
                }

                // Define the CopyJob.
                IJobDetail job = JobBuilder.Create <BackupJob>()
                                 .WithIdentity(jobID)
                                 .UsingJobData("originPath", backupItem.OriginPath)
                                 .UsingJobData("backupPath", backupItem.BackupPath)
                                 .Build();

                // Setup the job trigger.
                ITrigger trigger = TriggerBuilder.Create()
                                   .WithIdentity(backupItem.HashCode, "ActiveBackups")
                                   .StartAt(backupItem.NextBackupDate)
                                   .WithSimpleSchedule(x => x
                                                       .WithIntervalInHours(backupItem.BackupInterval.Days * 24)
                                                       .RepeatForever()
                                                       .WithMisfireHandlingInstructionFireNow())
                                   .Build();

                // Tell quartz to schedule the job using our trigger.
                await scheduler.ScheduleJob(job, trigger);

                // Update the BackupItem to indicate that its BackupJob has been successfully queued.
                BackupInfoViewModel.SetBackupItemStatus(backupItem.HashCode, (int)StatusCodes.QUEUED);

                BackupInfoViewModel.SaveConfig();

                Debug.WriteLine(string.Format("'{0}' running at: {1} and ticking every {2} days(s)", backupItem.HashCode.Substring(0, 5), backupItem.NextBackupDate, backupItem.BackupInterval.Days));

                //Debug.WriteLine("Job queued, saving config...");
            }
            catch (Exception e)
            {
                Debug.WriteLine("QueueBackupJob: " + e.Message);
            }
        }
Esempio n. 4
0
        public async Task Execute(IJobExecutionContext context)
        {
            //Debug.WriteLine(string.Format("'{0}' tick: {1}", context.JobDetail.Key.Name.ToString().Substring(0, 5), DateTime.Now));

            // Grab the Scheduler instance from the Factory
            IScheduler scheduler = await TaskManager.SchedulerFactory.GetScheduler();

            // Get the list of all active hash codes according to the view model.
            List <string> jobHashCodes = BackupInfoViewModel.GetAllActiveHashCodes();

            // Get all running job names aka hash codes from the scheduler.
            GroupMatcher <JobKey> groupMatcher = GroupMatcher <JobKey> .GroupContains("ActiveBackups");

            // Get all JobKeys using groupMatcher.
            IReadOnlyCollection <JobKey> keys = await scheduler.GetJobKeys(groupMatcher);

            // Loop through the active jobs according to the viewmodel and and add orphaned job hash codes to a list to be removed.
            List <string> jobsToRemove = new List <string>();

            foreach (JobKey jobKeys in keys)
            {
                if (!jobHashCodes.Contains(jobKeys.Name))
                {
                    jobsToRemove.Add(jobKeys.Name);
                }
            }

            // Now loop through the jobsToRemove list and tell the TaskManager to remove those jobs.
            foreach (string jobHashCode in jobsToRemove)
            {
                TaskManager.DequeueBackupJob(jobHashCode);
            }

            // Finally, check to make sure BackupItems in the main view are scheduled and running.
            BackupInfoViewModel.QueueAllJobs();
        }
 public void Execute(object parameter)
 {
     BackupInfoViewModel.SaveConfig();
 }
 /// <summary>
 /// Initialize a new instance of the LoadConfigCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public LoadConfigCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }
 /// <summary>
 /// Initialize a new instance of the ResetConfigCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public ResetConfigCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }
Esempio n. 8
0
 /// <summary>
 /// Initialize a new instance of the DeleteBackupItemCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public DeleteBackupItemCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }
 /// <summary>
 /// Initialize a new instance of the AddBackupItemCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public AddBackupItemCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }
Esempio n. 10
0
        public async Task Execute(IJobExecutionContext context)
        {
            Debug.WriteLine(string.Format("'{0}' tick: {1}", context.JobDetail.Key.Name.ToString().Substring(0, 5), DateTime.Now));

            // Notify the UI that the job is now running.
            BackupInfoViewModel.SetBackupItemStatus(context.JobDetail.Key.Name.ToString(), (int)StatusCodes.RUNNING);

            JobDataMap dataMap = context.JobDetail.JobDataMap;

            string originPath = dataMap.GetString("originPath");
            string backupPath = dataMap.GetString("backupPath");

            if (originPath.EndsWith(@"\"))
            {
                // Copy directory
                try
                {
                    Debug.WriteLine(string.Format("{0} Copying directory: '{1}' to '{2}'", DateTime.Now, originPath, backupPath));

                    RoboCommand roboCopy = new RoboCommand();

                    // Copy options
                    roboCopy.CopyOptions.Source             = originPath;
                    roboCopy.CopyOptions.Destination        = Path.Combine(backupPath, Path.GetFileName(Path.GetDirectoryName(originPath)));
                    roboCopy.CopyOptions.CopySubdirectories = true;
                    roboCopy.CopyOptions.UseUnbufferedIo    = true;
                    roboCopy.CopyOptions.Mirror             = true;

                    // Selection options
                    roboCopy.SelectionOptions.IncludeSame    = false;
                    roboCopy.SelectionOptions.IncludeTweaked = true;
                    roboCopy.SelectionOptions.ExcludeOlder   = true;

                    // Retry options
                    roboCopy.RetryOptions.RetryCount    = 3;
                    roboCopy.RetryOptions.RetryWaitTime = 5;

                    // Start and wait for the robocopy to finish.
                    await roboCopy.Start();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error in BackupJob.Execute() folder " + e.Message);

                    // Notify the UI that the job has errored.
                    BackupInfoViewModel.SetBackupItemStatus(context.JobDetail.Key.Name.ToString(), (int)StatusCodes.ERROR);
                }
            }
            else
            {
                // Copy file
                try
                {
                    Debug.WriteLine(string.Format("{0} Copying file: '{1}' to '{2}'", DateTime.Now, originPath, backupPath));

                    RoboCommand roboCopy = new RoboCommand();

                    //Debug.WriteLine(Path.GetDirectoryName(originPath));
                    //Debug.WriteLine(backupPath);
                    //Debug.WriteLine(Path.GetFileName(originPath));

                    // Copy options
                    roboCopy.CopyOptions.Source      = Path.GetDirectoryName(originPath);
                    roboCopy.CopyOptions.Destination = backupPath;
                    roboCopy.CopyOptions.FileFilter  = new string[] { Path.GetFileName(originPath) };

                    // Selection options
                    roboCopy.SelectionOptions.IncludeSame    = false;
                    roboCopy.SelectionOptions.IncludeTweaked = true;
                    roboCopy.SelectionOptions.ExcludeOlder   = true;

                    // Retry options
                    roboCopy.RetryOptions.RetryCount    = 3;
                    roboCopy.RetryOptions.RetryWaitTime = 5;

                    // Start and wait for the robocopy to finish.
                    await roboCopy.Start();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error in BackupJob.Execute() file" + e.Message);

                    // Notify the UI that the job has errored.
                    BackupInfoViewModel.SetBackupItemStatus(context.JobDetail.Key.Name.ToString(), (int)StatusCodes.ERROR);
                }
            }
            Debug.WriteLine(string.Format("Copy job '{0}' completed at: {1}", context.JobDetail.Key.Name.ToString().Substring(0, 5), DateTime.Now));

            // If the backup was successful, then update information for the item and save the BackupItem collection config to file.
            BackupInfoViewModel.NotifyItemHasBeenBackedUp(context.JobDetail.Key.Name);
            BackupInfoViewModel.UpdateNextBackupDate(context.JobDetail.Key.Name);
            BackupInfoViewModel.SaveConfig();
        }
 /// <summary>
 /// Initialize a new instance of the ToggleRunOnStartupCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public ToggleRunOnStartupCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }
 /// <summary>
 /// Initialize a new instance of the SelectOriginFolderDialogCommand class.
 /// </summary>
 /// <param name="viewModel"></param>
 public SelectOriginFolderDialogCommand(BackupInfoViewModel viewModel)
 {
     _ViewModel = viewModel;
 }