Ejemplo n.º 1
0
        protected override void Backup(string sourcePath, List <string> destinationPaths)
        {
            //oprava formatu sourcePath
            if (sourcePath.Contains(@"\\"))
            {
                sourcePath = sourcePath.Replace(@"\\", "\\");
            }


            for (int i = 0; i < destinationPaths.Count; i++)
            {
                if (destinationPaths[i].Contains(@"\\"))
                {
                    destinationPaths[i] = destinationPaths[i].Replace(@"\\", "\\");
                }
            }

            string newLogPath = "";

            this.newLog = new List <LogModel>();

            //Zmena destinationPath I
            for (int i = 0; i < destinationPaths.Count; i++)
            {
                destinationPaths[i] += $"\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
            }

            //Zjisteni slozek
            string[] sourceFolder;
            int      cnt = 0;

            try
            {
                sourceFolder = Directory.GetDirectories(sourcePath, "*", SearchOption.TopDirectoryOnly);
            }
            catch
            {
                return;
            }

            foreach (string item in sourceFolder)
            {
                if (item.Contains("incr_backup"))
                {
                    cnt++;
                }
            }

            //Nalezeni posledniho logu
            List <LogModel> oldLog     = new List <LogModel>();
            string          oldLogPath = "";

            //vytvoreni slozek v sourcePath pro log
            if (!this.backup.Override)
            {
                if (cnt == 0)
                {
                    try
                    {
                        Directory.CreateDirectory(sourcePath + "\\incr_backup_0");
                        oldLogPath = sourcePath + "\\BackupsLog.log";
                        for (int i = 0; i < destinationPaths.Count; i++)
                        {
                            destinationPaths[i] += $"\\incr_backup";
                        }
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            ProblemPath = sourcePath + "\\incr_backup_0",
                            Problem     = "Can't create the folder for incremental backup. Backup hasn't been done."
                        });
                        return;
                    }
                }
                else
                {
                    try
                    {
                        newLogPath = sourcePath + $"\\incr_backup_{cnt}";
                        Directory.CreateDirectory(newLogPath);
                        oldLogPath = sourcePath + $"\\incr_backup_{cnt - 1}\\BackupsLog.log";
                        //uprava cesty na DestinationPath II
                        for (int i = 0; i < destinationPaths.Count; i++)
                        {
                            destinationPaths[i] += $"\\incr_backup_{cnt}";
                        }
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            ProblemPath = sourcePath + $"\\incr_backup_{cnt}",
                            Problem     = "Can't create the folder for incremental backup. Backup hasn't been done."
                        });
                        return;
                    }
                }
            }
            else
            {
                try
                {
                    Directory.Delete(sourcePath + "\\incr_backup", true);
                }
                catch { }

                try
                {
                    Directory.CreateDirectory(sourcePath + "\\incr_backup");
                    oldLogPath = sourcePath + "\\BackupsLog.log";
                    //uprava cesty na DestinationPath II
                    for (int i = 0; i < destinationPaths.Count; i++)
                    {
                        destinationPaths[i] += $"\\incr_backup";
                    }
                }
                catch (Exception ex)
                {
                    this.reportMaker.AddError(new ErrorDetails()
                    {
                        Exception   = ex.Message,
                        ProblemPath = sourcePath + "\\incr_backup_0",
                        Problem     = "Can't create the folder for incremental backup. Backup hasn't been done."
                    });
                    return;
                }
            }



            //Nacteni logu
            try
            {
                using (StreamReader streamReader = new StreamReader(oldLogPath, false))
                {
                    string logtext = streamReader.ReadToEnd();
                    oldLog = JsonConvert.DeserializeObject <List <LogModel> >(logtext);
                }
            }
            catch (Exception ex)
            {
                this.reportMaker.AddError(new ErrorDetails()
                {
                    Exception   = ex.Message,
                    Problem     = "The program didn't find the full backup log. INCR backup for this source folder hadn't been done.",
                    ProblemPath = sourcePath
                });

                try
                {
                    Directory.Delete(sourcePath + $"//incr_backup{cnt}", true);
                }
                catch { }

                return;
            }

            foreach (string filePath in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories))
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Name == "BackupsLog.log")
                {
                    continue;
                }
                bool found = false;

                foreach (LogModel item in oldLog)
                {
                    if (item.FilePath != new FileInfo(filePath).FullName)
                    {
                        continue;
                    }

                    //nezmeneno - EXISTS
                    if (fileInfo.FullName == item.FilePath && Convert.ToDateTime(fileInfo.LastWriteTime) == item.LastWriteTime)
                    {
                        this.newLog.Add(new LogModel()
                        {
                            Action = "EXI", FileName = fileInfo.Name, FilePath = fileInfo.FullName, LastWriteTime = Convert.ToDateTime(fileInfo.LastWriteTime)
                        });
                        found = true;
                        break;
                    }
                    //zmena zapisu - MODIFIED
                    else if (fileInfo.FullName == item.FilePath && Convert.ToDateTime(fileInfo.LastWriteTime) != item.LastWriteTime)
                    {
                        this.newLog.Add(new LogModel()
                        {
                            Action = "MOD", FileName = fileInfo.Name, FilePath = fileInfo.FullName, LastWriteTime = Convert.ToDateTime(fileInfo.LastWriteTime)
                        });

                        foreach (string destinationPath in destinationPaths)
                        {
                            string dirPath = destinationPath;
                            dirPath += fileInfo.FullName.Replace(sourcePath, "");
                            dirPath  = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);
                            if (!Directory.Exists(dirPath))
                            {
                                try
                                {
                                    Directory.CreateDirectory(dirPath);
                                }
                                catch (Exception ex)
                                {
                                    this.reportMaker.AddError(new ErrorDetails()
                                    {
                                        Exception   = ex.Message,
                                        Problem     = "The program was unable to create directory.",
                                        ProblemPath = dirPath
                                    });
                                    continue;
                                }
                            }

                            try
                            {
                                File.Copy(filePath, dirPath + $"\\{fileInfo.Name}", true);
                                reportMaker.AddFile(new FileInfo(filePath));
                            }
                            catch (Exception ex)
                            {
                                this.reportMaker.AddError(new ErrorDetails()
                                {
                                    Exception   = ex.Message,
                                    Problem     = "The program was unable to copy file.",
                                    ProblemPath = filePath
                                });
                                continue;
                            }
                        }
                        found = true;
                    }
                }
                if (found == true)
                {
                    continue;
                }

                //nenalezene v minulem logu - NEW
                foreach (string destinationPath in destinationPaths)
                {
                    string dirPath = destinationPath;
                    dirPath += fileInfo.FullName.Replace(sourcePath, "");
                    dirPath  = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);

                    if (!Directory.Exists(dirPath))
                    {
                        try
                        {
                            Directory.CreateDirectory(dirPath);
                        }
                        catch (Exception ex)
                        {
                            this.reportMaker.AddError(new ErrorDetails()
                            {
                                Exception   = ex.Message,
                                Problem     = "Backupper was unable to create destination path.",
                                ProblemPath = dirPath
                            });
                        }
                    }
                    try
                    {
                        File.Copy(filePath, dirPath + $"\\{fileInfo.Name}", true);
                        reportMaker.AddFile(new FileInfo(filePath));
                        this.newLog.Add(new LogModel()
                        {
                            Action = "NEW", FileName = fileInfo.Name, FilePath = fileInfo.FullName, LastWriteTime = Convert.ToDateTime(fileInfo.LastWriteTime)
                        });
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            Problem     = "The program was unable to copy file.",
                            ProblemPath = filePath
                        });
                    }
                }
            }

            string newlogpath = (this.backup.Override) ? sourcePath + $"\\incr_backup" + "\\BackupsLog.log" : sourcePath + $"\\incr_backup_{cnt}" + "\\BackupsLog.log";

            try
            {
                using (StreamWriter sw = new StreamWriter(newlogpath, false))
                {
                    string toWrite = JsonConvert.SerializeObject(this.newLog);
                    sw.Write(toWrite);

                    sw.Flush();
                }
            }
            catch (Exception ex)
            {
                this.reportMaker.AddError(new ErrorDetails()
                {
                    Exception   = ex.Message,
                    Problem     = "The program was unable to create log. Future Incremental backups won't be possible",
                    ProblemPath = newlogpath
                });
            }
        }
