/// <summary> /// Starts a backup action, using a file-to-file comparison to look for changes. /// Safer than database backups, allows for modification of jobs at the user's risk. However, can be bottlenecked by IO speeds. /// </summary> /// <param name="action">The action containing the source, destination and files to backup</param> public void Start(BackupAction backupAction) { action = backupAction; Console.WriteLine("Proceeding with a simple file cross-comparison backup job"); Console.WriteLine("Difference mechanism: Write-time based"); Console.WriteLine("Backing up " + action.SourcePath + " to " + action.DestinationPath); List <string> deletedFiles = RecursiveFileFinder.ProcessPath(action.DestinationPath, false); List <string> relativeFiles = new List <string>(); for (int i = 0; i < action.FilesToCopy.Count; i++) { relativeFiles.Add(action.FilesToCopy[i].Replace(action.SourcePath, "").Trim('\\').Trim('/')); } deletedFiles.RemoveAll((s) => relativeFiles.Contains(s.Replace(action.DestinationPath, "").Trim('\\').Trim('/'))); deletedFiles.RemoveAll((s) => s.Contains(".copyMinus")); foreach (var file in action.FilesToCopy) { string targetPath = action.DestinationPath + (file.Replace(action.SourcePath, "")); if (File.Exists(targetPath)) { if (action.Comparator.WasFileUpdated(file, targetPath)) // file changed { PushNewCopy(file, targetPath); Console.WriteLine("Replaced file " + Path.GetFileName(targetPath)); } else { if (Program.Verbose) { Console.WriteLine("File doesn't need replacing " + Path.GetFileName(targetPath)); } } } else { if (!Directory.Exists(targetPath.Replace(Path.GetFileName(file), ""))) { Directory.CreateDirectory(targetPath.Replace(Path.GetFileName(file), "")); //ensure the directory to backup to exists } File.Copy(file, targetPath, false); Console.WriteLine("Added new file " + Path.GetFileName(targetPath)); } } foreach (var file in deletedFiles) { PushNewCopy(null, file); } Console.WriteLine("Done!"); }
/// <summary> /// Starts a backup action, using an internal database to speed up file comparation. /// Good for cases of relatively slow IO speeds. /// </summary> /// <param name="action">The action containing the source, destination and files to backup</param> public void Start(BackupAction backupAction) { action = backupAction; Console.WriteLine("Proceeding with a database based comparison backup job"); Console.WriteLine("Difference mechanism: Write-time based"); Console.WriteLine("Backing up " + action.SourcePath + " to " + action.DestinationPath); using (var db = new FileContext(action.ActionName)) { db.Database.EnsureCreated(); deletedFiles = new List <ProcessedFile>(db.Files.Where((f) => f.FilePath.Contains(action.SourcePath))); ProcessedFile cataloguedFile; foreach (var file in action.FilesToCopy) { string targetPath = action.DestinationPath + (file.Replace(action.SourcePath, "")); if ((cataloguedFile = db.Files.Find(file, targetPath)) == null) { CatalogueAndCopyNewFile(db, targetPath, file); } else { deletedFiles.Remove(cataloguedFile); ReplaceCataloguedFile(db, cataloguedFile); }; } foreach (var file in deletedFiles) { PushNewCopy(null, file.BackupPath); db.Files.Remove(file); } var count = db.SaveChanges(); if (Program.Verbose) { Console.WriteLine("{0} records saved to database", count); Console.WriteLine(); Console.WriteLine("All files in database:"); foreach (var file in db.Files) { Console.WriteLine(" - {0}", file.FileName); } } } }