public void FM_Callback(BackupDetail details)
        {
            switch (details.Type)
            {
            case Enums.BackupDetailType.ProcessFile:
                previousDetails           = details;
                CurrentFolder             = details.CurrentFolder;
                MaxFilesCurrentFolder     = details.MaxFilesFound;
                CurrentCountCurrentFolder = details.CurrentFileNumber;

                // Add the overall previous totals to the current counts
                ProcessedCount = details.ProcessedCount + DetailsTotals.ProcessedCount;
                CopiedCount    = details.CopiedCount + DetailsTotals.CopiedCount;

                OnPropertyChanged("CurrentFolder");
                OnPropertyChanged("MaxFilesCurrentFolder");
                OnPropertyChanged("CurrentCountCurrentFolder");
                OnPropertyChanged("ProcessedCount");
                OnPropertyChanged("CopiedCount");
                break;

            case Enums.BackupDetailType.ResultMessage:
                ErrorMessage = details.ResultMessage;
                _exitOnEnd   = false;
                break;

            default:
                break;
            }
        }
Пример #2
0
        private void SaveBackupDetails()
        {
            BackupDetail backupDetail = new BackupDetail();

            backupDetail.BackupFolder = txtPath.Text;
            backupDetail.Comments     = txtComment.Text;
            backupDetail.EndTime      = endTime;
            backupDetail.StartTime    = startTime;
            backupDetail.BackupDate   = DateTime.Now;

            new GenericService <BackupDetail>().Add(backupDetail);
        }
Пример #3
0
        public void GetTest()
        {
            var testList = backupDetailsTestList;

            var first = new BackupDetail();

            first = testList.First().DeepCopy();

            var gotObject = testList.Get(first.ID);

            Assert.Equal(first, gotObject);
        }
Пример #4
0
        private static List <BackupDetail> BackupDetailsTestList()
        {
            var backupDetails = new List <BackupDetail>();

            for (int i = 0; i < 10; i++)
            {
                var backupDetail = new BackupDetail();
                backupDetail.ActualName      = $"test{i}";
                backupDetail.BackupDirectory = i % 2 == 0 ? BackupDirectory.Other : BackupDirectory.Roms;
                backupDetail.BaseDirectory   = $"test/dir{i}";
                backupDetail.FileSystemType  = i % 2 != 0 ? FileSystemType.Directory : FileSystemType.File;
                backupDetail.ID        = Guid.NewGuid();
                backupDetail.SavedName = $"test{i}";

                backupDetails.Add(backupDetail);
            }

            return(backupDetails);
        }
Пример #5
0
        public IActionResult NewEdit(string id, string fileSystemType, string backupDirectory, string actualName)
        {
            var backupDetail = BackupDetails.StoredBackupDetails().Get(id);

            if (backupDetail != null)
            {
                ViewBag.PageType = "Edit";
                return(View("NewEdit", backupDetail));
            }
            else
            {
                backupDetail = new BackupDetail();
                backupDetail.FileSystemType  = BackupDetails.FileSystemType_TryParse(fileSystemType);
                backupDetail.BackupDirectory = BackupDetails.BackupDirectory_TryParse(backupDirectory);
                backupDetail.ActualName      = actualName;
            }

            ViewBag.PageType = "New";
            return(View("NewEdit", backupDetail));
        }
Пример #6
0
        public IActionResult NewEditPost(BackupDetail model)
        {
            if (ModelState.IsValid)
            {
                var backupDetail = BackupDetails.StoredBackupDetails().Get(model.ID);

                List <BackupDetail> storedBackupDetails;

                var newBackupDetail = false;
                if (backupDetail != null)
                {
                    storedBackupDetails = BackupDetails.StoredBackupDetails().Exclude(model.ID);
                }
                else
                {
                    newBackupDetail     = true;
                    storedBackupDetails = BackupDetails.StoredBackupDetails();
                }

                // Check if file/folder is already recorded
                if (storedBackupDetails.Where(sbd => sbd.BaseDirectory == model.BaseDirectory && sbd.BackupDirectory == model.BackupDirectory &&
                                              sbd.ActualName == model.ActualName && sbd.FileSystemType == model.FileSystemType).Count() == 0)
                {
                    model.BackedUp = false;

                    if (newBackupDetail)
                    {
                        string name = "";

                        if (model.FileSystemType == FileSystemType.File)
                        {
                            if (!storedBackupDetails.Exists(sbd => sbd.SavedName == model.ActualName && sbd.FileSystemType == model.FileSystemType && sbd.BackupDirectory == model.BackupDirectory))
                            {
                                name = model.ActualName;
                            }
                            else
                            {
                                var counter = 2;
                                name = counter + "-" + model.ActualName;

                                while (model.FileSystemType == FileSystemType.File && (System.IO.File.Exists(Path.Combine(BackupDetails.ServerDir(), model.BackupDirectory.ToString(), name)) ||
                                                                                       storedBackupDetails.Exists(sbd => sbd.SavedName == name && sbd.FileSystemType == model.FileSystemType)))
                                {
                                    counter++;
                                    name = counter + "-" + model.ActualName;
                                }
                            }
                        }
                        else
                        {
                            if (!storedBackupDetails.Exists(sbd => sbd.SavedName == model.ActualName && sbd.FileSystemType == model.FileSystemType && sbd.BackupDirectory == model.BackupDirectory))
                            {
                                name = model.ActualName;
                            }
                            else
                            {
                                var counter = 2;
                                name = counter + "-" + model.ActualName;

                                while (model.FileSystemType == FileSystemType.Directory && (System.IO.Directory.Exists(Path.Combine(BackupDetails.ServerDir(), model.BackupDirectory.ToString(), name)) ||
                                                                                            storedBackupDetails.Exists(sbd => sbd.SavedName == name && sbd.FileSystemType == model.FileSystemType)))
                                {
                                    counter++;
                                    name = counter + "-" + model.ActualName;
                                }
                            }
                        }

                        if (string.IsNullOrEmpty(name))
                        {
                            throw new Exception("Saved name is empty or null.");
                        }
                        else
                        {
                            model.SavedName = name;
                            model.ID        = Guid.NewGuid();
                            storedBackupDetails.Add(model);
                        }
                    }
                    else
                    {
                        storedBackupDetails.Add(model);
                    }

                    storedBackupDetails.Save();

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", $"This {model.FileSystemType.ToString().ToLower()} is already recorded, search for and edit the {model.FileSystemType.ToString().ToLower()}.");
                }
            }

            return(View("NewEdit", model));
        }