Ejemplo n.º 2
0
        protected override void Backup(string sourcePath, List <string> destinationPaths)
        {
            //oprava formatu sourcePath
            if (sourcePath.Contains(@"\\"))
            {
                sourcePath = sourcePath.Replace(@"\\", "\\");
            }


            for (int i = 0; i < destinationPaths.Count; i++)
            {
                if (destinationPaths[i].Contains(@"\\"))
                {
                    destinationPaths[i] = destinationPaths[i].Replace(@"\\", "\\");
                }
            }

            //Nacteni FULL backup logu
            List <LogModel> oldLog = new List <LogModel>();

            //Nacetni stareho logu
            try
            {
                using (StreamReader streamReader = new StreamReader(sourcePath + "\\BackupsLog.log", false))
                {
                    string logtext = streamReader.ReadToEnd();
                    oldLog = JsonConvert.DeserializeObject <List <LogModel> >(logtext);
                }
            }
            catch (Exception ex)
            {
                this.reportMaker.AddError(new ErrorDetails()
                {
                    Exception   = ex.Message,
                    Problem     = "The program didn't find the full backup log. DIFF backup for this source folder hadn't been done.",
                    ProblemPath = sourcePath
                });
                return;
            }



            //Prespsani destination path
            for (int i = 0; i < destinationPaths.Count; i++)
            {
                destinationPaths[i] += $"\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
                int count = 0;
                try
                {
                    foreach (string item in Directory.GetDirectories(destinationPaths[i], "*", SearchOption.TopDirectoryOnly).Where(s => s.Contains("diff_backup")).ToList())
                    {
                        if (item.Contains("diff_backup"))
                        {
                            count++;
                        }
                    }
                }
                catch { }

                if (this.backup.Override)
                {
                    try
                    {
                        Directory.Delete(destinationPaths[i] + $"diff_backup", true);
                        destinationPaths[i] += $"\\diff_backup\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
                    }
                    catch
                    {
                        destinationPaths[i] += $"\\diff_backup\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
                    }
                }
                else
                {
                    destinationPaths[i] += $"\\diff_backup_{count}\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
                }

                Directory.CreateDirectory(destinationPaths[i]);
            }

            int index;

            //Porovnavani logu s aktualni verzi vychozi slozky pro fullbackup
            foreach (string filePath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories).OrderBy(f => new FileInfo(f).FullName).ToList())
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Name == "BackupsLog.log")
                {
                    continue;
                }

                bool found = false;

                foreach (LogModel item in oldLog)
                {
                    //nezmeneno - EXISTS
                    if (fileInfo.FullName == item.FilePath && Convert.ToDateTime(fileInfo.LastWriteTime) == item.LastWriteTime)
                    {
                        found = true;
                        break;
                    }
                    //zmeneno
                    else if (fileInfo.FullName == item.FilePath && Convert.ToDateTime(fileInfo.LastWriteTime) != item.LastWriteTime)
                    {
                        foreach (string destinationPath in destinationPaths)
                        {
                            try
                            {
                                //string dirPath = fileInfo.FullName.Replace(sourcePath, destinationPath);
                                string dirPath = destinationPath;
                                dirPath += fileInfo.FullName.Replace(sourcePath, "");
                                dirPath  = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);
                                //dirPath = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);
                                if (!Directory.Exists(dirPath))
                                {
                                    try
                                    {
                                        Directory.CreateDirectory(dirPath);
                                    }
                                    catch (Exception ex)
                                    {
                                        this.reportMaker.AddError(new ErrorDetails()
                                        {
                                            Exception   = ex.Message,
                                            Problem     = "Backupper was unable to create destination path.",
                                            ProblemPath = dirPath
                                        });
                                    }
                                }

                                File.Copy(filePath, dirPath + $"\\{fileInfo.Name}");
                            }
                            catch (Exception ex)
                            {
                                this.reportMaker.AddError(new ErrorDetails()
                                {
                                    Exception   = ex.Message,
                                    Problem     = "Backupper was unable to copy a file.",
                                    ProblemPath = filePath
                                });
                            }
                        }

                        reportMaker.AddFile(fileInfo);
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }
                else
                {
                    foreach (string destinationPath in destinationPaths)
                    {
                        try
                        {
                            //string dirPath = fileInfo.FullName.Replace(sourcePath, destinationPath);
                            //dirPath = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);
                            string dirPath = destinationPath;
                            dirPath += fileInfo.FullName.Replace(sourcePath, "");
                            dirPath  = dirPath.Substring(0, Functionality.IdentifyCharPosition('\\', dirPath) - 1);

                            if (!Directory.Exists(dirPath))
                            {
                                try
                                {
                                    Directory.CreateDirectory(dirPath);
                                }
                                catch (Exception ex)
                                {
                                    this.reportMaker.AddError(new ErrorDetails()
                                    {
                                        Exception   = ex.Message,
                                        Problem     = "Backupper was unable to create destination path.",
                                        ProblemPath = dirPath
                                    });
                                }
                            }

                            File.Copy(filePath, dirPath + $"\\{fileInfo.Name}");
                        }
                        catch (Exception ex)
                        {
                            this.reportMaker.AddError(new ErrorDetails()
                            {
                                Exception   = ex.Message,
                                Problem     = "Backupper was unable to copy a file.",
                                ProblemPath = filePath
                            });
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        protected override void Backup(string sourcePath, List <string> destinationPaths)
        {
            //Prespsani destination path
            for (int i = 0; i < destinationPaths.Count; i++)
            {
                destinationPaths[i] += $"\\{sourcePath.Substring(Functionality.IdentifyCharPosition('\\', sourcePath))}";
            }

            //Vytvoří podsložky
            foreach (string destinationPath in destinationPaths)
            {
                int index = 0;
                if (Directory.Exists(destinationPath) && backup.Override)
                {
                    try
                    {
                        Directory.Delete(destinationPath, true);
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            Problem     = "Could not delete the old folder. Override failed",
                            ProblemPath = destinationPath
                        });
                    }
                }

                foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
                {
                    try
                    {
                        Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            Problem     = "The program was unable to create folder.",
                            ProblemPath = dirPath
                        });
                    }
                    index++;
                }

                if (index == 0)
                {
                    try
                    {
                        Directory.CreateDirectory(destinationPath);
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Exception   = ex.Message,
                            Problem     = "The program was unable to destination create folder.",
                            ProblemPath = destinationPath
                        });
                    }
                }
            }

            //Zkopíruje všechny soubory a přepíše existující, zanese o nich zaznamy do logu
            foreach (string destinationPath in destinationPaths)
            {
                foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories).OrderBy(f => new FileInfo(f).FullName))
                {
                    try
                    {
                        File.Copy(newPath, newPath.Replace(sourcePath, destinationPath), true);
                        FileInfo fileInfo = new FileInfo(newPath);
                        //*newLog.Add(new LogModel() { Action = "EXI", FileName = fileInfo.Name, FilePath = fileInfo.FullName, LastWriteTime = Convert.ToDateTime(fileInfo.LastWriteTime) });
                        reportMaker.AddFile(new FileInfo(newPath));
                    }
                    catch (Exception ex)
                    {
                        this.reportMaker.AddError(new ErrorDetails()
                        {
                            Problem       = "The file was not copied",
                            ProblemPath   = newPath,
                            Exception     = ex.Message,
                            AffectedFiles = 1
                        });
                    }
                }
            }
        }