Ejemplo n.º 1
0
 // The protected OnProgressEvent method raises the event by invoking
 // the delegate. The sender is always this: the current instance 
 // of the class.
 protected virtual void OnProgressEvent(ProgressEventParams e)
 {
     if (ProgressEvent != null)
     {
         // Invokes the delegate. 
         ProgressEvent(this, e);
     }
 }
Ejemplo n.º 2
0
 // The protected OnProgressEvent method raises the event by invoking
 // the delegate. The sender is always this: the current instance
 // of the class.
 protected virtual void OnProgressEvent(ProgressEventParams e)
 {
     if (ProgressEvent != null)
     {
         // Invokes the delegate.
         ProgressEvent(this, e);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Backup a directory recursively.
        /// </summary>
        /// <param name="dirsToBackup"> Full path to the dir to backuped (recursively) </param>
        /// <param name="dirsToExclude"> Full path to the dir to backuped (recursively) </param>
        private void BackupDirs(DirectoryInfo[] dirsToBackup, DirectoryInfo[] dirsToExclude)
        {
            // First cleanup excess backups...
            CleanupBackupLocation();

            // Init thereport file in the backup dir...
            _reportFile = new FileInfo(_dirToBackupTo + "\\BackupReport.log");

            // Get list of all files...
            DirectoryWithFiles[] dirsAndFiles = null;
            Log.Info("Start getting dirs and files");

            try
            {
                dirsAndFiles = GetDirectoriesWithFiles(dirsToBackup, dirsToExclude);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return;
            }

            Log.Info("Ready getting dirs and files");

            FileInfo curFileToBackup = null;
            string curDirToBackup = null;

            ProgressEventParams pE;
            Boolean fileChanged = false;
            int nbFilesDoneChanged = 0;
            int nbFilesDoneUnchanged = 0;
            long nbMBDoneChanged = 0;
            long nbMBDoneUnchanged = 0;

            int nbDirs = dirsAndFiles.Length;

            // Count the total number of files to backup...
            int totalNbFiles = 0;
            for (int i = 0; i <= (nbDirs - 1); i++)
            {
                totalNbFiles += dirsAndFiles[i].Files.Length;
            }

            Log.Info("Ready counting files");

            // Take the necessary actions directory per directory...
            for (int i = 0; i <= (nbDirs - 1); i++)
            {
                int nbFiles = dirsAndFiles[i].Files.Length;
                curDirToBackup = dirsAndFiles[i].Dir.FullName;

                // Prepare the dir where to put the file inside the backup directory
                string curDirTmp = GetDirToBackupToRelativePart(curDirToBackup);
                string curDirToBackupTo = _dirToBackupTo + curDirTmp;
                string prevDirBackedupTo = _dirPrevBackup + curDirTmp;

                // If not simulating the backup, create the directory if it doesn't exist...
                if (OnlySimulate != true)
                    Directory.CreateDirectory(curDirToBackupTo);

                // Take the necessary actions file per file...           
                for (int j = 0; j <= (nbFiles - 1); j++)
                {
                    if (_bw != null && _bw.CancellationPending)
                    {
                        Log.Info("Backup cancelled... delete partial backup...");
                        try
                        {
                            IOHelper.DeleteRecursive(_dirToBackupTo);
                            Log.Info("Partial backup deleted");
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.Message);
                        }

                        return;
                    }

                    // The file to be backed up...
                    string curFileNameToBackup = dirsAndFiles[i].Files[j].Name;
                    curFileToBackup = new FileInfo(curDirToBackup + '\\' + curFileNameToBackup);
                    // Where does the backup need to be put?
                    FileInfo CurFileToBackupTo = new FileInfo(curDirToBackupTo + '\\' + curFileNameToBackup);
                    // Where is the previous backup of the file standing, if it exists...
                    FileInfo PrevFileBackedupTo = new FileInfo(prevDirBackedupTo + '\\' + curFileNameToBackup);

                    fileChanged = BackupFile(curFileToBackup, CurFileToBackupTo, PrevFileBackedupTo);

                    // Change the counters for the progress reporting...
                    if (fileChanged == true)
                    {
                        nbFilesDoneChanged++;
                        nbMBDoneChanged += curFileToBackup.Length;
                    }
                    else
                    {
                        nbFilesDoneUnchanged++;
                        nbMBDoneUnchanged += curFileToBackup.Length;
                    }

                    pE = new ProgressEventParams(curDirToBackup, curFileNameToBackup, totalNbFiles, nbFilesDoneChanged,
                            nbFilesDoneUnchanged, nbMBDoneChanged, nbMBDoneUnchanged);

                    // If running assynchronously, report via backgroundworker object...
                    if (_bw != null)
                        _bw.ReportProgress(0, pE);
                    else
                        OnProgressEvent(pE);
                }
            }

            // If there aren't any files that were changed... better delete the backup.
            if (nbFilesDoneChanged == 0)
            {
                Log.Info("There were no files changed in backup... so better to delete it again...");

                pE = new ProgressEventParams("- Cancelling backup as there were no changed files...",
                        "", totalNbFiles, nbFilesDoneChanged,
                        nbFilesDoneUnchanged, nbMBDoneChanged, nbMBDoneUnchanged);

                // If running assynchronously, report via backgroundworker object...
                if (_bw != null)
                    _bw.ReportProgress(0, pE);
                else
                    OnProgressEvent(pE);

                try
                {
                    IOHelper.DeleteRecursive(_dirToBackupTo);
                    Log.Info("Partial backup deleted");
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Backup a directory recursively.
        /// </summary>
        /// <param name="dirsToBackup"> Full path to the dir to backuped (recursively) </param>
        /// <param name="dirsToExclude"> Full path to the dir to backuped (recursively) </param>
        private void BackupDirs(DirectoryInfo[] dirsToBackup, DirectoryInfo[] dirsToExclude)
        {
            // First cleanup excess backups...
            CleanupBackupLocation();

            // Init thereport file in the backup dir...
            _reportFile = new FileInfo(_dirToBackupTo + "\\BackupReport.log");

            // Get list of all files...
            DirectoryWithFiles[] dirsAndFiles = null;
            Log.Info("Start getting dirs and files");

            try
            {
                dirsAndFiles = GetDirectoriesWithFiles(dirsToBackup, dirsToExclude);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return;
            }

            Log.Info("Ready getting dirs and files");

            FileInfo curFileToBackup = null;
            string   curDirToBackup  = null;

            ProgressEventParams pE;
            Boolean             fileChanged = false;
            int  nbFilesDoneChanged         = 0;
            int  nbFilesDoneUnchanged       = 0;
            long nbMBDoneChanged            = 0;
            long nbMBDoneUnchanged          = 0;

            int nbDirs = dirsAndFiles.Length;

            // Count the total number of files to backup...
            int totalNbFiles = 0;

            for (int i = 0; i <= (nbDirs - 1); i++)
            {
                totalNbFiles += dirsAndFiles[i].Files.Length;
            }

            Log.Info("Ready counting files");

            // Take the necessary actions directory per directory...
            for (int i = 0; i <= (nbDirs - 1); i++)
            {
                int nbFiles = dirsAndFiles[i].Files.Length;
                curDirToBackup = dirsAndFiles[i].Dir.FullName;

                // Prepare the dir where to put the file inside the backup directory
                string curDirTmp         = GetDirToBackupToRelativePart(curDirToBackup);
                string curDirToBackupTo  = _dirToBackupTo + curDirTmp;
                string prevDirBackedupTo = _dirPrevBackup + curDirTmp;

                // If not simulating the backup, create the directory if it doesn't exist...
                if (OnlySimulate != true)
                {
                    Directory.CreateDirectory(curDirToBackupTo);
                }

                // Take the necessary actions file per file...
                for (int j = 0; j <= (nbFiles - 1); j++)
                {
                    if (_bw != null && _bw.CancellationPending)
                    {
                        Log.Info("Backup cancelled... delete partial backup...");
                        try
                        {
                            IOHelper.DeleteRecursive(_dirToBackupTo);
                            Log.Info("Partial backup deleted");
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.Message);
                        }

                        return;
                    }

                    // The file to be backed up...
                    string curFileNameToBackup = dirsAndFiles[i].Files[j].Name;
                    curFileToBackup = new FileInfo(curDirToBackup + '\\' + curFileNameToBackup);
                    // Where does the backup need to be put?
                    FileInfo CurFileToBackupTo = new FileInfo(curDirToBackupTo + '\\' + curFileNameToBackup);
                    // Where is the previous backup of the file standing, if it exists...
                    FileInfo PrevFileBackedupTo = new FileInfo(prevDirBackedupTo + '\\' + curFileNameToBackup);

                    fileChanged = BackupFile(curFileToBackup, CurFileToBackupTo, PrevFileBackedupTo);

                    // Change the counters for the progress reporting...
                    if (fileChanged == true)
                    {
                        nbFilesDoneChanged++;
                        nbMBDoneChanged += curFileToBackup.Length;
                    }
                    else
                    {
                        nbFilesDoneUnchanged++;
                        nbMBDoneUnchanged += curFileToBackup.Length;
                    }

                    pE = new ProgressEventParams(curDirToBackup, curFileNameToBackup, totalNbFiles, nbFilesDoneChanged,
                                                 nbFilesDoneUnchanged, nbMBDoneChanged, nbMBDoneUnchanged);

                    // If running assynchronously, report via backgroundworker object...
                    if (_bw != null)
                    {
                        _bw.ReportProgress(0, pE);
                    }
                    else
                    {
                        OnProgressEvent(pE);
                    }
                }
            }

            // If there aren't any files that were changed... better delete the backup.
            if (nbFilesDoneChanged == 0)
            {
                Log.Info("There were no files changed in backup... so better to delete it again...");

                pE = new ProgressEventParams("- Cancelling backup as there were no changed files...",
                                             "", totalNbFiles, nbFilesDoneChanged,
                                             nbFilesDoneUnchanged, nbMBDoneChanged, nbMBDoneUnchanged);

                // If running assynchronously, report via backgroundworker object...
                if (_bw != null)
                {
                    _bw.ReportProgress(0, pE);
                }
                else
                {
                    OnProgressEvent(pE);
                }

                try
                {
                    IOHelper.DeleteRecursive(_dirToBackupTo);
                    Log.Info("Partial backup deleted");
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                }
            }
        }