Пример #7
0
        public void ProcessFolder(Config config, List <string> ignoreFolders)
        {
            if (!Directory.Exists(config.Source))
            {
                throw new System.ArgumentException($"Error.  Source folder does not exist.  {config.Source}", "original");
            }
            List <string> folders = new List <string>();

            folders.Add(config.Source);
            folders.AddRange(Directory.GetDirectories(config.Source, "*.*", SearchOption.AllDirectories));

            // Remove any folders matching the ignore list
            foreach (string ignoreFolder in ignoreFolders)
            {
                folders = folders.Where(x => !x.StartsWith(ignoreFolder, StringComparison.OrdinalIgnoreCase)).ToList();
            }

            foreach (string folder in folders)
            {
                //if (folder.IndexOf("MyBackupSoftware") > 0 && folder.IndexOf(".git") > 0)
                //{
                //    var x = 1;
                //}
                //foreach (string ignoreFolder in ignoreFolders)
                //{
                //    // Skip this folder if it's in the ignoreFolder list or one of it's children
                //    int length = ignoreFolder.Length;
                //    if (folder.Length >= length && ignoreFolder.Equals(folder.Substring(0, length), StringComparison.OrdinalIgnoreCase))
                //    {
                //        continue;
                //    }
                //}
                // Skip the folder if it's in the ignore list
                //if (ignoreFolders.Count(x =>x.Equals(folder, StringComparison.OrdinalIgnoreCase)) > 0)
                //{
                //    continue;
                //}
                Debug.WriteLine($"Scanning folder {folder} for files");
                string[] files = Directory.GetFiles(folder, "*.*", SearchOption.TopDirectoryOnly);
                Debug.WriteLine($"Done scanning folder {folder} for files.  Calling ProcessFolder.");
                try
                {
                    ProcessFolder(config, files);
                }
                catch (Exception ex)
                {
                    BackupDetail details = new BackupDetail();
                    details.Type          = Enums.BackupDetailType.ResultMessage;
                    details.ResultMessage = $"Error: {ex.Message}\r\n{ex.InnerException}";
                    if (Callback != null)
                    {
                        Callback(details);
                    }
                }
                if (_cancelFlag)
                {
                    break;
                }
                while (_pauseFlag)
                {
                    // Pause the thread for a second until the user releases it
                    Thread.Sleep(1000);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Process the found files
        /// </summary>
        /// <param name="config"></param>
        /// <param name="files"></param>
        public void ProcessFolder(Config config, string[] files)
        {
            int cc = files.Count();

            //Console.WriteLine($"Comparing {cc} files");
            Debug.WriteLine($"Processing {cc} files");
            //int copied = 0;
            BackupDetail details = new BackupDetail();

            details.MaxFilesFound     = files.Count();
            details.CurrentFileNumber = 0;
            foreach (var item in files)
            {
                string itemFolder = Path.GetDirectoryName(item);
                details.CurrentFileNumber++;
                details.CurrentFolder = itemFolder;
                details.Type          = Enums.BackupDetailType.ProcessFile;
                if (Callback != null)
                {
                    Callback(details);
                }


                // In order to correctly do subfolders, we need to copy it to the matching subfolder on the destination
                // If the file is "sourcefolder\foo\myfile.txt", we need to copy it to "destinationfolder\foo\myfile.txt"
                //   so we need to take the directory name and remove the sourcefolder part
                int sourceFolderLength = config.Source.Length;
                if (itemFolder.Length < sourceFolderLength)
                {
                    details.Type          = Enums.BackupDetailType.ResultMessage;
                    details.ResultMessage = $"Error.Length of found file is too short!";
                    if (Callback != null)
                    {
                        Callback(details);
                    }
                    //throw new System.ArgumentException($""Error.  Length of found file is too short!"", "original");
                }
                // If the length is identical, it's not a subfolder
                // If the length is greater, it is a subfolder, and we only want everything after the first X characters (x being the length variable)
                int    itemFolderLength = itemFolder.Length;
                string itemName         = item;
                if (itemFolder.Length > sourceFolderLength)
                {
                    // Just grab everything from "\foo" and after from "sourcefolder\foo\myfile.txt"
                    itemName = item.Substring(sourceFolderLength + 1);
                }
                else
                {
                    itemName = Path.GetFileName(item);
                }

                string filename          = Path.GetFileName(item);
                string destinationName   = $"{config.Destination}\\{itemName}";
                string destinationFolder = Path.GetDirectoryName(destinationName);
                if (!Directory.Exists(destinationFolder))
                {
                    Directory.CreateDirectory(destinationFolder);
                }
                if (!FileMatch(item, destinationName))
                {
                    details.LastFileCopied = destinationName;
                    FileCopy(item, destinationName);
                    details.CopiedCount++;
                }
                details.ProcessedCount++;
                details.Type = Enums.BackupDetailType.ProcessFile;
                if (Callback != null)
                {
                    Callback(details);
                }
            }
            //Console.WriteLine($"Updated {copied} items");
        }